// File: /workspace/github_c_makefile_star2_O0/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:
pushq %rbp
movq %rsp, %rbp
subq $0xa0, %rsp
movl $0x0, -0x4(%rbp)
leaq -0xc(%rbp), %rcx
movl $0x1, %esi
xorl %edx, %edx
movl %esi, %edi
callq 0x1160
cmpl $0x0, %eax
jne 0x134b
jmp 0x1363
leaq 0x2cb2(%rip), %rdi      # 0x4004
leaq 0x2cdd(%rip), %rsi      # 0x4036
movl $0x5, %edx
callq 0x16c0
callq 0x1210
movl %eax, -0x10(%rbp)
cmpl $0x0, -0x10(%rbp)
jne 0x1575
movl -0x8(%rbp), %edi
callq 0x10d0
movb $0x0, -0x11(%rbp)
movl -0xc(%rbp), %edi
leaq -0x11(%rbp), %rsi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rcx
callq 0x1700
cmpl $0x0, %eax
jne 0x13a1
jmp 0x13b9
leaq 0x2ca2(%rip), %rdi      # 0x404a
leaq 0x2c87(%rip), %rsi      # 0x4036
movl $0xc, %edx
callq 0x16c0
movsbl -0x11(%rbp), %eax
cmpl $0x62, %eax
jne 0x13c4
jmp 0x13dc
leaq 0x2cb9(%rip), %rdi      # 0x4084
leaq 0x2c64(%rip), %rsi      # 0x4036
movl $0xd, %edx
callq 0x16c0
movq 0x2e71(%rip), %rax      # 0x4254
movq %rax, -0x20(%rbp)
movl 0x2e6f(%rip), %eax      # 0x425c
movl %eax, -0x18(%rbp)
movl -0xc(%rbp), %edi
leaq -0x20(%rbp), %rcx
leaq -0x11(%rbp), %rsi
movl $0x1, %edx
movl $0x2, %r8d
callq 0x1700
cmpl $0x0, %eax
jne 0x1412
jmp 0x142a
leaq 0x2c74(%rip), %rdi      # 0x408d
leaq 0x2c16(%rip), %rsi      # 0x4036
movl $0x15, %edx
callq 0x16c0
movsbl -0x11(%rbp), %eax
cmpl $0x61, %eax
jne 0x1435
jmp 0x144d
leaq 0x2c8b(%rip), %rdi      # 0x40c7
leaq 0x2bf3(%rip), %rsi      # 0x4036
movl $0x16, %edx
callq 0x16c0
cmpl $0x2, -0x20(%rbp)
jle 0x1455
jmp 0x146d
leaq 0x2c74(%rip), %rdi      # 0x40d0
leaq 0x2bd3(%rip), %rsi      # 0x4036
movl $0x17, %edx
callq 0x16c0
cmpl $-0x1, -0x1c(%rbp)
jne 0x1475
jmp 0x148d
leaq 0x2c5f(%rip), %rdi      # 0x40db
leaq 0x2bb3(%rip), %rsi      # 0x4036
movl $0x18, %edx
callq 0x16c0
cmpl $-0x1, -0x18(%rbp)
jne 0x1495
jmp 0x14ad
leaq 0x2c4c(%rip), %rdi      # 0x40e8
leaq 0x2b93(%rip), %rsi      # 0x4036
movl $0x19, %edx
callq 0x16c0
movl -0x20(%rbp), %edi
leaq -0x90(%rbp), %rsi
movl $0x63, %edx
callq 0x1100
cmpq $0x0, %rax
jle 0x14c9
jmp 0x14e1
leaq 0x2c25(%rip), %rdi      # 0x40f5
leaq 0x2b5f(%rip), %rsi      # 0x4036
movl $0x1c, %edx
callq 0x16c0
leaq -0x90(%rbp), %rdi
leaq 0x2c2e(%rip), %rsi      # 0x411d
callq 0x1110
cmpl $0x0, %eax
jne 0x14fb
jmp 0x1513
leaq 0x2c21(%rip), %rdi      # 0x4123
leaq 0x2b2d(%rip), %rsi      # 0x4036
movl $0x1d, %edx
callq 0x16c0
movl -0xc(%rbp), %edi
leaq -0x11(%rbp), %rsi
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rcx
callq 0x1700
cmpl $0x0, %eax
jne 0x1533
jmp 0x154b
leaq 0x2b10(%rip), %rdi      # 0x404a
leaq 0x2af5(%rip), %rsi      # 0x4036
movl $0x1f, %edx
callq 0x16c0
movsbl -0x11(%rbp), %eax
cmpl $0x72, %eax
jne 0x1556
jmp 0x156e
leaq 0x2be3(%rip), %rdi      # 0x4140
leaq 0x2ad2(%rip), %rsi      # 0x4036
movl $0x20, %edx
callq 0x16c0
xorl %edi, %edi
callq 0x11e0
movl -0xc(%rbp), %edi
callq 0x10d0
leaq -0x98(%rbp), %rdi
callq 0x10f0
cmpl $0x0, %eax
jne 0x1590
jmp 0x15a8
leaq 0x2bb2(%rip), %rdi      # 0x4149
leaq 0x2a98(%rip), %rsi      # 0x4036
movl $0x28, %edx
callq 0x16c0
movl -0x8(%rbp), %edi
leaq 0x2bab(%rip), %rsi      # 0x415d
movl $0x1, %edx
xorl %eax, %eax
movl %eax, %r8d
movq %r8, %rcx
callq 0x1a90
cmpl $0x0, %eax
jne 0x15cb
jmp 0x15e3
leaq 0x2b8d(%rip), %rdi      # 0x415f
leaq 0x2a5d(%rip), %rsi      # 0x4036
movl $0x2a, %edx
callq 0x16c0
movl -0x8(%rbp), %edi
leaq -0x98(%rbp), %rcx
leaq 0x2ba7(%rip), %rsi      # 0x419b
movl $0x3, %edx
movl $0x1, %r8d
callq 0x1a90
cmpl $0x0, %eax
jne 0x160b
jmp 0x1623
leaq 0x2b8c(%rip), %rdi      # 0x419e
leaq 0x2a1d(%rip), %rsi      # 0x4036
movl $0x2b, %edx
callq 0x16c0
movl -0x98(%rbp), %edi
callq 0x10d0
cmpl $0x0, %eax
jne 0x1635
jmp 0x164d
leaq 0x2ba7(%rip), %rdi      # 0x41e3
leaq 0x29f3(%rip), %rsi      # 0x4036
movl $0x2d, %edx
callq 0x16c0
movl -0x94(%rbp), %edi
leaq 0x2ac3(%rip), %rsi      # 0x411d
movl $0x6, %edx
callq 0x1080
cmpq $0x6, %rax
jne 0x166c
jmp 0x1684
leaq 0x2b88(%rip), %rdi      # 0x41fb
leaq 0x29bc(%rip), %rsi      # 0x4036
movl $0x2f, %edx
callq 0x16c0
leaq -0x9c(%rbp), %rdi
callq 0x1200
cmpl $-0x1, %eax
je 0x1697
jmp 0x16af
leaq 0x2b81(%rip), %rdi      # 0x421f
leaq 0x2991(%rip), %rsi      # 0x4036
movl $0x32, %edx
callq 0x16c0
movl -0x4(%rbp), %eax
addq $0xa0, %rsp
popq %rbp
retq
nopl (%rax,%rax)