#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;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int);
int /*<<< orphan*/ execl(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ panic(char *);
int /*<<< orphan*/ perror(char *);
int pipe(int *);
int /*<<< orphan*/ stdin;
int /*<<< orphan*/ strcmp(char *, char *);
void test_hello_world(void) {
  int pipefd[2];
  char *buf;
  pid_t pid;
  if (pipe(pipefd) == -1) {
    perror("pipe");
    exit(1);
  }
  if ((pid = fork()) == -1) {
    perror("fork");
    exit(1);
  }
  if (pid == 0) {
    dup2(pipefd[1], 1);
    close(pipefd[0]);
    execl("hello", "hello", NULL);
  } else {
    buf = malloc(0x100);
    dup2(pipefd[0], 0);
    close(pipefd[1]);
    fgets(buf, 0x100, stdin);
    if (!strcmp(buf, "Hello World!"))
      panic("Hello World binary did not properly output text.\n");
    free(buf);
  }
}


x86 assembly (clang, -O0):
<test_hello_world>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
jne    11f0 <test_hello_world+0x30>
lea    0xe1f(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    $0x0,%al
call   10e0 <fork@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    1219 <test_hello_world+0x59>
lea    0xdfb(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
cmpl   $0x0,-0x14(%rbp)
jne    1250 <test_hello_world+0x90>
mov    -0x4(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
lea    0xdcb(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
mov    %rsi,%rdi
call   10d0 <execl@plt>
jmp    12b5 <test_hello_world+0xf5>
mov    $0x100,%edi
call   10a0 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x4(%rbp),%edi
call   1060 <close@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d6d(%rip),%rax
mov    (%rax),%edx
mov    $0x100,%esi
call   1080 <fgets@plt>
mov    -0x10(%rbp),%rdi
lea    0xd7e(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%eax
jne    12ac <test_hello_world+0xec>
lea    0xd76(%rip),%rdi
call   1050 <panic@plt>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_hello_world>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rdi
call   1070 <pipe@plt>
cmp    $0xffffffff,%eax
je     127a <test_hello_world+0xba>
xor    %eax,%eax
call   10e0 <fork@plt>
test   %eax,%eax
je     124c <test_hello_world+0x8c>
cmp    $0xffffffff,%eax
je     1283 <test_hello_world+0xc3>
mov    $0x100,%edi
call   10a0 <malloc@plt>
mov    %rax,%rbx
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    0xc(%rsp),%edi
call   1060 <close@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x100,%esi
call   1080 <fgets@plt>
lea    0xde6(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcmp@plt>
test   %eax,%eax
jne    1242 <test_hello_world+0x82>
lea    0xde0(%rip),%rdi
call   1050 <panic@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    1274 <test_hello_world+0xb4>
mov    0xc(%rsp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    0x8(%rsp),%edi
call   1060 <close@plt>
lea    0xda0(%rip),%rdi
mov    %rdi,%rsi
xor    %edx,%edx
call   10d0 <execl@plt>
add    $0x10,%rsp
pop    %rbx
ret
lea    0xd7f(%rip),%rdi
jmp    128a <test_hello_world+0xca>
lea    0xd7b(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [6 x i8] c"hello\00"
@stdin = global i32 0
@.str.3 = [13 x i8] c"Hello World!\00"
@.str.4 = [50 x i8] c"Hello World binary did not properly output text.\0A\00"
define void @test_hello_world() {
%1 = alloca [2 x i32]
%2 = alloca i8*
%3 = alloca i32
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i32 @pipe(i32* %4)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %0
%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 = %0
%11 = call i32 (...) @fork()
store i32 %11, i32* %3
%12 = icmp eq i32 %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 i32, i32* %3
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%21 = load i32, i32* %20
%22 = call i32 @dup2(i32 %21, i32 1)
%23 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%24 = load i32, i32* %23
%25 = call i32 @close(i32 %24)
%26 = call i32 @execl(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %46
27: ; preds = %16
%28 = call i8* @malloc(i32 256)
store i8* %28, i8** %2
%29 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%30 = load i32, i32* %29
%31 = call i32 @dup2(i32 %30, i32 0)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%33 = load i32, i32* %32
%34 = call i32 @close(i32 %33)
%35 = load i8*, i8** %2
%36 = load i32, i32* @stdin
%37 = call i32 @fgets(i8* %35, i32 256, i32 %36)
%38 = load i8*, i8** %2
%39 = call i32 @strcmp(i8* %38, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%40 = icmp ne i32 %39, 0
br i1 %40, label %43, label %41
41: ; preds = %27
%42 = call i32 @panic(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0))
br label %43
43: ; preds = %41, %27
%44 = load i8*, i8** %2
%45 = call i32 @free(i8* %44)
br label %46
46: ; preds = %43, %19
ret void
}

LLVM IR (-O2):
@.str = [5 x i8] c"pipe\00"
@.str.1 = [5 x i8] c"fork\00"
@.str.2 = [6 x i8] c"hello\00"
@stdin = global i32 0
@.str.3 = [13 x i8] c"Hello World!\00"
@.str.4 = [50 x i8] c"Hello World binary did not properly output text.\0A\00"
define void @test_hello_world() {
%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 i32 @pipe(i32* %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %6, label %9
6: ; preds = %0
%7 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = call i32 (...) @fork()
switch i32 %10, label %21 [
i32 -1, label %11
i32 0, label %14
]
11: ; preds = %9
%12 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %9
%15 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%16 = load i32, i32* %15
%17 = call i32 @dup2(i32 %16, i32 1)
%18 = load i32, i32* %3
%19 = call i32 @close(i32 %18)
%20 = call i32 @execl(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* null)
br label %36
21: ; preds = %9
%22 = call dereferenceable_or_null(256) i8* @malloc(i32 256)
%23 = load i32, i32* %3
%24 = call i32 @dup2(i32 %23, i32 0)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* @stdin
%29 = call i32 @fgets(i8* %22, i32 256, i32 %28)
%30 = call i32 @strcmp(i8* dereferenceable(1) %22, i8* dereferenceable(13) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %34
32: ; preds = %21
%33 = call i32 @panic(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.4, i64 0, i64 0))
br label %34
34: ; preds = %32, %21
%35 = call i32 @free(i8* %22)
br label %36
36: ; preds = %34, %14
ret void
}