// File: /workspace/github_cpp_makefile_star2_O1/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 0xa8dc
leaq 0x10ab6(%rip), %rsi     # 0x1ef8f
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 0xe61e
movl %ebp, %edi
movq %r12, %rsi
callq 0x4420
cmpl $-0x1, %eax
je 0xe69e
xorl %edi, %edi
movq %r12, %rsi
movl $0x2, %edx
movl $0x1, %ecx
movl %ebp, %r8d
xorl %r9d, %r9d
callq 0x45c0
cmpq $-0x1, %rax
je 0xe6a3
movq %rax, %r13
movl %ebp, %edi
callq 0x4650
cmpl $-0x1, %eax
je 0xe6a8
movq %r15, %rdi
movq %r13, %rsi
movq %r12, %rdx
callq 0x43b0
movq %r13, %rdi
movq %r12, %rsi
callq 0x41c0
cmpl $-0x1, %eax
je 0xe6ad
leaq 0x10ab4(%rip), %rsi     # 0x1f05a
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rdx
leaq 0x38(%rsp), %r13
callq 0xe71a
leaq 0x10a99(%rip), %rsi     # 0x1f05a
leaq 0x68(%rsp), %rdi
movq %r14, %rdx
callq 0xe71a
movq 0x48(%rsp), %rdi
movq 0x68(%rsp), %rsi
callq 0x40f0
cmpl $-0x1, %eax
je 0xe6b2
leaq 0x78(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe601
movq 0x78(%rsp), %rsi
incq %rsi
callq 0x4300
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe63c
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4300
jmp 0xe63c
callq 0x4060
cmpl $0x11, (%rax)
jne 0xe6b7
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 0xe7a2
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
je 0xe689
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 0x481d
callq 0x4808
callq 0x47f3
callq 0x47de
callq 0x47c9
callq 0x4832
movq %rax, %rbx
leaq 0x58(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe6f6
movq 0x58(%rsp), %rsi
incq %rsi
callq 0x4300
jmp 0xe6f6
jmp 0xe6f3
jmp 0xe6f3
movq %rax, %rbx
movq 0x28(%rsp), %rdi
cmpq %r13, %rdi
jne 0xe705
jmp 0xe712
jmp 0xe6f3
jmp 0xe6f3
movq %rax, %rbx
movq 0x28(%rsp), %rdi
leaq 0x38(%rsp), %rax
cmpq %rax, %rdi
je 0xe712
movq 0x38(%rsp), %rsi
incq %rsi
callq 0x4300
movq %rbx, %rdi
callq 0x45b0