// File: /workspace/github_c_makefile_star2_O0/motet-a[P]libforks/libforks.c
// Function: libforks_write_socket_fds
int libforks_write_socket_fds(
    int socket_fd,
    void *data, size_t length,
    const int *fds, size_t fd_count) {
  // See https://man.openbsd.org/CMSG_DATA and https://man.openbsd.org/recv

  if (fd_count == 0) {
    // Same macOS issue, same workaround:
    return write_exact(socket_fd, data, length);
  }

  struct iovec iovec = {
    .iov_base = data,
    .iov_len = length,
  };

  char cmsg_buffer[CMSG_BUFFER_SIZE] = {0};
  size_t cmsg_buffer_space = CMSG_SPACE(sizeof(int) * fd_count);

  if (cmsg_buffer_space > sizeof cmsg_buffer) {
    errno = ENOMEM;
    return -1;
  }

  struct msghdr msg = {0};
  msg.msg_iov = &iovec;
  msg.msg_iovlen = 1;
  msg.msg_control = cmsg_buffer;
  msg.msg_controllen = cmsg_buffer_space;

  struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
  assert(cmsg);
  cmsg->cmsg_len = CMSG_LEN(sizeof(int) * fd_count);
  cmsg->cmsg_level = SOL_SOCKET;
  cmsg->cmsg_type = SCM_RIGHTS;
  memcpy(CMSG_DATA(cmsg), fds, sizeof(int) * fd_count);

  ssize_t send_res = sendmsg(socket_fd, &msg, 0);
  if (send_res == -1) {
    return -1;
  }
  if (send_res != (ssize_t)length) {
    errno = EMSGSIZE;
    return -1;
  }
  return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x120, %rsp            # imm = 0x120
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
jne 0x18dd
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x1aa0
movl %eax, -0x4(%rbp)
jmp 0x1a87
movq -0x10(%rbp), %rax
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0xc0(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x10c0
movq -0x28(%rbp), %rax
shlq $0x2, %rax
addq $0x8, %rax
subq $0x1, %rax
andq $-0x8, %rax
addq $0x10, %rax
movq %rax, -0xc8(%rbp)
cmpq $0x80, -0xc8(%rbp)
jbe 0x1943
callq 0x1050
movl $0xc, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1a87
leaq -0x100(%rbp), %rdi
xorl %esi, %esi
movl $0x38, %edx
callq 0x10c0
leaq -0x38(%rbp), %rax
movq %rax, -0xf0(%rbp)
movq $0x1, -0xe8(%rbp)
leaq -0xc0(%rbp), %rax
movq %rax, -0xe0(%rbp)
movq -0xc8(%rbp), %rax
movq %rax, -0xd8(%rbp)
cmpq $0x10, -0xd8(%rbp)
jb 0x19a2
movq -0xe0(%rbp), %rax
movq %rax, -0x118(%rbp)
jmp 0x19ad
xorl %eax, %eax
movq %rax, -0x118(%rbp)
jmp 0x19ad
movq -0x118(%rbp), %rax
movq %rax, -0x108(%rbp)
cmpq $0x0, -0x108(%rbp)
je 0x19c7
jmp 0x19e6
leaq 0x2831(%rip), %rdi      # 0x41ff
leaq 0x27de(%rip), %rsi      # 0x41b3
movl $0x12c, %edx            # imm = 0x12C
leaq 0x2823(%rip), %rcx      # 0x4204
callq 0x10b0
movq -0x28(%rbp), %rcx
shlq $0x2, %rcx
addq $0x10, %rcx
movq -0x108(%rbp), %rax
movq %rcx, (%rax)
movq -0x108(%rbp), %rax
movl $0x1, 0x8(%rax)
movq -0x108(%rbp), %rax
movl $0x1, 0xc(%rax)
movq -0x108(%rbp), %rdi
addq $0x10, %rdi
movq -0x20(%rbp), %rsi
movq -0x28(%rbp), %rdx
shlq $0x2, %rdx
callq 0x1140
movl -0x8(%rbp), %edi
leaq -0x100(%rbp), %rsi
xorl %edx, %edx
callq 0x1060
movq %rax, -0x110(%rbp)
cmpq $-0x1, -0x110(%rbp)
jne 0x1a5f
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1a87
movq -0x110(%rbp), %rax
cmpq -0x18(%rbp), %rax
je 0x1a80
callq 0x1050
movl $0x5a, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x1a87
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x120, %rsp            # imm = 0x120
popq %rbp
retq
nopw %cs:(%rax,%rax)