// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0x150, %rsp            # imm = 0x150
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 0x154d
movl -0x8(%rbp), %edi
movq -0x10(%rbp), %rsi
movq -0x18(%rbp), %rdx
callq 0x1830
movl %eax, -0x4(%rbp)
jmp 0x181e
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 0x15b3
callq 0x1050
movl $0xc, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x181e
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)
movl -0x8(%rbp), %edi
leaq -0x100(%rbp), %rsi
xorl %edx, %edx
callq 0x1180
movq %rax, -0x108(%rbp)
cmpq $-0x1, -0x108(%rbp)
jne 0x1626
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x181e
movq -0x108(%rbp), %rax
cmpq -0x18(%rbp), %rax
jne 0x164f
movl -0xd0(%rbp), %eax
andl $0x20, %eax
cmpl $0x0, %eax
jne 0x164f
movl -0xd0(%rbp), %eax
andl $0x8, %eax
cmpl $0x0, %eax
je 0x1666
callq 0x1050
movl $0x5a, (%rax)
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x181e
cmpq $0x10, -0xd8(%rbp)
jb 0x1680
movq -0xe0(%rbp), %rax
movq %rax, -0x140(%rbp)
jmp 0x168b
xorl %eax, %eax
movq %rax, -0x140(%rbp)
jmp 0x168b
movq -0x140(%rbp), %rax
movq %rax, -0x110(%rbp)
xorl %eax, %eax
cmpq $0x0, -0x110(%rbp)
movb %al, -0x141(%rbp)
je 0x16b9
cmpq $0x0, -0x28(%rbp)
setne %al
movb %al, -0x141(%rbp)
movb -0x141(%rbp), %al
testb $0x1, %al
jne 0x16c8
jmp 0x1817
movq -0x110(%rbp), %rax
cmpl $0x1, 0x8(%rax)
jne 0x17f6
movq -0x110(%rbp), %rax
cmpl $0x1, 0xc(%rax)
jne 0x17f6
movq -0x110(%rbp), %rax
movq -0x110(%rbp), %rcx
addq (%rcx), %rax
movq %rax, -0x118(%rbp)
movq -0x118(%rbp), %rax
movq -0x110(%rbp), %rcx
addq $0x10, %rcx
subq %rcx, %rax
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rax
andq $0x3, %rax
cmpq $0x0, %rax
jne 0x1731
jmp 0x1750
leaq 0x2a5c(%rip), %rdi      # 0x4194
leaq 0x2a74(%rip), %rsi      # 0x41b3
movl $0xfe, %edx
leaq 0x2a73(%rip), %rcx      # 0x41be
callq 0x10b0
movq -0x120(%rbp), %rax
shrq $0x2, %rax
movq %rax, -0x128(%rbp)
movq -0x110(%rbp), %rax
addq $0x10, %rax
movq %rax, -0x130(%rbp)
movq $0x0, -0x138(%rbp)
movq -0x138(%rbp), %rcx
xorl %eax, %eax
cmpq -0x128(%rbp), %rcx
movb %al, -0x142(%rbp)
jae 0x17a5
cmpq $0x0, -0x28(%rbp)
setne %al
movb %al, -0x142(%rbp)
movb -0x142(%rbp), %al
testb $0x1, %al
jne 0x17b1
jmp 0x17f4
movq -0x130(%rbp), %rax
movq -0x138(%rbp), %rcx
movl (%rax,%rcx,4), %ecx
movq -0x20(%rbp), %rax
movl %ecx, (%rax)
movq -0x20(%rbp), %rax
addq $0x4, %rax
movq %rax, -0x20(%rbp)
movq -0x28(%rbp), %rax
addq $-0x1, %rax
movq %rax, -0x28(%rbp)
movq -0x138(%rbp), %rax
addq $0x1, %rax
movq %rax, -0x138(%rbp)
jmp 0x177f
jmp 0x17f6
jmp 0x17f8
movq -0x110(%rbp), %rsi
leaq -0x100(%rbp), %rdi
callq 0x11e0
movq %rax, -0x110(%rbp)
jmp 0x1699
movl $0x0, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x150, %rsp            # imm = 0x150
popq %rbp
retq
nopw (%rax,%rax)