#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 pid_t;

/* Variables and functions */
int fork();
int getpid();
int /*<<< orphan*/ printf(char *, int, int);
void letsfork() {
  pid_t mypid, pidafterfork;
  static int b;
  b = 100;
  mypid = getpid();
  printf("inside letsfork before calling fork(): pid=%d  b=%d\n", mypid, b);
  pidafterfork = fork();
  if (pidafterfork == 0) {
    b++;
    printf("inside letsfork I'm the child: pid=%d  b=%d\n", getpid(), b);
  } else {
    b++;
    printf("inside letsfork I'm the parent: pid=%d  b=%d\n", getpid(), b);
  }
}


x86 assembly (clang, -O0):
<letsfork>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x64,0x2efa(%rip)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%esi
mov    0x2ee7(%rip),%edx
lea    0xea4(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1050 <fork@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    11a4 <letsfork+0x74>
mov    0x2ec1(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2eb8(%rip)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    0x2ea9(%rip),%edx
lea    0xe9b(%rip),%rdi
call   1040 <printf@plt>
jmp    11ce <letsfork+0x9e>
mov    0x2e92(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e89(%rip)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    0x2e7a(%rip),%edx
lea    0xe99(%rip),%rdi
call   1040 <printf@plt>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<letsfork>:
push   %rbx
movl   $0x64,0x2f01(%rip)
xor    %eax,%eax
call   1030 <getpid@plt>
mov    0x2ef4(%rip),%edx
lea    0xeb1(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
xor    %eax,%eax
call   1050 <fork@plt>
mov    %eax,%ebx
addl   $0x1,0x2ed6(%rip)
xor    %eax,%eax
call   1030 <getpid@plt>
test   %ebx,%ebx
mov    0x2ec7(%rip),%edx
lea    0xeb9(%rip),%rcx
lea    0xedf(%rip),%rdi
cmove  %rcx,%rdi
mov    %eax,%esi
pop    %rbx
jmp    1040 <printf@plt>

LLVM IR (-O0):
@letsfork.b = internal global i32 0
@.str = [53 x i8] c"inside letsfork before calling fork(): pid=%d b=%d\0A\00"
@.str.1 = [45 x i8] c"inside letsfork I'm the child: pid=%d b=%d\0A\00"
@.str.2 = [46 x i8] c"inside letsfork I'm the parent: pid=%d b=%d\0A\00"
define void @letsfork() {
%1 = alloca i32
%2 = alloca i32
store i32 100, i32* @letsfork.b
%3 = call i32 (...) @getpid()
store i32 %3, i32* %1
%4 = load i32, i32* %1
%5 = load i32, i32* @letsfork.b
%6 = call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %4, i32 %5)
%7 = call i32 (...) @fork()
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %16
10: ; preds = %0
%11 = load i32, i32* @letsfork.b
%12 = add nsw i32 %11, 1
store i32 %12, i32* @letsfork.b
%13 = call i32 (...) @getpid()
%14 = load i32, i32* @letsfork.b
%15 = call i32 @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 %13, i32 %14)
br label %22
16: ; preds = %0
%17 = load i32, i32* @letsfork.b
%18 = add nsw i32 %17, 1
store i32 %18, i32* @letsfork.b
%19 = call i32 (...) @getpid()
%20 = load i32, i32* @letsfork.b
%21 = call i32 @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0), i32 %19, i32 %20)
br label %22
22: ; preds = %16, %10
ret void
}

LLVM IR (-O2):
@letsfork.b = internal unnamed_addr global i32 0
@.str = [53 x i8] c"inside letsfork before calling fork(): pid=%d b=%d\0A\00"
@.str.1 = [45 x i8] c"inside letsfork I'm the child: pid=%d b=%d\0A\00"
@.str.2 = [46 x i8] c"inside letsfork I'm the parent: pid=%d b=%d\0A\00"
define void @letsfork() {
store i32 100, i32* @letsfork.b
%1 = tail call i32 (...) @getpid()
%2 = load i32, i32* @letsfork.b
%3 = tail call i32 @printf(i8* getelementptr inbounds ([53 x i8], [53 x i8]* @.str, i64 0, i64 0), i32 %1, i32 %2)
%4 = tail call i32 (...) @fork()
%5 = icmp eq i32 %4, 0
%6 = load i32, i32* @letsfork.b
%7 = add nsw i32 %6, 1
store i32 %7, i32* @letsfork.b
%8 = tail call i32 (...) @getpid()
%9 = load i32, i32* @letsfork.b
%10 = select i1 %5, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.2, i64 0, i64 0)
%11 = tail call i32 @printf(i8* %10, i32 %8, i32 %9)
ret void
}