LockLayerInfo:
    move.l a5,-(a7)
    lea -$30(a0),a5
     bsr.b LB_D02A
    movea.l (a7)+,a5
    movea.l PLayersBase(pc),a6
   rts
 cnop 0,4
LockLayer:
    exg a1,a5
    bsr.b   LB_D02A
    exg a1,a5
    movea.l PLayersBase(pc),a6
   rts
 cnop 0,4
LB_D02A
    movea.l  PExecBase(pc),a6
    addq.b  #1,$0127(a6)
    addq.w  #1,$0056(a5)
    addq.w  #1,$0074(a5)
    bne.b   LB_D04C
    move.l  $0114(a6),$0070(a5)
     jsr _LVOPermit(a6)
   rts
 cnop 0,4
LB_D04C move.l  a1,-(a7)
    move.l  $0114(a6),a1
    cmpa.l  $0070(a5),a1
    bne.b   LB_D062
     jsr    _LVOPermit(a6)
    move.l  (a7)+,a1
   rts
 cnop 0,4
LB_D062 movem.l d0/d1/a0,-(a7)
    subq.w  #1,$0056(a5)
    move.l  a1,-(a7)
    bclr    #$04,$001d(a1)
    lea $0060(a5),a0
    subq.l  #8,a7
    move.l  (a0),d0
    move.l  a7,(a0)
    subq.l  #4,a0
    exg d0,a0
    movem.l d0/a0,(a7)
    move.l  a7,(a0)
    moveq   #$10,d0
     jsr _LVOWait(a6)
    adda.l  #$c,a7
     jsr _LVOPermit(a6)
    movem.l (a7)+,d0/d1/a0/a1
   rts
  cnop 0,4
UnlockLayerInfo:
    move.l  a5,-(a7)
    lea -$0030(a0),a5
     bsr.b   LB_D0AE
    movea.l  (a7)+,a5
    movea.l PLayersBase(pc),a6
    rts 
 cnop 0,4
UnlockLayer:
    exg a0,a5
     bsr.b   LB_D0AE
    exg a0,a5
    movea.l PLayersBase(pc),a6
    rts 
 cnop 0,4
LB_D0AE movea.l  PExecBase(pc),a6
    addq.b  #1,$0127(a6)
    subq.w  #1,$0074(a5)
     bge.b   LB_D0D2
    subq.w  #1,$0056(a5)
     bne.b   LB_D0CA
    clr.l   $0070(a5)
LB_D0CA jsr _LVOPermit(a6)
    rts 
 cnop 0,4
LB_D0D2 addq.w  #1,$0074(a5)
    movem.l d0/d1/a0/a1,-(a7)
    lea $0048(a5),a0
     jsr _LVOReleaseSemaphore(a6)
     jsr _LVOPermit(a6)
    movem.l (a7)+,d0/d1/a0/a1
    rts 
 cnop 0,4
;   1 - less stack usage
;   2 - inlined small routines
DoHookClipRects:  cmpa.l #LAYERS_NOBACKFILL,A0  ; a0->hook, a1->rport, a2->rect
   beq.b .exit
  movem.l D2-D7/A2-A5,-(A7)
  move.l A2,D6
  movea.l (A1),A2
  move.l A0,D7
  move.l A1,D5
  move.l rp_BitMap(A1),-(A7)
  subq.l #8,A7
  move.l A2,D4
   bne.b JL_0_34
  movea.l D7,A0
  movea.l D4,A1
  movea.l D5,A2
  movea.l D6,A3
  movea.l D6,A4
  moveq.l #$00,D2
  moveq.l #$00,D3
  pea JL_0_142(PC)
 bra.w JL_0_015A
 cnop 0,4 
.exit: rts
 cnop 0,4
JL_0_34:  movea.l A2,A1
  lea $48(A1),A0
  movea.l A6,A1
  movea.l PExecBase(PC),A6
   jsr _LVOObtainSemaphore(A6)
  movea.l A1,A6
  move.w $10(A2),D2
  sub.w $2C(A2),D2
  move.w $12(A2),D3
  sub.w $2E(A2),D3
  movea.l A7,A1
  movea.l D6,A0
  move.l D6,-(A7)
   beq.b JL_0_6C
  move.l A1,D6
  move.w (A0)+,D0
  add.w D2,D0
  move.w D0,(A1)+
  move.w (A0)+,D0
  add.w D3,D0
  move.w D0,(A1)+
  move.w (A0)+,D0
  add.w D2,D0
  move.w D0,(A1)+
  move.w (A0)+,D0
  add.w D3,D0
  move.w D0,(A1)+
JL_0_6C:  lea $8(A2),A5
  subq.l #8,A7
JL_0_72:  move.l (A5),D0
   beq.w JL_0_F2
  movea.l D0,A5
  lea $10(A5),A3
  tst.l D6
   beq.b JL_0_98
  movea.l A3,A0
  movea.l D6,A1
  movea.l A7,A2
; inlined ->
  movea.l A2,A3
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   ble.b .JL_0_274
  move.w D1,D0
.JL_0_274:  move.w D0,(A2)+
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   ble.b .JL_0_280
  move.w D1,D0
.JL_0_280:  move.w D0,(A2)+
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   bge.b .JL_0_28C
  move.w D1,D0
