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

  if (max_fd_count == 0) {
    // For some reason `sendmsg` fails with ENOMEM on macOS in this special
    // case. Weird. The workaround is dead simple:
    return read_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) * max_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;

  ssize_t recv_res = recvmsg(socket_fd, &msg, 0);
  if (recv_res == -1) {
    return -1;
  }
  if (recv_res != (ssize_t)length ||
      (msg.msg_flags & MSG_TRUNC) ||
      (msg.msg_flags & MSG_CTRUNC)) {
    errno = EMSGSIZE; // there is probably a better way to handle this error
    return -1;
  }

  struct cmsghdr *cmsg;
  for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL && max_fd_count; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
    if (cmsg->cmsg_level == SOL_SOCKET &&
        cmsg->cmsg_type == SCM_RIGHTS) {

      char *data_end = (char *)cmsg + cmsg->cmsg_len;
      size_t data_length = data_end - (char *)CMSG_DATA(cmsg);
      assert(data_length % sizeof(int) == 0);
      size_t cmsg_fd_count = data_length / sizeof(int);

      int *cmsg_fds = (int *)CMSG_DATA(cmsg);
      for (size_t i = 0; i < cmsg_fd_count && max_fd_count; i++) {
        *fds = cmsg_fds[i];
        fds++;
        max_fd_count--;
      }
    }
  }

  return 0;
}

// Assembly:
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdx, %r12
testq %r8, %r8
je 0x14d1
movq %r8, %rbx
movq %rsi, (%rsp)
movq %r12, 0x8(%rsp)
xorps %xmm0, %xmm0
movaps %xmm0, 0xc0(%rsp)
movaps %xmm0, 0xb0(%rsp)
movaps %xmm0, 0xa0(%rsp)
movaps %xmm0, 0x90(%rsp)
movaps %xmm0, 0x80(%rsp)
movaps %xmm0, 0x70(%rsp)
movaps %xmm0, 0x60(%rsp)
movaps %xmm0, 0x50(%rsp)
leaq 0x7(,%r8,4), %rax
andq $-0x8, %rax
addq $0x10, %rax
cmpq $0x81, %rax
jb 0x14e7
callq 0x1050
movl $0xc, (%rax)
pushq $-0x1
popq %r15
jmp 0x1543
movq %r12, %rdx
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
jmp 0x15e1
movq %rcx, %r14
leaq 0x10(%rsp), %rsi
movaps %xmm0, 0x10(%rsi)
andq $0x0, 0x30(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, (%rsi)
movq %rsp, %rcx
movq %rcx, 0x10(%rsi)
movq $0x1, 0x18(%rsi)
leaq 0x50(%rsp), %rcx
movq %rcx, 0x20(%rsi)
movq %rax, 0x28(%rsi)
xorl %edx, %edx
callq 0x1170
pushq $-0x1
popq %r15
cmpq $-0x1, %rax
je 0x1543
cmpq %r12, %rax
jne 0x1538
testb $0x28, 0x40(%rsp)
je 0x1555
callq 0x1050
movl $0x5a, (%rax)
movl %r15d, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r14
popq %r15
retq
cmpq $0x10, 0x38(%rsp)
jb 0x1564
movq 0x30(%rsp), %rax
jmp 0x1566
xorl %eax, %eax
xorl %r15d, %r15d
leaq 0x10(%rsp), %r12
testq %rax, %rax
je 0x1543
testq %rbx, %rbx
je 0x1543
cmpl $0x1, 0x8(%rax)
jne 0x15b5
cmpl $0x1, 0xc(%rax)
jne 0x15b5
movq (%rax), %rcx
testb $0x3, %cl
jne 0x15c2
addq $-0x10, %rcx
shrq $0x2, %rcx
cmpq %rcx, %rbx
cmovbq %rbx, %rcx
xorl %edx, %edx
cmpq %rdx, %rcx
je 0x15b2
movl 0x10(%rax,%rdx,4), %esi
movl %esi, (%r14)
addq $0x4, %r14
incq %rdx
jmp 0x159d
subq %rdx, %rbx
movq %r12, %rdi
movq %rax, %rsi
callq 0x11d0
jmp 0x156e
leaq 0x1ba7(%rip), %rdi      # 0x3170
leaq 0x1bbf(%rip), %rsi      # 0x318f
leaq 0x1bc3(%rip), %rcx      # 0x319a
movl $0xfe, %edx
callq 0x10b0