#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 stat {
  int st_size;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ err(int /*<<< orphan*/, char *, ...);
scalar_t__ fork();
int /*<<< orphan*/ fstat(int, struct stat *);
int pipe(int *);
int /*<<< orphan*/ printf(char *, int, int);
int read(int, char **, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char **, int);
int main(void) {
  char buffer[32768], buffer2[32768];
  int desc[2];
  int error, successes = 0;
  struct stat status;
  pid_t new_pid;
  error = pipe(desc);
  if (error)
    err(0, "Couldn't allocate fds\n");
  new_pid = fork();
  if (new_pid == 0) {
    write(desc[1], &buffer, 145);
    usleep(1000000);
    write(desc[1], &buffer, 2048);
    usleep(1000000);
    write(desc[1], &buffer, 4096);
    usleep(1000000);
    write(desc[1], &buffer, 8191);
    usleep(1000000);
    write(desc[1], &buffer, 8192);
    usleep(1000000);
  } else {
    while (successes < 5) {
      usleep(3000);
      fstat(desc[0], &status);
      error = read(desc[0], &buffer2, 32768);
      if (status.st_size != error)
        err(0, "FAILURE: stat size %d read size %d\n", (int)status.st_size,
            error);
      if (error > 0) {
        printf("SUCCESS at stat size %d read size %d\n", (int)status.st_size,
               error);
        successes++;
        usleep(1000000);
      }
    }
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10030,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x10020(%rbp)
lea    -0x10018(%rbp),%rdi
call   1050 <pipe@plt>
mov    %eax,-0x1001c(%rbp)
cmpl   $0x0,-0x1001c(%rbp)
je     11cb <main+0x4b>
xor    %edi,%edi
lea    0xe3c(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %rax,-0x10030(%rbp)
cmpq   $0x0,-0x10030(%rbp)
jne    1291 <main+0x111>
mov    -0x10014(%rbp),%edi
lea    -0x8010(%rbp),%rsi
mov    $0x91,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
mov    -0x10014(%rbp),%edi
lea    -0x8010(%rbp),%rsi
mov    $0x800,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
mov    -0x10014(%rbp),%edi
lea    -0x8010(%rbp),%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
mov    -0x10014(%rbp),%edi
lea    -0x8010(%rbp),%rsi
mov    $0x1fff,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
mov    -0x10014(%rbp),%edi
lea    -0x8010(%rbp),%rsi
mov    $0x2000,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
jmp    1352 <main+0x1d2>
jmp    1296 <main+0x116>
cmpl   $0x5,-0x10020(%rbp)
jge    134d <main+0x1cd>
mov    $0xbb8,%edi
call   10a0 <usleep@plt>
mov    -0x10018(%rbp),%edi
lea    -0x10028(%rbp),%rsi
call   1080 <fstat@plt>
mov    -0x10018(%rbp),%edi
lea    -0x10010(%rbp),%rsi
mov    $0x8000,%edx
call   1060 <read@plt>
mov    %eax,-0x1001c(%rbp)
mov    -0x10028(%rbp),%eax
cmp    -0x1001c(%rbp),%eax
je     130a <main+0x18a>
mov    -0x10028(%rbp),%edx
mov    -0x1001c(%rbp),%ecx
xor    %edi,%edi
lea    0xd14(%rip),%rsi
mov    $0x0,%al
call   1070 <err@plt>
cmpl   $0x0,-0x1001c(%rbp)
jle    1348 <main+0x1c8>
mov    -0x10028(%rbp),%esi
mov    -0x1001c(%rbp),%edx
lea    0xd11(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10020(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10020(%rbp)
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
jmp    1296 <main+0x116>
jmp    1352 <main+0x1d2>
mov    -0x4(%rbp),%eax
add    $0x10030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x8018,%rsp
mov    %rsp,%rdi
call   1050 <pipe@plt>
test   %eax,%eax
je     11ad <main+0x2d>
lea    0xe5c(%rip),%rsi
xor    %edi,%edi
xor    %eax,%eax
call   1070 <err@plt>
xor    %eax,%eax
call   1090 <fork@plt>
test   %rax,%rax
je     124d <main+0xcd>
lea    0x8(%rsp),%r14
lea    0x10(%rsp),%r13
lea    0xe6d(%rip),%r15
xor    %r12d,%r12d
jmp    11ea <main+0x6a>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
cmp    $0x5,%r12d
jge    12d9 <main+0x159>
mov    $0xbb8,%edi
call   10a0 <usleep@plt>
mov    (%rsp),%edi
mov    %r14,%rsi
call   1080 <fstat@plt>
mov    (%rsp),%edi
mov    %r13,%rsi
mov    $0x8000,%edx
call   1060 <read@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%ebx
cmp    %eax,%ebx
je     122d <main+0xad>
xor    %edi,%edi
lea    0xdf5(%rip),%rsi
mov    %ebx,%edx
mov    %ebp,%ecx
xor    %eax,%eax
call   1070 <err@plt>
test   %ebp,%ebp
jle    11e0 <main+0x60>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %ebp,%edx
call   1040 <printf@plt>
add    $0x1,%r12d
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
jmp    11e0 <main+0x60>
mov    0x4(%rsp),%edi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
mov    $0x91,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0x800,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1000,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0x1fff,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
mov    0x4(%rsp),%edi
mov    %rbx,%rsi
mov    $0x2000,%edx
call   1030 <write@plt>
mov    $0xf4240,%edi
call   10a0 <usleep@plt>
xor    %eax,%eax
add    $0x8018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [23 x i8] c"Couldn't allocate fds\0A\00"
@.str.1 = [36 x i8] c"FAILURE: stat size %d read size %d\0A\00"
@.str.2 = [38 x i8] c"SUCCESS at stat size %d read size %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [32768 x i8]
%3 = alloca [32768 x i8]
%4 = alloca [2 x i32]
%5 = alloca i32
%6 = alloca i32
%7 = alloca %struct.stat
%8 = alloca i64
store i32 0, i32* %1
store i32 0, i32* %6
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
store i32 %10, i32* %5
%11 = load i32, i32* %5
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %0
%14 = call i32 (i32, i8*, ...) @err(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %15
15: ; preds = %13, %0
%16 = call i64 (...) @fork()
store i64 %16, i64* %8
%17 = load i64, i64* %8
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %45
19: ; preds = %15
%20 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%21 = load i32, i32* %20
%22 = bitcast [32768 x i8]* %2 to i8**
%23 = call i32 @write(i32 %21, i8** %22, i32 145)
%24 = call i32 @usleep(i32 1000000)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%26 = load i32, i32* %25
%27 = bitcast [32768 x i8]* %2 to i8**
%28 = call i32 @write(i32 %26, i8** %27, i32 2048)
%29 = call i32 @usleep(i32 1000000)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%31 = load i32, i32* %30
%32 = bitcast [32768 x i8]* %2 to i8**
%33 = call i32 @write(i32 %31, i8** %32, i32 4096)
%34 = call i32 @usleep(i32 1000000)
%35 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%36 = load i32, i32* %35
%37 = bitcast [32768 x i8]* %2 to i8**
%38 = call i32 @write(i32 %36, i8** %37, i32 8191)
%39 = call i32 @usleep(i32 1000000)
%40 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 1
%41 = load i32, i32* %40
%42 = bitcast [32768 x i8]* %2 to i8**
%43 = call i32 @write(i32 %41, i8** %42, i32 8192)
%44 = call i32 @usleep(i32 1000000)
br label %80
45: ; preds = %15
br label %46
46: ; preds = %78, %45
%47 = load i32, i32* %6
%48 = icmp slt i32 %47, 5
br i1 %48, label %49, label %79
49: ; preds = %46
%50 = call i32 @usleep(i32 3000)
%51 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%52 = load i32, i32* %51
%53 = call i32 @fstat(i32 %52, %struct.stat* %7)
%54 = getelementptr inbounds [2 x i32], [2 x i32]* %4, i64 0, i64 0
%55 = load i32, i32* %54
%56 = bitcast [32768 x i8]* %3 to i8**
%57 = call i32 @read(i32 %55, i8** %56, i32 32768)
store i32 %57, i32* %5
%58 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%59 = load i32, i32* %58
%60 = load i32, i32* %5
%61 = icmp ne i32 %59, %60
br i1 %61, label %62, label %67
62: ; preds = %49
%63 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%64 = load i32, i32* %63
%65 = load i32, i32* %5
%66 = call i32 (i32, i8*, ...) @err(i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %64, i32 %65)
br label %67
67: ; preds = %62, %49
%68 = load i32, i32* %5
%69 = icmp sgt i32 %68, 0
br i1 %69, label %70, label %78
70: ; preds = %67
%71 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%72 = load i32, i32* %71
%73 = load i32, i32* %5
%74 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %72, i32 %73)
%75 = load i32, i32* %6
%76 = add nsw i32 %75, 1
store i32 %76, i32* %6
%77 = call i32 @usleep(i32 1000000)
br label %78
78: ; preds = %70, %67
br label %46
79: ; preds = %46
br label %80
80: ; preds = %79, %19
%81 = load i32, i32* %1
ret i32 %81
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [23 x i8] c"Couldn't allocate fds\0A\00"
@.str.1 = [36 x i8] c"FAILURE: stat size %d read size %d\0A\00"
@.str.2 = [38 x i8] c"SUCCESS at stat size %d read size %d\0A\00"
define i32 @main() {
%1 = alloca [32768 x i8]
%2 = alloca [32768 x i8]
%3 = alloca [2 x i32]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [32768 x i8], [32768 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [32768 x i8], [32768 x i8]* %2, i64 0, i64 0
%7 = bitcast [2 x i32]* %3 to i8*
%8 = bitcast %struct.stat* %4 to i8*
%9 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 0
%10 = call i32 @pipe(i32* %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %14, label %12
12: ; preds = %0
%13 = call i32 (i32, i8*, ...) @err(i32 0, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %14
14: ; preds = %12, %0
%15 = call i64 (...) @fork()
%16 = icmp eq i64 %15, 0
br i1 %16, label %20, label %17
17: ; preds = %14
%18 = bitcast [32768 x i8]* %2 to i8**
%19 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
br label %38
20: ; preds = %14
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %3, i64 0, i64 1
%22 = load i32, i32* %21
%23 = bitcast [32768 x i8]* %1 to i8**
%24 = call i32 @write(i32 %22, i8** %23, i32 145)
%25 = call i32 @usleep(i32 1000000)
%26 = load i32, i32* %21
%27 = call i32 @write(i32 %26, i8** %23, i32 2048)
%28 = call i32 @usleep(i32 1000000)
%29 = load i32, i32* %21
%30 = call i32 @write(i32 %29, i8** %23, i32 4096)
%31 = call i32 @usleep(i32 1000000)
%32 = load i32, i32* %21
%33 = call i32 @write(i32 %32, i8** %23, i32 8191)
%34 = call i32 @usleep(i32 1000000)
%35 = load i32, i32* %21
%36 = call i32 @write(i32 %35, i8** %23, i32 8192)
%37 = call i32 @usleep(i32 1000000)
br label %58
38: ; preds = %17, %55
%39 = phi i32 [ 0, %17 ], [ %56, %55 ]
%40 = call i32 @usleep(i32 3000)
%41 = load i32, i32* %9
%42 = call i32 @fstat(i32 %41, %struct.stat* %4)
%43 = load i32, i32* %9
%44 = call i32 @read(i32 %43, i8** %18, i32 32768)
%45 = load i32, i32* %19
%46 = icmp eq i32 %45, %44
br i1 %46, label %49, label %47
47: ; preds = %38
%48 = call i32 (i32, i8*, ...) @err(i32 0, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.1, i64 0, i64 0), i32 %45, i32 %44)
br label %49
49: ; preds = %47, %38
%50 = icmp sgt i32 %44, 0
br i1 %50, label %51, label %55
51: ; preds = %49
%52 = call i32 @printf(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i32 %45, i32 %44)
%53 = add nsw i32 %39, 1
%54 = call i32 @usleep(i32 1000000)
br label %55
55: ; preds = %51, %49
%56 = phi i32 [ %53, %51 ], [ %39, %49 ]
%57 = icmp slt i32 %56, 5
br i1 %57, label %38, label %58
58: ; preds = %55, %20
ret i32 0
}