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

/* Variables and functions */
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int *);
int main(int argc, char **argv) {
  FILE *fd = fopen("message.txt", "w");
  int p = fork();
  if (p == -1) {
    printf("fork() error.\n");
    fclose(fd);
  } else if (p == 0) {
    fwrite("From child\n", 1, 11, fd);
    exit(0);
  } else {
    int status;
    wait(&status);
    fwrite("From parent\n", 1, 12, fd);
    fclose(fd);
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe7a(%rip),%rdi
lea    0xe7f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x18(%rbp)
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11c4 <main+0x54>
lea    0xe5d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
jmp    1222 <main+0xb2>
cmpl   $0x0,-0x1c(%rbp)
jne    11ef <main+0x7f>
mov    -0x18(%rbp),%rcx
lea    0xe44(%rip),%rdi
mov    $0x1,%esi
mov    $0xb,%edx
call   1070 <fwrite@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    -0x20(%rbp),%rdi
call   1080 <wait@plt>
mov    -0x18(%rbp),%rcx
lea    0xe26(%rip),%rdi
mov    $0x1,%esi
mov    $0xc,%edx
call   1070 <fwrite@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   1060 <exit@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xe84(%rip),%rdi
lea    0xe89(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1090 <fork@plt>
cmp    $0xffffffff,%eax
jne    11b3 <main+0x43>
lea    0xe89(%rip),%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
test   %eax,%eax
jne    11d7 <main+0x67>
lea    0xe50(%rip),%rdi
mov    $0x1,%esi
mov    $0xb,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
xor    %edi,%edi
call   1060 <exit@plt>
lea    0xc(%rsp),%rdi
call   1080 <wait@plt>
lea    0xe32(%rip),%rdi
mov    $0x1,%esi
mov    $0xc,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"message.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [15 x i8] c"fork() error.\0A\00"
@.str.3 = [12 x i8] c"From child\0A\00"
@.str.4 = [13 x i8] c"From parent\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %3
store i8** %1, i8*** %4
%8 = call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %8, i32** %5
%9 = call i32 (...) @fork()
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
%14 = load i32*, i32** %5
%15 = call i32 @fclose(i32* %14)
br label %30
16: ; preds = %2
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %16
%20 = load i32*, i32** %5
%21 = call i32 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 11, i32* %20)
%22 = call i32 @exit(i32 0)
unreachable
23: ; preds = %16
%24 = call i32 @wait(i32* %7)
%25 = load i32*, i32** %5
%26 = call i32 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 12, i32* %25)
%27 = load i32*, i32** %5
%28 = call i32 @fclose(i32* %27)
%29 = call i32 @exit(i32 0)
unreachable
30: ; preds = %12
ret i32 0
}

LLVM IR (-O2):
@.str = [12 x i8] c"message.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [12 x i8] c"From child\0A\00"
@.str.4 = [13 x i8] c"From parent\0A\00"
@str = [14 x i8] c"fork() error.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = tail call i32 (...) @fork()
switch i32 %5, label %12 [
i32 -1, label %6
i32 0, label %9
]
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
%8 = tail call i32 @fclose(i32* %4)
ret i32 0
9: ; preds = %2
%10 = tail call i32 @fwrite(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0), i32 1, i32 11, i32* %4)
%11 = tail call i32 @exit(i32 0)
unreachable
12: ; preds = %2
%13 = bitcast i32* %3 to i8*
%14 = call i32 @wait(i32* %3)
%15 = call i32 @fwrite(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i32 1, i32 12, i32* %4)
%16 = call i32 @fclose(i32* %4)
%17 = call i32 @exit(i32 0)
unreachable
}