// File: /workspace/github_c_makefile_star2_O3/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 %eax, %edi
xorl %esi, %esi
movl $0x2, %edx
callq 0x1120
movq %rax, %r14
bswapq %rax
movq %rax, -0x68(%rbp)
movl 0x2d0e(%rip), %r8d      # 0x40c8
xorl %edi, %edi
movq %r14, %rsi
movl $0x3, %edx
movl $0x1, %ecx
xorl %r9d, %r9d
callq 0x10c0
movq %rax, 0x102d10(%rip)    # 0x1040e8
cmpq $-0x1, %rax
je 0x1476
movq %r14, -0x50(%rbp)
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1140
movl %eax, 0x102cf3(%rip)    # 0x1040f0
movw $0x1, 0x102cee(%rip)    # 0x1040f4
leaq 0x102ce7(%rip), %r14    # 0x1040f4
movq 0x10(%rbx), %rsi
leaq 0x102cde(%rip), %rbx    # 0x1040f6
movq %rbx, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1040
movl 0x102cc1(%rip), %r15d   # 0x1040f0
movq %rbx, %rdi
callq 0x1080
leal 0x2(%rax), %edx
movl %r15d, %edi
movq %r14, %rsi
callq 0x10e0
cmpl $-0x1, %eax
je 0x1476
movl 0x102ca0(%rip), %edi    # 0x1040f0
xorl %esi, %esi
callq 0x10d0
cmpl $-0x1, %eax
je 0x1476
movl 0x102c8e(%rip), %edi    # 0x1040f0
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
movl %eax, 0x102c6f(%rip)    # 0x1040e0
cmpl $-0x1, %eax
jne 0x148a
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x124c
movq 0x2c27(%rip), %rax      # 0x40b8
bswapq %rax
movq %rax, -0x60(%rbp)
movl 0x2c26(%rip), %eax      # 0x40c4
bswapl %eax
movl %eax, -0x2c(%rbp)
xorl %ebx, %ebx
leaq 0xb9b(%rip), %r14       # 0x2047
movl 0x102c2e(%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 0x14ac
xorl %ebx, %ebx
movq -0x50(%rbp), %r15
movl 0x102c06(%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 0x14d4
xorl %ebx, %ebx
movl 0x102bde(%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 0x14fc
xorl %r14d, %r14d
leaq 0xb24(%rip), %rbx       # 0x2050
movl 0x102bae(%rip), %edi    # 0x1040e0
movl $0x1, %edx
movq %rbx, %rsi
callq 0x1060
testl %eax, %eax
je 0x152c
incl %r14d
cmpl $0x80, %r14d
jne 0x152c
movl $0x100000, %r14d        # imm = 0x100000
leaq 0xafb(%rip), %r12       # 0x2057
movl $0x4, %esi
leaq -0x3c(%rbp), %rdi
callq 0x12c1
movl $0x4, %esi
leaq -0x38(%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 -0x34(%rbp), %rdi
callq 0x12c1
movq -0x58(%rbp), %r13
bswapq %r13
testq %r13, %r13
js 0x15cb
movl -0x3c(%rbp), %eax
bswapl %eax
cmpl 0x2b07(%rip), %eax      # 0x40c0
jne 0x15cb
movl -0x34(%rbp), %ecx
movl %ecx, %ebx
bswapl %ebx
leaq (%rbx,%r13), %rax
cmpq %r15, %rax
jbe 0x1644
xorl %ebx, %ebx
movl 0x102b0d(%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 0x15cd
xorl %ebx, %ebx
movl 0x102ae5(%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 0x15f5
xorl %ebx, %ebx
movl 0x102ac1(%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 0x1619
jmp 0x155c
movl -0x38(%rbp), %eax
bswapl %eax
cmpl $0x1, %eax
je 0x1721
testl %eax, %eax
jne 0x155c
movl %ecx, -0x30(%rbp)
xorl %r15d, %r15d
movl 0x102a7a(%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 0x1660
xorl %r15d, %r15d
leaq 0x9c1(%rip), %r14       # 0x2052
movl 0x102a49(%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 0x1691
xorl %r15d, %r15d
movl $0x100000, %r14d        # imm = 0x100000
movl 0x102a1d(%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 0x16bd
cmpl $0x0, -0x30(%rbp)
movq -0x50(%rbp), %r15
je 0x155c
movq 0x1029ef(%rip), %rdi    # 0x1040e8
addq %r13, %rdi
cmpq %r14, %rbx
movl $0x100000, %esi         # imm = 0x100000
cmovbq %rbx, %rsi
callq 0x127e
leaq -0x100000(%rbx), %rax
cmpq %r14, %rbx
movq %rax, %rbx
jg 0x16f2
jmp 0x155c
testl %ecx, %ecx
je 0x174f
movq 0x1029bc(%rip), %rdi    # 0x1040e8
addq %r13, %rdi
cmpq %r14, %rbx
movl $0x100000, %esi         # imm = 0x100000
cmovbq %rbx, %rsi
callq 0x12c1
leaq -0x100000(%rbx), %rax
cmpq %r14, %rbx
movq %rax, %rbx
ja 0x1725
movq 0x102992(%rip), %rdi    # 0x1040e8
movq %r15, %rsi
movl $0x4, %edx
callq 0x1070
cmpl $-0x1, %eax
je 0x1476
xorl %ebx, %ebx
movl 0x10296c(%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 0x176e
xorl %ebx, %ebx
leaq 0x8b5(%rip), %r14       # 0x2052
movl 0x10293d(%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 0x179d
xorl %ebx, %ebx
movl $0x100000, %r14d        # imm = 0x100000
movl 0x102913(%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 0x17c7
jmp 0x155c