#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*/ dup2(int, int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int pipe(int *);
pid_t pipe_and_fork(int *pip) {
  pid_t pid;
  if (pipe(pip) == -1) {
    perror("pipe");
    exit(1);
  }
  if ((pid = fork()) == (pid_t)-1) {
    perror("fork");
    exit(1);
  }
  if (pid == 0) {
    close(pip[0]);
    dup2(pip[1], 1);
    close(pip[1]);
  } else
    close(pip[1]);
  return pid;
}


x86 assembly (clang, -O0):
<pipe_and_fork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
jne    1194 <pipe_and_fork+0x34>
lea    0xe7b(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmp    $0xffffffffffffffff,%rax
jne    11bf <pipe_and_fork+0x5f>
lea    0xe55(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    11f7 <pipe_and_fork+0x97>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
call   1040 <close@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
jmp    1203 <pipe_and_fork+0xa3>
mov    -0x8(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <close@plt>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_and_fork>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <pipe@plt>
cmp    $0xffffffff,%eax
je     11b6 <pipe_and_fork+0x56>
xor    %eax,%eax
call   1080 <fork@plt>
mov    %rax,%r14
test   %rax,%rax
je     118c <pipe_and_fork+0x2c>
cmp    $0xffffffffffffffff,%r14
je     11bf <pipe_and_fork+0x5f>
add    $0x4,%rbx
jmp    11a4 <pipe_and_fork+0x44>
mov    (%rbx),%edi
call   1040 <close@plt>
mov    0x4(%rbx),%edi
add    $0x4,%rbx
mov    $0x1,%esi
call   1030 <dup2@plt>
mov    (%rbx),%edi
call   1040 <close@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe43(%rip),%rdi
jmp    11c6 <pipe_and_fork+0x66>
lea    0xe3f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
define i64 @pipe_and_fork(i32* %0) {
%2 = alloca i32*
%3 = alloca i64
store i32* %0, i32** %2
%4 = load i32*, i32** %2
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = call i64 (...) @fork()
store i64 %11, i64* %3
%12 = icmp eq i64 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = load i64, i64* %3
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %16
%20 = load i32*, i32** %2
%21 = getelementptr inbounds i32, i32* %20, i64 0
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = load i32*, i32** %2
%25 = getelementptr inbounds i32, i32* %24, i64 1
%26 = load i32, i32* %25
%27 = call i32 @dup2(i32 %26, i32 1)
%28 = load i32*, i32** %2
%29 = getelementptr inbounds i32, i32* %28, i64 1
%30 = load i32, i32* %29
%31 = call i32 @close(i32 %30)
br label %37
32: ; preds = %16
%33 = load i32*, i32** %2
%34 = getelementptr inbounds i32, i32* %33, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
br label %37
37: ; preds = %32, %19
%38 = load i64, i64* %3
ret i64 %38
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
define i64 @pipe_and_fork(i32* %0) {
%2 = tail call i32 @pipe(i32* %0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %4, label %7
4: ; preds = %1
%5 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 1)
unreachable
7: ; preds = %1
%8 = tail call i64 (...) @fork()
switch i64 %8, label %18 [
i64 -1, label %9
i64 0, label %12
]
9: ; preds = %7
%10 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @exit(i32 1)
unreachable
12: ; preds = %7
%13 = load i32, i32* %0
%14 = tail call i32 @close(i32 %13)
%15 = getelementptr inbounds i32, i32* %0, i64 1
%16 = load i32, i32* %15
%17 = tail call i32 @dup2(i32 %16, i32 1)
br label %20
18: ; preds = %7
%19 = getelementptr inbounds i32, i32* %0, i64 1
br label %20
20: ; preds = %18, %12
%21 = phi i32* [ %19, %18 ], [ %15, %12 ]
%22 = load i32, i32* %21
%23 = tail call i32 @close(i32 %22)
ret i64 %8
}