#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 */
struct sockaddr_un {
  int dummy;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ b;

/* Variables and functions */
scalar_t__ ancil_recv_fd_from(int, int *, struct sockaddr *, int);
int ancil_recv_fds_from(int, int *, int, struct sockaddr *, 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, struct sockaddr *dst_addr) {
  int fd;
  int fds[3], nfds;
  char b[] = "This is on the received fd!\n";
  if (ancil_recv_fd_from(sock, &fd, dst_addr, sizeof(struct sockaddr_un))) {
    perror("ancil_recv_fd_from");
    exit(1);
  } else {
    printf("Received fd: %d\n", fd);
  }
  write(fd, b, sizeof(b));
  close(fd);
  sleep(2);
  nfds =
      ancil_recv_fds_from(sock, fds, 3, dst_addr, sizeof(struct sockaddr_un));
  if (nfds < 0) {
    perror("ancil_recv_fds_from");
    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    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe67(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe64(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe62(%rip),%eax
mov    %eax,-0x38(%rbp)
mov    0xe5d(%rip),%al
mov    %al,-0x34(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
lea    -0x14(%rbp),%rsi
mov    $0x4,%ecx
call   1060 <ancil_recv_fd_from@plt>
cmp    $0x0,%rax
je     11f7 <child_process+0x77>
lea    0xe35(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x14(%rbp),%esi
lea    0xe2f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%edi
lea    -0x50(%rbp),%rsi
mov    $0x1d,%edx
call   1030 <write@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
mov    -0x4(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x3,%edx
mov    $0x4,%r8d
call   1040 <ancil_recv_fds_from@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1269 <child_process+0xe9>
lea    0xde7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x24(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x18(%rbp),%r8d
lea    0xdd8(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child_process>:
push   %rbp
push   %rbx
sub    $0x38,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
movups 0xe7b(%rip),%xmm0
movups %xmm0,0x1d(%rsp)
movaps 0xe62(%rip),%xmm0
movaps %xmm0,0x10(%rsp)
mov    %rsp,%rsi
mov    %rbx,%rdx
mov    $0x4,%ecx
call   1060 <ancil_recv_fd_from@plt>
test   %rax,%rax
jne    122f <child_process+0xaf>
mov    (%rsp),%esi
lea    0xe6e(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rsp),%edi
lea    0x10(%rsp),%rsi
mov    $0x1d,%edx
call   1030 <write@plt>
mov    (%rsp),%edi
call   1070 <close@plt>
mov    $0x2,%edi
call   10a0 <sleep@plt>
lea    0x4(%rsp),%rsi
mov    %ebp,%edi
mov    $0x3,%edx
mov    %rbx,%rcx
mov    $0x4,%r8d
call   1040 <ancil_recv_fds_from@plt>
test   %eax,%eax
js     1238 <child_process+0xb8>
mov    0x4(%rsp),%edx
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%r8d
lea    0xe36(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x38,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xde7(%rip),%rdi
jmp    123f <child_process+0xbf>
lea    0xe02(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

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

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