#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 EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ close(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ read_from_pipe(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ write_to_pipe(int);
int main(void) {
  pid_t pid;
  int mypipe[2];
  if (pipe(mypipe)) {
    fprintf(stderr, "Pipe failed.\n");
    return EXIT_FAILURE;
  }
  pid = fork();
  if (pid == (pid_t)0) {
    close(mypipe[1]);
    read_from_pipe(mypipe[0]);
    return EXIT_SUCCESS;
  } else if (pid < (pid_t)0) {
    fprintf(stderr, "Fork failed.\n");
    return EXIT_FAILURE;
  } else {
    close(mypipe[0]);
    write_to_pipe(mypipe[1]);
    return EXIT_SUCCESS;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11a8 <main+0x48>
mov    0x2e57(%rip),%rax
mov    (%rax),%edi
lea    0xe6e(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <main+0xcc>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11df <main+0x7f>
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
mov    -0x18(%rbp),%edi
call   1040 <read_from_pipe@plt>
mov    0x2dfb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <main+0xcc>
cmpq   $0x0,-0x10(%rbp)
jge    1210 <main+0xb0>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe14(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2dd2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    122c <main+0xcc>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    -0x14(%rbp),%edi
call   1030 <write_to_pipe@plt>
mov    0x2da9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rax
mov    %rsp,%rdi
call   1060 <pipe@plt>
test   %rax,%rax
je     118c <main+0x2c>
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
call   1070 <fprintf@plt>
mov    0x2e4e(%rip),%rax
jmp    11c5 <main+0x65>
xor    %eax,%eax
call   1080 <fork@plt>
test   %rax,%rax
je     11ad <main+0x4d>
js     11c9 <main+0x69>
mov    (%rsp),%edi
call   1050 <close@plt>
mov    0x4(%rsp),%edi
call   1030 <write_to_pipe@plt>
jmp    11be <main+0x5e>
mov    0x4(%rsp),%edi
call   1050 <close@plt>
mov    (%rsp),%edi
call   1040 <read_from_pipe@plt>
mov    0x2e0b(%rip),%rax
mov    (%rax),%eax
pop    %rcx
ret
mov    0x2e10(%rip),%rax
mov    (%rax),%edi
lea    0xe35(%rip),%rsi
jmp    117e <main+0x1e>

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

LLVM IR (-O2):
@stderr = global i32 0
@.str = [14 x i8] c"Pipe failed.\0A\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
@.str.1 = [14 x i8] c"Fork failed.\0A\00"
define i32 @main() {
%1 = alloca [2 x i32]
%2 = bitcast [2 x i32]* %1 to i8*
%3 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%4 = call i64 @pipe(i32* %3)
%5 = icmp eq i64 %4, 0
br i1 %5, label %9, label %6
6: ; preds = %0
%7 = load i32, i32* @stderr
%8 = call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %29
9: ; preds = %0
%10 = call i64 (...) @fork()
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %18
12: ; preds = %9
%13 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%14 = load i32, i32* %13
%15 = call i32 @close(i32 %14)
%16 = load i32, i32* %3
%17 = call i32 @read_from_pipe(i32 %16)
br label %29
18: ; preds = %9
%19 = icmp slt i64 %10, 0
br i1 %19, label %20, label %23
20: ; preds = %18
%21 = load i32, i32* @stderr
%22 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
br label %29
23: ; preds = %18
%24 = load i32, i32* %3
%25 = call i32 @close(i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @write_to_pipe(i32 %27)
br label %29
29: ; preds = %23, %20, %12, %6
%30 = phi i32* [ @EXIT_FAILURE, %6 ], [ @EXIT_SUCCESS, %12 ], [ @EXIT_FAILURE, %20 ], [ @EXIT_SUCCESS, %23 ]
%31 = load i32, i32* %30
ret i32 %31
}