// File: /workspace/github_c_makefile_star2_O3/regularfry[P]tinynbd/src/tinynbd_socket_sendfile.c
// Function: main
int main(int argc, char **argv){
  if(argc<2){err("Need a file to serve\n");}
  if(argc<3){err("Need a unix socket path\n");}
  fd=open(argv[1],O_RDWR|O_SYNC); if(-1==fd){err("Couldn't open file\n");};

  s=socket(AF_UNIX,SOCK_STREAM,0);
  sock.sun_family=AF_UNIX;
  strcpy(sock.sun_path,argv[2]);
  unlink(sock.sun_path);
  E(bind(s,(struct sockaddr *)&sock, strlen(sock.sun_path)+sizeof(sock.sun_family)));
  E(listen(s,0));
  s2 = accept(s,NULL,NULL);
  E(s2);

  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;
      lseek64(fd,f,SEEK_SET);
      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 {
      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 0x188f
cmpl $0x2, %edi
je 0x189b
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x10e0
movl %eax, 0x2c65(%rip)      # 0x40c0
cmpl $-0x1, %eax
je 0x18a7
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1130
movl %eax, 0x102c69(%rip)    # 0x1040e4
movw $0x1, 0x102c64(%rip)    # 0x1040e8
leaq 0x102c5d(%rip), %r14    # 0x1040e8
movq 0x10(%rbx), %rsi
leaq 0x102c54(%rip), %rbx    # 0x1040ea
movq %rbx, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1040
movl 0x102c37(%rip), %r15d   # 0x1040e4
movq %rbx, %rdi
callq 0x1070
leal 0x2(%rax), %edx
movl %r15d, %edi
movq %r14, %rsi
callq 0x10d0
cmpl $-0x1, %eax
je 0x187b
movl 0x102c12(%rip), %edi    # 0x1040e4
xorl %esi, %esi
callq 0x10c0
cmpl $-0x1, %eax
je 0x187b
movl 0x102bfc(%rip), %edi    # 0x1040e4
xorl %esi, %esi
xorl %edx, %edx
callq 0x10f0
movl %eax, 0x102be9(%rip)    # 0x1040e0
cmpl $-0x1, %eax
je 0x187b
movl 0x2bba(%rip), %edi      # 0x40c0
xorl %ebx, %ebx
xorl %esi, %esi
movl $0x2, %edx
callq 0x1110
movq %rax, -0x48(%rbp)
bswapq %rax
movq %rax, -0x68(%rbp)
movq 0x2b8a(%rip), %rax      # 0x40b0
bswapq %rax
movq %rax, -0x60(%rbp)
movl 0x2b89(%rip), %eax      # 0x40bc
bswapl %eax
movl %eax, -0x34(%rbp)
leaq 0xb08(%rip), %r14       # 0x2047
movl 0x102b9b(%rip), %edi    # 0x1040e0
leaq (%r14,%rbx), %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x153f
xorl %ebx, %ebx
movl 0x102b77(%rip), %edi    # 0x1040e0
leaq (%rbx,%rbp), %rsi
addq $-0x60, %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x1563
xorl %ebx, %ebx
movl 0x102b4f(%rip), %edi    # 0x1040e0
leaq (%rbx,%rbp), %rsi
addq $-0x68, %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x158b
xorl %r14d, %r14d
leaq 0xa95(%rip), %rbx       # 0x2050
movl 0x102b1f(%rip), %edi    # 0x1040e0
movl $0x1, %edx
movq %rbx, %rsi
callq 0x1060
testl %eax, %eax
je 0x15bb
incl %r14d
cmpl $0x80, %r14d
jne 0x15bb
leaq -0x2c(%rbp), %r14
leaq -0x30(%rbp), %r13
leaq 0xa6a(%rip), %rbx       # 0x2057
movl $0x4, %esi
movq %r14, %rdi
callq 0x1337
movl $0x4, %esi
movq %r13, %rdi
callq 0x1337
movl $0x8, %esi
leaq -0x40(%rbp), %rdi
callq 0x1337
movl $0x8, %esi
leaq -0x58(%rbp), %rdi
callq 0x1337
movl $0x4, %esi
leaq -0x38(%rbp), %rdi
callq 0x1337
movq -0x58(%rbp), %rsi
bswapq %rsi
testq %rsi, %rsi
js 0x165e
movl -0x2c(%rbp), %eax
bswapl %eax
cmpl 0x2a70(%rip), %eax      # 0x40b8
jne 0x165e
movl -0x38(%rbp), %r12d
movl %r12d, %r15d
bswapl %r15d
leaq (%rsi,%r15), %rax
cmpq -0x48(%rbp), %rax
jbe 0x16dd
xorl %r15d, %r15d
movl 0x102a79(%rip), %edi    # 0x1040e0
leaq (%r15,%rbp), %rsi
addq $-0x34, %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1060
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x1661
xorl %r15d, %r15d
movl 0x102a4f(%rip), %edi    # 0x1040e0
leaq (%rbx,%r15), %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1060
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x168b
xorl %r15d, %r15d
movl 0x102a29(%rip), %edi    # 0x1040e0
leaq (%r15,%rbp), %rsi
addq $-0x40, %rsi
movl $0x8, %edx
subq %r15, %rdx
callq 0x1060
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x8, %r15
jb 0x16b1
jmp 0x15ed
movl -0x30(%rbp), %r14d
bswapl %r14d
movq %rsi, -0x50(%rbp)
movl 0x29d2(%rip), %edi      # 0x40c0
xorl %edx, %edx
callq 0x1110
cmpl $0x1, %r14d
je 0x17a6
testl %r14d, %r14d
leaq -0x2c(%rbp), %r14
leaq -0x30(%rbp), %r13
jne 0x15ed
xorl %r12d, %r12d
movl 0x1029c7(%rip), %edi    # 0x1040e0
leaq (%r12,%rbp), %rsi
addq $-0x34, %rsi
movl $0x4, %edx
subq %r12, %rdx
callq 0x1060
addl %eax, %r12d
movslq %r12d, %r12
cmpq $0x4, %r12
jb 0x1713
xorl %r12d, %r12d
leaq 0x90e(%rip), %r14       # 0x2052
movl 0x102996(%rip), %edi    # 0x1040e0
leaq (%r14,%r12), %rsi
movl $0x4, %edx
subq %r12, %rdx
callq 0x1060
addl %eax, %r12d
movslq %r12d, %r12
cmpq $0x4, %r12
jb 0x1744
xorl %r12d, %r12d
leaq -0x2c(%rbp), %r14
movl 0x10296c(%rip), %edi    # 0x1040e0
leaq (%r12,%rbp), %rsi
addq $-0x40, %rsi
movl $0x8, %edx
subq %r12, %rdx
callq 0x1060
addl %eax, %r12d
movslq %r12d, %r12
cmpq $0x8, %r12
jb 0x176e
leaq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x13ac
jmp 0x15ed
testl %r12d, %r12d
movl $0x100000, %r14d        # imm = 0x100000
leaq 0x292a(%rip), %r13      # 0x40e0
je 0x17ea
cmpq %r14, %r15
movl $0x100000, %r12d        # imm = 0x100000
cmovbq %r15, %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x1337
movq %r13, %rdi
movq %r12, %rsi
callq 0x126e
leaq -0x100000(%r15), %rax
cmpq %r14, %r15
movq %rax, %r15
ja 0x17b8
xorl %r15d, %r15d
leaq -0x2c(%rbp), %r12
movl 0x1028e9(%rip), %edi    # 0x1040e0
leaq (%r15,%rbp), %rsi
addq $-0x34, %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1060
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x17f1
xorl %r15d, %r15d
leaq -0x30(%rbp), %r13
leaq 0x82c(%rip), %r14       # 0x2052
movl 0x1028b4(%rip), %edi    # 0x1040e0
leaq (%r14,%r15), %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1060
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x1826
xorl %r15d, %r15d
movq %r12, %r14
movl 0x10288b(%rip), %edi    # 0x1040e0
leaq (%r15,%rbp), %rsi
addq $-0x40, %rsi
movl $0x8, %edx
subq %r15, %rdx
callq 0x1060
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x8, %r15
jb 0x184f
jmp 0x15ed
callq 0x1030
movl (%rax), %edi
callq 0x1120
movq %rax, %rdi
callq 0x123c
leaq 0x76e(%rip), %rdi       # 0x2004
callq 0x123c
leaq 0x778(%rip), %rdi       # 0x201a
callq 0x123c
leaq 0x785(%rip), %rdi       # 0x2033
callq 0x123c