// File: /workspace/github_c_makefile_star2_O0/regularfry[P]tinynbd/src/tinynbd_sendfile.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; off64_t fo=(off64_t)f;
      if(t==0){
        W(&rmag,4);W("\0\0\0\0",4);W(h,8);WS(&fo,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 {
      fprintf(stderr, "Error\n");
      W(&rmag,4);W("\1\0\0\0",4);W(h,8);
    }
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x60, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x15f8
leaq 0xa11(%rip), %rdi       # 0x2004
callq 0x11d0
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x101002, %esi         # imm = 0x101002
movb $0x0, %al
callq 0x1090
movl %eax, 0x2a76(%rip)      # 0x4088
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x2a6b(%rip), %eax      # 0x4088
jne 0x162b
leaq 0x9f4(%rip), %rdi       # 0x201a
callq 0x11d0
movl 0x2a57(%rip), %edi      # 0x4088
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x10b0
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x14f0
movq %rax, -0x20(%rbp)
movq 0x2a21(%rip), %rdi      # 0x4078
callq 0x14f0
movq %rax, -0x28(%rbp)
movl 0x2a1e(%rip), %edi      # 0x4084
callq 0x1480
movl %eax, -0x2c(%rbp)
leaq 0x9b9(%rip), %rdi       # 0x202e
movl $0x8, %esi
callq 0x12d0
leaq -0x28(%rbp), %rdi
movl $0x8, %esi
callq 0x12d0
leaq -0x20(%rbp), %rdi
movl $0x8, %esi
callq 0x12d0
movl $0x0, -0x30(%rbp)
cmpl $0x80, -0x30(%rbp)
jge 0x16c7
leaq 0x98c(%rip), %rdi       # 0x203e
movl $0x1, %esi
callq 0x12d0
movl -0x30(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x30(%rbp)
jmp 0x16a2
jmp 0x16c9
leaq -0x34(%rbp), %rdi
movl $0x4, %esi
callq 0x1330
leaq -0x38(%rbp), %rdi
movl $0x4, %esi
callq 0x1330
leaq -0x44(%rbp), %rdi
movl $0x8, %esi
callq 0x1330
leaq -0x50(%rbp), %rdi
movl $0x8, %esi
callq 0x1330
leaq -0x3c(%rbp), %rdi
movl $0x4, %esi
callq 0x1330
leaq -0x34(%rbp), %rdi
callq 0x1450
leaq -0x38(%rbp), %rdi
callq 0x1450
leaq -0x50(%rbp), %rdi
callq 0x14c0
leaq -0x3c(%rbp), %rdi
callq 0x1450
movl -0x34(%rbp), %eax
cmpl 0x2944(%rip), %eax      # 0x4080
jne 0x1838
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmpq %rax, -0x50(%rbp)
jae 0x1838
movl -0x3c(%rbp), %eax
addq -0x50(%rbp), %rax
cmpq -0x18(%rbp), %rax
ja 0x1838
movl -0x3c(%rbp), %eax
movq %rax, -0x58(%rbp)
movq -0x50(%rbp), %rax
movq %rax, -0x60(%rbp)
cmpl $0x0, -0x38(%rbp)
jne 0x17b8
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
callq 0x12d0
leaq 0x8af(%rip), %rdi       # 0x2040
movl $0x4, %esi
callq 0x12d0
leaq -0x44(%rbp), %rdi
movl $0x8, %esi
callq 0x12d0
movq -0x58(%rbp), %rsi
leaq -0x60(%rbp), %rdi
callq 0x13f0
jmp 0x1836
cmpl $0x1, -0x38(%rbp)
jne 0x1834
jmp 0x17c0
cmpq $0x0, -0x58(%rbp)
jle 0x1807
movq -0x58(%rbp), %rdi
callq 0x15a0
movq %rax, %rsi
leaq 0x28c6(%rip), %rdi      # 0x40a0
callq 0x1330
movq -0x58(%rbp), %rdi
callq 0x15a0
movq %rax, %rsi
leaq 0x28ae(%rip), %rdi      # 0x40a0
callq 0x1210
movq -0x58(%rbp), %rax
subq $0x100000, %rax         # imm = 0x100000
movq %rax, -0x58(%rbp)
jmp 0x17c0
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
callq 0x12d0
leaq 0x824(%rip), %rdi       # 0x2040
movl $0x4, %esi
callq 0x12d0
leaq -0x44(%rbp), %rdi
movl $0x8, %esi
callq 0x12d0
jmp 0x1836
jmp 0x187d
movq 0x27b9(%rip), %rax      # 0x3ff8
movq (%rax), %rdi
leaq 0x7ee(%rip), %rsi       # 0x2037
movb $0x0, %al
callq 0x1070
leaq -0x2c(%rbp), %rdi
movl $0x4, %esi
callq 0x12d0
leaq 0x7e0(%rip), %rdi       # 0x2045
movl $0x4, %esi
callq 0x12d0
leaq -0x44(%rbp), %rdi
movl $0x8, %esi
callq 0x12d0
jmp 0x16c9