// File: /workspace/github_c_makefile_star2_O0/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
subq $0x80, %rsp
movl $0x0, -0x4(%rbp)
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
cmpl $0x2, -0x8(%rbp)
jge 0x15cb
leaq 0xa3e(%rip), %rdi       # 0x2004
callq 0x1250
cmpl $0x3, -0x8(%rbp)
jge 0x15dd
leaq 0xa42(%rip), %rdi       # 0x201a
callq 0x1250
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x101002, %esi         # imm = 0x101002
movb $0x0, %al
callq 0x10e0
movl %eax, 0x2ad1(%rip)      # 0x40c8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x2ac6(%rip), %eax      # 0x40c8
jne 0x1610
leaq 0xa1c(%rip), %rdi       # 0x2027
callq 0x1250
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rdi
callq 0x1100
movl %eax, -0x14(%rbp)
movl $0x2, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1140
movl %eax, 0x102aad(%rip)    # 0x1040e4
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x102aa2(%rip), %eax    # 0x1040e4
jne 0x1658
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movw $0x2, 0x102a87(%rip)    # 0x1040e8
movl -0x14(%rbp), %eax
movzwl %ax, %edi
callq 0x1060
movw %ax, 0x102a77(%rip)     # 0x1040ea
leaq 0x9c1(%rip), %rdi       # 0x203b
callq 0x10a0
movl %eax, 0x102a67(%rip)    # 0x1040ec
leaq 0x102a5c(%rip), %rdi    # 0x1040e8
addq $0x8, %rdi
xorl %esi, %esi
movl $0x8, %edx
callq 0x1070
movl 0x102a42(%rip), %edi    # 0x1040e4
leaq 0x102a3f(%rip), %rax    # 0x1040e8
movq %rax, -0x20(%rbp)
movq -0x20(%rbp), %rsi
movl $0x10, %edx
callq 0x10d0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x16da
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movl 0x102a04(%rip), %edi    # 0x1040e4
xorl %esi, %esi
callq 0x10c0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x1706
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movl 0x1029d8(%rip), %edi    # 0x1040e4
movq $0x0, -0x28(%rbp)
movq -0x28(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x10f0
movl %eax, 0x1029b9(%rip)    # 0x1040e0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x1029ae(%rip), %eax    # 0x1040e0
jne 0x1748
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movl $0x1, -0x2c(%rbp)
movl $0x0, -0x30(%rbp)
movl 0x102984(%rip), %edi    # 0x1040e0
movl $0x1, %esi
movl $0x2, %edx
leaq -0x2c(%rbp), %rcx
movl $0x4, %r8d
callq 0x1040
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x1794
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movl 0x102946(%rip), %edi    # 0x1040e0
movl $0x6, %esi
movl $0x1, %edx
leaq -0x2c(%rbp), %rcx
movl $0x4, %r8d
callq 0x1040
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x17d2
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movl 0x28f0(%rip), %edi      # 0x40c8
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1120
movq %rax, -0x38(%rbp)
movq -0x38(%rbp), %rdi
callq 0x14c0
movq %rax, -0x40(%rbp)
movq 0x28ba(%rip), %rdi      # 0x40b8
callq 0x14c0
movq %rax, -0x48(%rbp)
movl 0x28b7(%rip), %edi      # 0x40c4
callq 0x1450
movl %eax, -0x4c(%rbp)
leaq 0x829(%rip), %rdi       # 0x2045
movl $0x8, %esi
callq 0x1350
leaq -0x48(%rbp), %rdi
movl $0x8, %esi
callq 0x1350
leaq -0x40(%rbp), %rdi
movl $0x8, %esi
callq 0x1350
movl $0x0, -0x50(%rbp)
cmpl $0x80, -0x50(%rbp)
jge 0x186e
leaq 0x7f5(%rip), %rdi       # 0x204e
movl $0x1, %esi
callq 0x1350
movl -0x50(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x50(%rbp)
jmp 0x1849
jmp 0x1870
leaq -0x54(%rbp), %rdi
movl $0x4, %esi
callq 0x13b0
leaq -0x58(%rbp), %rdi
movl $0x4, %esi
callq 0x13b0
leaq -0x64(%rbp), %rdi
movl $0x8, %esi
callq 0x13b0
leaq -0x70(%rbp), %rdi
movl $0x8, %esi
callq 0x13b0
leaq -0x5c(%rbp), %rdi
movl $0x4, %esi
callq 0x13b0
leaq -0x54(%rbp), %rdi
callq 0x1420
leaq -0x58(%rbp), %rdi
callq 0x1420
leaq -0x70(%rbp), %rdi
callq 0x1490
leaq -0x5c(%rbp), %rdi
callq 0x1420
movl -0x54(%rbp), %eax
cmpl 0x27dd(%rip), %eax      # 0x40c0
jne 0x1a22
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmpq %rax, -0x70(%rbp)
jae 0x1a22
movl -0x5c(%rbp), %eax
addq -0x70(%rbp), %rax
cmpq -0x38(%rbp), %rax
ja 0x1a22
movl -0x5c(%rbp), %eax
movq %rax, -0x78(%rbp)
movl 0x27ad(%rip), %edi      # 0x40c8
movq -0x70(%rbp), %rsi
xorl %edx, %edx
callq 0x1120
cmpl $0x0, -0x58(%rbp)
jne 0x19a2
leaq -0x4c(%rbp), %rdi
movl $0x4, %esi
callq 0x1350
leaq 0x70f(%rip), %rdi       # 0x2050
movl $0x4, %esi
callq 0x1350
leaq -0x64(%rbp), %rdi
movl $0x8, %esi
callq 0x1350
cmpq $0x0, -0x78(%rbp)
jle 0x19a0
movq -0x78(%rbp), %rdi
callq 0x1570
movq %rax, %rsi
leaq 0x276d(%rip), %rdi      # 0x40e0
callq 0x12f0
movq -0x78(%rbp), %rdi
callq 0x1570
movq %rax, %rsi
leaq 0x2755(%rip), %rdi      # 0x40e0
callq 0x1350
movq -0x78(%rbp), %rax
subq $0x100000, %rax         # imm = 0x100000
movq %rax, -0x78(%rbp)
jmp 0x1959
jmp 0x1a20
cmpl $0x1, -0x58(%rbp)
jne 0x1a1e
jmp 0x19aa
cmpq $0x0, -0x78(%rbp)
jle 0x19f1
movq -0x78(%rbp), %rdi
callq 0x1570
movq %rax, %rsi
leaq 0x271c(%rip), %rdi      # 0x40e0
callq 0x13b0
movq -0x78(%rbp), %rdi
callq 0x1570
movq %rax, %rsi
leaq 0x2704(%rip), %rdi      # 0x40e0
callq 0x1290
movq -0x78(%rbp), %rax
subq $0x100000, %rax         # imm = 0x100000
movq %rax, -0x78(%rbp)
jmp 0x19aa
leaq -0x4c(%rbp), %rdi
movl $0x4, %esi
callq 0x1350
leaq 0x64a(%rip), %rdi       # 0x2050
movl $0x4, %esi
callq 0x1350
leaq -0x64(%rbp), %rdi
movl $0x8, %esi
callq 0x1350
jmp 0x1a20
jmp 0x1a4f
leaq -0x4c(%rbp), %rdi
movl $0x4, %esi
callq 0x1350
leaq 0x61e(%rip), %rdi       # 0x2055
movl $0x4, %esi
callq 0x1350
leaq -0x64(%rbp), %rdi
movl $0x8, %esi
callq 0x1350
jmp 0x1870