#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*/ execvp(char *, char **);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fork();
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
int main(int argc, char **argv) {
  if (argc > 1) {
    FILE *f = fopen("/proc/sys/vm/laptop_mode", "w");
    if (f) {
      puts("writing...");
      fputs("4", f);
      fclose(f);
    foo:
      goto foo;
    }
  } else {
    puts("forking...");
    int pid = fork();
    if (pid == 0) {
      puts("i am the child, executing...");
      char *args[3] = {"a.out", "foo", NULL};
      execvp("./a.out", args);
    } else {
      puts("i'm leaving the child!");
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    11d1 <main+0x71>
lea    0xe79(%rip),%rdi
lea    0xe8b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11cc <main+0x6c>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
mov    -0x18(%rbp),%rsi
lea    0xe6d(%rip),%rdi
call   1050 <fputs@plt>
mov    -0x18(%rbp),%rdi
call   1040 <fclose@plt>
jmp    11c7 <main+0x67>
jmp    1244 <main+0xe4>
lea    0xe50(%rip),%rdi
call   1030 <puts@plt>
mov    $0x0,%al
call   1080 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jne    1233 <main+0xd3>
lea    0xe3b(%rip),%rdi
call   1030 <puts@plt>
mov    0x2bfc(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2bf9(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0x2bf6(%rip),%rax
mov    %rax,-0x30(%rbp)
lea    -0x40(%rbp),%rsi
lea    0xe2b(%rip),%rdi
call   1070 <execvp@plt>
jmp    123f <main+0xdf>
lea    0xe22(%rip),%rdi
call   1030 <puts@plt>
jmp    1244 <main+0xe4>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x20,%rsp
cmp    $0x2,%edi
jl     11b2 <main+0x52>
lea    0xe8f(%rip),%rdi
lea    0xea1(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1209 <main+0xa9>
mov    %rax,%rbx
lea    0xe8b(%rip),%rdi
call   1030 <puts@plt>
mov    $0x34,%edi
mov    %rbx,%rsi
call   1050 <fputc@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
nopw   0x0(%rax,%rax,1)
jmp    11b0 <main+0x50>
lea    0xe6d(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
call   1080 <fork@plt>
test   %eax,%eax
je     11d7 <main+0x77>
lea    0xe8a(%rip),%rdi
call   1030 <puts@plt>
jmp    1209 <main+0xa9>
lea    0xe53(%rip),%rdi
call   1030 <puts@plt>
mov    0x2c26(%rip),%rax
mov    %rax,0x10(%rsp)
movaps 0x2c0a(%rip),%xmm0
movaps %xmm0,(%rsp)
lea    0xe51(%rip),%rdi
mov    %rsp,%rsi
call   1070 <execvp@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [25 x i8] c"/proc/sys/vm/laptop_mode\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"writing...\00"
@.str.3 = [2 x i8] c"4\00"
@.str.4 = [11 x i8] c"forking...\00"
@.str.5 = [29 x i8] c"i am the child, executing...\00"
@.str.6 = [6 x i8] c"a.out\00"
@.str.7 = [4 x i8] c"foo\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i32 0, i32 0), i8* null]
@.str.8 = [8 x i8] c"./a.out\00"
@.str.9 = [23 x i8] c"i'm leaving the child!\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca [3 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp sgt i32 %9, 1
br i1 %10, label %11, label %23
11: ; preds = %2
%12 = call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %12, i32** %6
%13 = load i32*, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %22
15: ; preds = %11
%16 = call i32 @puts(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%17 = load i32*, i32** %6
%18 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32* %17)
%19 = load i32*, i32** %6
%20 = call i32 @fclose(i32* %19)
br label %21
21: ; preds = %21, %15
br label %21
22: ; preds = %11
br label %36
23: ; preds = %2
%24 = call i32 @puts(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%25 = call i32 (...) @fork()
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %33
28: ; preds = %23
%29 = call i32 @puts(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%30 = bitcast [3 x i8*]* %8 to i8*
%31 = getelementptr inbounds [3 x i8*], [3 x i8*]* %8, i64 0, i64 0
%32 = call i32 @execvp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8** %31)
br label %35
33: ; preds = %23
%34 = call i32 @puts(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0))
br label %35
35: ; preds = %33, %28
br label %36
36: ; preds = %35, %22
ret i32 0
}

LLVM IR (-O2):
@.str = [25 x i8] c"/proc/sys/vm/laptop_mode\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [11 x i8] c"writing...\00"
@.str.4 = [11 x i8] c"forking...\00"
@.str.5 = [29 x i8] c"i am the child, executing...\00"
@.str.6 = [6 x i8] c"a.out\00"
@.str.7 = [4 x i8] c"foo\00"
@__const.main.args = [3 x i8*] [i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i32 0, i32 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i32 0, i32 0), i8* null]
@.str.8 = [8 x i8] c"./a.out\00"
@.str.9 = [23 x i8] c"i'm leaving the child!\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [3 x i8*]
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %13
5: ; preds = %2
%6 = tail call i32* @fopen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %24, label %8
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%10 = tail call i32 @fputc(i32 52, i32* %6)
%11 = tail call i32 @fclose(i32* %6)
br label %12
12: ; preds = %12, %8
br label %12
13: ; preds = %2
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.4, i64 0, i64 0))
%15 = tail call i32 (...) @fork()
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %13
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @.str.5, i64 0, i64 0))
%19 = bitcast [3 x i8*]* %3 to i8*
%20 = getelementptr inbounds [3 x i8*], [3 x i8*]* %3, i64 0, i64 0
%21 = call i32 @execvp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0), i8** %20)
br label %24
22: ; preds = %13
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.9, i64 0, i64 0))
br label %24
24: ; preds = %17, %22, %5
ret i32 0
}