// File: /workspace/github_cpp_makefile_star2_O3/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 $0x88, %rsp
movq %rdx, %r14
movq %rsi, %r15
movq %rdi, %rbx
movl %ecx, 0x14(%rsp)
leaq 0x38(%rsp), %r13
movq %r13, -0x10(%r13)
movq (%rdx), %rsi
movq 0x8(%rdx), %rdx
addq %rsi, %rdx
leaq 0x28(%rsp), %r12
movq %r12, %rdi
callq 0xa910
leaq 0x1085e(%rip), %rsi     # 0x1efa7
movq %r12, %rdi
callq 0x4620
leaq 0x20(%rsp), %rsi
movl $0x8, %edx
movq %r15, %rdi
callq 0x43b0
leaq 0x18(%rsp), %rsi
movl $0x8, %edx
movq %r15, %rdi
callq 0x43b0
movq 0x20(%rsp), %r12
imulq 0x18(%rsp), %r12
movq 0x28(%rsp), %rdi
movl $0xc2, %esi
movl $0x124, %edx            # imm = 0x124
callq 0x4550
movl %eax, %ebp
shlq $0x2, %r12
cmpl $-0x1, %eax
je 0xe88e
movl %ebp, %edi
movq %r12, %rsi
callq 0x4420
cmpl $-0x1, %eax
je 0xe90e
xorl %edi, %edi
movq %r12, %rsi
movl $0x2, %edx
movl $0x1, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x45c0
cmpq $-0x1, %rax
je 0xe913
movq %rax, %r13
movl %ebp, %edi
callq 0x4650
cmpl $-0x1, %eax
je 0xe918
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x43b0
movq %r13, %rdi
movq %r12, %rsi
callq 0x41c0
cmpl $-0x1, %eax
je 0xe91d
leaq 0x1085c(%rip), %rsi     # 0x1f072
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rdx
leaq 0x38(%rsp), %r13
callq 0xe98a
leaq 0x10841(%rip), %rsi     # 0x1f072
leaq 0x68(%rsp), %rdi
movq %r14, %rdx
callq 0xe98a
movq 0x48(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x40f0
cmpl $-0x1, %eax
je 0xe922
leaq 0x78(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe871
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x4300
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe8ac
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4300
jmp 0xe8ac
callq 0x4060
cmpl $0x11, (%rax)
jne 0xe927
movq %r15, %rdi
movq %r12, %rsi
movl $0x1, %edx
callq 0x4410
movq (%r14), %rax
leaq 0x48(%rsp), %rcx
movq %rax, (%rcx)
movq $0x0, (%rbx)
leaq 0x8(%rbx), %rdi
leaq 0x14(%rsp), %rax
movq %rax, (%rsp)
leaq 0x68(%rsp), %rdx
leaq 0x20(%rsp), %r8
leaq 0x18(%rsp), %r9
movq %rbx, %rsi
callq 0xea12
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0xe8f9
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4300
movq %rbx, %rax
addq $0x88, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
callq 0x481b
callq 0x4806
callq 0x47f1
callq 0x47dc
callq 0x47c7
callq 0x4830
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe966
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4300
jmp 0xe966
jmp 0xe963
jmp 0xe963
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
jne 0xe975
jmp 0xe982
jmp 0xe963
jmp 0xe963
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xe982
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4300
movq %rbx, %rdi
callq 0x45b0