#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*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ pipe(int *);
scalar_t__ pipe_child(int, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
pid_t create_pipe_child(int *fd, int orig_fd) {
  int fds[2];
  pid_t pid;
  if (pipe(fds)) {
    fprintf(stderr, "unable to create pipe: %s\n", strerror(errno));
    exit(1);
  }
  *fd = fds[1];
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "unable to fork: %s\n", strerror(errno));
    return pid;
  }
  if (pid) {
    close(fds[0]);
    return pid;
  }
  close(fds[1]);
  return pipe_child(fds[0], orig_fd);
}


x86 assembly (clang, -O0):
<create_pipe_child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <pipe@plt>
cmp    $0x0,%rax
je     11c8 <create_pipe_child+0x58>
mov    0x2e47(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x2c(%rbp),%edi
mov    %rax,%rdx
lea    0xe47(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    1220 <create_pipe_child+0xb0>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x30(%rbp),%edi
mov    %rax,%rdx
lea    0xe0d(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1257 <create_pipe_child+0xe7>
cmpq   $0x0,-0x28(%rbp)
je     1240 <create_pipe_child+0xd0>
mov    -0x1c(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1257 <create_pipe_child+0xe7>
mov    -0x18(%rbp),%edi
call   1030 <close@plt>
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <pipe_child@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_pipe_child>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1040 <pipe@plt>
test   %rax,%rax
jne    11f0 <create_pipe_child+0x80>
mov    0x4(%rsp),%eax
mov    %eax,(%rbx)
xor    %eax,%eax
call   1090 <fork@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11a6 <create_pipe_child+0x36>
je     11d0 <create_pipe_child+0x60>
mov    (%rsp),%edi
call   1030 <close@plt>
jmp    11e6 <create_pipe_child+0x76>
mov    0x2e33(%rip),%rax
mov    (%rax),%ebp
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe57(%rip),%rsi
mov    %ebp,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
jmp    11e6 <create_pipe_child+0x76>
mov    0x4(%rsp),%edi
call   1030 <close@plt>
mov    (%rsp),%edi
mov    %ebp,%esi
call   1050 <pipe_child@plt>
mov    %rax,%rbx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2de9(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xdf2(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [27 x i8] c"unable to create pipe: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"unable to fork: %s\0A\00"
define i64 @create_pipe_child(i32* %0, i32 %1) {
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32
%6 = alloca [2 x i32]
%7 = alloca i64
store i32* %0, i32** %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%9 = call i64 @pipe(i32* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %17
11: ; preds = %2
%12 = load i32, i32* @stderr
%13 = load i32, i32* @errno
%14 = call i8* @strerror(i32 %13)
%15 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %2
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%19 = load i32, i32* %18
%20 = load i32*, i32** %4
store i32 %19, i32* %20
%21 = call i64 (...) @fork()
store i64 %21, i64* %7
%22 = load i64, i64* %7
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = load i32, i32* @errno
%27 = call i8* @strerror(i32 %26)
%28 = call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %27)
%29 = load i64, i64* %7
store i64 %29, i64* %3
br label %46
30: ; preds = %17
%31 = load i64, i64* %7
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %38
33: ; preds = %30
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = load i64, i64* %7
store i64 %37, i64* %3
br label %46
38: ; preds = %30
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%43 = load i32, i32* %42
%44 = load i32, i32* %5
%45 = call i64 @pipe_child(i32 %43, i32 %44)
store i64 %45, i64* %3
br label %46
46: ; preds = %38, %33, %24
%47 = load i64, i64* %3
ret i64 %47
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [27 x i8] c"unable to create pipe: %s\0A\00"
@errno = global i32 0
@.str.1 = [20 x i8] c"unable to fork: %s\0A\00"
define i64 @create_pipe_child(i32* nocapture writeonly %0, i32 %1) {
%3 = alloca [2 x i32]
%4 = bitcast [2 x i32]* %3 to i8*
%5 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%6 = call i64 @pipe(i32* %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i32, i32* @errno
%11 = call i8* @strerror(i32 %10)
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %11)
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%16 = load i32, i32* %15
store i32 %16, i32* %0
%17 = call i64 (...) @fork()
%18 = icmp slt i64 %17, 0
br i1 %18, label %19, label %24
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = load i32, i32* @errno
%22 = call i8* @strerror(i32 %21)
%23 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %22)
br label %34
24: ; preds = %14
%25 = icmp eq i64 %17, 0
br i1 %25, label %29, label %26
26: ; preds = %24
%27 = load i32, i32* %5
%28 = call i32 @close(i32 %27)
br label %34
29: ; preds = %24
%30 = load i32, i32* %15
%31 = call i32 @close(i32 %30)
%32 = load i32, i32* %5
%33 = call i64 @pipe_child(i32 %32, i32 %1)
br label %34
34: ; preds = %29, %26, %19
%35 = phi i64 [ %17, %19 ], [ %17, %26 ], [ %33, %29 ]
ret i64 %35
}