#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 pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ PF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int fork();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ read(int, char *, int);
int recv_fd(int);
int /*<<< orphan*/ send_fd(int, int);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int main(void) {
  int sockfds[2];
  if (socketpair(PF_UNIX, SOCK_STREAM, 0, sockfds) < 0)
    do {
      perror("socketpair");
      exit(EXIT_FAILURE);
    } while (0);
  pid_t pid;
  pid = fork();
  if (pid == -1)
    do {
      perror("fork");
      exit(EXIT_FAILURE);
    } while (0);
  if (pid > 0) {
    close(sockfds[1]);
    int fd = recv_fd(sockfds[0]);
    char buf[1024] = {0};
    read(fd, buf, sizeof(buf));
    printf("buf=%s\n", buf);
  } else if (pid == 0) {
    close(sockfds[0]);
    int fd;
    fd = open("test.txt", O_RDONLY);
    if (fd == -1)
      ;
    send_fd(sockfds[1], fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    0x2dfe(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1080 <socketpair@plt>
cmp    $0x0,%rax
jge    1208 <main+0x58>
jmp    11ee <main+0x3e>
lea    0xe0b(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0xffffffff,-0x10(%rbp)
jne    123b <main+0x8b>
jmp    1221 <main+0x71>
lea    0xde3(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x10(%rbp)
jle    1297 <main+0xe7>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0xc(%rbp),%edi
call   1090 <recv_fd@plt>
mov    %eax,-0x14(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    -0x420(%rbp),%rsi
lea    0xd83(%rip),%rdi
call   1040 <printf@plt>
jmp    12e9 <main+0x139>
cmpl   $0x0,-0x10(%rbp)
jne    12e4 <main+0x134>
mov    -0xc(%rbp),%edi
call   1060 <close@plt>
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
lea    0xd5f(%rip),%rdi
call   10a0 <open@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    12d6 <main+0x126>
jmp    12d6 <main+0x126>
mov    -0x8(%rbp),%edi
mov    -0x424(%rbp),%esi
call   1030 <send_fd@plt>
jmp    12e9 <main+0x139>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    0x2e06(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rcx
xor    %edx,%edx
call   1080 <socketpair@plt>
test   %rax,%rax
js     1275 <main+0xc5>
xor    %eax,%eax
call   10d0 <fork@plt>
cmp    $0xffffffff,%eax
je     127e <main+0xce>
test   %eax,%eax
jle    123d <main+0x8d>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x8(%rsp),%edi
call   1090 <recv_fd@plt>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    $0x400,%edx
mov    %r14,%rdi
xor    %esi,%esi
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
call   1070 <read@plt>
lea    0xddd(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
jmp    1268 <main+0xb8>
jne    1268 <main+0xb8>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
mov    0x2d91(%rip),%rax
mov    (%rax),%esi
lea    0xdc0(%rip),%rdi
call   10a0 <open@plt>
mov    0xc(%rsp),%edi
mov    %eax,%esi
call   1030 <send_fd@plt>
xor    %eax,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret
lea    0xd84(%rip),%rdi
jmp    1285 <main+0xd5>
lea    0xd86(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d37(%rip),%rax
mov    (%rax),%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [8 x i8] c"buf=%s\0A\00"
@.str.3 = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
define i32 @main() {
%1 = alloca i32
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i32
store i32 0, i32* %1
%7 = load i32, i32* @PF_UNIX
%8 = load i32, i32* @SOCK_STREAM
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%10 = call i64 @socketpair(i32 %7, i32 %8, i32 0, i32* %9)
%11 = icmp slt i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %0
br label %13
13: ; preds = %12
%14 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; No predecessors!
br label %18
18: ; preds = %17, %0
%19 = call i32 (...) @fork()
store i32 %19, i32* %3
%20 = load i32, i32* %3
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %28
22: ; preds = %18
br label %23
23: ; preds = %22
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; No predecessors!
br label %28
28: ; preds = %27, %18
%29 = load i32, i32* %3
%30 = icmp sgt i32 %29, 0
br i1 %30, label %31, label %44
31: ; preds = %28
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%36 = load i32, i32* %35
%37 = call i32 @recv_fd(i32 %36)
store i32 %37, i32* %4
%38 = bitcast [1024 x i8]* %5 to i8*
%39 = load i32, i32* %4
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%41 = call i32 @read(i32 %39, i8* %40, i32 1024)
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%43 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %42)
br label %62
44: ; preds = %28
%45 = load i32, i32* %3
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %61
47: ; preds = %44
%48 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* @O_RDONLY
%52 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %51)
store i32 %52, i32* %6
%53 = load i32, i32* %6
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %56
55: ; preds = %47
br label %56
56: ; preds = %55, %47
%57 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%58 = load i32, i32* %57
%59 = load i32, i32* %6
%60 = call i32 @send_fd(i32 %58, i32 %59)
br label %61
61: ; preds = %56, %44
br label %62
62: ; preds = %61, %31
ret i32 0
}

LLVM IR (-O2):
@PF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str = [11 x i8] c"socketpair\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [8 x i8] c"buf=%s\0A\00"
@.str.3 = [9 x i8] c"test.txt\00"
@O_RDONLY = global i32 0
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_STREAM
%6 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%7 = call i64 @socketpair(i32 %4, i32 %5, i32 0, i32* %6)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %0
%10 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %0
%14 = call i32 (...) @fork()
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %13
%17 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = load i32, i32* @EXIT_FAILURE
%19 = call i32 @exit(i32 %18)
unreachable
20: ; preds = %13
%21 = icmp sgt i32 %14, 0
br i1 %21, label %22, label %31
22: ; preds = %20
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = load i32, i32* %6
%27 = call i32 @recv_fd(i32 %26)
%28 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%29 = call i32 @read(i32 %27, i8* %28, i32 1024)
%30 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* %28)
br label %41
31: ; preds = %20
%32 = icmp eq i32 %14, 0
br i1 %32, label %33, label %41
33: ; preds = %31
%34 = load i32, i32* %6
%35 = call i32 @close(i32 %34)
%36 = load i32, i32* @O_RDONLY
%37 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%39 = load i32, i32* %38
%40 = call i32 @send_fd(i32 %39, i32 %37)
br label %41
41: ; preds = %31, %33, %22
ret i32 0
}