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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ memset(char *, char, int);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ wait();
int /*<<< orphan*/ write(int, char *, int);
int main(int argc, char *argv[]) {
  int fd, i;
  char path[] = "stressfs0";
  char data[512];
  printf("stressfs starting\n");
  memset(data, 'a', sizeof(data));
  for (i = 0; i < 4; i++)
    if (fork() > 0)
      break;
  printf("write %d\n", i);
  path[8] += i;
  fd = open(path, 0x200 | 0x002);
  for (i = 0; i < 20; i++)
    write(fd, data, sizeof(data));
  close(fd);
  printf("read\n");
  fd = open(path, 0x000);
  for (i = 0; i < 20; i++)
    read(fd, data, sizeof(data));
  close(fd);
  wait();
  exit(1);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe50(%rip),%rax
mov    %rax,-0x22(%rbp)
mov    0xe4d(%rip),%ax
mov    %ax,-0x1a(%rbp)
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x230(%rbp),%rdi
mov    $0x61,%esi
mov    $0x200,%edx
call   1050 <memset@plt>
movl   $0x0,-0x18(%rbp)
cmpl   $0x4,-0x18(%rbp)
jge    121d <main+0x8d>
mov    $0x0,%al
call   10b0 <fork@plt>
cmp    $0x0,%rax
jle    120a <main+0x7a>
jmp    121d <main+0x8d>
jmp    120f <main+0x7f>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    11ea <main+0x5a>
mov    -0x18(%rbp),%esi
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%ecx
movsbl -0x1a(%rbp),%eax
add    %ecx,%eax
mov    %al,-0x1a(%rbp)
lea    -0x22(%rbp),%rdi
mov    $0x202,%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x14,-0x18(%rbp)
jge    127e <main+0xee>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1030 <write@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1252 <main+0xc2>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
lea    0xd9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    -0x22(%rbp),%rdi
xor    %esi,%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x14,-0x18(%rbp)
jge    12d5 <main+0x145>
mov    -0x14(%rbp),%edi
lea    -0x230(%rbp),%rsi
mov    $0x200,%edx
call   1070 <read@plt>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12a9 <main+0x119>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
mov    $0x0,%al
call   10a0 <wait@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x210,%rsp
movabs $0x7366737365727473,%rax
73 66 73
mov    %rax,(%rsp)
movw   $0x30,0x8(%rsp)
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rdi
mov    $0x61,%esi
mov    $0x200,%edx
call   1060 <memset@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
jg     1226 <main+0x86>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    $0x1,%ebx
test   %rax,%rax
jg     1226 <main+0x86>
xor    %eax,%eax
call   10c0 <fork@plt>
mov    $0x2,%ebx
test   %rax,%rax
jg     1226 <main+0x86>
xor    %eax,%eax
call   10c0 <fork@plt>
xor    %ecx,%ecx
test   %rax,%rax
setg   %cl
mov    $0x4,%ebx
sub    %ecx,%ebx
lea    0xddd(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
or     $0x30,%bl
mov    %bl,0x8(%rsp)
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0x202,%esi
call   1090 <open@plt>
mov    %eax,%ebp
lea    0x10(%rsp),%rbx
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1040 <write@plt>
mov    %ebp,%edi
call   1070 <close@plt>
lea    0xc98(%rip),%rdi
call   1030 <puts@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1090 <open@plt>
mov    %eax,%ebp
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <read@plt>
mov    %ebp,%edi
call   1070 <close@plt>
xor    %eax,%eax
call   10b0 <wait@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@__const.main.path = [10 x i8] c"stressfs0\00"
@.str = [19 x i8] c"stressfs starting\0A\00"
@.str.1 = [10 x i8] c"write %d\0A\00"
@.str.2 = [6 x i8] c"read\0A\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 [10 x i8]
%9 = alloca [512 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = bitcast [10 x i8]* %8 to i8*
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%13 = call i32 @memset(i8* %12, i8 signext 97, i32 512)
store i32 0, i32* %7
br label %14
14: ; preds = %22, %2
%15 = load i32, i32* %7
%16 = icmp slt i32 %15, 4
br i1 %16, label %17, label %25
17: ; preds = %14
%18 = call i64 (...) @fork()
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %21
20: ; preds = %17
br label %25
21: ; preds = %17
br label %22
22: ; preds = %21
%23 = load i32, i32* %7
%24 = add nsw i32 %23, 1
store i32 %24, i32* %7
br label %14
25: ; preds = %20, %14
%26 = load i32, i32* %7
%27 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* %7
%29 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 8
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = add nsw i32 %31, %28
%33 = trunc i32 %32 to i8
store i8 %33, i8* %29
%34 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%35 = call i32 @open(i8* %34, i32 514)
store i32 %35, i32* %6
store i32 0, i32* %7
br label %36
36: ; preds = %43, %25
%37 = load i32, i32* %7
%38 = icmp slt i32 %37, 20
br i1 %38, label %39, label %46
39: ; preds = %36
%40 = load i32, i32* %6
%41 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%42 = call i32 @write(i32 %40, i8* %41, i32 512)
br label %43
43: ; preds = %39
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %7
br label %36
46: ; preds = %36
%47 = load i32, i32* %6
%48 = call i32 @close(i32 %47)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%50 = getelementptr inbounds [10 x i8], [10 x i8]* %8, i64 0, i64 0
%51 = call i32 @open(i8* %50, i32 0)
store i32 %51, i32* %6
store i32 0, i32* %7
br label %52
52: ; preds = %59, %46
%53 = load i32, i32* %7
%54 = icmp slt i32 %53, 20
br i1 %54, label %55, label %62
55: ; preds = %52
%56 = load i32, i32* %6
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %9, i64 0, i64 0
%58 = call i32 @read(i32 %56, i8* %57, i32 512)
br label %59
59: ; preds = %55
%60 = load i32, i32* %7
%61 = add nsw i32 %60, 1
store i32 %61, i32* %7
br label %52
62: ; preds = %52
%63 = load i32, i32* %6
%64 = call i32 @close(i32 %63)
%65 = call i32 (...) @wait()
%66 = call i32 @exit(i32 1)
unreachable
}

LLVM IR (-O2):
@__const.main.path = [10 x i8] c"stressfs0\00"
@.str.1 = [10 x i8] c"write %d\0A\00"
@str = [18 x i8] c"stressfs starting\00"
@str.3 = [5 x i8] c"read\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [10 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%8 = call i32 @memset(i8* %6, i8 signext 97, i32 512)
%9 = call i64 (...) @fork()
%10 = icmp sgt i64 %9, 0
br i1 %10, label %21, label %11
11: ; preds = %2
%12 = call i64 (...) @fork()
%13 = icmp sgt i64 %12, 0
br i1 %13, label %21, label %14
14: ; preds = %11
%15 = call i64 (...) @fork()
%16 = icmp sgt i64 %15, 0
br i1 %16, label %21, label %17
17: ; preds = %14
%18 = call i64 (...) @fork()
%19 = icmp sgt i64 %18, 0
%20 = select i1 %19, i32 3, i32 4
br label %21
21: ; preds = %17, %14, %11, %2
%22 = phi i32 [ 0, %2 ], [ 1, %11 ], [ 2, %14 ], [ %20, %17 ]
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = getelementptr inbounds [10 x i8], [10 x i8]* %3, i64 0, i64 8
%25 = trunc i32 %22 to i8
%26 = or i8 %25, 48
store i8 %26, i8* %24
%27 = call i32 @open(i8* %5, i32 514)
%28 = call i32 @write(i32 %27, i8* %6, i32 512)
%29 = call i32 @write(i32 %27, i8* %6, i32 512)
%30 = call i32 @write(i32 %27, i8* %6, i32 512)
%31 = call i32 @write(i32 %27, i8* %6, i32 512)
%32 = call i32 @write(i32 %27, i8* %6, i32 512)
%33 = call i32 @write(i32 %27, i8* %6, i32 512)
%34 = call i32 @write(i32 %27, i8* %6, i32 512)
%35 = call i32 @write(i32 %27, i8* %6, i32 512)
%36 = call i32 @write(i32 %27, i8* %6, i32 512)
%37 = call i32 @write(i32 %27, i8* %6, i32 512)
%38 = call i32 @write(i32 %27, i8* %6, i32 512)
%39 = call i32 @write(i32 %27, i8* %6, i32 512)
%40 = call i32 @write(i32 %27, i8* %6, i32 512)
%41 = call i32 @write(i32 %27, i8* %6, i32 512)
%42 = call i32 @write(i32 %27, i8* %6, i32 512)
%43 = call i32 @write(i32 %27, i8* %6, i32 512)
%44 = call i32 @write(i32 %27, i8* %6, i32 512)
%45 = call i32 @write(i32 %27, i8* %6, i32 512)
%46 = call i32 @write(i32 %27, i8* %6, i32 512)
%47 = call i32 @write(i32 %27, i8* %6, i32 512)
%48 = call i32 @close(i32 %27)
%49 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str.3, i64 0, i64 0))
%50 = call i32 @open(i8* %5, i32 0)
%51 = call i32 @read(i32 %50, i8* %6, i32 512)
%52 = call i32 @read(i32 %50, i8* %6, i32 512)
%53 = call i32 @read(i32 %50, i8* %6, i32 512)
%54 = call i32 @read(i32 %50, i8* %6, i32 512)
%55 = call i32 @read(i32 %50, i8* %6, i32 512)
%56 = call i32 @read(i32 %50, i8* %6, i32 512)
%57 = call i32 @read(i32 %50, i8* %6, i32 512)
%58 = call i32 @read(i32 %50, i8* %6, i32 512)
%59 = call i32 @read(i32 %50, i8* %6, i32 512)
%60 = call i32 @read(i32 %50, i8* %6, i32 512)
%61 = call i32 @read(i32 %50, i8* %6, i32 512)
%62 = call i32 @read(i32 %50, i8* %6, i32 512)
%63 = call i32 @read(i32 %50, i8* %6, i32 512)
%64 = call i32 @read(i32 %50, i8* %6, i32 512)
%65 = call i32 @read(i32 %50, i8* %6, i32 512)
%66 = call i32 @read(i32 %50, i8* %6, i32 512)
%67 = call i32 @read(i32 %50, i8* %6, i32 512)
%68 = call i32 @read(i32 %50, i8* %6, i32 512)
%69 = call i32 @read(i32 %50, i8* %6, i32 512)
%70 = call i32 @read(i32 %50, i8* %6, i32 512)
%71 = call i32 @close(i32 %50)
%72 = call i32 (...) @wait()
%73 = call i32 @exit(i32 1)
unreachable
}