// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
cmpl $0x1, %edi
jle 0x1511
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x1070
movl %eax, 0x2d51(%rip)      # 0x4070
cmpl $-0x1, %eax
je 0x151d
pushq $0x2
popq %rdx
movl %eax, %edi
xorl %esi, %esi
callq 0x1090
movq %rax, -0x48(%rbp)
bswapq %rax
leaq -0x60(%rbp), %rbx
movq %rax, (%rbx)
movq 0x2d17(%rip), %rax      # 0x4060
bswapq %rax
leaq -0x58(%rbp), %r14
movq %rax, (%r14)
movl 0x2d13(%rip), %eax      # 0x406c
bswapl %eax
movl %eax, -0x2c(%rbp)
leaq 0xcc9(%rip), %rdi       # 0x202e
pushq $0x8
popq %r15
movq %r15, %rsi
callq 0x1244
movq %r14, %rdi
movq %r15, %rsi
callq 0x1244
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1244
movl $0x80, %r15d
leaq 0xca3(%rip), %rbx       # 0x2037
pushq $0x1
popq %r14
subl $0x1, %r15d
jb 0x13ab
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1244
jmp 0x1398
pushq $0x4
popq %r15
movl $0x100000, %r13d        # imm = 0x100000
leaq 0x2cd4(%rip), %r14      # 0x4090
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x1280
leaq -0x34(%rbp), %rdi
movq %r15, %rsi
callq 0x1280
leaq -0x40(%rbp), %rdi
pushq $0x8
popq %rbx
movq %rbx, %rsi
callq 0x1280
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x1280
leaq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x1280
movq -0x50(%rbp), %rsi
bswapq %rsi
leaq 0xc35(%rip), %rcx       # 0x203e
movq %rcx, %r12
testq %rsi, %rsi
js 0x148d
movl -0x38(%rbp), %eax
bswapl %eax
movq %rcx, %r12
cmpl 0x2c49(%rip), %eax      # 0x4068
jne 0x148d
movl -0x30(%rbp), %ebx
bswapl %ebx
leaq (%rsi,%rbx), %rax
movq %rcx, %r12
cmpq -0x48(%rbp), %rax
ja 0x148d
movl -0x34(%rbp), %r12d
bswapl %r12d
movl 0x2c30(%rip), %edi      # 0x4070
xorl %edx, %edx
callq 0x1090
testl %r12d, %r12d
je 0x14b5
cmpl $0x1, %r12d
je 0x1481
jmp 0x13bc
cmpq %r13, %rbx
movl $0x100000, %r12d        # imm = 0x100000
cmovbq %rbx, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x1280
movq %r14, %rdi
movq %r12, %rsi
callq 0x11cc
addq $-0x100000, %rbx        # imm = 0xFFF00000
testq %rbx, %rbx
jg 0x1457
leaq 0xbac(%rip), %r12       # 0x2039
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x1244
movq %r12, %rdi
movq %r15, %rsi
callq 0x1244
leaq -0x40(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x1244
jmp 0x13bc
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x1244
leaq 0xb71(%rip), %rdi       # 0x2039
movq %r15, %rsi
callq 0x1244
leaq -0x40(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x1244
testq %rbx, %rbx
jle 0x13bc
cmpq %r13, %rbx
movl $0x100000, %r12d        # imm = 0x100000
cmovbq %rbx, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x1208
movq %r14, %rdi
movq %r12, %rsi
callq 0x1244
addq $-0x100000, %rbx        # imm = 0xFFF00000
jmp 0x14dc
leaq 0xaec(%rip), %rdi       # 0x2004
callq 0x119c
leaq 0xaf6(%rip), %rdi       # 0x201a
callq 0x119c