// File: /workspace/github_cpp_makefile_star2_O2/epfml[P]Bi-Sent2Vec/src/shmem_matrix.cc
// Function: fasttext::ShmemMatrix::load(std::istream&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, int)
std::shared_ptr<ShmemMatrix> ShmemMatrix::load(std::istream& in,
                                               const std::string& name,
                                               const int timeout_sec) {
  std::string init_name = name + ".init";

  int64_t m, n;
  in.read((char*)&m, sizeof(int64_t));
  in.read((char*)&n, sizeof(int64_t));
  size_t size = m * n * sizeof(real);

  // Create a shared memory segment to be initialized with the input matrix
  bool new_segment = true;
  int fd = shm_open(init_name.c_str(), O_RDWR | O_CREAT | O_EXCL, 0444);
  if (fd == -1) {
    if (errno == EEXIST) {
      new_segment = false;
    } else {
      perror("ERROR ShmemMatrix::load: shm_open failed");
      exit(-1);
    }
  }

  if (new_segment) {
    // Set the size for shared memory segment
    int ret = ftruncate(fd, size);
    if (ret == -1) {
      perror("ERROR ShmemMatrix::load: ftruncate failed");
      exit(-1);
    }

    // Map the shared memory segment
    void* ptr = mmap(NULL, size, PROT_WRITE, MAP_SHARED, fd, 0);
    if (ptr == (void*)-1) {
      perror("ERROR ShmemMatrix::load: mmap failed");
      exit(-1);
    }

    // Close the file descriptor
    ret = close(fd);
    if (ret == -1) {
      perror("ERROR ShmemMatrix::load: close failed");
      exit(-1);
    }

    // Populate the shared memory segment
    in.read((char*)ptr, size);

    // Unmap the shared memory segment
    ret = munmap(ptr, size);
    if (ret == -1) {
      perror("ERROR ShmemMatrix::load: munmap failed");
      exit(-1);
    }

    // Atomically link to the expected name
    std::string init_name_path = "/dev/shm/" + init_name;
    std::string name_path = "/dev/shm/" + name;
    ret = link(init_name_path.c_str(), name_path.c_str());
    if (ret == -1) {
      perror("ERROR ShmemMatrix::load: link failed");
      exit(-1);
    }
  } else {
    // Seek in the stream to skip the input matrix data
    in.seekg(size, in.cur);
  }

  return std::make_shared<ShmemMatrix>(name.c_str(), m, n, timeout_sec);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movl %ecx, 0x4(%rsp)
leaq 0xea2b(%rip), %rdx      # 0x1af8f
leaq 0x38(%rsp), %rdi
movq %r14, %rsi
callq 0xc742
leaq 0x10(%rsp), %rsi
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x43e0
leaq 0x8(%rsp), %rsi
pushq $0x8
popq %rdx
movq %r15, %rdi
callq 0x43e0
movq 0x10(%rsp), %r12
imulq 0x8(%rsp), %r12
movq 0x38(%rsp), %rdi
movl $0xc2, %esi
movl $0x124, %edx            # imm = 0x124
callq 0x4580
movl %eax, %ebp
shlq $0x2, %r12
cmpl $-0x1, %eax
je 0xc67f
movl %ebp, %edi
movq %r12, %rsi
callq 0x4470
cmpl $-0x1, %eax
je 0xc6d5
pushq $0x2
popq %rdx
pushq $0x1
popq %rcx
xorl %edi, %edi
movq %r12, %rsi
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x45e0
cmpq $-0x1, %rax
je 0xc6de
movq %rax, %r13
movl %ebp, %edi
callq 0x4680
cmpl $-0x1, %eax
je 0xc6e7
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x43e0
movq %r13, %rdi
movq %r12, %rsi
callq 0x41f0
cmpl $-0x1, %eax
je 0xc6f0
leaq 0xea2c(%rip), %rsi      # 0x1b05a
leaq 0x18(%rsp), %rdi
leaq 0x38(%rsp), %rdx
callq 0xc77a
leaq 0xea16(%rip), %rsi      # 0x1b05a
leaq 0x58(%rsp), %rdi
movq %r14, %rdx
callq 0xc77a
movq 0x18(%rsp), %rdi
movq 0x58(%rsp), %rsi
callq 0x4110
cmpl $-0x1, %eax
je 0xc6f9
leaq 0x58(%rsp), %rdi
callq 0x46c8
leaq 0x18(%rsp), %rdi
callq 0x46c8
jmp 0xc697
callq 0x4060
cmpl $0x11, (%rax)
jne 0xc702
pushq $0x1
popq %rdx
movq %r15, %rdi
movq %r12, %rsi
callq 0x4450
movq (%r14), %rax
leaq 0x18(%rsp), %rsi
movq %rax, (%rsi)
leaq 0x10(%rsp), %rdx
leaq 0x8(%rsp), %rcx
leaq 0x4(%rsp), %r8
movq %rbx, %rdi
callq 0xc7f0
leaq 0x38(%rsp), %rdi
callq 0x46c8
movq %rbx, %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0xe8e2(%rip), %rdi      # 0x1afbe
jmp 0xc709
leaq 0xe903(%rip), %rdi      # 0x1afe8
jmp 0xc709
leaq 0xe91f(%rip), %rdi      # 0x1b00d
jmp 0xc709
leaq 0xe93c(%rip), %rdi      # 0x1b033
jmp 0xc709
leaq 0xe964(%rip), %rdi      # 0x1b064
jmp 0xc709
leaq 0xe88c(%rip), %rdi      # 0x1af95
callq 0x42c0
pushq $-0x1
popq %rdi
callq 0x43b0
movq %rax, %rbx
leaq 0x18(%rsp), %rdi
callq 0x46c8
jmp 0xc730
jmp 0xc72d
jmp 0xc72d
jmp 0xc72d
jmp 0xc72d
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x46c8
movq %rbx, %rdi
callq 0x45d0