// File: /workspace/github_c_makefile_star2_O1/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x48, %rsp
cmpl $0x1, %edi
jg 0x1360
leaq 0xca9(%rip), %rdi       # 0x2004
callq 0x124c
cmpl $0x2, %edi
jne 0x1371
leaq 0xcae(%rip), %rdi       # 0x201a
callq 0x124c
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x10f0
movl %eax, 0x2d3e(%rip)      # 0x40c8
cmpl $-0x1, %eax
jne 0x139b
leaq 0xc9d(%rip), %rdi       # 0x2033
callq 0x124c
movl 0x2d27(%rip), %edi      # 0x40c8
xorl %esi, %esi
movl $0x2, %edx
callq 0x1120
movq %rax, %r14
bswapq %rax
movq %rax, -0x68(%rbp)
movl 0x2d0a(%rip), %r8d      # 0x40c8
xorl %edi, %edi
movq %r14, %rsi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r9d, %r9d
callq 0x10c0
movq %rax, 0x102d0c(%rip)    # 0x1040e8
cmpq $-0x1, %rax
je 0x147a
movq %r14, -0x50(%rbp)
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1140
movl %eax, 0x102cef(%rip)    # 0x1040f0
movw $0x1, 0x102cea(%rip)    # 0x1040f4
leaq 0x102ce3(%rip), %r14    # 0x1040f4
movq 0x10(%rbx), %rsi
leaq 0x102cda(%rip), %rbx    # 0x1040f6
movq %rbx, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1040
movl 0x102cbd(%rip), %r15d   # 0x1040f0
movq %rbx, %rdi
callq 0x1080
leal 0x2(%rax), %edx
movl %r15d, %edi
movq %r14, %rsi
callq 0x10e0
cmpl $-0x1, %eax
je 0x147a
movl 0x102c9c(%rip), %edi    # 0x1040f0
xorl %esi, %esi
callq 0x10d0
cmpl $-0x1, %eax
je 0x147a
movl 0x102c8a(%rip), %edi    # 0x1040f0
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
movl %eax, 0x102c6b(%rip)    # 0x1040e0
cmpl $-0x1, %eax
jne 0x148e
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x124c
movq 0x2c23(%rip), %rax      # 0x40b8
bswapq %rax
movq %rax, -0x60(%rbp)
movl 0x2c22(%rip), %eax      # 0x40c4
bswapl %eax
movl %eax, -0x2c(%rbp)
xorl %ebx, %ebx
leaq 0xb97(%rip), %r14       # 0x2047
movl 0x102c2a(%rip), %edi    # 0x1040e0
leaq (%r14,%rbx), %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x14b0
xorl %ebx, %ebx
movq -0x50(%rbp), %r15
movl 0x102c02(%rip), %edi    # 0x1040e0
leaq (%rbx,%rbp), %rsi
addq $-0x60, %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x14d8
xorl %ebx, %ebx
movl 0x102bda(%rip), %edi    # 0x1040e0
leaq (%rbx,%rbp), %rsi
addq $-0x68, %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x1500
xorl %ebx, %ebx
leaq 0xb21(%rip), %r14       # 0x2050
xorl %esi, %esi
addq %r14, %rsi
movl 0x102ba6(%rip), %edi    # 0x1040e0
movl $0x1, %edx
callq 0x1060
movslq %eax, %rsi
shlq $0x20, %rax
je 0x1531
incl %ebx
cmpl $0x80, %ebx
jne 0x152f
movl $0x100000, %r14d        # imm = 0x100000
leaq 0xaf3(%rip), %r12       # 0x2057
movl $0x4, %esi
leaq -0x38(%rbp), %rdi
callq 0x12c1
movl $0x4, %esi
leaq -0x34(%rbp), %rdi
callq 0x12c1
movl $0x8, %esi
leaq -0x44(%rbp), %rdi
callq 0x12c1
movl $0x8, %esi
leaq -0x58(%rbp), %rdi
callq 0x12c1
movl $0x4, %esi
leaq -0x30(%rbp), %rdi
callq 0x12c1
movl -0x38(%rbp), %ecx
bswapl %ecx
movl %ecx, -0x38(%rbp)
movl -0x34(%rbp), %eax
bswapl %eax
movl %eax, -0x34(%rbp)
movq -0x58(%rbp), %r13
bswapq %r13
movq %r13, -0x58(%rbp)
movl -0x30(%rbp), %esi
movl %esi, %edx
bswapl %edx
movl %edx, -0x30(%rbp)
testq %r13, %r13
js 0x15e7
cmpl 0x2ae6(%rip), %ecx      # 0x40c0
jne 0x15e7
movl %edx, %ebx
leaq (%rbx,%r13), %rcx
cmpq %r15, %rcx
jbe 0x1660
xorl %ebx, %ebx
movl 0x102af1(%rip), %edi    # 0x1040e0
leaq (%rbx,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x4, %rbx
jb 0x15e9
xorl %ebx, %ebx
movl 0x102ac9(%rip), %edi    # 0x1040e0
leaq (%r12,%rbx), %rsi
movl $0x4, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x4, %rbx
jb 0x1611
xorl %ebx, %ebx
movl 0x102aa5(%rip), %edi    # 0x1040e0
leaq (%rbx,%rbp), %rsi
addq $-0x44, %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x1635
jmp 0x1564
cmpl $0x1, %eax
je 0x1738
testl %eax, %eax
jne 0x1564
movl %esi, -0x3c(%rbp)
xorl %r15d, %r15d
movl 0x102a63(%rip), %edi    # 0x1040e0
leaq (%r15,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1060
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x1677
xorl %r15d, %r15d
leaq 0x9aa(%rip), %r14       # 0x2052
movl 0x102a32(%rip), %edi    # 0x1040e0
leaq (%r14,%r15), %rsi
movl $0x4, %edx
subq %r15, %rdx
callq 0x1060
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x4, %r15
jb 0x16a8
xorl %r15d, %r15d
movl $0x100000, %r14d        # imm = 0x100000
movl 0x102a06(%rip), %edi    # 0x1040e0
leaq (%r15,%rbp), %rsi
addq $-0x44, %rsi
movl $0x8, %edx
subq %r15, %rdx
callq 0x1060
addl %eax, %r15d
movslq %r15d, %r15
cmpq $0x8, %r15
jb 0x16d4
cmpl $0x0, -0x3c(%rbp)
movq -0x50(%rbp), %r15
je 0x1564
movq 0x1029d8(%rip), %rdi    # 0x1040e8
addq %r13, %rdi
cmpq %r14, %rbx
movl $0x100000, %esi         # imm = 0x100000
cmovlq %rbx, %rsi
callq 0x127e
leaq -0x100000(%rbx), %rax
cmpq %r14, %rbx
movq %rax, %rbx
jg 0x1709
jmp 0x1564
testl %esi, %esi
je 0x1766
movq 0x1029a5(%rip), %rdi    # 0x1040e8
addq %r13, %rdi
cmpq %r14, %rbx
movl $0x100000, %esi         # imm = 0x100000
cmovlq %rbx, %rsi
callq 0x12c1
leaq -0x100000(%rbx), %rax
cmpq %r14, %rbx
movq %rax, %rbx
jg 0x173c
movq 0x10297b(%rip), %rdi    # 0x1040e8
movq %r15, %rsi
movl $0x4, %edx
callq 0x1070
cmpl $-0x1, %eax
je 0x147a
xorl %ebx, %ebx
movl 0x102955(%rip), %edi    # 0x1040e0
leaq (%rbx,%rbp), %rsi
addq $-0x2c, %rsi
movl $0x4, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x4, %rbx
jb 0x1785
xorl %ebx, %ebx
leaq 0x89e(%rip), %r14       # 0x2052
movl 0x102926(%rip), %edi    # 0x1040e0
leaq (%r14,%rbx), %rsi
movl $0x4, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x4, %rbx
jb 0x17b4
xorl %ebx, %ebx
movl $0x100000, %r14d        # imm = 0x100000
movl 0x1028fc(%rip), %edi    # 0x1040e0
leaq (%rbx,%rbp), %rsi
addq $-0x44, %rsi
movl $0x8, %edx
subq %rbx, %rdx
callq 0x1060
addl %eax, %ebx
movslq %ebx, %rbx
cmpq $0x8, %rbx
jb 0x17de
jmp 0x1564