// File: /workspace/github_c_makefile_star2_O3/regularfry[P]tinynbd/src/tinynbd.c
// Function: main
int main(int argc, char **argv){
  if(argc<2){err("Need a file to serve\n");}
  fd=open(argv[1],O_RDWR|O_SYNC); if(-1==fd){err("Couldn't open file\n");};
  off64_t size=lseek64(fd,0,SEEK_END);__be64 wsz=htobe64(size);
  __be64 wmag=htobe64(init_magic); __be32 rmag=htobe32(reply_magic);
  W("NBDMAGIC",8);W(&wmag,8);W(&wsz,8); int i; for(i=0;i<128;i++){W("\0",1);}
  while(1){
    uint32_t m,t,l;char h[8];uint64_t f;
    R(&m,4);R(&t,4);R(h,8);R(&f,8);R(&l,4);
    in4(&m);in4(&t);in8(&f);in4(&l);
    if(m == request_magic && f < (uint64_t)1<<63 && l+f <= size){
      int64_t ll=l;
      lseek64(fd,f,SEEK_SET);
      if(t==0){
        W(&rmag,4);W("\0\0\0\0",4);W(h,8);
        for(;ll>0;ll-=B){r(rbuf,clip(ll));W(rbuf,clip(ll));}
      } else if(t==1){
        for(;ll>0;ll-=B){R(rbuf,clip(ll));w(rbuf,clip(ll));}
        W(&rmag,4);W("\0\0\0\0",4);W(h,8);
      }
    } else {
      W(&rmag,4);W("\1\0\0\0",4);W(h,8);
    }
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpl $0x1, %edi
jle 0x16ea
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x1070
movl %eax, 0x2d31(%rip)      # 0x4070
cmpl $-0x1, %eax
je 0x16f6
xorl %ebx, %ebx
movl %eax, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1090
movq %rax, -0x50(%rbp)
bswapq %rax
movq %rax, -0x68(%rbp)
movq 0x2cf6(%rip), %rax      # 0x4060
bswapq %rax
movq %rax, -0x60(%rbp)
movl 0x2cf5(%rip), %eax      # 0x406c
bswapl %eax
movl %eax, -0x2c(%rbp)
leaq 0xcab(%rip), %r14       # 0x202e
leaq (%r14,%rbx), %rsi
movl $0x8, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x1383
xorl %ebx, %ebx
leaq (%rbx,%rbp), %rsi
addq $-0x60, %rsi
movl $0x8, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x13a6
xorl %ebx, %ebx
leaq (%rbx,%rbp), %rsi
addq $-0x68, %rsi
movl $0x8, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x13cd
xorl %r14d, %r14d
leaq 0xc3b(%rip), %rbx       # 0x2037
movl $0x1, %edx
movl $0x1, %edi
movq %rbx, %rsi
callq 0x1030
testl %eax, %eax
je 0x13fc
incl %r14d
cmpl $0x80, %r14d
jne 0x13fc
leaq 0x2c6b(%rip), %r14      # 0x4090
leaq 0xc12(%rip), %rbx       # 0x203e
movl $0x4, %esi
leaq -0x3c(%rbp), %rdi
callq 0x1296
movl $0x4, %esi
leaq -0x38(%rbp), %rdi
callq 0x1296
movl $0x8, %esi
leaq -0x44(%rbp), %rdi
callq 0x1296
movl $0x8, %esi
leaq -0x58(%rbp), %rdi
callq 0x1296
movl $0x4, %esi
leaq -0x34(%rbp), %rdi
callq 0x1296
movq -0x58(%rbp), %rsi
bswapq %rsi
testq %rsi, %rsi
js 0x149f
movl -0x3c(%rbp), %eax
bswapl %eax
cmpl 0x2bdf(%rip), %eax      # 0x4068
jne 0x149f
movl -0x34(%rbp), %r15d
movl %r15d, %r13d
bswapl %r13d
leaq (%rsi,%r13), %rax
cmpq -0x50(%rbp), %rax
jbe 0x151b
xorl %r15d, %r15d
leaq (%r15,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r15, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x14a2
xorl %r15d, %r15d
leaq (%rbx,%r15), %rsi
movl $0x4, %edx
subq %r15, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x14cb
xorl %r15d, %r15d
leaq (%r15,%rbp), %rsi
addq $-0x44, %rsi
movl $0x8, %edx
subq %r15, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x8, %r15
jb 0x14f0
jmp 0x142c
movl -0x38(%rbp), %r12d
bswapl %r12d
movl 0x2b48(%rip), %edi      # 0x4070
xorl %edx, %edx
callq 0x1090
cmpl $0x1, %r12d
je 0x162a
testl %r12d, %r12d
jne 0x142c
movl %r15d, -0x30(%rbp)
xorl %r12d, %r12d
leaq (%r12,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r12, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r12d
movslq %r12d, %r12
cmpq $0x4, %r12
jb 0x1549
xorl %r12d, %r12d
leaq 0xac0(%rip), %r15       # 0x2039
leaq (%r15,%r12), %rsi
movl $0x4, %edx
subq %r12, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r12d
movslq %r12d, %r12
cmpq $0x4, %r12
jb 0x1579
xorl %r12d, %r12d
leaq (%r12,%rbp), %rsi
addq $-0x44, %rsi
movl $0x8, %edx
subq %r12, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r12d
movslq %r12d, %r12
cmpq $0x8, %r12
jb 0x159e
cmpl $0x0, -0x30(%rbp)
movl $0x100000, %eax         # imm = 0x100000
je 0x142c
cmpq %rax, %r13
movl $0x100000, %r15d        # imm = 0x100000
cmovbq %r13, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1211
xorl %r12d, %r12d
leaq (%r14,%r12), %rsi
movq %r15, %rdx
subq %r12, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r12d
movslq %r12d, %r12
cmpq %r15, %r12
jb 0x15ee
leaq -0x100000(%r13), %rax
cmpq $0x100000, %r13         # imm = 0x100000
movq %rax, %r13
movl $0x100000, %eax         # imm = 0x100000
jg 0x15d3
jmp 0x142c
testl %r15d, %r15d
movl $0x100000, %r12d        # imm = 0x100000
je 0x1667
cmpq %r12, %r13
movl $0x100000, %r15d        # imm = 0x100000
cmovbq %r13, %r15
movq %r14, %rdi
movq %r15, %rsi
callq 0x1296
movq %r14, %rdi
movq %r15, %rsi
callq 0x11ce
leaq -0x100000(%r13), %rax
cmpq %r12, %r13
movq %rax, %r13
ja 0x1635
xorl %r15d, %r15d
leaq (%r15,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r15, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x166a
xorl %r15d, %r15d
leaq 0x99f(%rip), %r12       # 0x2039
leaq (%r12,%r15), %rsi
movl $0x4, %edx
subq %r15, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x169a
xorl %r15d, %r15d
leaq (%r15,%rbp), %rsi
addq $-0x44, %rsi
movl $0x8, %edx
subq %r15, %rdx
movl $0x1, %edi
callq 0x1030
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x8, %r15
jb 0x16bf
jmp 0x142c
leaq 0x913(%rip), %rdi       # 0x2004
callq 0x119c
leaq 0x91d(%rip), %rdi       # 0x201a
callq 0x119c