// File: /workspace/github_c_makefile_star2_O2/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpl $0x1, %edi
jle 0x15af
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x1090
movl %eax, 0x2ccc(%rip)      # 0x4090
cmpl $-0x1, %eax
je 0x15bb
pushq $0x2
popq %rdx
movl %eax, %edi
xorl %esi, %esi
callq 0x10c0
movq %rax, -0x48(%rbp)
bswapq %rax
leaq -0x68(%rbp), %rbx
movq %rax, (%rbx)
movq 0x2c92(%rip), %rax      # 0x4080
bswapq %rax
leaq -0x60(%rbp), %r14
movq %rax, (%r14)
movl 0x2c8e(%rip), %eax      # 0x408c
bswapl %eax
movl %eax, -0x2c(%rbp)
leaq 0xc24(%rip), %rdi       # 0x202e
pushq $0x8
popq %r15
movq %r15, %rsi
callq 0x1284
movq %r14, %rdi
movq %r15, %rsi
callq 0x1284
movq %rbx, %rdi
movq %r15, %rsi
callq 0x1284
movl $0x80, %r15d
leaq 0xc05(%rip), %rbx       # 0x203e
pushq $0x1
popq %r14
subl $0x1, %r15d
jb 0x1450
movq %rbx, %rdi
movq %r14, %rsi
callq 0x1284
jmp 0x143d
pushq $0x4
popq %r15
movl $0x100000, %r14d        # imm = 0x100000
leaq 0x2c4f(%rip), %r12      # 0x40b0
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x12c0
leaq -0x34(%rbp), %rdi
movq %r15, %rsi
callq 0x12c0
leaq -0x40(%rbp), %rdi
pushq $0x8
popq %rbx
movq %rbx, %rsi
callq 0x12c0
leaq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x12c0
leaq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x12c0
movq -0x58(%rbp), %rax
bswapq %rax
testq %rax, %rax
js 0x1567
movl -0x38(%rbp), %ecx
bswapl %ecx
cmpl 0x2bcd(%rip), %ecx      # 0x4088
jne 0x1567
movl -0x30(%rbp), %ebx
bswapl %ebx
leaq (%rax,%rbx), %rcx
cmpq -0x48(%rbp), %rcx
ja 0x1567
movl -0x34(%rbp), %ecx
bswapl %ecx
movq %rax, -0x50(%rbp)
testl %ecx, %ecx
je 0x152f
cmpl $0x1, %ecx
je 0x1515
jmp 0x1461
cmpq %r14, %rbx
movl $0x100000, %r13d        # imm = 0x100000
cmovbq %rbx, %r13
movq %r12, %rdi
movq %r13, %rsi
callq 0x12c0
movq %r12, %rdi
movq %r13, %rsi
callq 0x120c
addq $-0x100000, %rbx        # imm = 0xFFF00000
testq %rbx, %rbx
jg 0x14eb
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x1284
leaq 0xb13(%rip), %rdi       # 0x2040
jmp 0x1596
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x1284
leaq 0xafe(%rip), %rdi       # 0x2040
movq %r15, %rsi
callq 0x1284
leaq -0x40(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x1284
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x1327
jmp 0x1461
movq 0x2a8a(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
leaq 0xabf(%rip), %rdi       # 0x2037
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
callq 0x10b0
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x1284
leaq 0xaaf(%rip), %rdi       # 0x2045
movq %r15, %rsi
callq 0x1284
leaq -0x40(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x1284
jmp 0x1461
leaq 0xa4e(%rip), %rdi       # 0x2004
callq 0x11dc
leaq 0xa58(%rip), %rdi       # 0x201a
callq 0x11dc