#define NULL ((void *)0)
typedef unsigned long size_t; // Customize by platform.
typedef long intptr_t;
typedef unsigned long uintptr_t;
typedef long scalar_t__; // Either arithmetic or pointer type.
/* By default, we understand bool (as a convenience). */
typedef int bool;
#define false 0
#define true 1

/* Forward declarations */

/* Type definitions */
typedef int /*<<< orphan*/ b;

/* Variables and functions */
scalar_t__ ancil_recv_fd(int, int *);
int ancil_recv_fds(int, int *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ write(int, char *, int);
void child_process(int sock) {
  int fd;
  int fds[3], nfds;
  char b[] = "This is on the received fd!\n";
  if (ancil_recv_fd(sock, &fd)) {
    perror("ancil_recv_fd");
    exit(1);
  } else {
    printf("Received fd: %d\n", fd);
  }
  write(fd, b, sizeof(b));
  close(fd);
  sleep(2);
  nfds = ancil_recv_fds(sock, fds, 3);
  if (nfds < 0) {
    perror("ancil_recv_fds");
    exit(1);
  } else {
    printf("Received %d/3 fds : %d %d %d.\n", nfds, fds[0], fds[1], fds[2]);
  }
}


x86 assembly (clang, -O0):
<child_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
mov    0xe6e(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe6b(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe68(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe66(%rip),%eax
mov    %eax,-0x28(%rbp)
mov    0xe61(%rip),%al
mov    %al,-0x24(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x8(%rbp),%rsi
call   10a0 <ancil_recv_fd@plt>
cmp    $0x0,%rax
je     11ea <child_process+0x6a>
lea    0xe42(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x8(%rbp),%esi
lea    0xe37(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x40(%rbp),%rsi
mov    $0x1d,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
mov    -0x4(%rbp),%edi
lea    -0x14(%rbp),%rsi
mov    $0x3,%edx
call   1050 <ancil_recv_fds@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    1252 <child_process+0xd2>
lea    0xdf9(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%esi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0xc(%rbp),%r8d
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child_process>:
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
movups 0xe7f(%rip),%xmm0
movups %xmm0,0x1d(%rsp)
movaps 0xe66(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rsp,%rsi
call   10a0 <ancil_recv_fd@plt>
test   %rax,%rax
jne    1219 <child_process+0x99>
mov    (%rsp),%esi
lea    0xe75(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
mov    (%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x1d,%edx
call   1030 <write@plt>
mov    (%rsp),%edi
call   1060 <close@plt>
mov    $0x2,%edi
call   1090 <sleep@plt>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
mov    $0x3,%edx
call   1050 <ancil_recv_fds@plt>
test   %eax,%eax
js     1222 <child_process+0xa2>
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
lea    0xe41(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x30,%rsp
pop    %rbx
ret
lea    0xdfd(%rip),%rdi
jmp    1229 <child_process+0xa9>
lea    0xe13(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@__const.child_process.b = [29 x i8] c"This is on the received fd!\0A\00"
@.str = [14 x i8] c"ancil_recv_fd\00"
@.str.1 = [17 x i8] c"Received fd: %d\0A\00"
@.str.2 = [15 x i8] c"ancil_recv_fds\00"
@.str.3 = [31 x i8] c"Received %d/3 fds : %d %d %d.\0A\00"
define void @child_process(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [3 x i32]
%5 = alloca i32
%6 = alloca [29 x i8]
store i32 %0, i32* %2
%7 = bitcast [29 x i8]* %6 to i8*
%8 = load i32, i32* %2
%9 = call i64 @ancil_recv_fd(i32 %8, i32* %3)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i32, i32* %3
%16 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %15)
br label %17
17: ; preds = %14
%18 = load i32, i32* %3
%19 = getelementptr inbounds [29 x i8], [29 x i8]* %6, i64 0, i64 0
%20 = call i32 @write(i32 %18, i8* %19, i32 29)
%21 = load i32, i32* %3
%22 = call i32 @close(i32 %21)
%23 = call i32 @sleep(i32 2)
%24 = load i32, i32* %2
%25 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 0
%26 = call i32 @ancil_recv_fds(i32 %24, i32* %25, i32 3)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %17
%30 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %17
%33 = load i32, i32* %5
%34 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 1
%37 = load i32, i32* %36
%38 = getelementptr inbounds [3 x i32], [3 x i32]* %4, i64 0, i64 2
%39 = load i32, i32* %38
%40 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %33, i32 %35, i32 %37, i32 %39)
br label %41
41: ; preds = %32
ret void
}

LLVM IR (-O2):
@__const.child_process.b = [29 x i8] c"This is on the received fd!\0A\00"
@.str = [14 x i8] c"ancil_recv_fd\00"
@.str.1 = [17 x i8] c"Received fd: %d\0A\00"
@.str.2 = [15 x i8] c"ancil_recv_fds\00"
@.str.3 = [31 x i8] c"Received %d/3 fds : %d %d %d.\0A\00"
define void @child_process(i32 %0) {
%2 = alloca i32
%3 = alloca [3 x i32]
%4 = alloca [29 x i8]
%5 = bitcast i32* %2 to i8*
%6 = bitcast [3 x i32]* %3 to i8*
%7 = getelementptr inbounds [29 x i8], [29 x i8]* %4, i64 0, i64 0
%8 = call i64 @ancil_recv_fd(i32 %0, i32* %2)
%9 = icmp eq i64 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 1)
unreachable
13: ; preds = %1
%14 = load i32, i32* %2
%15 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = load i32, i32* %2
%17 = call i32 @write(i32 %16, i8* %7, i32 29)
%18 = load i32, i32* %2
%19 = call i32 @close(i32 %18)
%20 = call i32 @sleep(i32 2)
%21 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%22 = call i32 @ancil_recv_fds(i32 %0, i32* %21, i32 3)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %13
%25 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %13
%28 = load i32, i32* %21
%29 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 1
%30 = load i32, i32* %29
%31 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 2
%32 = load i32, i32* %31
%33 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0), i32 %22, i32 %28, i32 %30, i32 %32)
ret void
}