#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*/ _IONBF;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ setvbuf(int /*<<< orphan*/, int /*<<< orphan*/ *,
                           int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stdout;
void forkme() {
  int pid;
  FILE *fp;
  printf("Forking...\n");
  pid = fork();
  if (pid < 0) {
    printf("Unable to fork.\n");
    exit(0);
  }
  if (pid == 0) {
    setvbuf(stdout, NULL, _IONBF, 0);
  } else {
    fp = fopen("claw.pid", "w");
    if (fp == NULL)
      return;
    fprintf(fp, "%d\n", pid);
    fclose(fp);
    exit(0);
  }
  return;
}


x86 assembly (clang, -O0):
<forkme>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
lea    0xe81(%rip),%rdi
call   1040 <printf@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11ab <forkme+0x3b>
lea    0xe6d(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1080 <exit@plt>
cmpl   $0x0,-0x4(%rbp)
jne    11d7 <forkme+0x67>
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
mov    0x2e23(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
xor    %ecx,%ecx
call   1060 <setvbuf@plt>
jmp    1221 <forkme+0xb1>
lea    0xe3f(%rip),%rdi
lea    0xe41(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11fe <forkme+0x8e>
jmp    1226 <forkme+0xb6>
mov    -0x10(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe1c(%rip),%rsi
call   1050 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
xor    %edi,%edi
call   1080 <exit@plt>
jmp    1226 <forkme+0xb6>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forkme>:
push   %r14
push   %rbx
push   %rax
lea    0xe94(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %eax,%eax
js     11d1 <forkme+0x61>
je     11af <forkme+0x3f>
mov    %eax,%ebx
lea    0xe6a(%rip),%rdi
lea    0xe6c(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
jne    11e4 <forkme+0x74>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
xor    %ecx,%ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <setvbuf@plt>
lea    0xe42(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1080 <exit@plt>
mov    %rax,%r14
lea    0xe1d(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
call   1050 <fprintf@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
xor    %edi,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@.str = [12 x i8] c"Forking...\0A\00"
@.str.1 = [17 x i8] c"Unable to fork.\0A\00"
@stdout = global i32 0
@_IONBF = global i32 0
@.str.2 = [9 x i8] c"claw.pid\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [4 x i8] c"%d\0A\00"
define void @forkme() {
%1 = alloca i32
%2 = alloca i32*
%3 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%4 = call i32 (...) @fork()
store i32 %4, i32* %1
%5 = load i32, i32* %1
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
%9 = call i32 @exit(i32 0)
unreachable
10: ; preds = %0
%11 = load i32, i32* %1
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %17
13: ; preds = %10
%14 = load i32, i32* @stdout
%15 = load i32, i32* @_IONBF
%16 = call i32 @setvbuf(i32 %14, i32* null, i32 %15, i32 0)
br label %29
17: ; preds = %10
%18 = call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
store i32* %18, i32** %2
%19 = load i32*, i32** %2
%20 = icmp eq i32* %19, null
br i1 %20, label %21, label %22
21: ; preds = %17
br label %30
22: ; preds = %17
%23 = load i32*, i32** %2
%24 = load i32, i32* %1
%25 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %24)
%26 = load i32*, i32** %2
%27 = call i32 @fclose(i32* %26)
%28 = call i32 @exit(i32 0)
unreachable
29: ; preds = %13
br label %30
30: ; preds = %29, %21
ret void
}

LLVM IR (-O2):
@stdout = global i32 0
@_IONBF = global i32 0
@.str.2 = [9 x i8] c"claw.pid\00"
@.str.3 = [2 x i8] c"w\00"
@.str.4 = [4 x i8] c"%d\0A\00"
@str = [11 x i8] c"Forking...\00"
@str.5 = [16 x i8] c"Unable to fork.\00"
define void @forkme() {
%1 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%2 = tail call i32 (...) @fork()
%3 = icmp slt i32 %2, 0
br i1 %3, label %4, label %7
4: ; preds = %0
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.5, i64 0, i64 0))
%6 = tail call i32 @exit(i32 0)
unreachable
7: ; preds = %0
%8 = icmp eq i32 %2, 0
br i1 %8, label %9, label %13
9: ; preds = %7
%10 = load i32, i32* @stdout
%11 = load i32, i32* @_IONBF
%12 = tail call i32 @setvbuf(i32 %10, i32* null, i32 %11, i32 0)
br label %20
13: ; preds = %7
%14 = tail call i32* @fopen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%15 = icmp eq i32* %14, null
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = tail call i32 @fprintf(i32* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i32 %2)
%18 = tail call i32 @fclose(i32* %14)
%19 = tail call i32 @exit(i32 0)
unreachable
20: ; preds = %13, %9
ret void
}