// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x167b
leaq 0x98e(%rip), %rdi       # 0x2004
callq 0x1240
cmpl $0x3, -0x8(%rbp)
jge 0x168d
leaq 0x992(%rip), %rdi       # 0x201a
callq 0x1240
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x101002, %esi         # imm = 0x101002
movb $0x0, %al
callq 0x10e0
movl %eax, 0x2a19(%rip)      # 0x40c0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x2a0e(%rip), %eax      # 0x40c0
jne 0x16c0
leaq 0x978(%rip), %rdi       # 0x2033
callq 0x1240
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1130
movl %eax, 0x102a10(%rip)    # 0x1040e4
movw $0x1, 0x102a0b(%rip)    # 0x1040e8
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0x1029fe(%rip), %rdi    # 0x1040ea
callq 0x1050
leaq 0x1029f0(%rip), %rdi    # 0x1040e8
addq $0x2, %rdi
callq 0x1040
movl 0x1029dd(%rip), %eax    # 0x1040e4
movl %eax, -0x74(%rbp)
leaq 0x1029d7(%rip), %rax    # 0x1040e8
movq %rax, -0x18(%rbp)
leaq 0x1029ce(%rip), %rdi    # 0x1040ea
callq 0x1070
movl -0x74(%rbp), %edi
addq $0x2, %rax
movl %eax, %edx
movq -0x18(%rbp), %rsi
callq 0x10d0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x1752
callq 0x1030
movl (%rax), %edi
callq 0x1120
movq %rax, %rdi
callq 0x1240
movl 0x10298c(%rip), %edi    # 0x1040e4
xorl %esi, %esi
callq 0x10c0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x177e
callq 0x1030
movl (%rax), %edi
callq 0x1120
movq %rax, %rdi
callq 0x1240
movl 0x102960(%rip), %edi    # 0x1040e4
movq $0x0, -0x20(%rbp)
movq -0x20(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x10f0
movl %eax, 0x102941(%rip)    # 0x1040e0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x102936(%rip), %eax    # 0x1040e0
jne 0x17c0
callq 0x1030
movl (%rax), %edi
callq 0x1120
movq %rax, %rdi
callq 0x1240
movl 0x28fa(%rip), %edi      # 0x40c0
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1110
movq %rax, -0x28(%rbp)
movq -0x28(%rbp), %rdi
callq 0x1570
movq %rax, -0x30(%rbp)
movq 0x28c4(%rip), %rdi      # 0x40b0
callq 0x1570
movq %rax, -0x38(%rbp)
movl 0x28c1(%rip), %edi      # 0x40bc
callq 0x1500
movl %eax, -0x3c(%rbp)
leaq 0x83d(%rip), %rdi       # 0x2047
movl $0x8, %esi
callq 0x1340
leaq -0x38(%rbp), %rdi
movl $0x8, %esi
callq 0x1340
leaq -0x30(%rbp), %rdi
movl $0x8, %esi
callq 0x1340
movl $0x0, -0x40(%rbp)
cmpl $0x80, -0x40(%rbp)
jge 0x185c
leaq 0x809(%rip), %rdi       # 0x2050
movl $0x1, %esi
callq 0x1340
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x1837
jmp 0x185e
leaq -0x44(%rbp), %rdi
movl $0x4, %esi
callq 0x13a0
leaq -0x48(%rbp), %rdi
movl $0x4, %esi
callq 0x13a0
leaq -0x54(%rbp), %rdi
movl $0x8, %esi
callq 0x13a0
leaq -0x60(%rbp), %rdi
movl $0x8, %esi
callq 0x13a0
leaq -0x4c(%rbp), %rdi
movl $0x4, %esi
callq 0x13a0
leaq -0x44(%rbp), %rdi
callq 0x14d0
leaq -0x48(%rbp), %rdi
callq 0x14d0
leaq -0x60(%rbp), %rdi
callq 0x1540
leaq -0x4c(%rbp), %rdi
callq 0x14d0
movl -0x44(%rbp), %eax
cmpl 0x27e7(%rip), %eax      # 0x40b8
jne 0x19de
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmpq %rax, -0x60(%rbp)
jae 0x19de
movl -0x4c(%rbp), %eax
addq -0x60(%rbp), %rax
cmpq -0x28(%rbp), %rax
ja 0x19de
movl -0x4c(%rbp), %eax
movq %rax, -0x68(%rbp)
movq -0x60(%rbp), %rax
movq %rax, -0x70(%rbp)
movl 0x27af(%rip), %edi      # 0x40c0
movq -0x60(%rbp), %rsi
xorl %edx, %edx
callq 0x1110
cmpl $0x0, -0x48(%rbp)
jne 0x195e
leaq -0x3c(%rbp), %rdi
movl $0x4, %esi
callq 0x1340
leaq 0x71b(%rip), %rdi       # 0x2052
movl $0x4, %esi
callq 0x1340
leaq -0x54(%rbp), %rdi
movl $0x8, %esi
callq 0x1340
movq -0x68(%rbp), %rsi
leaq -0x70(%rbp), %rdi
callq 0x1470
jmp 0x19dc
cmpl $0x1, -0x48(%rbp)
jne 0x19da
jmp 0x1966
cmpq $0x0, -0x68(%rbp)
jle 0x19ad
movq -0x68(%rbp), %rdi
callq 0x1620
movq %rax, %rsi
leaq 0x2760(%rip), %rdi      # 0x40e0
callq 0x13a0
movq -0x68(%rbp), %rdi
callq 0x1620
movq %rax, %rsi
leaq 0x2748(%rip), %rdi      # 0x40e0
callq 0x1280
movq -0x68(%rbp), %rax
subq $0x100000, %rax         # imm = 0x100000
movq %rax, -0x68(%rbp)
jmp 0x1966
leaq -0x3c(%rbp), %rdi
movl $0x4, %esi
callq 0x1340
leaq 0x690(%rip), %rdi       # 0x2052
movl $0x4, %esi
callq 0x1340
leaq -0x54(%rbp), %rdi
movl $0x8, %esi
callq 0x1340
jmp 0x19dc
jmp 0x1a0b
leaq -0x3c(%rbp), %rdi
movl $0x4, %esi
callq 0x1340
leaq 0x664(%rip), %rdi       # 0x2057
movl $0x4, %esi
callq 0x1340
leaq -0x54(%rbp), %rdi
movl $0x8, %esi
callq 0x1340
jmp 0x185e