#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 */
struct timeval {
  int tv_sec;
  int tv_usec;
};
struct dirent {
  char *d_name;
};
typedef int pid_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int fork();
int /*<<< orphan*/ free(void *);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
void *malloc(int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ printf(char *, ...);
struct dirent *readdir(int /*<<< orphan*/ *);
int main(int argc, char **argv) {
  printf("Test %d\n", argc);
  int i;
  for (i = 0; i < argc; i++) {
    printf("%d: %s\n", i, argv[i]);
  }
  struct timeval tv;
  if (gettimeofday(&tv, NULL) == 0) {
    printf("Gettimeofday %d %d\n", tv.tv_sec, tv.tv_usec);
    void *test = malloc(1024 * 1024);
    if (test > 0) {
      printf("Malloc %x\n", test);
      free(test);
      printf("Free\n");
      DIR *dir = opendir("file:///");
      if (dir != NULL) {
        struct dirent *ent;
        while ((ent = readdir(dir)) != NULL) {
          printf("%s\n", ent->d_name);
        }
        closedir(dir);
        pid_t pid = fork();
        if (pid == 0) {
          printf("Fork Parent\n");
        } else if (pid > 0) {
          printf("Fork Child %d\n", pid);
        } else {
          printf("Fork Failed\n");
        }
      } else {
        printf("Opendir Failed\n");
      }
    } else {
      printf("Malloc Failed\n");
    }
  } else {
    printf("Gettimeofday Failed\n");
  }
  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)
mov    -0x8(%rbp),%esi
lea    0xe60(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11e5 <main+0x65>
mov    -0x14(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe39(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11ae <main+0x2e>
lea    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1060 <gettimeofday@plt>
cmp    $0x0,%rax
jne    1331 <main+0x1b1>
mov    -0x20(%rbp),%esi
mov    -0x1c(%rbp),%edx
lea    0xe08(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0x100000,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jbe    131e <main+0x19e>
mov    -0x28(%rbp),%rsi
lea    0xdf1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
lea    0xde5(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    0xddd(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
je     130b <main+0x18b>
jmp    1272 <main+0xf2>
mov    -0x30(%rbp),%rdi
call   1080 <readdir@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     12a3 <main+0x123>
mov    -0x38(%rbp),%rax
mov    (%rax),%rsi
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1272 <main+0xf2>
mov    -0x30(%rbp),%rdi
call   1070 <closedir@plt>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jne    12d3 <main+0x153>
lea    0xd78(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1306 <main+0x186>
cmpl   $0x0,-0x3c(%rbp)
jle    12f3 <main+0x173>
mov    -0x3c(%rbp),%esi
lea    0xd65(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1301 <main+0x181>
lea    0xd61(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    1306 <main+0x186>
jmp    1319 <main+0x199>
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    132c <main+0x1ac>
lea    0xd53(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    133f <main+0x1bf>
lea    0xd4f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %edi,%ebx
lea    0xe5c(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebx,%ebx
jle    11d9 <main+0x49>
mov    %ebx,%r12d
lea    0xe4e(%rip),%r14
xor    %ebx,%ebx
nopl   (%rax)
mov    (%r15,%rbx,8),%rdx
mov    %r14,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11c0 <main+0x30>
mov    %rsp,%rdi
xor    %esi,%esi
call   1070 <gettimeofday@plt>
test   %rax,%rax
je     1202 <main+0x72>
lea    0xe59(%rip),%rdi
call   1040 <puts@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
lea    0xe01(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x100000,%edi
call   10a0 <malloc@plt>
test   %rax,%rax
je     12b1 <main+0x121>
mov    %rax,%rbx
lea    0xdf1(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
lea    0xe1d(%rip),%rdi
call   1040 <puts@plt>
lea    0xdd7(%rip),%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     12bd <main+0x12d>
mov    %rax,%rbx
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1287 <main+0xf7>
mov    (%rax),%rdi
call   1040 <puts@plt>
jmp    1270 <main+0xe0>
mov    %rbx,%rdi
call   1080 <closedir@plt>
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
je     12c9 <main+0x139>
jle    12d5 <main+0x145>
lea    0xd96(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    11f4 <main+0x64>
lea    0xda4(%rip),%rdi
jmp    11ef <main+0x5f>
lea    0xdab(%rip),%rdi
jmp    11ef <main+0x5f>
lea    0xdba(%rip),%rdi
jmp    11ef <main+0x5f>
lea    0xda2(%rip),%rdi
jmp    11ef <main+0x5f>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.dirent = type { i8* }
@.str = [9 x i8] c"Test %d\0A\00"
@.str.1 = [8 x i8] c"%d: %s\0A\00"
@.str.2 = [20 x i8] c"Gettimeofday %d %d\0A\00"
@.str.3 = [11 x i8] c"Malloc %x\0A\00"
@.str.4 = [6 x i8] c"Free\0A\00"
@.str.5 = [9 x i8] c"file:///\00"
@.str.6 = [4 x i8] c"%s\0A\00"
@.str.7 = [13 x i8] c"Fork Parent\0A\00"
@.str.8 = [15 x i8] c"Fork Child %d\0A\00"
@.str.9 = [13 x i8] c"Fork Failed\0A\00"
@.str.10 = [16 x i8] c"Opendir Failed\0A\00"
@.str.11 = [15 x i8] c"Malloc Failed\0A\00"
@.str.12 = [21 x i8] c"Gettimeofday Failed\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca %struct.dirent*
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = load i32, i32* %4
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %12)
store i32 0, i32* %6
br label %14
14: ; preds = %26, %2
%15 = load i32, i32* %6
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = load i32, i32* %6
%20 = load i8**, i8*** %5
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8*, i8** %20, i64 %22
%24 = load i8*, i8** %23
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %19, i8* %24)
br label %26
26: ; preds = %18
%27 = load i32, i32* %6
%28 = add nsw i32 %27, 1
store i32 %28, i32* %6
br label %14
29: ; preds = %14
%30 = call i64 @gettimeofday(%struct.timeval* %7, i32* null)
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %84
32: ; preds = %29
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %34, i32 %36)
%38 = call i8* @malloc(i32 1048576)
store i8* %38, i8** %8
%39 = load i8*, i8** %8
%40 = icmp ugt i8* %39, null
br i1 %40, label %41, label %81
41: ; preds = %32
%42 = load i8*, i8** %8
%43 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %42)
%44 = load i8*, i8** %8
%45 = call i32 @free(i8* %44)
%46 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0))
%47 = call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
store i32* %47, i32** %9
%48 = load i32*, i32** %9
%49 = icmp ne i32* %48, null
br i1 %49, label %50, label %78
50: ; preds = %41
br label %51
51: ; preds = %55, %50
%52 = load i32*, i32** %9
%53 = call %struct.dirent* @readdir(i32* %52)
store %struct.dirent* %53, %struct.dirent** %10
%54 = icmp ne %struct.dirent* %53, null
br i1 %54, label %55, label %60
55: ; preds = %51
%56 = load %struct.dirent*, %struct.dirent** %10
%57 = getelementptr inbounds %struct.dirent, %struct.dirent* %56, i32 0, i32 0
%58 = load i8*, i8** %57
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i8* %58)
br label %51
60: ; preds = %51
%61 = load i32*, i32** %9
%62 = call i32 @closedir(i32* %61)
%63 = call i32 (...) @fork()
store i32 %63, i32* %11
%64 = load i32, i32* %11
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %60
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0))
br label %77
68: ; preds = %60
%69 = load i32, i32* %11
%70 = icmp sgt i32 %69, 0
br i1 %70, label %71, label %74
71: ; preds = %68
%72 = load i32, i32* %11
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i32 %72)
br label %76
74: ; preds = %68
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
br label %76
76: ; preds = %74, %71
br label %77
77: ; preds = %76, %66
br label %80
78: ; preds = %41
%79 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.10, i64 0, i64 0))
br label %80
80: ; preds = %78, %77
br label %83
81: ; preds = %32
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.11, i64 0, i64 0))
br label %83
83: ; preds = %81, %80
br label %86
84: ; preds = %29
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.12, i64 0, i64 0))
br label %86
86: ; preds = %84, %83
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
%struct.dirent = type { i8* }
@.str = [9 x i8] c"Test %d\0A\00"
@.str.1 = [8 x i8] c"%d: %s\0A\00"
@.str.2 = [20 x i8] c"Gettimeofday %d %d\0A\00"
@.str.3 = [11 x i8] c"Malloc %x\0A\00"
@.str.5 = [9 x i8] c"file:///\00"
@.str.8 = [15 x i8] c"Fork Child %d\0A\00"
@str = [20 x i8] c"Gettimeofday Failed\00"
@str.13 = [14 x i8] c"Malloc Failed\00"
@str.14 = [5 x i8] c"Free\00"
@str.15 = [15 x i8] c"Opendir Failed\00"
@str.16 = [12 x i8] c"Fork Failed\00"
@str.17 = [12 x i8] c"Fork Parent\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.timeval
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %0)
%5 = icmp sgt i32 %0, 0
br i1 %5, label %6, label %16
6: ; preds = %2
%7 = zext i32 %0 to i64
br label %8
8: ; preds = %6, %8
%9 = phi i64 [ 0, %6 ], [ %14, %8 ]
%10 = getelementptr inbounds i8*, i8** %1, i64 %9
%11 = load i8*, i8** %10
%12 = trunc i64 %9 to i32
%13 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %12, i8* %11)
%14 = add nuw nsw i64 %9, 1
%15 = icmp eq i64 %14, %7
br i1 %15, label %16, label %8
16: ; preds = %8, %2
%17 = bitcast %struct.timeval* %3 to i8*
%18 = call i64 @gettimeofday(%struct.timeval* %3, i32* null)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %60
20: ; preds = %16
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%22 = load i32, i32* %21
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
%24 = load i32, i32* %23
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %22, i32 %24)
%26 = tail call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%27 = icmp eq i8* %26, null
br i1 %27, label %58, label %28
28: ; preds = %20
%29 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8* %26)
%30 = tail call i32 @free(i8* %26)
%31 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.14, i64 0, i64 0))
%32 = tail call i32* @opendir(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.5, i64 0, i64 0))
%33 = icmp eq i32* %32, null
br i1 %33, label %56, label %34
34: ; preds = %28
%35 = tail call %struct.dirent* @readdir(i32* %32)
%36 = icmp eq %struct.dirent* %35, null
br i1 %36, label %44, label %37
37: ; preds = %34, %37
%38 = phi %struct.dirent* [ %42, %37 ], [ %35, %34 ]
%39 = getelementptr inbounds %struct.dirent, %struct.dirent* %38, i64 0, i32 0
%40 = load i8*, i8** %39
%41 = tail call i32 @puts(i8* dereferenceable(1) %40)
%42 = tail call %struct.dirent* @readdir(i32* %32)
%43 = icmp eq %struct.dirent* %42, null
br i1 %43, label %44, label %37
44: ; preds = %37, %34
%45 = tail call i32 @closedir(i32* %32)
%46 = tail call i32 (...) @fork()
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.17, i64 0, i64 0))
br label %62
50: ; preds = %44
%51 = icmp sgt i32 %46, 0
br i1 %51, label %52, label %54
52: ; preds = %50
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.8, i64 0, i64 0), i32 %46)
br label %62
54: ; preds = %50
%55 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @str.16, i64 0, i64 0))
br label %62
56: ; preds = %28
%57 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @str.15, i64 0, i64 0))
br label %62
58: ; preds = %20
%59 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.13, i64 0, i64 0))
br label %62
60: ; preds = %16
%61 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @str, i64 0, i64 0))
br label %62
62: ; preds = %58, %48, %54, %52, %56, %60
ret i32 0
}