// File: /workspace/github_c_makefile_star2_O1/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 %rbp
pushq %r15
pushq %r14
pushq %rbx
subq $0xd8, %rsp
movq %rdx, %r15
testq %r8, %r8
je 0x1511
movq %r8, %rbx
movq %rsi, (%rsp)
movq %r15, 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 0x1526
callq 0x1050
movl $0xc, (%rax)
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
jmp 0x1586
movq %r15, %rdx
addq $0xd8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
jmp 0x163f
movq %rcx, %r14
leaq 0x10(%rsp), %rsi
movaps %xmm0, 0x10(%rsi)
movaps %xmm0, 0x20(%rsi)
movaps %xmm0, (%rsi)
movq $0x0, 0x30(%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
movl $0xffffffff, %ebp       # imm = 0xFFFFFFFF
cmpq $-0x1, %rax
je 0x1586
cmpq %r15, %rax
jne 0x157b
testb $0x28, 0x40(%rsp)
je 0x1596
callq 0x1050
movl $0x5a, (%rax)
movl %ebp, %eax
addq $0xd8, %rsp
popq %rbx
popq %r14
popq %r15
popq %rbp
retq
movq 0x30(%rsp), %rax
cmpq $0x10, 0x38(%rsp)
setb %cl
testq %rax, %rax
sete %dl
xorl %ebp, %ebp
orb %cl, %dl
jne 0x1586
leaq 0x10(%rsp), %r15
xorl %ebp, %ebp
cmpl $0x1, 0x8(%rax)
jne 0x1602
cmpl $0x1, 0xc(%rax)
jne 0x1602
movq (%rax), %rcx
testb $0x3, %cl
jne 0x1620
addq $-0x10, %rcx
je 0x1602
shrq $0x2, %rcx
leaq -0x1(%rbx), %rsi
xorl %edx, %edx
xorl %r8d, %r8d
movl 0x10(%rax,%r8,4), %edi
movl %edi, (%r14,%r8,4)
leaq 0x1(%r8), %rdi
addq $-0x4, %rdx
cmpq %rcx, %rdi
jae 0x15fc
cmpq %r8, %rsi
movq %rdi, %r8
jne 0x15de
subq %rdx, %r14
subq %rdi, %rbx
movq %r15, %rdi
movq %rax, %rsi
callq 0x11d0
testq %rax, %rax
je 0x1586
testq %rbx, %rbx
jne 0x15b7
jmp 0x1586
leaq 0x1b49(%rip), %rdi      # 0x3170
leaq 0x1b61(%rip), %rsi      # 0x318f
leaq 0x1b65(%rip), %rcx      # 0x319a
movl $0xfe, %edx
callq 0x10b0