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

/* Variables and functions */
int O_CREAT;
int O_RDONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ sleep(int);
main() {
  int fd, i, j, oflag;
  char buf[1000];
  oflag = (O_CREAT | O_RDONLY);
  fd = open("tempfile", oflag, 0x1c0);
  if (fd < 0) {
    printf("Error opening\n");
    exit(1);
  }
  j = 0;
  while ((i = read(fd, buf, 25)) == 0 && j < 10) {
    sleep(5);
    printf("reader: waiting for file contents\n");
    j++;
  };
  printf("reader: # bytes read %d which were: %s\n", i, buf);
  j = 0;
  while ((i = read(fd, buf, 25)) == 0 && j < 10) {
    sleep(5);
    printf("reader: waiting for file contents\n");
    j++;
  };
  printf("reader: # bytes read %d which were: %s\n", i, buf);
  printf("reader: done\n");
  close(fd);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e7f(%rip),%rax
mov    (%rax),%eax
mov    0x2e5e(%rip),%rcx
or     (%rcx),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe6f(%rip),%rdi
mov    $0x1c0,%edx
call   1060 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c0 <main+0x60>
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x400(%rbp),%rsi
mov    $0x19,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x401(%rbp)
jne    11fe <main+0x9e>
cmpl   $0xa,-0x10(%rbp)
setl   %al
mov    %al,-0x401(%rbp)
mov    -0x401(%rbp),%al
test   $0x1,%al
jne    1211 <main+0xb1>
jmp    1237 <main+0xd7>
mov    $0x5,%edi
call   1080 <sleep@plt>
lea    0xdf6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    11c7 <main+0x67>
mov    -0xc(%rbp),%esi
lea    -0x400(%rbp),%rdx
lea    0xdf3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x400(%rbp),%rsi
mov    $0x19,%edx
call   1050 <read@plt>
mov    %eax,%ecx
mov    %ecx,-0xc(%rbp)
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x402(%rbp)
jne    128d <main+0x12d>
cmpl   $0xa,-0x10(%rbp)
setl   %al
mov    %al,-0x402(%rbp)
mov    -0x402(%rbp),%al
test   $0x1,%al
jne    12a0 <main+0x140>
jmp    12c6 <main+0x166>
mov    $0x5,%edi
call   1080 <sleep@plt>
lea    0xd67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1256 <main+0xf6>
mov    -0xc(%rbp),%esi
lea    -0x400(%rbp),%rdx
lea    0xd64(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x8(%rbp),%edi
call   1040 <close@plt>
mov    -0x4(%rbp),%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x3e8,%rsp
mov    0x2e74(%rip),%rax
mov    0x2e55(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6a(%rip),%rdi
mov    $0x1c0,%edx
call   1070 <open@plt>
test   %eax,%eax
js     12a5 <main+0x135>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x19,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11ff <main+0x8f>
xor    %ebx,%ebx
lea    0xe78(%rip),%r14
mov    %rsp,%r15
nopl   0x0(%rax)
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x19,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    11ff <main+0x8f>
lea    0x1(%rbx),%ecx
cmp    $0x9,%ebx
mov    %ecx,%ebx
jb     11d0 <main+0x60>
lea    0xe03(%rip),%rdi
xor    %ebx,%ebx
mov    %rsp,%r14
mov    %eax,%esi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <printf@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x19,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    126f <main+0xff>
lea    0xe0d(%rip),%r14
mov    %rsp,%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    $0x5,%edi
call   1090 <sleep@plt>
mov    %r14,%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0x19,%edx
call   1060 <read@plt>
test   %eax,%eax
jne    126f <main+0xff>
lea    0x1(%rbx),%ecx
cmp    $0x9,%ebx
mov    %ecx,%ebx
jb     1240 <main+0xd0>
lea    0xd93(%rip),%rdi
mov    %rsp,%rdx
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xda8(%rip),%rdi
call   1030 <puts@plt>
mov    %ebp,%edi
call   1050 <close@plt>
xor    %eax,%eax
add    $0x3e8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb4(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str = [9 x i8] c"tempfile\00"
@.str.1 = [15 x i8] c"Error opening\0A\00"
@.str.2 = [35 x i8] c"reader: waiting for file contents\0A\00"
@.str.3 = [40 x i8] c"reader: # bytes read %d which were: %s\0A\00"
@.str.4 = [14 x i8] c"reader: done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca [1000 x i8]
store i32 0, i32* %1
%7 = load i32, i32* @O_CREAT
%8 = load i32, i32* @O_RDONLY
%9 = or i32 %7, %8
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10, i32 448)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %0
store i32 0, i32* %4
br label %18
18: ; preds = %28, %17
%19 = load i32, i32* %2
%20 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%21 = call i32 @read(i32 %19, i8* %20, i32 25)
store i32 %21, i32* %3
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %18
%24 = load i32, i32* %4
%25 = icmp slt i32 %24, 10
br label %26
26: ; preds = %23, %18
%27 = phi i1 [ false, %18 ], [ %25, %23 ]
br i1 %27, label %28, label %33
28: ; preds = %26
%29 = call i32 @sleep(i32 5)
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %4
%32 = add nsw i32 %31, 1
store i32 %32, i32* %4
br label %18
33: ; preds = %26
%34 = load i32, i32* %3
%35 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %34, i8* %35)
store i32 0, i32* %4
br label %37
37: ; preds = %47, %33
%38 = load i32, i32* %2
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 25)
store i32 %40, i32* %3
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i32, i32* %4
%44 = icmp slt i32 %43, 10
br label %45
45: ; preds = %42, %37
%46 = phi i1 [ false, %37 ], [ %44, %42 ]
br i1 %46, label %47, label %52
47: ; preds = %45
%48 = call i32 @sleep(i32 5)
%49 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.2, i64 0, i64 0))
%50 = load i32, i32* %4
%51 = add nsw i32 %50, 1
store i32 %51, i32* %4
br label %37
52: ; preds = %45
%53 = load i32, i32* %3
%54 = getelementptr inbounds [1000 x i8], [1000 x i8]* %6, i64 0, i64 0
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %53, i8* %54)
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%57 = load i32, i32* %2
%58 = call i32 @close(i32 %57)
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str = [9 x i8] c"tempfile\00"
@.str.3 = [40 x i8] c"reader: # bytes read %d which were: %s\0A\00"
@str = [13 x i8] c"reader: done\00"
@str.6 = [34 x i8] c"reader: waiting for file contents\00"
@str.7 = [14 x i8] c"Error opening\00"
define i32 @main() {
%1 = alloca [1000 x i8]
%2 = getelementptr inbounds [1000 x i8], [1000 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_CREAT
%4 = load i32, i32* @O_RDONLY
%5 = or i32 %4, %3
%6 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %5, i32 448)
%7 = icmp slt i32 %6, 0
br i1 %7, label %11, label %8
8: ; preds = %0
%9 = call i32 @read(i32 %6, i8* %2, i32 25)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %23
11: ; preds = %0
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.7, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %8, %14
%15 = phi i32 [ %18, %14 ], [ 0, %8 ]
%16 = tail call i32 @sleep(i32 5)
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0))
%18 = add nuw nsw i32 %15, 1
%19 = call i32 @read(i32 %6, i8* %2, i32 25)
%20 = icmp eq i32 %19, 0
%21 = icmp ult i32 %15, 9
%22 = select i1 %20, i1 %21, i1 false
br i1 %22, label %14, label %23
23: ; preds = %14, %8
%24 = phi i32 [ %9, %8 ], [ %19, %14 ]
%25 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %24, i8* %2)
%26 = call i32 @read(i32 %6, i8* %2, i32 25)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %37
28: ; preds = %23, %28
%29 = phi i32 [ %32, %28 ], [ 0, %23 ]
%30 = call i32 @sleep(i32 5)
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @str.6, i64 0, i64 0))
%32 = add nuw nsw i32 %29, 1
%33 = call i32 @read(i32 %6, i8* %2, i32 25)
%34 = icmp eq i32 %33, 0
%35 = icmp ult i32 %29, 9
%36 = select i1 %34, i1 %35, i1 false
br i1 %36, label %28, label %37
37: ; preds = %28, %23
%38 = phi i32 [ %26, %23 ], [ %33, %28 ]
%39 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.3, i64 0, i64 0), i32 %38, i8* %2)
%40 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
%41 = call i32 @close(i32 %6)
ret i32 0
}