#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;
};

/* Variables and functions */
scalar_t__ ancil_send_fd_to(int, int, struct sockaddr *, int);
scalar_t__ ancil_send_fds_to(int, int *, int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
void parent_process(int sock, struct sockaddr *dst_addr) {
  int fds[2] = {1, 2};
  if (ancil_send_fd_to(sock, 1, dst_addr, sizeof(struct sockaddr_un))) {
    perror("ancil_send_fd_to");
    exit(1);
  } else {
    printf("Sent fd.\n");
  }
  sleep(1);
  if (ancil_send_fds_to(sock, fds, 2, dst_addr, sizeof(struct sockaddr_un))) {
    perror("ancil_send_fds_to");
    exit(1);
  } else {
    printf("Sent two fds.\n");
  }
}


x86 assembly (clang, -O0):
<parent_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe8a(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rdx
mov    $0x1,%esi
mov    $0x4,%ecx
call   1050 <ancil_send_fd_to@plt>
cmp    $0x0,%rax
je     11b0 <parent_process+0x50>
lea    0xe67(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe62(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rcx
mov    $0x2,%edx
mov    $0x4,%r8d
call   1040 <ancil_send_fds_to@plt>
cmp    $0x0,%rax
je     1201 <parent_process+0xa1>
lea    0xe31(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe2d(%rip),%rdi
call   1030 <printf@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parent_process>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,%ebp
movabs $0x200000001,%rax
mov    %rax,(%rsp)
mov    $0x1,%esi
mov    %rbx,%rdx
mov    $0x4,%ecx
call   1050 <ancil_send_fd_to@plt>
test   %rax,%rax
jne    11d3 <parent_process+0x73>
lea    0xe8f(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <sleep@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x2,%edx
mov    %rbx,%rcx
mov    $0x4,%r8d
call   1040 <ancil_send_fds_to@plt>
test   %rax,%rax
jne    11dc <parent_process+0x7c>
lea    0xe65(%rip),%rdi
call   1030 <puts@plt>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe26(%rip),%rdi
jmp    11e3 <parent_process+0x83>
lea    0xe2e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr = type { i32 }
@__const.parent_process.fds = [2 x i32] [i32 1, i32 2]
@.str = [17 x i8] c"ancil_send_fd_to\00"
@.str.1 = [10 x i8] c"Sent fd.\0A\00"
@.str.2 = [18 x i8] c"ancil_send_fds_to\00"
@.str.3 = [15 x i8] c"Sent two fds.\0A\00"
define void @parent_process(i32 %0, %struct.sockaddr* %1) {
%3 = alloca i32
%4 = alloca %struct.sockaddr*
%5 = alloca [2 x i32]
store i32 %0, i32* %3
store %struct.sockaddr* %1, %struct.sockaddr** %4
%6 = bitcast [2 x i32]* %5 to i8*
%7 = load i32, i32* %3
%8 = load %struct.sockaddr*, %struct.sockaddr** %4
%9 = call i64 @ancil_send_fd_to(i32 %7, i32 1, %struct.sockaddr* %8, i32 4)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %16
16: ; preds = %14
%17 = call i32 @sleep(i32 1)
%18 = load i32, i32* %3
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%20 = load %struct.sockaddr*, %struct.sockaddr** %4
%21 = call i64 @ancil_send_fds_to(i32 %18, i32* %19, i32 2, %struct.sockaddr* %20, i32 4)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%25 = call i32 @exit(i32 1)
unreachable
26: ; preds = %16
%27 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
br label %28
28: ; preds = %26
ret void
}

LLVM IR (-O2):
%struct.sockaddr = type { i32 }
@.str = [17 x i8] c"ancil_send_fd_to\00"
@.str.2 = [18 x i8] c"ancil_send_fds_to\00"
@str = [9 x i8] c"Sent fd.\00"
@str.4 = [14 x i8] c"Sent two fds.\00"
define void @parent_process(i32 %0, %struct.sockaddr* %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i64 8589934593, i64* %3
%5 = tail call i64 @ancil_send_fd_to(i32 %0, i32 1, %struct.sockaddr* %1, i32 4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0))
%9 = tail call i32 @exit(i32 1)
unreachable
10: ; preds = %2
%11 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
%12 = tail call i32 @sleep(i32 1)
%13 = bitcast i64* %3 to i32*
%14 = call i64 @ancil_send_fds_to(i32 %0, i32* %13, i32 2, %struct.sockaddr* %1, i32 4)
%15 = icmp eq i64 %14, 0
br i1 %15, label %19, label %16
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %10
%20 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.4, i64 0, i64 0))
ret void
}