// File: /workspace/github_c_makefile_star2_O1/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 0x18b8
cmpl $0x2, %edi
je 0x18c4
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 0x18d0
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 0x18a4
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 0x18a4
movl 0x102c66(%rip), %edi    # 0x1040d4
xorl %esi, %esi
callq 0x10a0
cmpl $-0x1, %eax
je 0x18a4
movl 0x102c50(%rip), %edi    # 0x1040d4
xorl %esi, %esi
xorl %edx, %edx
callq 0x10d0
movl %eax, 0x102c3d(%rip)    # 0x1040d0
cmpl $-0x1, %eax
je 0x18a4
leaq -0x3c(%rbp), %rcx
movl $0x1, (%rcx)
movl 0x102c24(%rip), %edi    # 0x1040d0
movl $0x1, %esi
movl $0x2, %edx
movl $0x4, %r8d
callq 0x1040
cmpl $-0x1, %eax
je 0x18a4
movl 0x102c00(%rip), %edi    # 0x1040d0
leaq -0x3c(%rbp), %rcx
movl $0x6, %esi
movl $0x1, %edx
movl $0x4, %r8d
callq 0x1040
cmpl $-0x1, %eax
je 0x18a4
movl 0x2bc0(%rip), %edi      # 0x40b8
xorl %ebx, %ebx
xorl %esi, %esi
movl $0x2, %edx
callq 0x1100
movq %rax, -0x58(%rbp)
bswapq %rax
movq %rax, -0x68(%rbp)
movq 0x2b90(%rip), %rax      # 0x40a8
bswapq %rax
movq %rax, -0x60(%rbp)
movl 0x2b8f(%rip), %eax      # 0x40b4
bswapl %eax
movl %eax, -0x2c(%rbp)
leaq 0xb14(%rip), %r14       # 0x2045
movl 0x102b99(%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 0x1531
xorl %ebx, %ebx
movl 0x102b75(%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 0x1555
xorl %ebx, %ebx
movl 0x102b4d(%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 0x157d
xorl %ebx, %ebx
leaq 0xaa2(%rip), %r14       # 0x204e
xorl %esi, %esi
addq %r14, %rsi
movl 0x102b19(%rip), %edi    # 0x1040d0
movl $0x1, %edx
callq 0x1050
movslq %eax, %rsi
shlq $0x20, %rax
je 0x15ae
incl %ebx
cmpl $0x80, %ebx
jne 0x15ac
movl $0x100000, %r14d        # imm = 0x100000
leaq 0xa74(%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 -0x50(%rbp), %rdi
callq 0x1327
movl $0x4, %esi
leaq -0x30(%rbp), %rdi
callq 0x1327
movl -0x38(%rbp), %eax
bswapl %eax
movl %eax, -0x38(%rbp)
movl -0x34(%rbp), %r13d
bswapl %r13d
movl %r13d, -0x34(%rbp)
movq -0x50(%rbp), %rsi
bswapq %rsi
movq %rsi, -0x50(%rbp)
movl -0x30(%rbp), %r15d
movl %r15d, %ecx
bswapl %ecx
movl %ecx, -0x30(%rbp)
testq %rsi, %rsi
js 0x166b
cmpl 0x2a54(%rip), %eax      # 0x40b0
jne 0x166b
movl %ecx, %r12d
leaq (%rsi,%r12), %rax
cmpq -0x58(%rbp), %rax
jbe 0x16ea
xorl %r15d, %r15d
movl 0x102a5c(%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 0x166e
xorl %r15d, %r15d
movl 0x102a32(%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 0x1698
xorl %r15d, %r15d
movl 0x102a0c(%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 0x16be
jmp 0x15e1
movl 0x29c8(%rip), %edi      # 0x40b8
xorl %edx, %edx
callq 0x1100
cmpl $0x1, %r13d
je 0x17df
testl %r13d, %r13d
jne 0x15e1
xorl %r13d, %r13d
movl 0x1029bd(%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 0x170d
xorl %r13d, %r13d
leaq 0x90f(%rip), %r14       # 0x2050
movl 0x102989(%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 0x1741
xorl %r13d, %r13d
movl $0x100000, %r14d        # imm = 0x100000
movl 0x10295d(%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 0x176d
testl %r15d, %r15d
leaq 0x292f(%rip), %r13      # 0x40d0
je 0x15e1
cmpq %r14, %r12
movl $0x100000, %r15d        # imm = 0x100000
cmovlq %r12, %r15
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 0x17a7
jmp 0x15e1
testl %r15d, %r15d
leaq 0x28e7(%rip), %r13      # 0x40d0
je 0x181e
cmpq %r14, %r12
movl $0x100000, %r15d        # imm = 0x100000
cmovlq %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
jg 0x17eb
xorl %r15d, %r15d
movl 0x1028a9(%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 0x1821
xorl %r15d, %r15d
leaq 0x7fe(%rip), %r12       # 0x2050
movl 0x102878(%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 0x1852
xorl %r15d, %r15d
movl 0x102852(%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 0x1878
jmp 0x15e1
callq 0x1030
movl (%rax), %edi
callq 0x1110
movq %rax, %rdi
callq 0x122c
leaq 0x745(%rip), %rdi       # 0x2004
callq 0x122c
leaq 0x74f(%rip), %rdi       # 0x201a
callq 0x122c
leaq 0x750(%rip), %rdi       # 0x2027
callq 0x122c