#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*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fork();
int /*<<< orphan*/ kill(int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ recv_fd(int);
int send_fd(int, int);
scalar_t__ socketpair(int /*<<< orphan*/, int /*<<< orphan*/,
                      int /*<<< orphan*/, int *);
int /*<<< orphan*/ waitpid(int /*<<< orphan*/, int *, int /*<<< orphan*/);
int write(int, char *, int);
int main(void) {
  int fd[2];
  int pip[2];
  pid_t pid;
  int status;
  if (pipe(pip)) {
    perror("pipe");
    return 1;
  }
  if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd)) {
    perror("socketpair");
    return 1;
  }
  if ((pid = fork()) < 0) {
    perror("fork");
  } else if (!pid) {
    exit(recv_fd(fd[1]));
  } else {
    if (send_fd(fd[0], pip[0]) == -1) {
      kill(pid, 9);
      waitpid(pid, NULL, 0);
      return 2;
    }
    if (write(pip[1], "test", sizeof("test")) != sizeof("test")) {
      close(pip[1]);
      return -1;
    }
    close(pip[1]);
    waitpid(pid, &status, 0);
  }
  return status;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x14(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11ea <main+0x3a>
lea    0xe27(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f6 <main+0x146>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
lea    -0xc(%rbp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
cmp    $0x0,%rax
je     1229 <main+0x79>
lea    0xded(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12f6 <main+0x146>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    124d <main+0x9d>
lea    0xdcd(%rip),%rdi
call   10b0 <perror@plt>
jmp    12f0 <main+0x140>
cmpl   $0x0,-0x18(%rbp)
jne    1266 <main+0xb6>
mov    -0x8(%rbp),%edi
call   1090 <recv_fd@plt>
mov    %eax,%edi
call   10c0 <exit@plt>
mov    -0xc(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1040 <send_fd@plt>
cmp    $0xffffffff,%eax
jne    12a1 <main+0xf1>
mov    -0x18(%rbp),%edi
mov    $0x9,%esi
call   1080 <kill@plt>
mov    -0x18(%rbp),%edi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
movl   $0x2,-0x4(%rbp)
jmp    12f6 <main+0x146>
mov    -0x10(%rbp),%edi
lea    0xd6a(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
cltq
cmp    $0x5,%rax
je     12d5 <main+0x125>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f6 <main+0x146>
mov    -0x10(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
jmp    12f0 <main+0x140>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
call   1060 <pipe@plt>
test   %rax,%rax
jne    1275 <main+0xc5>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
lea    0x10(%rsp),%rcx
xor    %edx,%edx
call   1070 <socketpair@plt>
test   %rax,%rax
jne    127e <main+0xce>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %eax,%eax
js     1291 <main+0xe1>
je     12a4 <main+0xf4>
mov    %eax,%ebx
mov    0x10(%rsp),%edi
mov    0x8(%rsp),%esi
call   1040 <send_fd@plt>
cmp    $0xffffffff,%eax
je     1257 <main+0xa7>
mov    0xc(%rsp),%edi
lea    0xdf1(%rip),%rsi
mov    $0x5,%edx
call   1030 <write@plt>
mov    %eax,%ebp
mov    0xc(%rsp),%edi
call   1050 <close@plt>
mov    $0xffffffff,%eax
cmp    $0x5,%ebp
jne    129d <main+0xed>
lea    0x4(%rsp),%rsi
mov    %ebx,%edi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    0x4(%rsp),%eax
jmp    129d <main+0xed>
mov    %ebx,%edi
mov    $0x9,%esi
call   1080 <kill@plt>
mov    %ebx,%edi
xor    %esi,%esi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    $0x2,%eax
jmp    129d <main+0xed>
lea    0xd84(%rip),%rdi
jmp    1285 <main+0xd5>
lea    0xd80(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%eax
jmp    129d <main+0xed>
lea    0xd78(%rip),%rdi
call   10b0 <perror@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x14(%rsp),%edi
call   1090 <recv_fd@plt>
mov    %eax,%edi
call   10c0 <exit@plt>

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

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@.str.1 = [11 x i8] c"socketpair\00"
@.str.2 = [5 x i8] c"fork\00"
@.str.3 = [5 x i8] c"test\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = alloca [2 x i32]
%3 = alloca i32
%4 = bitcast [2 x i32]* %1 to i8*
%5 = bitcast [2 x i32]* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %0
%11 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %50
12: ; preds = %0
%13 = load i32, i32* @AF_UNIX
%14 = load i32, i32* @SOCK_STREAM
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%16 = call i64 @socketpair(i32 %13, i32 %14, i32 0, i32* %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %20, label %18
18: ; preds = %12
%19 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
br label %50
20: ; preds = %12
%21 = call i32 (...) @fork()
%22 = icmp slt i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %50
25: ; preds = %20
%26 = icmp eq i32 %21, 0
br i1 %26, label %27, label %32
27: ; preds = %25
%28 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%29 = load i32, i32* %28
%30 = call i32 @recv_fd(i32 %29)
%31 = call i32 @exit(i32 %30)
unreachable
32: ; preds = %25
%33 = load i32, i32* %15
%34 = load i32, i32* %7
%35 = call i32 @send_fd(i32 %33, i32 %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %40
37: ; preds = %32
%38 = call i32 @kill(i32 %21, i32 9)
%39 = call i32 @waitpid(i32 %21, i32* null, i32 0)
br label %50
40: ; preds = %32
%41 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%42 = load i32, i32* %41
%43 = call i32 @write(i32 %42, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 5)
%44 = icmp eq i32 %43, 5
%45 = load i32, i32* %41
%46 = call i32 @close(i32 %45)
br i1 %44, label %47, label %50
47: ; preds = %40
%48 = call i32 @waitpid(i32 %21, i32* %3, i32 0)
%49 = load i32, i32* %3
br label %50
50: ; preds = %40, %23, %47, %37, %18, %10
%51 = phi i32 [ 1, %10 ], [ 1, %18 ], [ 2, %37 ], [ %49, %47 ], [ undef, %23 ], [ -1, %40 ]
ret i32 %51
}