// File: /workspace/github_c_makefile_star2_O3/regularfry[P]tinynbd/src/tinynbd_tcp.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 port\n");}
  fd=open(argv[1],O_RDWR|O_SYNC); if(-1==fd){err("Couldn't open file\n");};
  int port=atoi(argv[2]);

  s = socket(AF_INET, SOCK_STREAM, 0);
  E(s);
  my_addr.sin_family = AF_INET;
  my_addr.sin_port = htons(port);
  my_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  memset(&(my_addr.sin_zero), '\0', 8);
  E(bind(s, (struct sockaddr *)&my_addr, sizeof(struct sockaddr)));
  E(listen(s,0));
  s2 = accept(s,NULL,NULL);
  E(s2);
  int y=1,n=0;
  E(setsockopt(s2, SOL_SOCKET, SO_REUSEADDR, &y, sizeof(y) ));
  E(setsockopt(s2, IPPROTO_TCP, TCP_NODELAY, &y, sizeof(y) ));

  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;
      lseek64(fd,f,SEEK_SET);
      if(t==0){
        W(&rmag,4);W("\0\0\0\0",4);W(h,8);
        for(;ll>0;ll-=B){r(rbuf,clip(ll));W(rbuf,clip(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 0x18a0
cmpl $0x2, %edi
je 0x18ac
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x10c0
movl %eax, 0x2cd8(%rip)      # 0x40b8
cmpl $-0x1, %eax
je 0x18b8
movq 0x10(%rbx), %rdi
callq 0x10e0
movl %eax, %ebx
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1120
movl %eax, 0x102cc9(%rip)    # 0x1040d4
cmpl $-0x1, %eax
je 0x188c
movw $0x2, 0x102cbb(%rip)    # 0x1040d8
leaq 0x102cb4(%rip), %r14    # 0x1040d8
rolw $0x8, %bx
movw %bx, 0x102cab(%rip)     # 0x1040da
leaq 0xc05(%rip), %rdi       # 0x203b
callq 0x1080
movl %eax, 0x102c9b(%rip)    # 0x1040dc
movq $0x0, 0x102c94(%rip) # 0x1040e0
movl 0x102c82(%rip), %edi    # 0x1040d4
movq %r14, %rsi
movl $0x10, %edx
callq 0x10b0
cmpl $-0x1, %eax
je 0x188c
movl 0x102c66(%rip), %edi    # 0x1040d4
xorl %esi, %esi
callq 0x10a0
cmpl $-0x1, %eax
je 0x188c
movl 0x102c50(%rip), %edi    # 0x1040d4
xorl %esi, %esi
xorl %edx, %edx
callq 0x10d0
movl %eax, 0x102c3d(%rip)    # 0x1040d0
cmpl $-0x1, %eax
je 0x188c
leaq -0x3c(%rbp), %rcx
movl $0x1, (%rcx)
movl %eax, %edi
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x1040
cmpl $-0x1, %eax
je 0x188c
movl 0x102c04(%rip), %edi    # 0x1040d0
leaq -0x3c(%rbp), %rcx
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0x1040
cmpl $-0x1, %eax
je 0x188c
movl 0x2bc4(%rip), %edi      # 0x40b8
xorl %ebx, %ebx
xorl %esi, %esi
movl $0x2, %edx
callq 0x1100
movq %rax, -0x50(%rbp)
bswapq %rax
movq %rax, -0x68(%rbp)
movq 0x2b94(%rip), %rax      # 0x40a8
bswapq %rax
movq %rax, -0x60(%rbp)
movl 0x2b93(%rip), %eax      # 0x40b4
bswapl %eax
movl %eax, -0x2c(%rbp)
leaq 0xb18(%rip), %r14       # 0x2045
movl 0x102b9d(%rip), %edi    # 0x1040d0
leaq (%r14,%rbx), %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1050
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x152d
xorl %ebx, %ebx
movl 0x102b79(%rip), %edi    # 0x1040d0
leaq (%rbx,%rbp), %rsi
addq $-0x60, %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1050
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x1551
xorl %ebx, %ebx
movl 0x102b51(%rip), %edi    # 0x1040d0
leaq (%rbx,%rbp), %rsi
addq $-0x68, %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1050
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x1579
xorl %r14d, %r14d
leaq 0xaa5(%rip), %rbx       # 0x204e
movl 0x102b21(%rip), %edi    # 0x1040d0
movl $0x1, %edx
movq %rbx, %rsi
callq 0x1050
testl %eax, %eax
je 0x15a9
incl %r14d
cmpl $0x80, %r14d
jne 0x15a9
movl $0x100000, %r14d        # imm = 0x100000
leaq 0xa7c(%rip), %rbx       # 0x2055
movl $0x4, %esi
leaq -0x38(%rbp), %rdi
callq 0x1327
movl $0x4, %esi
leaq -0x34(%rbp), %rdi
callq 0x1327
movl $0x8, %esi
leaq -0x44(%rbp), %rdi
callq 0x1327
movl $0x8, %esi
leaq -0x58(%rbp), %rdi
callq 0x1327
movl $0x4, %esi
leaq -0x30(%rbp), %rdi
callq 0x1327
movq -0x58(%rbp), %rsi
bswapq %rsi
testq %rsi, %rsi
js 0x164c
movl -0x38(%rbp), %eax
bswapl %eax
cmpl 0x2a7a(%rip), %eax      # 0x40b0
jne 0x164c
movl -0x30(%rbp), %r15d
movl %r15d, %r12d
bswapl %r12d
leaq (%rsi,%r12), %rax
cmpq -0x50(%rbp), %rax
jbe 0x16cb
xorl %r15d, %r15d
movl 0x102a7b(%rip), %edi    # 0x1040d0
leaq (%r15,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1050
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x164f
xorl %r15d, %r15d
movl 0x102a51(%rip), %edi    # 0x1040d0
leaq (%rbx,%r15), %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1050
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x1679
xorl %r15d, %r15d
movl 0x102a2b(%rip), %edi    # 0x1040d0
leaq (%r15,%rbp), %rsi
addq $-0x44, %rsi
movl $0x8, %edx
subq %r15, %rdx
callq 0x1050
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x8, %r15
jb 0x169f
jmp 0x15d9
movl -0x34(%rbp), %r13d
bswapl %r13d
movl 0x29e0(%rip), %edi      # 0x40b8
xorl %edx, %edx
callq 0x1100
cmpl $0x1, %r13d
je 0x17c7
testl %r13d, %r13d
jne 0x15d9
xorl %r13d, %r13d
movl 0x1029d5(%rip), %edi    # 0x1040d0
leaq -0x2c(,%r13), %rsi
addq %rbp, %rsi
movl $0x4, %edx
subq %r13, %rdx
callq 0x1050
addl %eax, %r13d
movslq %r13d, %r13
cmpq $0x4, %r13
jb 0x16f5
xorl %r13d, %r13d
leaq 0x927(%rip), %r14       # 0x2050
movl 0x1029a1(%rip), %edi    # 0x1040d0
leaq (%r14,%r13), %rsi
movl $0x4, %edx
subq %r13, %rdx
callq 0x1050
addl %eax, %r13d
movslq %r13d, %r13
cmpq $0x4, %r13
jb 0x1729
xorl %r13d, %r13d
movl $0x100000, %r14d        # imm = 0x100000
movl 0x102975(%rip), %edi    # 0x1040d0
leaq -0x44(,%r13), %rsi
addq %rbp, %rsi
movl $0x8, %edx
subq %r13, %rdx
callq 0x1050
addl %eax, %r13d
movslq %r13d, %r13
cmpq $0x8, %r13
jb 0x1755
testl %r15d, %r15d
je 0x15d9
cmpq %r14, %r12
movl $0x100000, %r15d        # imm = 0x100000
cmovbq %r12, %r15
leaq 0x2934(%rip), %r13      # 0x40d0
movq %r13, %rdi
movq %r15, %rsi
callq 0x12a1
movq %r13, %rdi
movq %r15, %rsi
callq 0x12e4
leaq -0x100000(%r12), %rax
cmpq %r14, %r12
movq %rax, %r12
jg 0x1788
jmp 0x15d9
testl %r15d, %r15d
leaq 0x28ff(%rip), %r13      # 0x40d0
je 0x1806
cmpq %r14, %r12
movl $0x100000, %r15d        # imm = 0x100000
cmovbq %r12, %r15
movq %r13, %rdi
movq %r15, %rsi
callq 0x1327
movq %r13, %rdi
movq %r15, %rsi
callq 0x125e
leaq -0x100000(%r12), %rax
cmpq %r14, %r12
movq %rax, %r12
ja 0x17d3
xorl %r15d, %r15d
movl 0x1028c1(%rip), %edi    # 0x1040d0
leaq (%r15,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1050
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x1809
xorl %r15d, %r15d
leaq 0x816(%rip), %r12       # 0x2050
movl 0x102890(%rip), %edi    # 0x1040d0
leaq (%r12,%r15), %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1050
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x183a
xorl %r15d, %r15d
movl 0x10286a(%rip), %edi    # 0x1040d0
leaq (%r15,%rbp), %rsi
addq $-0x44, %rsi
movl $0x8, %edx
subq %r15, %rdx
callq 0x1050
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x8, %r15
jb 0x1860
jmp 0x15d9
callq 0x1030
movl (%rax), %edi
callq 0x1110
movq %rax, %rdi
callq 0x122c
leaq 0x75d(%rip), %rdi       # 0x2004
callq 0x122c
leaq 0x767(%rip), %rdi       # 0x201a
callq 0x122c
leaq 0x768(%rip), %rdi       # 0x2027
callq 0x122c