#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int WEXITSTATUS(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopen(int, char *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  int my_pipe[2];
  if (pipe(my_pipe)) {
    perror("Failed to create pipe\n");
    return -1;
  }
  pid_t child = fork();
  if (child == 0) {
    close(my_pipe[0]);
    FILE *out = fdopen(my_pipe[1], "w");
    fprintf(out, "Are you my mummy?");
    return 42;
  } else if (child > 0) {
    close(my_pipe[1]);
    FILE *in = fdopen(my_pipe[0], "r");
    char buffer[100];
    sleep(2);
    fgets(buffer, 100, in);
    printf("My child asked \"%s\"\n", buffer);
    int status;
    wait(&status);
    printf("And then returned %d\n", WEXITSTATUS(status));
  } else {
    perror("Failed to fork\n");
    return -1;
  }
  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)
lea    -0x18(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11f4 <main+0x44>
lea    0xe1d(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <main+0x13b>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1241 <main+0x91>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
mov    -0x14(%rbp),%edi
lea    0xdfb(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x2a,-0x4(%rbp)
jmp    12eb <main+0x13b>
cmpq   $0x0,-0x20(%rbp)
jle    12c7 <main+0x117>
mov    -0x14(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%edi
lea    0xdcd(%rip),%rsi
call   1080 <fdopen@plt>
mov    %rax,-0x30(%rbp)
mov    $0x2,%edi
call   10b0 <sleep@plt>
lea    -0xa0(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    $0x64,%esi
call   1060 <fgets@plt>
lea    -0xa0(%rbp),%rsi
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0xa4(%rbp),%rdi
call   10c0 <wait@plt>
mov    -0xa4(%rbp),%edi
call   1090 <WEXITSTATUS@plt>
mov    %eax,%esi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    12df <main+0x12f>
lea    0xd8a(%rip),%rdi
call   10a0 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12eb <main+0x13b>
jmp    12e4 <main+0x134>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x78,%rsp
lea    0x4(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    1288 <main+0xd8>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
je     124f <main+0x9f>
jle    1291 <main+0xe1>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0x4(%rsp),%edi
lea    0xe3b(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,%rbx
mov    $0x2,%edi
call   10b0 <sleep@plt>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0x64,%esi
mov    %rbx,%rdx
call   1060 <fgets@plt>
lea    0xe0f(%rip),%rdi
xor    %ebx,%ebx
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
lea    0xc(%rsp),%rdi
call   10c0 <wait@plt>
mov    0xc(%rsp),%edi
call   1080 <WEXITSTATUS@plt>
lea    0xdfe(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <printf@plt>
jmp    12a2 <main+0xf2>
mov    0x4(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
lea    0xdb4(%rip),%rsi
call   1070 <fdopen@plt>
lea    0xdaa(%rip),%rdi
mov    $0x11,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   10a0 <fwrite@plt>
mov    $0x2a,%ebx
jmp    12a2 <main+0xf2>
lea    0xd71(%rip),%rdi
jmp    1298 <main+0xe8>
lea    0xdc0(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [23 x i8] c"Failed to create pipe\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [18 x i8] c"Are you my mummy?\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [21 x i8] c"My child asked \22%s\22\0A\00"
@.str.5 = [22 x i8] c"And then returned %d\0A\00"
@.str.6 = [16 x i8] c"Failed to fork\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [2 x i32]
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca [100 x i8]
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%13 = call i64 @pipe(i32* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
17: ; preds = %2
%18 = call i64 (...) @fork()
store i64 %18, i64* %7
%19 = load i64, i64* %7
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %30
21: ; preds = %17
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32* @fdopen(i32 %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %27, i32** %8
%28 = load i32*, i32** %8
%29 = call i32 @fprintf(i32* %28, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
store i32 42, i32* %3
br label %54
30: ; preds = %17
%31 = load i64, i64* %7
%32 = icmp sgt i64 %31, 0
br i1 %32, label %33, label %50
33: ; preds = %30
%34 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%35 = load i32, i32* %34
%36 = call i32 @close(i32 %35)
%37 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%38 = load i32, i32* %37
%39 = call i32* @fdopen(i32 %38, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %39, i32** %9
%40 = call i32 @sleep(i32 2)
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%42 = load i32*, i32** %9
%43 = call i32 @fgets(i8* %41, i32 100, i32* %42)
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %44)
%46 = call i32 @wait(i32* %11)
%47 = load i32, i32* %11
%48 = call i32 @WEXITSTATUS(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %48)
br label %52
50: ; preds = %30
%51 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
store i32 -1, i32* %3
br label %54
52: ; preds = %33
br label %53
53: ; preds = %52
store i32 0, i32* %3
br label %54
54: ; preds = %53, %50, %21, %15
%55 = load i32, i32* %3
ret i32 %55
}

LLVM IR (-O2):
@.str = [23 x i8] c"Failed to create pipe\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [18 x i8] c"Are you my mummy?\00"
@.str.3 = [2 x i8] c"r\00"
@.str.4 = [21 x i8] c"My child asked \22%s\22\0A\00"
@.str.5 = [22 x i8] c"And then returned %d\0A\00"
@.str.6 = [16 x i8] c"Failed to fork\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [2 x i32]
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = bitcast [2 x i32]* %3 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp eq i64 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %41
12: ; preds = %2
%13 = call i64 (...) @fork()
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %22
15: ; preds = %12
%16 = load i32, i32* %7
%17 = call i32 @close(i32 %16)
%18 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%19 = load i32, i32* %18
%20 = call i32* @fdopen(i32 %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = call i64 @fwrite(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i64 17, i64 1, i32* %20)
br label %41
22: ; preds = %12
%23 = icmp sgt i64 %13, 0
br i1 %23, label %24, label %39
24: ; preds = %22
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i32, i32* %7
%29 = call i32* @fdopen(i32 %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%30 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%31 = call i32 @sleep(i32 2)
%32 = call i32 @fgets(i8* %30, i32 100, i32* %29)
%33 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.4, i64 0, i64 0), i8* %30)
%34 = bitcast i32* %5 to i8*
%35 = call i32 @wait(i32* %5)
%36 = load i32, i32* %5
%37 = call i32 @WEXITSTATUS(i32 %36)
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.5, i64 0, i64 0), i32 %37)
br label %41
39: ; preds = %22
%40 = call i32 @perror(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.6, i64 0, i64 0))
br label %41
41: ; preds = %15, %39, %24, %10
%42 = phi i32 [ -1, %10 ], [ 42, %15 ], [ 0, %24 ], [ -1, %39 ]
ret i32 %42
}