// File: /workspace/github_c_makefile_star2_O0/mabdi[P]bunney/bunny-exec.c
// Function: read_shm
static _u8 read_shm(void* ptr, _u32 siz) {
  _s32 avail;

  /* Races here are OK, because the amount of data to be read can only increase
     with time. */

  do {
    avail = shmreg->write_off - shmreg->read_off;
    if (avail < 0) avail += shmreg->length;
    if (avail >= siz) break;
    if (report_kids()) return 1;
    sched_yield();
  } while (1);

  do {
    while (shmreg->lock)
      if (report_kids()) return 1; else sched_yield();
    atomic_inc(&shmreg->lock);
    if (shmreg->lock == 1) break;
    atomic_dec(&shmreg->lock);
    if (shmreg->lock < 0 || shmreg->lock > 1)
      fatal("SHM spinlock state corrupted (up at %d)",shmreg->lock);
  } while (1);

  if (shmreg->read_off + siz >= shmreg->length) {
    _u32 copy1 = shmreg->length - shmreg->read_off, siz2 = siz - copy1;
    memcpy(ptr, (void*) shmreg->data + shmreg->read_off, copy1);
    ptr += copy1;
    memcpy(ptr, (void*) shmreg->data, siz2);
  } else
    memcpy(ptr, (void*) shmreg->data + shmreg->read_off, siz);

  shmreg->read_off = (shmreg->read_off + siz) % (shmreg->length);

  atomic_dec(&shmreg->lock);

  return 0;

}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x20, %rsp
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movq 0x4432(%rip), %rax      # 0x6188
movl 0x8(%rax), %eax
movq 0x4428(%rip), %rcx      # 0x6188
movl 0xc(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x18(%rbp)
cmpl $0x0, -0x18(%rbp)
jge 0x1d7e
movq 0x4413(%rip), %rax      # 0x6188
movl 0x4(%rax), %eax
addl -0x18(%rbp), %eax
movl %eax, -0x18(%rbp)
movl -0x18(%rbp), %eax
cmpl -0x14(%rbp), %eax
jb 0x1d88
jmp 0x1da7
callq 0x2ba0
cmpb $0x0, %al
je 0x1d9a
movb $0x1, -0x1(%rbp)
jmp 0x1f6d
callq 0x11c0
movb $0x1, %al
testb $0x1, %al
jne 0x1d4f
jmp 0x1da7
jmp 0x1da9
jmp 0x1dab
movq 0x43d6(%rip), %rax      # 0x6188
movl (%rax), %eax
cmpl $0x0, %eax
je 0x1dd2
callq 0x2ba0
cmpb $0x0, %al
je 0x1dcb
movb $0x1, -0x1(%rbp)
jmp 0x1f6d
callq 0x11c0
jmp 0x1dab
movq 0x43af(%rip), %rdi      # 0x6188
callq 0x2dd0
movq 0x43a3(%rip), %rax      # 0x6188
movl (%rax), %eax
cmpl $0x1, %eax
jne 0x1df1
jmp 0x1e80
movq 0x4390(%rip), %rdi      # 0x6188
callq 0x2df0
movq 0x4384(%rip), %rax      # 0x6188
movl (%rax), %eax
cmpl $0x0, %eax
jl 0x1e19
movq 0x4376(%rip), %rax      # 0x6188
movl (%rax), %eax
cmpl $0x1, %eax
jle 0x1e72
jmp 0x1e1b
movq 0x41be(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
movq 0x435c(%rip), %rax      # 0x6188
movl (%rax), %edx
leaq 0x2412(%rip), %rsi      # 0x4247
movb $0x0, %al
callq 0x11b0
movq 0x419d(%rip), %rax      # 0x5fe0
movq (%rax), %rdi
leaq 0x2212(%rip), %rsi      # 0x405f
leaq 0x2432(%rip), %rdx      # 0x4286
leaq 0x2219(%rip), %rcx      # 0x4074
movl $0x197, %r8d            # imm = 0x197
movb $0x0, %al
callq 0x11b0
movl $0x1, %edi
callq 0x1130
jmp 0x1e74
movb $0x1, %al
testb $0x1, %al
jne 0x1da9
jmp 0x1e80
movq 0x4301(%rip), %rax      # 0x6188
movl 0xc(%rax), %eax
addl -0x14(%rbp), %eax
movq 0x42f4(%rip), %rcx      # 0x6188
movl 0x4(%rcx), %ecx
cmpl %ecx, %eax
jb 0x1f10
movq 0x42e6(%rip), %rax      # 0x6188
movl 0x4(%rax), %eax
movq 0x42dc(%rip), %rcx      # 0x6188
movl 0xc(%rcx), %ecx
subl %ecx, %eax
movl %eax, -0x1c(%rbp)
movl -0x14(%rbp), %eax
subl -0x1c(%rbp), %eax
movl %eax, -0x20(%rbp)
movq -0x10(%rbp), %rdi
movq 0x42c0(%rip), %rsi      # 0x6188
addq $0x54, %rsi
movq 0x42b5(%rip), %rax      # 0x6188
movl 0xc(%rax), %eax
movl %eax, %eax
addq %rax, %rsi
movl -0x1c(%rbp), %eax
movl %eax, %edx
callq 0x10b0
movl -0x1c(%rbp), %ecx
movq -0x10(%rbp), %rax
movl %ecx, %ecx
addq %rcx, %rax
movq %rax, -0x10(%rbp)
movq -0x10(%rbp), %rdi
movq 0x4288(%rip), %rsi      # 0x6188
addq $0x54, %rsi
movl -0x20(%rbp), %eax
movl %eax, %edx
callq 0x10b0
jmp 0x1f38
movq -0x10(%rbp), %rdi
movq 0x426d(%rip), %rsi      # 0x6188
addq $0x54, %rsi
movq 0x4262(%rip), %rax      # 0x6188
movl 0xc(%rax), %eax
movl %eax, %eax
addq %rax, %rsi
movl -0x14(%rbp), %eax
movl %eax, %edx
callq 0x10b0
movq 0x4249(%rip), %rax      # 0x6188
movl 0xc(%rax), %eax
addl -0x14(%rbp), %eax
movq 0x423c(%rip), %rcx      # 0x6188
movl 0x4(%rcx), %ecx
xorl %edx, %edx
divl %ecx
movq 0x422e(%rip), %rax      # 0x6188
movl %edx, 0xc(%rax)
movq 0x4224(%rip), %rdi      # 0x6188
callq 0x2df0
movb $0x0, -0x1(%rbp)
movb -0x1(%rbp), %al
addq $0x20, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)