#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_RDWR;
int O_TRUNC;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ doTest(int, int);
int /*<<< orphan*/ ftruncate(int, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int main(int argc, char *argv[]) {
  int fd = open(".testfile", O_RDWR | O_CREAT | O_TRUNC, 0600);
  if (fd == -1) {
    perror("open .testfile");
    return 1;
  };
  ftruncate(fd, 0x200000);
  printf("Testing with 512 byte blocks...\n");
  doTest(fd, 512);
  printf("testing with 512 byte blocks again...\n");
  doTest(fd, 512);
  printf("Testing with 4KB blocks...\n");
  doTest(fd, 0x1000);
  printf("testing with 2MB blocks...\n");
  doTest(fd, 0x200000);
  printf("Tests complete.\n");
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e72(%rip),%rax
or     (%rax),%esi
mov    0x2e61(%rip),%rax
or     (%rax),%esi
lea    0xe6d(%rip),%rdi
mov    $0x180,%edx
call   1070 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    11c7 <main+0x67>
lea    0xe4a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1253 <main+0xf3>
mov    -0x14(%rbp),%edi
mov    $0x200000,%esi
call   1050 <ftruncate@plt>
lea    0xe34(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x200,%esi
call   1040 <doTest@plt>
lea    0xe3c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x200,%esi
call   1040 <doTest@plt>
lea    0xe4a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x1000,%esi
call   1040 <doTest@plt>
lea    0xe4d(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
mov    $0x200000,%esi
call   1040 <doTest@plt>
lea    0xe50(%rip),%rdi
call   1030 <printf@plt>
mov    -0x14(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
mov    0x2e70(%rip),%rax
mov    0x2e89(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2e76(%rip),%rax
or     (%rax),%esi
lea    0xe82(%rip),%rdi
mov    $0x180,%edx
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     121b <main+0xbb>
mov    %eax,%ebx
mov    %eax,%edi
mov    $0x200000,%esi
call   1050 <ftruncate@plt>
lea    0xe64(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x200,%esi
call   1040 <doTest@plt>
lea    0xe6c(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x200,%esi
call   1040 <doTest@plt>
lea    0xe7a(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x1000,%esi
call   1040 <doTest@plt>
lea    0xe7d(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
mov    $0x200000,%esi
call   1040 <doTest@plt>
lea    0xe80(%rip),%rdi
call   1030 <puts@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
pop    %rbx
ret
lea    0xdde(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [10 x i8] c".testfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [15 x i8] c"open .testfile\00"
@.str.2 = [33 x i8] c"Testing with 512 byte blocks...\0A\00"
@.str.3 = [39 x i8] c"testing with 512 byte blocks again...\0A\00"
@.str.4 = [28 x i8] c"Testing with 4KB blocks...\0A\00"
@.str.5 = [28 x i8] c"testing with 2MB blocks...\0A\00"
@.str.6 = [17 x i8] c"Tests complete.\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = load i32, i32* @O_RDWR
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %11, i32 384)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %35
17: ; preds = %2
%18 = load i32, i32* %6
%19 = call i32 @ftruncate(i32 %18, i32 2097152)
%20 = call i32 @printf(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0))
%21 = load i32, i32* %6
%22 = call i32 @doTest(i32 %21, i32 512)
%23 = call i32 @printf(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.3, i64 0, i64 0))
%24 = load i32, i32* %6
%25 = call i32 @doTest(i32 %24, i32 512)
%26 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.4, i64 0, i64 0))
%27 = load i32, i32* %6
%28 = call i32 @doTest(i32 %27, i32 4096)
%29 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%30 = load i32, i32* %6
%31 = call i32 @doTest(i32 %30, i32 2097152)
%32 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.6, i64 0, i64 0))
%33 = load i32, i32* %6
%34 = call i32 @close(i32 %33)
store i32 0, i32* %3
br label %35
35: ; preds = %17, %15
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
@.str = [10 x i8] c".testfile\00"
@O_RDWR = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [15 x i8] c"open .testfile\00"
@str = [32 x i8] c"Testing with 512 byte blocks...\00"
@str.7 = [38 x i8] c"testing with 512 byte blocks again...\00"
@str.8 = [27 x i8] c"Testing with 4KB blocks...\00"
@str.9 = [27 x i8] c"testing with 2MB blocks...\00"
@str.10 = [16 x i8] c"Tests complete.\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @O_RDWR
%4 = load i32, i32* @O_CREAT
%5 = or i32 %4, %3
%6 = load i32, i32* @O_TRUNC
%7 = or i32 %5, %6
%8 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %7, i32 384)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
br label %24
12: ; preds = %2
%13 = tail call i32 @ftruncate(i32 %8, i32 2097152)
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @str, i64 0, i64 0))
%15 = tail call i32 @doTest(i32 %8, i32 512)
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([38 x i8], [38 x i8]* @str.7, i64 0, i64 0))
%17 = tail call i32 @doTest(i32 %8, i32 512)
%18 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.8, i64 0, i64 0))
%19 = tail call i32 @doTest(i32 %8, i32 4096)
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str.9, i64 0, i64 0))
%21 = tail call i32 @doTest(i32 %8, i32 2097152)
%22 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.10, i64 0, i64 0))
%23 = tail call i32 @close(i32 %8)
br label %24
24: ; preds = %12, %10
%25 = phi i32 [ 1, %10 ], [ 0, %12 ]
ret i32 %25
}