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

/* Variables and functions */
int O_CREAT;
int O_RDWR;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
int fork();
int getpid();
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, int, ...);
int /*<<< orphan*/ write(int, char *, int);
int main() {
  int filedes;
  filedes = open("myfile.txt", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR);
  int rv;
  rv = fork();
  if (rv == 0) {
    char msg[] = "Process 1\n";
    printf("Hello, I'm in the child, my process ID is %d\n", getpid());
    write(filedes, msg, sizeof(msg));
  } else {
    char msg[] = "Process 2\n";
    printf(
        "This is the parent process, my process ID is %d and my child is %d\n",
        getpid(), rv);
    write(filedes, msg, sizeof(msg));
  }
  close(filedes);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
mov    0x2e79(%rip),%rax
or     (%rax),%esi
mov    0x2e40(%rip),%rax
mov    (%rax),%edx
mov    0x2e47(%rip),%rax
or     (%rax),%edx
lea    0xe66(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    1202 <main+0xa2>
mov    0xe4e(%rip),%rax
mov    %rax,-0x17(%rbp)
mov    0xe4b(%rip),%ax
mov    %ax,-0xf(%rbp)
mov    0xe43(%rip),%al
mov    %al,-0xd(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x17(%rbp),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
jmp    124c <main+0xec>
mov    0xe3b(%rip),%rax
mov    %rax,-0x22(%rbp)
mov    0xe38(%rip),%ax
mov    %ax,-0x1a(%rbp)
mov    0xe30(%rip),%al
mov    %al,-0x18(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    %eax,%esi
mov    -0xc(%rbp),%edx
lea    0xe1b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%edi
lea    -0x22(%rbp),%rsi
mov    $0xb,%edx
call   1030 <write@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e73(%rip),%rax
mov    0x2e84(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e49(%rip),%rax
mov    0x2e52(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
lea    0xe6f(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11d7 <main+0x77>
mov    %eax,%ebp
movabs $0x20737365636f7250,%rax
73 73 20
mov    %rax,0x8(%rsp)
movl   $0xa3220,0xf(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe85(%rip),%rdi
mov    %eax,%esi
mov    %ebp,%edx
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1205 <main+0xa5>
movabs $0x20737365636f7250,%rax
73 73 20
mov    %rax,0x8(%rsp)
movl   $0xa3120,0xf(%rsp)
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe1a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xb,%edx
call   1030 <write@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"myfile.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@__const.main.msg = [11 x i8] c"Process 1\0A\00"
@.str.1 = [46 x i8] c"Hello, I'm in the child, my process ID is %d\0A\00"
@__const.main.msg.2 = [11 x i8] c"Process 2\0A\00"
@.str.3 = [68 x i8] c"This is the parent process, my process ID is %d and my child is %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [11 x i8]
%5 = alloca [11 x i8]
store i32 0, i32* %1
%6 = load i32, i32* @O_RDWR
%7 = load i32, i32* @O_CREAT
%8 = or i32 %6, %7
%9 = load i32, i32* @S_IRUSR
%10 = load i32, i32* @S_IWUSR
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %8, i32 %11)
store i32 %12, i32* %2
%13 = call i32 (...) @fork()
store i32 %13, i32* %3
%14 = load i32, i32* %3
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %0
%17 = bitcast [11 x i8]* %4 to i8*
%18 = call i32 (...) @getpid()
%19 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = load i32, i32* %2
%21 = getelementptr inbounds [11 x i8], [11 x i8]* %4, i64 0, i64 0
%22 = call i32 @write(i32 %20, i8* %21, i32 11)
br label %31
23: ; preds = %0
%24 = bitcast [11 x i8]* %5 to i8*
%25 = call i32 (...) @getpid()
%26 = load i32, i32* %3
%27 = call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.3, i64 0, i64 0), i32 %25, i32 %26)
%28 = load i32, i32* %2
%29 = getelementptr inbounds [11 x i8], [11 x i8]* %5, i64 0, i64 0
%30 = call i32 @write(i32 %28, i8* %29, i32 11)
br label %31
31: ; preds = %23, %16
%32 = load i32, i32* %2
%33 = call i32 @close(i32 %32)
%34 = load i32, i32* %1
ret i32 %34
}

LLVM IR (-O2):
@.str = [11 x i8] c"myfile.txt\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@__const.main.msg = [11 x i8] c"Process 1\0A\00"
@.str.1 = [46 x i8] c"Hello, I'm in the child, my process ID is %d\0A\00"
@__const.main.msg.2 = [11 x i8] c"Process 2\0A\00"
@.str.3 = [68 x i8] c"This is the parent process, my process ID is %d and my child is %d\0A\00"
define i32 @main() {
%1 = alloca [11 x i8]
%2 = alloca [11 x i8]
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @S_IRUSR
%7 = load i32, i32* @S_IWUSR
%8 = or i32 %7, %6
%9 = tail call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32 %5, i32 %8)
%10 = tail call i32 (...) @fork()
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %0
%13 = getelementptr inbounds [11 x i8], [11 x i8]* %1, i64 0, i64 0
%14 = tail call i32 (...) @getpid()
%15 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0), i32 %14)
%16 = call i32 @write(i32 %9, i8* %13, i32 11)
br label %22
17: ; preds = %0
%18 = getelementptr inbounds [11 x i8], [11 x i8]* %2, i64 0, i64 0
%19 = tail call i32 (...) @getpid()
%20 = tail call i32 (i8*, i32, ...) @printf(i8* getelementptr inbounds ([68 x i8], [68 x i8]* @.str.3, i64 0, i64 0), i32 %19, i32 %10)
%21 = call i32 @write(i32 %9, i8* %18, i32 11)
br label %22
22: ; preds = %17, %12
%23 = tail call i32 @close(i32 %9)
ret i32 0
}