#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 scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ read(int, char *, int);
int recv_fd(int);
int /*<<< orphan*/ send_fd(int, int);
int socketpair(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
               int *);
int main(void) {
  int pipefd[2];
  int fd_to_pass = 0;
  int ret = socketpair(PF_UNIX, SOCK_DGRAM, 0, pipefd);
  assert(ret != -1);
  pid_t pid = fork();
  assert(pid >= 0);
  if (pid == 0) {
    close(pipefd[0]);
    fd_to_pass = open("fd.c", O_RDWR, 0666);
    send_fd(pipefd[1], fd_to_pass > 0 ? fd_to_pass : 0);
    close(fd_to_pass);
    exit(0);
  }
  close(pipefd[1]);
  fd_to_pass = recv_fd(pipefd[0]);
  char buf[1024];
  memset(buf, '\0', 1024);
  read(fd_to_pass, buf, 1024);
  printf("I got fd %d.\n", fd_to_pass);
  close(fd_to_pass);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1090 <socketpair@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1295 <main+0xe5>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    0x2daf(%rip),%rax
mov    (%rax),%esi
lea    0xdc6(%rip),%rdi
mov    $0x1b6,%edx
call   10b0 <open@plt>
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x10(%rbp)
jle    1268 <main+0xb8>
mov    -0x10(%rbp),%eax
mov    %eax,-0x428(%rbp)
jmp    1275 <main+0xc5>
xor    %eax,%eax
mov    %eax,-0x428(%rbp)
jmp    1275 <main+0xc5>
mov    -0x424(%rbp),%edi
mov    -0x428(%rbp),%esi
call   1030 <send_fd@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   10a0 <recv_fd@plt>
mov    %eax,-0x10(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x10(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
mov    -0x10(%rbp),%esi
lea    0xd2c(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x418,%rsp
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e0f(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1090 <socketpair@plt>
xor    %edi,%edi
cmp    $0xffffffff,%eax
setne  %dil
call   1080 <assert@plt>
xor    %eax,%eax
call   10d0 <fork@plt>
mov    %rax,%rbx
mov    %rax,%rdi
shr    $0x3f,%rdi
xor    $0x1,%edi
call   1080 <assert@plt>
test   %rbx,%rbx
je     125d <main+0xad>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   10a0 <recv_fd@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    0xdc2(%rip),%rdi
mov    %ebx,%esi
call   1040 <printf@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
lea    0xd8a(%rip),%rdi
mov    $0x1b6,%edx
call   10b0 <open@plt>
mov    %eax,%ebx
mov    0xc(%rsp),%edi
test   %eax,%eax
cmovns %eax,%ebp
mov    %ebp,%esi
call   1030 <send_fd@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %edi,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@PF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"fd.c\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"I got fd %d.\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca [1024 x i8]
store i32 0, i32* %1
store i32 0, i32* %3
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_DGRAM
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i32 @socketpair(i32 %7, i32 %8, i32 0, i32* %9)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, -1
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = call i64 (...) @fork()
store i64 %15, i64* %5
%16 = load i64, i64* %5
%17 = icmp sge i64 %16, 0
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i64, i64* %5
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %41
22: ; preds = %0
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* @O_RDWR
%27 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %26, i32 438)
store i32 %27, i32* %3
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%29 = load i32, i32* %28
%30 = load i32, i32* %3
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %22
%33 = load i32, i32* %3
br label %35
34: ; preds = %22
br label %35
35: ; preds = %34, %32
%36 = phi i32 [ %33, %32 ], [ 0, %34 ]
%37 = call i32 @send_fd(i32 %29, i32 %36)
%38 = load i32, i32* %3
%39 = call i32 @close(i32 %38)
%40 = call i32 @exit(i32 0)
unreachable
41: ; preds = %0
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%43 = load i32, i32* %42
%44 = call i32 @close(i32 %43)
%45 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%46 = load i32, i32* %45
%47 = call i32 @recv_fd(i32 %46)
store i32 %47, i32* %3
%48 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%49 = call i32 @memset(i8* %48, i8 signext 0, i32 1024)
%50 = load i32, i32* %3
%51 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%52 = call i32 @read(i32 %50, i8* %51, i32 1024)
%53 = load i32, i32* %3
%54 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %53)
%55 = load i32, i32* %3
%56 = call i32 @close(i32 %55)
ret i32 0
}

LLVM IR (-O2):
@PF_UNIX = global i32 0
@SOCK_DGRAM = global i32 0
@.str = [5 x i8] c"fd.c\00"
@O_RDWR = global i32 0
@.str.1 = [14 x i8] c"I got fd %d.\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [1024 x i8]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = load i32, i32* @PF_UNIX
%5 = load i32, i32* @SOCK_DGRAM
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%7 = call i32 @socketpair(i32 %4, i32 %5, i32 0, i32* %6)
%8 = icmp ne i32 %7, -1
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = call i64 (...) @fork()
%12 = lshr i64 %11, 63
%13 = trunc i64 %12 to i32
%14 = xor i32 %13, 1
%15 = call i32 @assert(i32 %14)
%16 = icmp eq i64 %11, 0
br i1 %16, label %17, label %29
17: ; preds = %0
%18 = load i32, i32* %6
%19 = call i32 @close(i32 %18)
%20 = load i32, i32* @O_RDWR
%21 = call i32 @open(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %20, i32 438)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%23 = load i32, i32* %22
%24 = icmp sgt i32 %21, 0
%25 = select i1 %24, i32 %21, i32 0
%26 = call i32 @send_fd(i32 %23, i32 %25)
%27 = call i32 @close(i32 %21)
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %0
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = load i32, i32* %6
%34 = call i32 @recv_fd(i32 %33)
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%36 = call i32 @memset(i8* %35, i8 signext 0, i32 1024)
%37 = call i32 @read(i32 %34, i8* %35, i32 1024)
%38 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 %34)
%39 = call i32 @close(i32 %34)
ret i32 0
}