#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 EOF;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
scalar_t__ fork();
int getpid();
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ putchar(int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ wait(int *);
int main(void) {
  int status;
  pid_t pid = fork();
  if (pid < 0) {
    puts("fork error.");
    exit(-1);
  } else if (pid == 0) {
    printf("child pid: %d\n", getpid());
    FILE *fp = fopen("Makefile", "r");
    int buf;
    while ((buf = fgetc(fp)) != EOF) {
      putchar(buf);
    }
    fclose(fp);
    puts("child process is end.");
    exit(0);
  } else {
    printf("parent pid: %d\n", getpid());
    puts("parent process is waitting.");
    wait(&status);
    puts("child process has been end.");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11db <main+0x3b>
lea    0xe34(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1256 <main+0xb6>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xe16(%rip),%rdi
call   1070 <printf@plt>
lea    0xe19(%rip),%rdi
lea    0xe1b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <fgetc@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dc3(%rip),%rcx
cmp    (%rcx),%eax
je     123a <main+0x9a>
mov    -0x1c(%rbp),%edi
call   1030 <putchar@plt>
jmp    1212 <main+0x72>
mov    -0x18(%rbp),%rdi
call   1060 <fclose@plt>
lea    0xddc(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   1050 <getpid@plt>
mov    %eax,%esi
lea    0xdd6(%rip),%rdi
call   1070 <printf@plt>
lea    0xdda(%rip),%rdi
call   1040 <puts@plt>
lea    -0x8(%rbp),%rdi
call   10b0 <wait@plt>
lea    0xde1(%rip),%rdi
call   1040 <puts@plt>
jmp    1291 <main+0xf1>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     11f9 <main+0x59>
mov    %rax,%rbx
xor    %eax,%eax
call   1050 <getpid@plt>
test   %rbx,%rbx
je     120f <main+0x6f>
lea    0xe76(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
lea    0xe78(%rip),%rdi
call   1040 <puts@plt>
lea    0x4(%rsp),%rdi
call   10b0 <wait@plt>
lea    0xe7e(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe00(%rip),%rdi
call   1040 <puts@plt>
mov    $0xffffffff,%edi
call   10a0 <exit@plt>
lea    0xdf6(%rip),%rdi
mov    %eax,%esi
call   1070 <printf@plt>
lea    0xdf7(%rip),%rdi
lea    0xdf9(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1080 <fgetc@plt>
mov    0x2da6(%rip),%r14
cmp    (%r14),%eax
je     1264 <main+0xc4>
nopw   0x0(%rax,%rax,1)
mov    %eax,%edi
call   1030 <putchar@plt>
mov    %rbx,%rdi
call   1080 <fgetc@plt>
cmp    (%r14),%eax
jne    1250 <main+0xb0>
mov    %rbx,%rdi
call   1060 <fclose@plt>
lea    0xdb3(%rip),%rdi
call   1040 <puts@plt>
xor    %edi,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"fork error.\00"
@.str.1 = [15 x i8] c"child pid: %d\0A\00"
@.str.2 = [9 x i8] c"Makefile\00"
@.str.3 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.4 = [22 x i8] c"child process is end.\00"
@.str.5 = [16 x i8] c"parent pid: %d\0A\00"
@.str.6 = [28 x i8] c"parent process is waitting.\00"
@.str.7 = [28 x i8] c"child process has been end.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i32*
%5 = alloca i32
store i32 0, i32* %1
%6 = call i64 (...) @fork()
store i64 %6, i64* %3
%7 = load i64, i64* %3
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %0
%10 = call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 -1)
unreachable
12: ; preds = %0
%13 = load i64, i64* %3
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %32
15: ; preds = %12
%16 = call i32 (...) @getpid()
%17 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %16)
%18 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %18, i32** %4
br label %19
19: ; preds = %24, %15
%20 = load i32*, i32** %4
%21 = call i32 @fgetc(i32* %20)
store i32 %21, i32* %5
%22 = load i32, i32* @EOF
%23 = icmp ne i32 %21, %22
br i1 %23, label %24, label %27
24: ; preds = %19
%25 = load i32, i32* %5
%26 = call i32 @putchar(i32 %25)
br label %19
27: ; preds = %19
%28 = load i32*, i32** %4
%29 = call i32 @fclose(i32* %28)
%30 = call i32 @puts(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%31 = call i32 @exit(i32 0)
unreachable
32: ; preds = %12
%33 = call i32 (...) @getpid()
%34 = call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %33)
%35 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%36 = call i32 @wait(i32* %2)
%37 = call i32 @puts(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
br label %38
38: ; preds = %32
br label %39
39: ; preds = %38
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"fork error.\00"
@.str.1 = [15 x i8] c"child pid: %d\0A\00"
@.str.2 = [9 x i8] c"Makefile\00"
@.str.3 = [2 x i8] c"r\00"
@EOF = global i32 0
@.str.4 = [22 x i8] c"child process is end.\00"
@.str.5 = [16 x i8] c"parent pid: %d\0A\00"
@.str.6 = [28 x i8] c"parent process is waitting.\00"
@.str.7 = [28 x i8] c"child process has been end.\00"
define i32 @main() {
%1 = alloca i32
%2 = bitcast i32* %1 to i8*
%3 = tail call i64 (...) @fork()
%4 = icmp slt i64 %3, 0
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @exit(i32 -1)
unreachable
8: ; preds = %0
%9 = icmp eq i64 %3, 0
%10 = tail call i32 (...) @getpid()
br i1 %9, label %11, label %27
11: ; preds = %8
%12 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %10)
%13 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%14 = tail call i32 @fgetc(i32* %13)
%15 = load i32, i32* @EOF
%16 = icmp eq i32 %14, %15
br i1 %16, label %23, label %17
17: ; preds = %11, %17
%18 = phi i32 [ %20, %17 ], [ %14, %11 ]
%19 = tail call i32 @putchar(i32 %18)
%20 = tail call i32 @fgetc(i32* %13)
%21 = load i32, i32* @EOF
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %17
23: ; preds = %17, %11
%24 = tail call i32 @fclose(i32* %13)
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%26 = tail call i32 @exit(i32 0)
unreachable
27: ; preds = %8
%28 = tail call i32 @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.5, i64 0, i64 0), i32 %10)
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.6, i64 0, i64 0))
%30 = call i32 @wait(i32* %1)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.7, i64 0, i64 0))
ret i32 0
}