// File: /workspace/github_c_makefile_star2_O2/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 0x16b9
cmpl $0x2, %edi
je 0x16c5
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x10c0
movl %eax, 0x2cf9(%rip)      # 0x40b8
cmpl $-0x1, %eax
je 0x16d1
movq 0x10(%rbx), %rdi
callq 0x10e0
movl %eax, %ebx
pushq $0x2
popq %rdi
pushq $0x1
popq %rsi
xorl %edx, %edx
callq 0x1120
movl %eax, 0x102cee(%rip)    # 0x1040d4
cmpl $-0x1, %eax
je 0x16a5
movw $0x2, 0x102ce0(%rip)    # 0x1040d8
leaq 0x102cd9(%rip), %r14    # 0x1040d8
rolw $0x8, %bx
movw %bx, 0x102cd0(%rip)     # 0x1040da
leaq 0xc2a(%rip), %rdi       # 0x203b
callq 0x1080
movl %eax, 0x102cc0(%rip)    # 0x1040dc
andq $0x0, 0x102cbc(%rip)    # 0x1040e0
movl 0x102caa(%rip), %edi    # 0x1040d4
pushq $0x10
popq %rdx
movq %r14, %rsi
callq 0x10b0
cmpl $-0x1, %eax
je 0x16a5
movl 0x102c90(%rip), %edi    # 0x1040d4
xorl %esi, %esi
callq 0x10a0
cmpl $-0x1, %eax
je 0x16a5
movl 0x102c7a(%rip), %edi    # 0x1040d4
xorl %esi, %esi
xorl %edx, %edx
callq 0x10d0
movl %eax, 0x102c67(%rip)    # 0x1040d0
cmpl $-0x1, %eax
je 0x16a5
leaq -0x3c(%rbp), %rcx
movl $0x1, (%rcx)
pushq $0x1
popq %rsi
pushq $0x2
popq %rdx
pushq $0x4
popq %r8
movl %eax, %edi
callq 0x1040
cmpl $-0x1, %eax
je 0x16a5
movl 0x102c34(%rip), %edi    # 0x1040d0
pushq $0x6
popq %rsi
pushq $0x1
popq %rdx
leaq -0x3c(%rbp), %rcx
pushq $0x4
popq %r8
callq 0x1040
cmpl $-0x1, %eax
je 0x16a5
movl 0x2bfa(%rip), %edi      # 0x40b8
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x1100
movq %rax, -0x50(%rbp)
bswapq %rax
leaq -0x68(%rbp), %rbx
movq %rax, (%rbx)
movq 0x2bcb(%rip), %rax      # 0x40a8
bswapq %rax
leaq -0x60(%rbp), %r14
movq %rax, (%r14)
movl 0x2bc7(%rip), %eax      # 0x40b4
bswapl %eax
movl %eax, -0x2c(%rbp)
leaq 0xb4c(%rip), %rdi       # 0x2045
pushq $0x8
popq %r15
movq %r15, %rsi
callq 0x12d4
movq %r14, %rdi
movq %r15, %rsi
callq 0x12d4
movq %rbx, %rdi
movq %r15, %rsi
callq 0x12d4
movl $0x80, %r15d
leaq 0xb26(%rip), %rbx       # 0x204e
pushq $0x1
popq %r14
subl $0x1, %r15d
jb 0x153f
movq %rbx, %rdi
movq %r14, %rsi
callq 0x12d4
jmp 0x152c
pushq $0x4
popq %r15
movl $0x100000, %r13d        # imm = 0x100000
leaq 0x2b80(%rip), %r14      # 0x40d0
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x1310
leaq -0x34(%rbp), %rdi
movq %r15, %rsi
callq 0x1310
leaq -0x44(%rbp), %rdi
pushq $0x8
popq %rbx
movq %rbx, %rsi
callq 0x1310
leaq -0x58(%rbp), %rdi
movq %rbx, %rsi
callq 0x1310
leaq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x1310
movq -0x58(%rbp), %rsi
bswapq %rsi
leaq 0xab8(%rip), %rcx       # 0x2055
movq %rcx, %r12
testq %rsi, %rsi
js 0x1621
movl -0x38(%rbp), %eax
bswapl %eax
movq %rcx, %r12
cmpl 0x2afd(%rip), %eax      # 0x40b0
jne 0x1621
movl -0x30(%rbp), %ebx
bswapl %ebx
leaq (%rsi,%rbx), %rax
movq %rcx, %r12
cmpq -0x50(%rbp), %rax
ja 0x1621
movl -0x34(%rbp), %r12d
bswapl %r12d
movl 0x2ae4(%rip), %edi      # 0x40b8
xorl %edx, %edx
callq 0x1100
testl %r12d, %r12d
je 0x1649
cmpl $0x1, %r12d
je 0x1615
jmp 0x1550
cmpq %r13, %rbx
movl $0x100000, %r12d        # imm = 0x100000
cmovbq %rbx, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x1310
movq %r14, %rdi
movq %r12, %rsi
callq 0x125c
addq $-0x100000, %rbx        # imm = 0xFFF00000
testq %rbx, %rbx
jg 0x15eb
leaq 0xa2f(%rip), %r12       # 0x2050
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x12d4
movq %r12, %rdi
movq %r15, %rsi
callq 0x12d4
leaq -0x44(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x12d4
jmp 0x1550
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x12d4
leaq 0x9f4(%rip), %rdi       # 0x2050
movq %r15, %rsi
callq 0x12d4
leaq -0x44(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x12d4
testq %rbx, %rbx
jle 0x1550
cmpq %r13, %rbx
movl $0x100000, %r12d        # imm = 0x100000
cmovbq %rbx, %r12
movq %r14, %rdi
movq %r12, %rsi
callq 0x1298
movq %r14, %rdi
movq %r12, %rsi
callq 0x12d4
addq $-0x100000, %rbx        # imm = 0xFFF00000
jmp 0x1670
callq 0x1030
movl (%rax), %edi
callq 0x1110
movq %rax, %rdi
callq 0x122c
leaq 0x944(%rip), %rdi       # 0x2004
callq 0x122c
leaq 0x94e(%rip), %rdi       # 0x201a
callq 0x122c
leaq 0x94f(%rip), %rdi       # 0x2027
callq 0x122c