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

/* Variables and functions */
int WEXITSTATUS(int);
scalar_t__ calloc(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/);
int fork();
int /*<<< orphan*/ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv, char **envp) {
  int pid;
  int *pipeFds;
  pipeFds = (int *)calloc(2, sizeof(int));
  pipe(pipeFds);
  pid = fork();
  if (pid == 0) {
    printf("child: fork returned %d\n", pid);
    close(1);
    dup(pipeFds[1]);
    close(pipeFds[0]);
    close(pipeFds[1]);
    printf("hello from child");
    exit(2);
  } else {
    int waitVal, waitStatus;
    char buf[100];
    int childStatus;
    printf("parent: child's pid=%d\n", pid);
    close(0);
    dup(pipeFds[0]);
    close(pipeFds[0]);
    close(pipeFds[1]);
    fgets(buf, 100, stdin);
    printf("parent read <%s> from child\n", buf);
    waitVal = waitpid(pid, &waitStatus, 0);
    if (waitVal == pid) {
      printf("child exited with value %d, %d\n", waitStatus,
             WEXITSTATUS(waitStatus));
    } else {
      printf("strange: waitpid returned %d\n", waitVal);
    }
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x2,%edi
mov    $0x4,%esi
call   1080 <calloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1060 <pipe@plt>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1243 <main+0xa3>
mov    -0x1c(%rbp),%esi
lea    0xe09(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   1040 <dup@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
lea    0xde7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>
mov    -0x1c(%rbp),%esi
lea    0xddd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1040 <dup@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%edi
call   1050 <close@plt>
mov    -0x28(%rbp),%rax
mov    0x4(%rax),%edi
call   1050 <close@plt>
lea    -0xa0(%rbp),%rdi
mov    0x2d5d(%rip),%rax
mov    (%rax),%edx
mov    $0x64,%esi
call   1070 <fgets@plt>
lea    -0xa0(%rbp),%rsi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x1c(%rbp),%edi
lea    -0x30(%rbp),%rsi
xor    %edx,%edx
call   10a0 <waitpid@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jne    12f5 <main+0x155>
mov    -0x30(%rbp),%eax
mov    %eax,-0xa8(%rbp)
mov    -0x30(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    -0xa8(%rbp),%esi
mov    %eax,%edx
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1306 <main+0x166>
mov    -0x2c(%rbp),%esi
lea    0xd80(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x78,%rsp
mov    $0x2,%edi
mov    $0x4,%esi
call   1080 <calloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <pipe@plt>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %eax,%eax
je     1274 <main+0xd4>
mov    %eax,%ebp
lea    0xe52(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %edi,%edi
call   1050 <close@plt>
mov    (%rbx),%edi
call   1040 <dup@plt>
mov    (%rbx),%edi
call   1050 <close@plt>
mov    0x4(%rbx),%edi
call   1050 <close@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
call   1070 <fgets@plt>
lea    0xe22(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rsi
mov    %ebp,%edi
xor    %edx,%edx
call   10a0 <waitpid@plt>
cmp    %ebp,%eax
jne    125b <main+0xbb>
mov    0xc(%rsp),%ebx
mov    %ebx,%edi
call   1090 <WEXITSTATUS@plt>
lea    0xe11(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    126b <main+0xcb>
lea    0xe1d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x78,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xd85(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <close@plt>
mov    0x4(%rbx),%edi
call   1040 <dup@plt>
mov    (%rbx),%edi
call   1050 <close@plt>
mov    0x4(%rbx),%edi
call   1050 <close@plt>
lea    0xd6d(%rip),%rdi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x2,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [25 x i8] c"child: fork returned %d\0A\00"
@.str.1 = [17 x i8] c"hello from child\00"
@.str.2 = [24 x i8] c"parent: child's pid=%d\0A\00"
@stdin = global i32 0
@.str.3 = [29 x i8] c"parent read <%s> from child\0A\00"
@.str.4 = [32 x i8] c"child exited with value %d, %d\0A\00"
@.str.5 = [30 x i8] c"strange: waitpid returned %d\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca [100 x i8]
%13 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%14 = call i64 @calloc(i32 2, i32 4)
%15 = inttoptr i64 %14 to i32*
store i32* %15, i32** %9
%16 = load i32*, i32** %9
%17 = call i32 @pipe(i32* %16)
%18 = call i32 (...) @fork()
store i32 %18, i32* %8
%19 = load i32, i32* %8
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %39
21: ; preds = %3
%22 = load i32, i32* %8
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = call i32 @close(i32 1)
%25 = load i32*, i32** %9
%26 = getelementptr inbounds i32, i32* %25, i64 1
%27 = load i32, i32* %26
%28 = call i32 @dup(i32 %27)
%29 = load i32*, i32** %9
%30 = getelementptr inbounds i32, i32* %29, i64 0
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = load i32*, i32** %9
%34 = getelementptr inbounds i32, i32* %33, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%38 = call i32 @exit(i32 2)
unreachable
39: ; preds = %3
%40 = load i32, i32* %8
%41 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %40)
%42 = call i32 @close(i32 0)
%43 = load i32*, i32** %9
%44 = getelementptr inbounds i32, i32* %43, i64 0
%45 = load i32, i32* %44
%46 = call i32 @dup(i32 %45)
%47 = load i32*, i32** %9
%48 = getelementptr inbounds i32, i32* %47, i64 0
%49 = load i32, i32* %48
%50 = call i32 @close(i32 %49)
%51 = load i32*, i32** %9
%52 = getelementptr inbounds i32, i32* %51, i64 1
%53 = load i32, i32* %52
%54 = call i32 @close(i32 %53)
%55 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%56 = load i32, i32* @stdin
%57 = call i32 @fgets(i8* %55, i32 100, i32 %56)
%58 = getelementptr inbounds [100 x i8], [100 x i8]* %12, i64 0, i64 0
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %58)
%60 = load i32, i32* %8
%61 = call i32 @waitpid(i32 %60, i32* %11, i32 0)
store i32 %61, i32* %10
%62 = load i32, i32* %10
%63 = load i32, i32* %8
%64 = icmp eq i32 %62, %63
br i1 %64, label %65, label %70
65: ; preds = %39
%66 = load i32, i32* %11
%67 = load i32, i32* %11
%68 = call i32 @WEXITSTATUS(i32 %67)
%69 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %66, i32 %68)
br label %73
70: ; preds = %39
%71 = load i32, i32* %10
%72 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %71)
br label %73
73: ; preds = %70, %65
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"child: fork returned %d\0A\00"
@.str.1 = [17 x i8] c"hello from child\00"
@.str.2 = [24 x i8] c"parent: child's pid=%d\0A\00"
@stdin = global i32 0
@.str.3 = [29 x i8] c"parent read <%s> from child\0A\00"
@.str.4 = [32 x i8] c"child exited with value %d, %d\0A\00"
@.str.5 = [30 x i8] c"strange: waitpid returned %d\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1, i8** nocapture readnone %2) {
%4 = alloca i32
%5 = alloca [100 x i8]
%6 = tail call i64 @calloc(i32 2, i32 4)
%7 = inttoptr i64 %6 to i32*
%8 = tail call i32 @pipe(i32* %7)
%9 = tail call i32 (...) @fork()
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %23
11: ; preds = %3
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 0)
%13 = tail call i32 @close(i32 1)
%14 = getelementptr inbounds i32, i32* %7, i64 1
%15 = load i32, i32* %14
%16 = tail call i32 @dup(i32 %15)
%17 = load i32, i32* %7
%18 = tail call i32 @close(i32 %17)
%19 = load i32, i32* %14
%20 = tail call i32 @close(i32 %19)
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%22 = tail call i32 @exit(i32 2)
unreachable
23: ; preds = %3
%24 = bitcast i32* %4 to i8*
%25 = getelementptr inbounds [100 x i8], [100 x i8]* %5, i64 0, i64 0
%26 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i32 %9)
%27 = tail call i32 @close(i32 0)
%28 = load i32, i32* %7
%29 = tail call i32 @dup(i32 %28)
%30 = load i32, i32* %7
%31 = tail call i32 @close(i32 %30)
%32 = getelementptr inbounds i32, i32* %7, i64 1
%33 = load i32, i32* %32
%34 = tail call i32 @close(i32 %33)
%35 = load i32, i32* @stdin
%36 = call i32 @fgets(i8* %25, i32 100, i32 %35)
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.3, i64 0, i64 0), i8* %25)
%38 = call i32 @waitpid(i32 %9, i32* %4, i32 0)
%39 = icmp eq i32 %38, %9
br i1 %39, label %40, label %44
40: ; preds = %23
%41 = load i32, i32* %4
%42 = call i32 @WEXITSTATUS(i32 %41)
%43 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i32 %41, i32 %42)
br label %46
44: ; preds = %23
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0), i32 %38)
br label %46
46: ; preds = %44, %40
ret i32 0
}