#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 /*<<< orphan*/ pid_t;

/* Variables and functions */
int /*<<< orphan*/ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int main() {
  pid_t child_pid;
  switch ((child_pid = fork())) {
  case -1:
    fprintf(stderr, "failed to fork");
    return 1;
  case 0:
    printf("child\n");
    break;
  default:
    printf("parent\n");
    break;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
sub    $0xffffffff,%eax
je     116c <main+0x3c>
jmp    115c <main+0x2c>
mov    -0xc(%rbp),%eax
test   %eax,%eax
je     118d <main+0x5d>
jmp    119e <main+0x6e>
mov    0x2e6d(%rip),%rax
mov    (%rax),%edi
lea    0xe84(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    11b1 <main+0x81>
lea    0xe7b(%rip),%rdi
call   1030 <printf@plt>
jmp    11aa <main+0x7a>
lea    0xe71(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fork@plt>
test   %eax,%eax
je     115f <main+0x2f>
cmp    $0xffffffff,%eax
jne    116d <main+0x3d>
mov    0x2e96(%rip),%rax
mov    (%rax),%edi
lea    0xead(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%ebx
jmp    117b <main+0x4b>
lea    0xea9(%rip),%rdi
call   1030 <puts@plt>
jmp    117b <main+0x4b>
lea    0xea1(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
pop    %rbx
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"failed to fork\00"
@.str.1 = [7 x i8] c"child\0A\00"
@.str.2 = [8 x i8] c"parent\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
store i32 0, i32* %1
%3 = call i32 (...) @fork()
store i32 %3, i32* %2
switch i32 %3, label %9 [
i32 -1, label %4
i32 0, label %7
]
4: ; preds = %0
%5 = load i32, i32* @stderr
%6 = call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %1
br label %12
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
br label %11
9: ; preds = %0
%10 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %11
11: ; preds = %9, %7
store i32 0, i32* %1
br label %12
12: ; preds = %11, %4
%13 = load i32, i32* %1
ret i32 %13
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"failed to fork\00"
@str = [6 x i8] c"child\00"
@str.3 = [7 x i8] c"parent\00"
define i32 @main() {
%1 = tail call i32 (...) @fork()
switch i32 %1, label %7 [
i32 -1, label %2
i32 0, label %5
]
2: ; preds = %0
%3 = load i32, i32* @stderr
%4 = tail call i32 @fprintf(i32 %3, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %9
5: ; preds = %0
%6 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
br label %9
7: ; preds = %0
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.3, i64 0, i64 0))
br label %9
9: ; preds = %5, %7, %2
%10 = phi i32 [ 1, %2 ], [ 0, %7 ], [ 0, %5 ]
ret i32 %10
}