// File: /workspace/github_c_makefile_star2_O2/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 0x16ee
cmpl $0x2, %edi
je 0x16fa
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x10e0
movl %eax, 0x2c8c(%rip)      # 0x40c0
cmpl $-0x1, %eax
je 0x1706
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1130
movl %eax, 0x102c95(%rip)    # 0x1040e4
movw $0x1, 0x102c90(%rip)    # 0x1040e8
leaq 0x102c89(%rip), %r14    # 0x1040e8
movq 0x10(%rbx), %rsi
leaq 0x102c80(%rip), %rbx    # 0x1040ea
movq %rbx, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1040
movl 0x102c63(%rip), %r15d   # 0x1040e4
movq %rbx, %rdi
callq 0x1070
leal 0x2(%rax), %edx
movl %r15d, %edi
movq %r14, %rsi
callq 0x10d0
cmpl $-0x1, %eax
je 0x16da
movl 0x102c3e(%rip), %edi    # 0x1040e4
xorl %esi, %esi
callq 0x10c0
cmpl $-0x1, %eax
je 0x16da
movl 0x102c28(%rip), %edi    # 0x1040e4
xorl %esi, %esi
xorl %edx, %edx
callq 0x10f0
movl %eax, 0x102c15(%rip)    # 0x1040e0
cmpl $-0x1, %eax
je 0x16da
movl 0x2be6(%rip), %edi      # 0x40c0
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x1110
movq %rax, -0x40(%rbp)
bswapq %rax
leaq -0x60(%rbp), %rbx
movq %rax, (%rbx)
movq 0x2bb7(%rip), %rax      # 0x40b0
bswapq %rax
leaq -0x58(%rbp), %r14
movq %rax, (%r14)
movl 0x2bb3(%rip), %eax      # 0x40bc
bswapl %eax
movl %eax, -0x2c(%rbp)
leaq 0xb32(%rip), %rdi       # 0x2047
pushq $0x8
popq %r15
movq %r15, %rsi
callq 0x12e4
movq %r14, %rdi
movq %r15, %rsi
callq 0x12e4
movq %rbx, %rdi
movq %r15, %rsi
callq 0x12e4
movl $0x80, %r15d
leaq 0xb0c(%rip), %rbx       # 0x2050
pushq $0x1
popq %r14
subl $0x1, %r15d
jb 0x155b
movq %rbx, %rdi
movq %r14, %rsi
callq 0x12e4
jmp 0x1548
pushq $0x4
popq %r15
leaq -0x68(%rbp), %r12
pushq $0x8
popq %r13
leaq 0x2b72(%rip), %r14      # 0x40e0
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x1320
leaq -0x34(%rbp), %rdi
movq %r15, %rsi
callq 0x1320
movq %r12, %rbx
movq %r12, %rdi
movq %r13, %rsi
callq 0x1320
leaq -0x50(%rbp), %rdi
movq %r13, %rsi
callq 0x1320
leaq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x1320
movq -0x50(%rbp), %rsi
bswapq %rsi
testq %rsi, %rsi
js 0x15d6
movl -0x38(%rbp), %eax
bswapl %eax
cmpl 0x2af5(%rip), %eax      # 0x40b8
jne 0x15d6
movl -0x30(%rbp), %r12d
bswapl %r12d
leaq (%rsi,%r12), %rax
cmpq -0x40(%rbp), %rax
jbe 0x1604
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x12e4
leaq 0xa6e(%rip), %rdi       # 0x2057
movq %r15, %rsi
callq 0x12e4
movq %rbx, %r12
movq %rbx, %rdi
movq %r13, %rsi
callq 0x12e4
jmp 0x156e
movq %r13, %r15
movl -0x34(%rbp), %r13d
bswapl %r13d
movq %rsi, -0x48(%rbp)
movl 0x2aa8(%rip), %edi      # 0x40c0
xorl %edx, %edx
callq 0x1110
testl %r13d, %r13d
je 0x1695
cmpl $0x1, %r13d
movq %r15, %r13
je 0x1664
jmp 0x16ce
movl $0x100000, %eax         # imm = 0x100000
cmpq %rax, %r12
movl $0x100000, %r13d        # imm = 0x100000
cmovbq %r12, %r13
movq %r14, %rdi
movq %r13, %rsi
callq 0x1320
movq %r14, %rdi
movq %r13, %rsi
movq %r15, %r13
callq 0x126c
addq $-0x100000, %r12        # imm = 0xFFF00000
testq %r12, %r12
jg 0x1632
leaq -0x2c(%rbp), %rdi
pushq $0x4
popq %r15
movq %r15, %rsi
callq 0x12e4
leaq 0x9d2(%rip), %rdi       # 0x2052
movq %r15, %rsi
callq 0x12e4
movq %rbx, %rdi
movq %r13, %rsi
callq 0x12e4
jmp 0x16ce
leaq -0x2c(%rbp), %rdi
pushq $0x4
popq %r13
movq %r13, %rsi
callq 0x12e4
leaq 0x9a6(%rip), %rdi       # 0x2052
movq %r13, %rsi
callq 0x12e4
movq %rbx, %rdi
movq %r15, %r13
movq %r15, %rsi
callq 0x12e4
leaq -0x48(%rbp), %rdi
movq %r12, %rsi
callq 0x138b
movq %rbx, %r12
pushq $0x4
popq %r15
jmp 0x156e
callq 0x1030
movl (%rax), %edi
callq 0x1120
movq %rax, %rdi
callq 0x123c
leaq 0x90f(%rip), %rdi       # 0x2004
callq 0x123c
leaq 0x919(%rip), %rdi       # 0x201a
callq 0x123c
leaq 0x926(%rip), %rdi       # 0x2033
callq 0x123c