// File: /workspace/github_c_makefile_star2_O1/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 0x178c
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x1090
movl %eax, 0x2ca7(%rip)      # 0x4090
cmpl $-0x1, %eax
je 0x1798
movl 0x2c98(%rip), %edi      # 0x4090
xorl %ebx, %ebx
xorl %esi, %esi
movl $0x2, %edx
callq 0x10c0
movq %rax, -0x50(%rbp)
bswapq %rax
movq %rax, -0x68(%rbp)
movq 0x2c68(%rip), %rax      # 0x4080
bswapq %rax
movq %rax, -0x60(%rbp)
movl 0x2c67(%rip), %eax      # 0x408c
bswapl %eax
movl %eax, -0x2c(%rbp)
leaq 0xbfd(%rip), %r14       # 0x202e
leaq (%r14,%rbx), %rsi
movl $0x8, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x1431
xorl %ebx, %ebx
leaq (%rbx,%rbp), %rsi
addq $-0x60, %rsi
movl $0x8, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x1454
xorl %ebx, %ebx
leaq (%rbx,%rbp), %rsi
addq $-0x68, %rsi
movl $0x8, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x147b
xorl %ebx, %ebx
leaq 0xb95(%rip), %r14       # 0x203e
xorl %esi, %esi
addq %r14, %rsi
movl $0x1, %edx
movl $0x1, %edi
callq 0x1040
movslq %eax, %rsi
shlq $0x20, %rax
je 0x14ab
incl %ebx
cmpl $0x80, %ebx
jne 0x14a9
leaq 0xb69(%rip), %r13       # 0x2040
movl $0x100000, %r14d        # imm = 0x100000
leaq 0x2bcc(%rip), %r15      # 0x40b0
movl $0x4, %esi
leaq -0x38(%rbp), %rdi
callq 0x12d6
movl $0x4, %esi
leaq -0x34(%rbp), %rdi
callq 0x12d6
movl $0x8, %esi
leaq -0x40(%rbp), %rdi
callq 0x12d6
movl $0x8, %esi
leaq -0x48(%rbp), %rdi
callq 0x12d6
movl $0x4, %esi
leaq -0x30(%rbp), %rdi
callq 0x12d6
movl -0x38(%rbp), %edx
bswapl %edx
movl %edx, -0x38(%rbp)
movl -0x34(%rbp), %eax
bswapl %eax
movl %eax, -0x34(%rbp)
movq -0x48(%rbp), %rcx
bswapq %rcx
movq %rcx, -0x48(%rbp)
movl -0x30(%rbp), %r12d
movl %r12d, %esi
bswapl %esi
movl %esi, -0x30(%rbp)
testq %rcx, %rcx
js 0x16ef
cmpl 0x2b28(%rip), %edx      # 0x4088
jne 0x16ef
movl %esi, %ebx
leaq (%rcx,%rbx), %rdx
cmpq -0x50(%rbp), %rdx
ja 0x16ef
movq %rcx, -0x58(%rbp)
cmpl $0x1, %eax
je 0x163c
testl %eax, %eax
jne 0x16e4
xorl %r14d, %r14d
leaq (%r14,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r14, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x4, %r14
jb 0x158e
xorl %r14d, %r14d
leaq (%r14,%r13), %rsi
movl $0x4, %edx
subq %r14, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x4, %r14
jb 0x15b7
xorl %r14d, %r14d
leaq (%r14,%rbp), %rsi
addq $-0x40, %rsi
movl $0x8, %edx
subq %r14, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x8, %r14
jb 0x15dc
testl %r12d, %r12d
je 0x16e4
xorl %r14d, %r14d
movl 0x2a7c(%rip), %esi      # 0x4090
leaq -0x58(,%r14,8), %rdx
addq %rbp, %rdx
movq %rbx, %rcx
subq %r14, %rcx
movl $0x1, %edi
callq 0x1322
addq %rax, %r14
cmpq %rbx, %r14
jb 0x160e
jmp 0x16e4
testl %r12d, %r12d
je 0x1673
cmpq %r14, %rbx
movl $0x100000, %r12d        # imm = 0x100000
cmovlq %rbx, %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x12d6
movq %r15, %rdi
movq %r12, %rsi
callq 0x120e
leaq -0x100000(%rbx), %rax
cmpq %r14, %rbx
movq %rax, %rbx
jg 0x1641
xorl %ebx, %ebx
leaq (%rbx,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x4, %rbx
jb 0x1675
xorl %ebx, %ebx
leaq (%rbx,%r13), %rsi
movl $0x4, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x4, %rbx
jb 0x169c
xorl %ebx, %ebx
leaq (%rbx,%rbp), %rsi
addq $-0x40, %rsi
movl $0x8, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x16bf
movl $0x100000, %r14d        # imm = 0x100000
jmp 0x14e4
movq 0x2902(%rip), %rax      # 0x3ff8
movq (%rax), %rcx
movl $0x6, %esi
movl $0x1, %edx
leaq 0x92d(%rip), %rdi       # 0x2037
callq 0x10b0
xorl %ebx, %ebx
leaq (%rbx,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x4, %rbx
jb 0x1711
xorl %ebx, %ebx
leaq 0x906(%rip), %r12       # 0x2045
leaq (%r12,%rbx), %rsi
movl $0x4, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x4, %rbx
jb 0x173f
xorl %ebx, %ebx
leaq (%rbx,%rbp), %rsi
addq $-0x40, %rsi
movl $0x8, %edx
subq %rbx, %rdx
movl $0x1, %edi
callq 0x1040
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x1762
jmp 0x14e4
leaq 0x871(%rip), %rdi       # 0x2004
callq 0x11dc
leaq 0x87b(%rip), %rdi       # 0x201a
callq 0x11dc