.JL_0_28C:  move.w D0,(A2)+
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   bge.b .JL_0_298
  move.w D1,D0
.JL_0_298:  move.w D0,(A2)+
  movea.l A3,A2
;<- 
  move.l (A2)+,D0
  move.l (A2)+,D1
  cmp.w D0,D1
   blt.b JL_0_72
  cmp.l D0,D1
   blt.b JL_0_72
  movea.l A7,A3
JL_0_98:  movea.l A3,A4
  movea.l D4,A1
  movea.l D7,A0
  movea.l D5,A2
  tst.l $8(A5)
   bne.b JL_0_AC
   bsr.w JL_0_015A
 bra.b JL_0_72
JL_0_AC:  tst.l $C(A5)
   beq.b JL_0_72
  moveq.l #$0F,D1
  and.w $10(A5),D1
  sub.w $10(A5),D1
  move.w $4(A4),D0
  add.w D1,D0
  swap D0
  move.w $6(A4),D0
  sub.w $12(A5),D0
  move.l D0,-(A7)
  add.w (A4),D1
  swap D1
  move.w $2(A4),D1
  sub.w $12(A5),D1
  move.l D1,-(A7)
  movea.l A7,A3
  move.l $4(A2),-(A7)
  move.l $C(A5),$4(A2)
   bsr.w JL_0_015A
  move.l (A7)+,$4(A2)
  addq.l #8,A7
 bra.w JL_0_72
JL_0_F2:  addq.l #8,A7
  move.l (A7)+,D6
  movea.l D4,A2
  move.l $20(A2),D0
   beq.b JL_0_142
  movea.l D5,A0
  move.l D0,$4(A0)
  moveq.l #$00,D2
  moveq.l #$00,D3
  lea $24(A2),A5
JL_0_10C:  move.l (A5),D0
   beq.b JL_0_142
  movea.l D0,A5
  lea $10(A5),A3
  tst.l D6
   beq.b JL_0_136
  movea.l A3,A0
  movea.l D6,A1
  movea.l A7,A2
; inlined ->   
  movea.l A2,A3
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   ble.b .JL_0_274
  move.w D1,D0
.JL_0_274:  move.w D0,(A2)+
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   ble.b .JL_0_280
  move.w D1,D0
.JL_0_280:  move.w D0,(A2)+
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   bge.b .JL_0_28C
  move.w D1,D0
.JL_0_28C:  move.w D0,(A2)+
  move.w (A0)+,D0
  move.w (A1)+,D1
  cmp.w D0,D1
   bge.b .JL_0_298
  move.w D1,D0
.JL_0_298:  move.w D0,(A2)+
  movea.l A3,A2
; <-
  move.l (A2)+,D0
  move.l (A2)+,D1
  cmp.w D0,D1
   blt.b JL_0_10C
  swap D0
  swap D1
  cmp.w D0,D1
   blt.b JL_0_10C
  movea.l A7,A3
JL_0_136:  movea.l A3,A4
  movea.l D4,A1
  movea.l D7,A0
  movea.l D5,A2
   bsr.b JL_0_015A
 bra.b JL_0_10C
 cnop 0,4
JL_0_142:  addq.l #8,A7
  movea.l D5,A0
  move.l (A7)+,$4(A0)
  tst.l D4
   beq.b JL_0_0154
  movea.l D4,A0
  movea.l A6,A2
  lea $48(A0),A0
  movea.l PExecBase(PC),A6
   jsr _LVOReleaseSemaphore(A6)
  movea.l A2,A6  
JL_0_0154:  movem.l (A7)+,D2-D7/A2-A5
 rtS 
 cnop 0,4
JL_0_015A:  move.l A0,D0
   beq.b JL_0_1D0
  subq.l #1,D0
   bne.b JL_0_0164
 rtS 
 cnop 0,4
JL_0_0164:  move.w $2(A4),D0
  sub.w D3,D0
  ext.l D0
  move.l D0,-(A7)
  move.w (A4),D0
  sub.w D2,D0
  ext.l D0
  move.l D0,-(A7)
  move.l $4(A3),-(A7)
  move.l (A3),-(A7)
  move.l A1,-(A7)
  movea.l A7,A1
  move.l A6,-(A7)
  movea.l $8(A0),A6
L_0_186:   jsr (A6)
  movea.l (A7)+,A6
  lea $14(A7),A7
 rtS 
 cnop 0,4
JL_0_1D0:  movem.l D2-D7,-(A7)
  movea.l $4(A2),A1
  movea.l A1,A0
  moveq.l #$00,D2
  move.w (A3),D2
  moveq.l #$00,D3
  move.w $2(A3),D3
  moveq.l #$00,D4
  move.w $4(A3),D4
  sub.l D2,D4
  addq.l #1,D4
  moveq.l #$00,D5
  move.w $6(A3),D5
  sub.l D3,D5
  addq.l #1,D5
  move.l D2,D0
  move.l D3,D1
  moveq.l #$00,D6
  moveq.l #-$01,D7
  movea.l PLGfxBase(PC),A6
   jsr _LVOBltBitMap(A6)
  movea.l PLayersBase(PC),A6
  movem.l (A7)+,D2-D7
 rtS 
 cnop 0,4
PLayersBase: ds.l 1
PLGfxBase:  ds.l 1
