// File: /workspace/github_c_makefile_star2_O1/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 0x1886
cmpl $0x2, %edi
je 0x1892
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 0x189e
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 0x1872
movl 0x102c12(%rip), %edi    # 0x1040e4
xorl %esi, %esi
callq 0x10c0
cmpl $-0x1, %eax
je 0x1872
movl 0x102bfc(%rip), %edi    # 0x1040e4
xorl %esi, %esi
xorl %edx, %edx
callq 0x10f0
movl %eax, 0x102be9(%rip)    # 0x1040e0
cmpl $-0x1, %eax
je 0x1872
movl 0x2bba(%rip), %edi      # 0x40c0
xorl %ebx, %ebx
xorl %esi, %esi
movl $0x2, %edx
callq 0x1110
movq %rax, -0x50(%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, -0x2c(%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 %ebx, %ebx
leaq 0xa96(%rip), %r14       # 0x2050
xorl %esi, %esi
addq %r14, %rsi
movl 0x102b1b(%rip), %edi    # 0x1040e0
movl $0x1, %edx
callq 0x1060
movslq %eax, %rsi
shlq $0x20, %rax
je 0x15bc
incl %ebx
cmpl $0x80, %ebx
jne 0x15ba
leaq 0xa69(%rip), %r13       # 0x2052
leaq 0xa67(%rip), %rbx       # 0x2057
movl $0x4, %esi
leaq -0x38(%rbp), %rdi
callq 0x1337
movl $0x4, %esi
leaq -0x34(%rbp), %rdi
callq 0x1337
movl $0x8, %esi
leaq -0x40(%rbp), %rdi
callq 0x1337
movl $0x8, %esi
leaq -0x48(%rbp), %rdi
callq 0x1337
movl $0x4, %esi
leaq -0x30(%rbp), %rdi
callq 0x1337
movl -0x38(%rbp), %eax
bswapl %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %r12d
bswapl %r12d
movl %r12d, -0x34(%rbp)
movq -0x48(%rbp), %rsi
bswapq %rsi
movq %rsi, -0x48(%rbp)
movl -0x30(%rbp), %r14d
movl %r14d, %ecx
bswapl %ecx
movl %ecx, -0x30(%rbp)
testq %rsi, %rsi
js 0x167a
cmpl 0x2a4d(%rip), %eax      # 0x40b8
jne 0x167a
movl %ecx, %r15d
leaq (%rsi,%r15), %rax
cmpq -0x50(%rbp), %rax
jbe 0x16f9
xorl %r14d, %r14d
movl 0x102a5d(%rip), %edi    # 0x1040e0
leaq (%r14,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r14, %rdx
callq 0x1060
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x4, %r14
jb 0x167d
xorl %r14d, %r14d
movl 0x102a33(%rip), %edi    # 0x1040e0
leaq (%rbx,%r14), %rsi
movl $0x4, %edx
subq %r14, %rdx
callq 0x1060
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x4, %r14
jb 0x16a7
xorl %r14d, %r14d
movl 0x102a0d(%rip), %edi    # 0x1040e0
leaq (%r14,%rbp), %rsi
addq $-0x40, %rsi
movl $0x8, %edx
subq %r14, %rdx
callq 0x1060
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x8, %r14
jb 0x16cd
jmp 0x15f0
movq %rsi, -0x58(%rbp)
movl 0x29bd(%rip), %edi      # 0x40c0
xorl %edx, %edx
callq 0x1110
cmpl $0x1, %r12d
je 0x17a8
testl %r12d, %r12d
jne 0x15f0
xorl %r14d, %r14d
movl 0x1029ba(%rip), %edi    # 0x1040e0
leaq (%r14,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r14, %rdx
callq 0x1060
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x4, %r14
jb 0x1720
xorl %r14d, %r14d
movl 0x102990(%rip), %edi    # 0x1040e0
leaq (%r14,%r13), %rsi
movl $0x4, %edx
subq %r14, %rdx
callq 0x1060
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x4, %r14
jb 0x174a
xorl %r14d, %r14d
movl 0x10296a(%rip), %edi    # 0x1040e0
leaq (%r14,%rbp), %rsi
addq $-0x40, %rsi
movl $0x8, %edx
subq %r14, %rdx
callq 0x1060
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x8, %r14
jb 0x1770
leaq -0x58(%rbp), %rdi
movq %r15, %rsi
callq 0x13ac
jmp 0x15f0
testl %r14d, %r14d
movl $0x100000, %r14d        # imm = 0x100000
leaq 0x2928(%rip), %r13      # 0x40e0
je 0x17ec
cmpq %r14, %r15
movl $0x100000, %r12d        # imm = 0x100000
cmovlq %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
jg 0x17ba
xorl %r14d, %r14d
movl 0x1028eb(%rip), %edi    # 0x1040e0
leaq (%r14,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r14, %rdx
callq 0x1060
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x4, %r14
jb 0x17ef
xorl %r14d, %r14d
leaq 0x832(%rip), %r13       # 0x2052
movl 0x1028ba(%rip), %edi    # 0x1040e0
leaq (%r14,%r13), %rsi
movl $0x4, %edx
subq %r14, %rdx
callq 0x1060
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x4, %r14
jb 0x1820
xorl %r14d, %r14d
movl 0x102894(%rip), %edi    # 0x1040e0
leaq (%r14,%rbp), %rsi
addq $-0x40, %rsi
movl $0x8, %edx
subq %r14, %rdx
callq 0x1060
addl %eax, %r14d
movslq %r14d, %r14
cmpq $0x8, %r14
jb 0x1846
jmp 0x15f0
callq 0x1030
movl (%rax), %edi
callq 0x1120
movq %rax, %rdi
callq 0x123c
leaq 0x777(%rip), %rdi       # 0x2004
callq 0x123c
leaq 0x781(%rip), %rdi       # 0x201a
callq 0x123c
leaq 0x78e(%rip), %rdi       # 0x2033
callq 0x123c