#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 */
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char **argv) {
  pid_t pid = fork();
  if (pid == 0) {
    printf("Hello from child!\n");
  } else if (pid > 0) {
    printf("Hello from parent!\n");
  } else {
    perror("fork");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    116d <main+0x3d>
lea    0xe9d(%rip),%rdi
call   1030 <printf@plt>
jmp    11a6 <main+0x76>
cmpq   $0x0,-0x18(%rbp)
jle    1189 <main+0x59>
lea    0xe94(%rip),%rdi
call   1030 <printf@plt>
jmp    11a1 <main+0x71>
lea    0xe97(%rip),%rdi
call   1040 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11ad <main+0x7d>
jmp    11a6 <main+0x76>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
je     1151 <main+0x21>
jle    1161 <main+0x31>
lea    0xebd(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
jmp    115d <main+0x2d>
lea    0xec0(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%eax
pop    %rbx
ret
lea    0xe98(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%ebx
jmp    115d <main+0x2d>

LLVM IR (-O0):
@.str = [19 x i8] c"Hello from child!\0A\00"
@.str.1 = [20 x i8] c"Hello from parent!\0A\00"
@.str.2 = [5 x i8] c"fork\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = call i64 (...) @fork()
store i64 %7, i64* %6
%8 = load i64, i64* %6
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
br label %20
12: ; preds = %2
%13 = load i64, i64* %6
%14 = icmp sgt i64 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %19
17: ; preds = %12
%18 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %21
19: ; preds = %15
br label %20
20: ; preds = %19, %10
store i32 0, i32* %3
br label %21
21: ; preds = %20, %17
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
@.str.2 = [5 x i8] c"fork\00"
@str = [19 x i8] c"Hello from parent!\00"
@str.3 = [18 x i8] c"Hello from child!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i64 (...) @fork()
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str.3, i64 0, i64 0))
br label %13
7: ; preds = %2
%8 = icmp sgt i64 %3, 0
br i1 %8, label %9, label %11
9: ; preds = %7
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([19 x i8], [19 x i8]* @str, i64 0, i64 0))
br label %13
11: ; preds = %7
%12 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %13
13: ; preds = %5, %9, %11
%14 = phi i32 [ 1, %11 ], [ 0, %9 ], [ 0, %5 ]
ret i32 %14
}