// File: /workspace/github_c_makefile_star2_O1/motet-a[P]libforks/tests/fd_transfer.c
// Function: main
int main() {
  int sockets[2];
  check(socketpair(AF_UNIX, SOCK_STREAM, 0, sockets) == 0);

  pid_t child_pid = fork();
  if (child_pid == 0) {
    close(sockets[1]);
    char c = '\0';

    check(libforks_read_socket_fds(sockets[0], &c, 1, NULL, 0) == 0);
    check(c == 'b');

    int fds[3] = {-1, -1, -1};
    check(
      libforks_read_socket_fds(
          sockets[0],
          &c, 1,
          fds, 2) == 0
    );
    check(c == 'a');
    check(fds[0] > 2);
    check(fds[1] == -1);
    check(fds[2] == -1);

    char buffer[99];
    check(read(fds[0], buffer, sizeof buffer) > 0);
    check(strcmp(buffer, "hello") == 0);

    check(libforks_read_socket_fds(sockets[0], &c, 1, NULL, 0) == 0);
    check(c == 'r');

    exit(0);
  }

  close(sockets[0]);

  int pipe_fds[2];
  check(pipe(pipe_fds) == 0);

  check(libforks_write_socket_fds(sockets[1], "b", 1, NULL, 0) == 0);
  check(libforks_write_socket_fds(sockets[1], "ar", 3, &pipe_fds[0], 1) == 0);

  check(close(pipe_fds[0]) == 0);

  check(write(pipe_fds[1], "hello", 6) == 6);

  int wait_status;
  check(wait(&wait_status) != -1);
}

// Assembly:
subq $0x88, %rsp
leaq 0x8(%rsp), %rcx
movl $0x1, %edi
movl $0x1, %esi
xorl %edx, %edx
callq 0x1140
testl %eax, %eax
jne 0x1400
callq 0x11f0
testl %eax, %eax
je 0x1411
movl 0x8(%rsp), %edi
callq 0x10c0
leaq 0x20(%rsp), %rdi
callq 0x10e0
testl %eax, %eax
jne 0x150d
movl 0xc(%rsp), %edi
leaq 0x1de9(%rip), %rsi      # 0x315d
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x183f
testl %eax, %eax
jne 0x151e
movl 0xc(%rsp), %edi
leaq 0x1e05(%rip), %rsi      # 0x319b
leaq 0x20(%rsp), %rcx
movl $0x3, %edx
movl $0x1, %r8d
callq 0x183f
testl %eax, %eax
jne 0x152f
movl 0x20(%rsp), %edi
callq 0x10c0
testl %eax, %eax
jne 0x1540
movl 0x24(%rsp), %edi
leaq 0x1d4e(%rip), %rsi      # 0x311d
movl $0x6, %edx
callq 0x1080
cmpq $0x6, %rax
jne 0x1551
leaq 0x10(%rsp), %rdi
callq 0x11e0
cmpl $-0x1, %eax
je 0x1562
xorl %eax, %eax
addq $0x88, %rsp
retq
leaq 0x1bfd(%rip), %rdi      # 0x3004
movl $0x5, %esi
callq 0x162e
movl 0xc(%rsp), %edi
callq 0x10c0
leaq 0x7(%rsp), %rsi
movb $0x0, (%rsi)
movl 0x8(%rsp), %edi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x165c
testl %eax, %eax
jne 0x1573
cmpb $0x62, 0x7(%rsp)
jne 0x1584
leaq 0x10(%rsp), %rcx
movl $0xffffffff, 0x8(%rcx)  # imm = 0xFFFFFFFF
movq $-0x1, (%rcx)
movl 0x8(%rsp), %edi
leaq 0x7(%rsp), %rsi
movl $0x1, %edx
movl $0x2, %r8d
callq 0x165c
testl %eax, %eax
jne 0x1595
cmpb $0x61, 0x7(%rsp)
jne 0x15a6
movl 0x10(%rsp), %edi
cmpl $0x2, %edi
jle 0x15b7
cmpl $-0x1, 0x14(%rsp)
jne 0x15c8
cmpl $-0x1, 0x18(%rsp)
jne 0x15d9
leaq 0x20(%rsp), %rsi
movl $0x63, %edx
callq 0x10f0
testq %rax, %rax
jle 0x15ea
movl $0x6c6c6568, %eax       # imm = 0x6C6C6568
xorl 0x20(%rsp), %eax
movzwl 0x24(%rsp), %ecx
xorl $0x6f, %ecx
orl %eax, %ecx
jne 0x15fb
movl 0x8(%rsp), %edi
leaq 0x7(%rsp), %rsi
movl $0x1, %edx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x165c
testl %eax, %eax
jne 0x160c
cmpb $0x72, 0x7(%rsp)
jne 0x161d
xorl %edi, %edi
callq 0x11c0
leaq 0x1c35(%rip), %rdi      # 0x3149
movl $0x28, %esi
callq 0x162e
leaq 0x1c3a(%rip), %rdi      # 0x315f
movl $0x2a, %esi
callq 0x162e
leaq 0x1c68(%rip), %rdi      # 0x319e
movl $0x2b, %esi
callq 0x162e
leaq 0x1c9c(%rip), %rdi      # 0x31e3
movl $0x2d, %esi
callq 0x162e
leaq 0x1ca3(%rip), %rdi      # 0x31fb
movl $0x2f, %esi
callq 0x162e
leaq 0x1cb6(%rip), %rdi      # 0x321f
movl $0x32, %esi
callq 0x162e
leaq 0x1ad0(%rip), %rdi      # 0x304a
movl $0xc, %esi
callq 0x162e
leaq 0x1af9(%rip), %rdi      # 0x3084
movl $0xd, %esi
callq 0x162e
leaq 0x1af1(%rip), %rdi      # 0x308d
movl $0x15, %esi
callq 0x162e
leaq 0x1b1a(%rip), %rdi      # 0x30c7
movl $0x16, %esi
callq 0x162e
leaq 0x1b12(%rip), %rdi      # 0x30d0
movl $0x17, %esi
callq 0x162e
leaq 0x1b0c(%rip), %rdi      # 0x30db
movl $0x18, %esi
callq 0x162e
leaq 0x1b08(%rip), %rdi      # 0x30e8
movl $0x19, %esi
callq 0x162e
leaq 0x1b04(%rip), %rdi      # 0x30f5
movl $0x1c, %esi
callq 0x162e
leaq 0x1b21(%rip), %rdi      # 0x3123
movl $0x1d, %esi
callq 0x162e
leaq 0x1a37(%rip), %rdi      # 0x304a
movl $0x1f, %esi
callq 0x162e
leaq 0x1b1c(%rip), %rdi      # 0x3140
movl $0x20, %esi
callq 0x162e