// File: /workspace/github_c_makefile_star2_O2/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
pushq $0x1
popq %rdi
leaq 0x8(%rsp), %rcx
movl %edi, %esi
xorl %edx, %edx
callq 0x1140
testl %eax, %eax
jne 0x13e3
callq 0x1200
testl %eax, %eax
je 0x13f1
movl 0x8(%rsp), %edi
callq 0x10c0
leaq 0x20(%rsp), %rdi
callq 0x10e0
testl %eax, %eax
jne 0x14e0
movl 0xc(%rsp), %edi
leaq 0x1dfe(%rip), %rsi      # 0x315d
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x17a3
testl %eax, %eax
jne 0x14ee
movl 0xc(%rsp), %edi
leaq 0x1e1c(%rip), %rsi      # 0x319b
pushq $0x3
popq %rdx
leaq 0x20(%rsp), %rcx
pushq $0x1
popq %r8
callq 0x17a3
testl %eax, %eax
jne 0x14fc
movl 0x20(%rsp), %edi
callq 0x10c0
testl %eax, %eax
jne 0x150a
movl 0x24(%rsp), %edi
leaq 0x1d69(%rip), %rsi      # 0x311d
pushq $0x6
popq %rdx
callq 0x1080
cmpq $0x6, %rax
jne 0x1518
leaq 0x10(%rsp), %rdi
callq 0x11f0
cmpl $-0x1, %eax
je 0x1526
xorl %eax, %eax
addq $0x88, %rsp
retq
leaq 0x1c1a(%rip), %rdi      # 0x3004
pushq $0x5
jmp 0x15a8
movl 0xc(%rsp), %edi
callq 0x10c0
leaq 0x7(%rsp), %rsi
movb $0x0, (%rsi)
movl 0x8(%rsp), %edi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x15dc
testl %eax, %eax
jne 0x1531
cmpb $0x62, 0x7(%rsp)
jne 0x153c
leaq 0x10(%rsp), %rcx
orl $-0x1, 0x8(%rcx)
orq $-0x1, (%rcx)
movl 0x8(%rsp), %edi
leaq 0x7(%rsp), %rsi
pushq $0x1
popq %rdx
pushq $0x2
popq %r8
callq 0x15dc
testl %eax, %eax
jne 0x1547
cmpb $0x61, 0x7(%rsp)
jne 0x1552
movl 0x10(%rsp), %edi
cmpl $0x2, %edi
jle 0x155d
cmpl $-0x1, 0x14(%rsp)
jne 0x1568
cmpl $-0x1, 0x18(%rsp)
jne 0x1573
leaq 0x20(%rsp), %rsi
pushq $0x63
popq %rdx
callq 0x10f0
testq %rax, %rax
jle 0x157e
leaq 0x1c82(%rip), %rsi      # 0x311d
leaq 0x20(%rsp), %rdi
pushq $0x6
popq %rdx
callq 0x1170
testl %eax, %eax
jne 0x1589
movl 0x8(%rsp), %edi
leaq 0x7(%rsp), %rsi
pushq $0x1
popq %rdx
xorl %ecx, %ecx
xorl %r8d, %r8d
callq 0x15dc
testl %eax, %eax
jne 0x1594
cmpb $0x72, 0x7(%rsp)
jne 0x159f
xorl %edi, %edi
callq 0x11d0
leaq 0x1c62(%rip), %rdi      # 0x3149
pushq $0x28
jmp 0x15a8
leaq 0x1c6a(%rip), %rdi      # 0x315f
pushq $0x2a
jmp 0x15a8
leaq 0x1c9b(%rip), %rdi      # 0x319e
pushq $0x2b
jmp 0x15a8
leaq 0x1cd2(%rip), %rdi      # 0x31e3
pushq $0x2d
jmp 0x15a8
leaq 0x1cdc(%rip), %rdi      # 0x31fb
pushq $0x2f
jmp 0x15a8
leaq 0x1cf2(%rip), %rdi      # 0x321f
pushq $0x32
jmp 0x15a8
leaq 0x1b12(%rip), %rdi      # 0x304a
pushq $0xc
jmp 0x15a8
leaq 0x1b41(%rip), %rdi      # 0x3084
pushq $0xd
jmp 0x15a8
leaq 0x1b3f(%rip), %rdi      # 0x308d
pushq $0x15
jmp 0x15a8
leaq 0x1b6e(%rip), %rdi      # 0x30c7
pushq $0x16
jmp 0x15a8
leaq 0x1b6c(%rip), %rdi      # 0x30d0
pushq $0x17
jmp 0x15a8
leaq 0x1b6c(%rip), %rdi      # 0x30db
pushq $0x18
jmp 0x15a8
leaq 0x1b6e(%rip), %rdi      # 0x30e8
pushq $0x19
jmp 0x15a8
leaq 0x1b70(%rip), %rdi      # 0x30f5
pushq $0x1c
jmp 0x15a8
leaq 0x1b93(%rip), %rdi      # 0x3123
pushq $0x1d
jmp 0x15a8
leaq 0x1aaf(%rip), %rdi      # 0x304a
pushq $0x1f
jmp 0x15a8
leaq 0x1b9a(%rip), %rdi      # 0x3140
pushq $0x20
popq %rsi
callq 0x15ae