#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 getpid();
int /*<<< orphan*/ printf(char *, int, ...);
void fork1() {
  int x = 1;
  pid_t pid = fork();
  if (pid == 0) {
    printf("Child has x = %d\n", ++x);
  } else {
    printf("Parent has x = %d\n", --x);
  }
  printf("Bye from process %d with x = %d\n", getpid(), x);
}


x86 assembly (clang, -O0):
<fork1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x1,-0x4(%rbp)
mov    $0x0,%al
call   1050 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1171 <fork1+0x41>
mov    -0x4(%rbp),%esi
add    $0x1,%esi
mov    %esi,-0x4(%rbp)
lea    0xe9b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
jmp    1188 <fork1+0x58>
mov    -0x4(%rbp),%esi
add    $0xffffffff,%esi
mov    %esi,-0x4(%rbp)
lea    0xe91(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x4(%rbp),%edx
lea    0xe8a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fork1>:
push   %rbx
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <fork@plt>
test   %rax,%rax
je     114a <fork1+0x1a>
lea    0xecc(%rip),%rdi
xor    %esi,%esi
jmp    115b <fork1+0x2b>
lea    0xeaf(%rip),%rdi
mov    $0x2,%ebx
mov    $0x2,%esi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
call   1030 <getpid@plt>
lea    0xeb5(%rip),%rdi
mov    %eax,%esi
mov    %ebx,%edx
xor    %eax,%eax
pop    %rbx
jmp    1040 <printf@plt>

LLVM IR (-O0):
@.str = [18 x i8] c"Child has x = %d\0A\00"
@.str.1 = [19 x i8] c"Parent has x = %d\0A\00"
@.str.2 = [33 x i8] c"Bye from process %d with x = %d\0A\00"
define void @fork1() {
%1 = alloca i32
%2 = alloca i64
store i32 1, i32* %1
%3 = call i64 (...) @fork()
store i64 %3, i64* %2
%4 = load i64, i64* %2
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %0
%7 = load i32, i32* %1
%8 = add nsw i32 %7, 1
store i32 %8, i32* %1
%9 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %14
10: ; preds = %0
%11 = load i32, i32* %1
%12 = add nsw i32 %11, -1
store i32 %12, i32* %1
%13 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %14
14: ; preds = %10, %6
%15 = call i32 (...) @getpid()
%16 = load i32, i32* %1
%17 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %15, i32 %16)
ret void
}

LLVM IR (-O2):
@.str = [18 x i8] c"Child has x = %d\0A\00"
@.str.1 = [19 x i8] c"Parent has x = %d\0A\00"
@.str.2 = [33 x i8] c"Bye from process %d with x = %d\0A\00"
define void @fork1() {
%1 = tail call i64 (...) @fork()
%2 = icmp eq i64 %1, 0
br i1 %2, label %3, label %5
3: ; preds = %0
%4 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 2)
br label %7
5: ; preds = %0
%6 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %7
7: ; preds = %5, %3
%8 = phi i32 [ 2, %3 ], [ 0, %5 ]
%9 = tail call i32 (...) @getpid()
%10 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32 %9, i32 %8)
ret void
}