// File: /workspace/github_c_makefile_star2_O2/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 $0x38, %rsp
cmpl $0x1, %edi
jg 0x134d
leaq 0xcbc(%rip), %rdi       # 0x2004
callq 0x124c
cmpl $0x2, %edi
jne 0x135e
leaq 0xcc1(%rip), %rdi       # 0x201a
callq 0x124c
movq %rsi, %rbx
movq 0x8(%rsi), %rdi
movl $0x101002, %esi         # imm = 0x101002
xorl %eax, %eax
callq 0x10f0
movl %eax, 0x2d51(%rip)      # 0x40c8
cmpl $-0x1, %eax
jne 0x1388
leaq 0xcb0(%rip), %rdi       # 0x2033
callq 0x124c
pushq $0x2
popq %rdx
movl %eax, %edi
xorl %esi, %esi
callq 0x1120
movq %rax, %rsi
bswapq %rax
movq %rax, -0x58(%rbp)
movl 0x2d23(%rip), %r8d      # 0x40c8
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
movq %rsi, -0x40(%rbp)
xorl %r9d, %r9d
callq 0x10c0
movq %rax, 0x102d28(%rip)    # 0x1040e8
cmpq $-0x1, %rax
je 0x1455
pushq $0x1
popq %rdi
movl %edi, %esi
xorl %edx, %edx
callq 0x1140
movl %eax, 0x102d14(%rip)    # 0x1040f0
movw $0x1, 0x102d0f(%rip)    # 0x1040f4
leaq 0x102d08(%rip), %r14    # 0x1040f4
movq 0x10(%rbx), %rsi
leaq 0x102cff(%rip), %rbx    # 0x1040f6
movq %rbx, %rdi
callq 0x1050
movq %rbx, %rdi
callq 0x1040
movl 0x102ce2(%rip), %r15d   # 0x1040f0
movq %rbx, %rdi
callq 0x1080
leal 0x2(%rax), %edx
movl %r15d, %edi
movq %r14, %rsi
callq 0x10e0
cmpl $-0x1, %eax
je 0x1455
movl 0x102cc1(%rip), %edi    # 0x1040f0
xorl %esi, %esi
callq 0x10d0
cmpl $-0x1, %eax
je 0x1455
movl 0x102caf(%rip), %edi    # 0x1040f0
xorl %esi, %esi
xorl %edx, %edx
callq 0x1100
movl %eax, 0x102c90(%rip)    # 0x1040e0
cmpl $-0x1, %eax
jne 0x1469
callq 0x1030
movl (%rax), %edi
callq 0x1130
movq %rax, %rdi
callq 0x124c
movq 0x2c48(%rip), %rax      # 0x40b8
bswapq %rax
leaq -0x60(%rbp), %rbx
movq %rax, (%rbx)
movl 0x2c44(%rip), %eax      # 0x40c4
bswapl %eax
movl %eax, -0x2c(%rbp)
leaq 0xbbb(%rip), %rdi       # 0x2047
pushq $0x8
popq %r14
movq %r14, %rsi
callq 0x127c
movq %rbx, %rdi
movq %r14, %rsi
callq 0x127c
leaq -0x58(%rbp), %rdi
movq %r14, %rsi
callq 0x127c
movl $0x80, %r15d
leaq 0xb94(%rip), %rbx       # 0x2050
pushq $0x1
popq %r14
subl $0x1, %r15d
jb 0x14d3
movq %rbx, %rdi
movq %r14, %rsi
callq 0x127c
jmp 0x14c0
pushq $0x4
popq %r15
movl $0x100000, %r13d        # imm = 0x100000
leaq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x12b8
leaq -0x34(%rbp), %rdi
movq %r15, %rsi
callq 0x12b8
leaq -0x48(%rbp), %rdi
pushq $0x8
popq %rbx
movq %rbx, %rsi
callq 0x12b8
leaq -0x50(%rbp), %rdi
movq %rbx, %rsi
callq 0x12b8
leaq -0x30(%rbp), %rdi
movq %r15, %rsi
callq 0x12b8
movq -0x50(%rbp), %r12
bswapq %r12
leaq 0xb2d(%rip), %rcx       # 0x2057
movq %rcx, %r14
testq %r12, %r12
js 0x15aa
movl -0x38(%rbp), %eax
bswapl %eax
movq %rcx, %r14
cmpl 0x2b80(%rip), %eax      # 0x40c0
jne 0x15aa
movl -0x30(%rbp), %ebx
bswapl %ebx
leaq (%r12,%rbx), %rax
movq %rcx, %r14
cmpq -0x40(%rbp), %rax
ja 0x15aa
movl -0x34(%rbp), %eax
bswapl %eax
testl %eax, %eax
je 0x15d2
cmpl $0x1, %eax
je 0x1582
jmp 0x14dd
addq %r12, %rdi
cmpq %r13, %rbx
movl $0x100000, %esi         # imm = 0x100000
cmovbq %rbx, %rsi
callq 0x12b8
addq $-0x100000, %rbx        # imm = 0xFFF00000
movq 0x102b5f(%rip), %rdi    # 0x1040e8
testq %rbx, %rbx
jg 0x1567
movq -0x40(%rbp), %rsi
pushq $0x4
popq %rdx
callq 0x1070
leaq 0xab1(%rip), %r14       # 0x2052
cmpl $-0x1, %eax
je 0x1455
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x127c
movq %r14, %rdi
movq %r15, %rsi
callq 0x127c
leaq -0x48(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x127c
jmp 0x14dd
leaq -0x2c(%rbp), %rdi
movq %r15, %rsi
callq 0x127c
leaq 0xa6d(%rip), %rdi       # 0x2052
movq %r15, %rsi
callq 0x127c
leaq -0x48(%rbp), %rdi
pushq $0x8
popq %rsi
callq 0x127c
testq %rbx, %rbx
jle 0x14dd
movq 0x102adf(%rip), %rdi    # 0x1040e8
addq %r12, %rdi
cmpq %r13, %rbx
movl $0x100000, %esi         # imm = 0x100000
cmovbq %rbx, %rsi
callq 0x127c
addq $-0x100000, %rbx        # imm = 0xFFF00000
jmp 0x15f9