// File: /workspace/github_c_makefile_star2_O0/regularfry[P]tinynbd/src/tinynbd_socket_mmap.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");};
  off64_t size=lseek64(fd,0,SEEK_END);__be64 wsz=htobe64(size);
  map=mmap64(NULL,(size_t)size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0);
  if((void*)-1 == map){ err(strerror(errno));exit(1);}

  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);

  __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;
      if(t==0){
        W(&rmag,4);W("\0\0\0\0",4);W(h,8);
        for(;ll>0;ll-=B){W(map+f,clip(ll));}
      } else if(t==1){
        for(;ll>0;ll-=B){R(map+f,clip(ll));}
        E(msync(map,size,MS_SYNC));
        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 0x150b
leaq 0xafe(%rip), %rdi       # 0x2004
callq 0x1250
cmpl $0x3, -0x8(%rbp)
jge 0x151d
leaq 0xb02(%rip), %rdi       # 0x201a
callq 0x1250
movq -0x10(%rbp), %rax
movq 0x8(%rax), %rdi
movl $0x101002, %esi         # imm = 0x101002
movb $0x0, %al
callq 0x10f0
movl %eax, 0x2b91(%rip)      # 0x40c8
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x2b86(%rip), %eax      # 0x40c8
jne 0x1550
leaq 0xae8(%rip), %rdi       # 0x2033
callq 0x1250
movl 0x2b72(%rip), %edi      # 0x40c8
xorl %eax, %eax
movl %eax, %esi
movl $0x2, %edx
callq 0x1120
movq %rax, -0x18(%rbp)
movq -0x18(%rbp), %rdi
callq 0x1400
movq %rax, -0x20(%rbp)
movq -0x18(%rbp), %rsi
movl 0x2b48(%rip), %r8d      # 0x40c8
xorl %eax, %eax
movl %eax, %r9d
movl $0x3, %edx
movl $0x1, %ecx
movq %r9, %rdi
callq 0x10c0
movq %rax, 0x102b4a(%rip)    # 0x1040e8
movq $-0x1, %rax
cmpq 0x102b3c(%rip), %rax    # 0x1040e8
jne 0x15cc
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movl $0x1, %edi
callq 0x1110
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1140
movl %eax, 0x102b10(%rip)    # 0x1040f0
movw $0x1, 0x102b0b(%rip)    # 0x1040f4
movq -0x10(%rbp), %rax
movq 0x10(%rax), %rsi
leaq 0x102afe(%rip), %rdi    # 0x1040f6
callq 0x1050
leaq 0x102af0(%rip), %rdi    # 0x1040f4
addq $0x2, %rdi
callq 0x1040
movl 0x102add(%rip), %eax    # 0x1040f0
movl %eax, -0x6c(%rbp)
leaq 0x102ad7(%rip), %rax    # 0x1040f4
movq %rax, -0x28(%rbp)
leaq 0x102ace(%rip), %rdi    # 0x1040f6
callq 0x1080
movl -0x6c(%rbp), %edi
addq $0x2, %rax
movl %eax, %edx
movq -0x28(%rbp), %rsi
callq 0x10e0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x165e
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movl 0x102a8c(%rip), %edi    # 0x1040f0
xorl %esi, %esi
callq 0x10d0
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x168a
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movl 0x102a60(%rip), %edi    # 0x1040f0
movq $0x0, -0x30(%rbp)
movq -0x30(%rbp), %rsi
xorl %eax, %eax
movl %eax, %edx
callq 0x1100
movl %eax, 0x102a35(%rip)    # 0x1040e0
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl 0x102a2a(%rip), %eax    # 0x1040e0
jne 0x16cc
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
movq 0x29e5(%rip), %rdi      # 0x40b8
callq 0x1400
movq %rax, -0x38(%rbp)
movl 0x29e2(%rip), %edi      # 0x40c4
callq 0x1390
movl %eax, -0x3c(%rbp)
leaq 0x956(%rip), %rdi       # 0x2047
movl $0x8, %esi
callq 0x1290
leaq -0x38(%rbp), %rdi
movl $0x8, %esi
callq 0x1290
leaq -0x20(%rbp), %rdi
movl $0x8, %esi
callq 0x1290
movl $0x0, -0x40(%rbp)
cmpl $0x80, -0x40(%rbp)
jge 0x1743
leaq 0x922(%rip), %rdi       # 0x2050
movl $0x1, %esi
callq 0x1290
movl -0x40(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x40(%rbp)
jmp 0x171e
jmp 0x1745
leaq -0x44(%rbp), %rdi
movl $0x4, %esi
callq 0x12f0
leaq -0x48(%rbp), %rdi
movl $0x4, %esi
callq 0x12f0
leaq -0x54(%rbp), %rdi
movl $0x8, %esi
callq 0x12f0
leaq -0x60(%rbp), %rdi
movl $0x8, %esi
callq 0x12f0
leaq -0x4c(%rbp), %rdi
movl $0x4, %esi
callq 0x12f0
leaq -0x44(%rbp), %rdi
callq 0x1360
leaq -0x48(%rbp), %rdi
callq 0x1360
leaq -0x60(%rbp), %rdi
callq 0x13d0
leaq -0x4c(%rbp), %rdi
callq 0x1360
movl -0x44(%rbp), %eax
cmpl 0x2908(%rip), %eax      # 0x40c0
jne 0x1909
movabsq $-0x8000000000000000, %rax # imm = 0x8000000000000000
cmpq %rax, -0x60(%rbp)
jae 0x1909
movl -0x4c(%rbp), %eax
addq -0x60(%rbp), %rax
cmpq -0x18(%rbp), %rax
ja 0x1909
movl -0x4c(%rbp), %eax
movq %rax, -0x68(%rbp)
cmpl $0x0, -0x48(%rbp)
jne 0x185d
leaq -0x3c(%rbp), %rdi
movl $0x4, %esi
callq 0x1290
leaq 0x84d(%rip), %rdi       # 0x2052
movl $0x4, %esi
callq 0x1290
leaq -0x54(%rbp), %rdi
movl $0x8, %esi
callq 0x1290
cmpq $0x0, -0x68(%rbp)
jle 0x1858
movq 0x1028bd(%rip), %rax    # 0x1040e8
addq -0x60(%rbp), %rax
movq %rax, -0x78(%rbp)
movq -0x68(%rbp), %rdi
callq 0x14b0
movq -0x78(%rbp), %rdi
movq %rax, %rsi
callq 0x1290
movq -0x68(%rbp), %rax
subq $0x100000, %rax         # imm = 0x100000
movq %rax, -0x68(%rbp)
jmp 0x181d
jmp 0x1907
cmpl $0x1, -0x48(%rbp)
jne 0x1905
jmp 0x1869
cmpq $0x0, -0x68(%rbp)
jle 0x18a4
movq 0x102871(%rip), %rax    # 0x1040e8
addq -0x60(%rbp), %rax
movq %rax, -0x80(%rbp)
movq -0x68(%rbp), %rdi
callq 0x14b0
movq -0x80(%rbp), %rdi
movq %rax, %rsi
callq 0x12f0
movq -0x68(%rbp), %rax
subq $0x100000, %rax         # imm = 0x100000
movq %rax, -0x68(%rbp)
jmp 0x1869
movq 0x10283d(%rip), %rdi    # 0x1040e8
movq -0x18(%rbp), %rsi
movl $0x4, %edx
callq 0x1070
movl %eax, %ecx
movl $0xffffffff, %eax       # imm = 0xFFFFFFFF
cmpl %ecx, %eax
jne 0x18d8
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x1250
leaq -0x3c(%rbp), %rdi
movl $0x4, %esi
callq 0x1290
leaq 0x765(%rip), %rdi       # 0x2052
movl $0x4, %esi
callq 0x1290
leaq -0x54(%rbp), %rdi
movl $0x8, %esi
callq 0x1290
jmp 0x1907
jmp 0x1936
leaq -0x3c(%rbp), %rdi
movl $0x4, %esi
callq 0x1290
leaq 0x739(%rip), %rdi       # 0x2057
movl $0x4, %esi
callq 0x1290
leaq -0x54(%rbp), %rdi
movl $0x8, %esi
callq 0x1290
jmp 0x1745