#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 ssize_t;

/* Variables and functions */
scalar_t__ EEXIST;
scalar_t__ ENOENT;
int O_CREAT;
int O_EXCL;
int /*<<< orphan*/ O_RDONLY;
int O_RDWR;
int /*<<< orphan*/ SEEK_SET;
int S_IRGRP;
int S_IROTH;
int S_IRUSR;
int S_IRWXG;
int S_IRWXU;
int S_IWGRP;
int S_IWUSR;
int S_IXOTH;
int close(int);
scalar_t__ errno;
int lseek(int, int, int /*<<< orphan*/);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int mkdir(char *, int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int rw_file(int, char *, int, int);
int unlink(char *);
int main(int argc, const char **argv) {
  char buf[4096];
  ssize_t bytes;
  int fd = 0;
  int rv = 0;
  memset(buf, 0, 4096);
  rv = mkdir("tmp", S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
  if (rv < 0 && errno != EEXIST) {
    perror("mkdir failed");
    return 1;
  }
  rv = unlink("tmp"
              "/"
              "__testfile__");
  if (rv < 0 && errno != ENOENT) {
    perror("unlink failed");
    return 1;
  }
  fd = open("tmp"
            "/"
            "__testfile__",
            O_CREAT | O_RDWR | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
  if (fd < 0) {
    perror("open failed");
    return 1;
  }
  bytes = rw_file(fd, buf, 4096, true);
  if (bytes != 4096) {
    perror("writing "
           "4096"
           " bytes to test file failed");
    return 1;
  }
  rv = lseek(fd, 0, SEEK_SET);
  if (rv < 0) {
    perror("lseek to beginning of file failed");
    return 1;
  }
  bytes = rw_file(fd, buf, 1, true);
  if (bytes != 1) {
    perror("writing one byte to test file failed");
    return 1;
  }
  rv = lseek(fd, 8192, SEEK_SET);
  if (rv < 0) {
    perror("lseek to "
           "8192"
           " offset failed");
    return 1;
  }
  bytes = rw_file(fd, buf, 4096, true);
  if (bytes != 4096) {
    perror("writing "
           "4096"
           " bytes to test file failed");
    return 1;
  }
  rv = close(fd);
  if (rv < 0) {
    perror("close failed");
    return 1;
  }
  fd = open("tmp"
            "/"
            "__testfile__",
            O_RDONLY);
  if (fd < 0) {
    perror("open failed");
    return 1;
  }
  rv = lseek(fd, 8192, SEEK_SET);
  if (rv < 0) {
    perror("lseek to "
           "8192"
           " offset failed");
    return 1;
  }
  bytes = rw_file(fd, buf, 4096, false);
  if (bytes != 4096) {
    perror("reading "
           "4096"
           " bytes from test file failed");
    return 1;
  }
  rv = close(fd);
  if (rv < 0) {
    perror("close failed");
    return 1;
  }
  rv = unlink("tmp"
              "/"
              "__testfile__");
  if (rv < 0) {
    perror("unlink failed");
    return 1;
  }
  printf("test completed successfully\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x1018(%rbp)
movl   $0x0,-0x101c(%rbp)
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    0x2e09(%rip),%rax
mov    (%rax),%esi
mov    0x2de8(%rip),%rax
or     (%rax),%esi
mov    0x2d8f(%rip),%rax
or     (%rax),%esi
mov    0x2dde(%rip),%rax
or     (%rax),%esi
lea    0xe05(%rip),%rdi
call   1040 <mkdir@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    1245 <main+0xb5>
mov    0x2d76(%rip),%rax
mov    (%rax),%rax
mov    0x2d7c(%rip),%rcx
cmp    (%rcx),%rax
je     1245 <main+0xb5>
lea    0xdd0(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
lea    0xdc5(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    1296 <main+0x106>
mov    0x2d25(%rip),%rax
mov    (%rax),%rax
mov    0x2ceb(%rip),%rcx
cmp    (%rcx),%rax
je     1296 <main+0x106>
lea    0xd9d(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    0x2d5b(%rip),%rax
mov    (%rax),%esi
mov    0x2d32(%rip),%rax
or     (%rax),%esi
mov    0x2ce9(%rip),%rax
or     (%rax),%esi
mov    0x2cb8(%rip),%rax
mov    (%rax),%edx
mov    0x2cef(%rip),%rax
or     (%rax),%edx
mov    0x2cb6(%rip),%rax
or     (%rax),%edx
mov    0x2ce5(%rip),%rax
or     (%rax),%edx
lea    0xd35(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    130e <main+0x17e>
lea    0xd33(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    -0x1018(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
mov    $0x1,%ecx
call   1060 <rw_file@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x1000,-0x1014(%rbp)
je     1358 <main+0x1c8>
lea    0xcf5(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    -0x1018(%rbp),%edi
mov    0x2c23(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1070 <lseek@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    1399 <main+0x209>
lea    0xcdb(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    -0x1018(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1,%ecx
mov    %ecx,%edx
call   1060 <rw_file@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x1,-0x1014(%rbp)
je     13dd <main+0x24d>
lea    0xcb9(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    -0x1018(%rbp),%edi
mov    0x2b9e(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
call   1070 <lseek@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    1421 <main+0x291>
lea    0xc9a(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    -0x1018(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
mov    $0x1,%ecx
call   1060 <rw_file@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x1000,-0x1014(%rbp)
je     146b <main+0x2db>
lea    0xbe2(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    -0x1018(%rbp),%edi
call   1090 <close@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    14a1 <main+0x311>
lea    0xc36(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    0x2b18(%rip),%rax
mov    (%rax),%esi
lea    0xb60(%rip),%rdi
mov    $0x0,%al
call   10a0 <open@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    14e3 <main+0x353>
lea    0xb5e(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    -0x1018(%rbp),%edi
mov    0x2a98(%rip),%rax
mov    (%rax),%edx
mov    $0x2000,%esi
call   1070 <lseek@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    1527 <main+0x397>
lea    0xb94(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    -0x1018(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1060 <rw_file@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x1000,-0x1014(%rbp)
je     156e <main+0x3de>
lea    0xb76(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
mov    -0x1018(%rbp),%edi
call   1090 <close@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    15a4 <main+0x414>
lea    0xb33(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
lea    0xa66(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x0,-0x101c(%rbp)
jge    15db <main+0x44b>
lea    0xa58(%rip),%rdi
call   10b0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    15ee <main+0x45e>
lea    0xb1a(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x1000,%rsp
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1080 <memset@plt>
mov    0x2e2f(%rip),%rax
mov    0x2e10(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2db5(%rip),%rax
or     (%rax),%esi
mov    0x2e04(%rip),%rax
or     (%rax),%esi
lea    0xe2b(%rip),%rdi
call   1040 <mkdir@plt>
test   %eax,%eax
jns    11f8 <main+0x68>
mov    0x2dab(%rip),%rax
mov    (%rax),%rax
mov    0x2db1(%rip),%rcx
cmp    (%rcx),%rax
jne    142b <main+0x29b>
lea    0xe12(%rip),%rdi
call   1030 <unlink@plt>
test   %eax,%eax
jns    1222 <main+0x92>
mov    0x2d81(%rip),%rax
mov    (%rax),%rax
mov    0x2d47(%rip),%rcx
cmp    (%rcx),%rax
jne    1434 <main+0x2a4>
mov    0x2dcf(%rip),%rax
mov    0x2da8(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d5d(%rip),%rax
or     (%rax),%esi
mov    0x2d2c(%rip),%rax
mov    0x2d65(%rip),%rcx
mov    (%rcx),%edx
or     (%rax),%edx
mov    0x2d2a(%rip),%rax
or     (%rax),%edx
mov    0x2d59(%rip),%rax
or     (%rax),%edx
lea    0xda9(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
js     13d9 <main+0x249>
mov    %eax,%ebp
mov    %rsp,%rsi
mov    $0x1,%ebx
mov    %eax,%edi
mov    $0x1000,%edx
mov    $0x1,%ecx
call   1060 <rw_file@plt>
cmp    $0x1000,%eax
jne    13af <main+0x21f>
mov    0x2ce4(%rip),%r14
mov    (%r14),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1070 <lseek@plt>
test   %eax,%eax
js     13e2 <main+0x252>
mov    %rsp,%rsi
mov    $0x1,%ebx
mov    %ebp,%edi
mov    $0x1,%edx
mov    $0x1,%ecx
call   1060 <rw_file@plt>
cmp    $0x1,%eax
jne    13f0 <main+0x260>
mov    (%r14),%edx
mov    %ebp,%edi
mov    $0x2000,%esi
call   1070 <lseek@plt>
test   %eax,%eax
js     13bd <main+0x22d>
mov    %rsp,%rsi
mov    $0x1,%ebx
mov    %ebp,%edi
mov    $0x1000,%edx
mov    $0x1,%ecx
call   1060 <rw_file@plt>
cmp    $0x1000,%eax
jne    13af <main+0x21f>
mov    %ebp,%edi
call   1090 <close@plt>
test   %eax,%eax
js     13cb <main+0x23b>
mov    0x2c95(%rip),%rax
mov    (%rax),%esi
lea    0xcdd(%rip),%rdi
xor    %eax,%eax
call   10a0 <open@plt>
test   %eax,%eax
js     13fe <main+0x26e>
mov    %eax,%ebp
mov    (%r14),%edx
mov    %eax,%edi
mov    $0x2000,%esi
call   1070 <lseek@plt>
test   %eax,%eax
js     13bd <main+0x22d>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1000,%edx
xor    %ecx,%ecx
call   1060 <rw_file@plt>
cmp    $0x1000,%eax
jne    140c <main+0x27c>
mov    %ebp,%edi
call   1090 <close@plt>
test   %eax,%eax
js     13cb <main+0x23b>
lea    0xc8b(%rip),%rdi
call   1030 <unlink@plt>
test   %eax,%eax
js     141a <main+0x28a>
lea    0xd62(%rip),%rdi
call   1050 <puts@plt>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x1000,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xc86(%rip),%rdi
call   10b0 <perror@plt>
jmp    13a1 <main+0x211>
lea    0xce6(%rip),%rdi
call   10b0 <perror@plt>
jmp    13a1 <main+0x211>
lea    0xcf4(%rip),%rdi
call   10b0 <perror@plt>
jmp    13a1 <main+0x211>
lea    0xc50(%rip),%rdi
jmp    143b <main+0x2ab>
lea    0xc7a(%rip),%rdi
call   10b0 <perror@plt>
jmp    13a1 <main+0x211>
lea    0xc8e(%rip),%rdi
call   10b0 <perror@plt>
jmp    13a1 <main+0x211>
lea    0xc2b(%rip),%rdi
call   10b0 <perror@plt>
jmp    13a1 <main+0x211>
lea    0xcc0(%rip),%rdi
call   10b0 <perror@plt>
jmp    13a1 <main+0x211>
lea    0xc01(%rip),%rdi
call   10b0 <perror@plt>
jmp    13a1 <main+0x211>
lea    0xbd2(%rip),%rdi
jmp    143b <main+0x2ab>
lea    0xbe7(%rip),%rdi
call   10b0 <perror@plt>
mov    $0x1,%ebx
jmp    13a1 <main+0x211>

LLVM IR (-O0):
@.str = [4 x i8] c"tmp\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [13 x i8] c"mkdir failed\00"
@.str.2 = [17 x i8] c"tmp/__testfile__\00"
@ENOENT = global i64 0
@.str.3 = [14 x i8] c"unlink failed\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str.4 = [12 x i8] c"open failed\00"
@.str.5 = [39 x i8] c"writing 4096 bytes to test file failed\00"
@SEEK_SET = global i32 0
@.str.6 = [34 x i8] c"lseek to beginning of file failed\00"
@.str.7 = [37 x i8] c"writing one byte to test file failed\00"
@.str.8 = [28 x i8] c"lseek to 8192 offset failed\00"
@.str.9 = [13 x i8] c"close failed\00"
@O_RDONLY = global i32 0
@.str.10 = [41 x i8] c"reading 4096 bytes from test file failed\00"
@.str.11 = [29 x i8] c"test completed successfully\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [4096 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %9
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%11 = call i32 @memset(i8* %10, i32 0, i32 4096)
%12 = load i32, i32* @S_IRWXU
%13 = load i32, i32* @S_IRWXG
%14 = or i32 %12, %13
%15 = load i32, i32* @S_IROTH
%16 = or i32 %14, %15
%17 = load i32, i32* @S_IXOTH
%18 = or i32 %16, %17
%19 = call i32 @mkdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %18)
store i32 %19, i32* %9
%20 = load i32, i32* %9
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %28
22: ; preds = %2
%23 = load i64, i64* @errno
%24 = load i64, i64* @EEXIST
%25 = icmp ne i64 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %141
28: ; preds = %22, %2
%29 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 %29, i32* %9
%30 = load i32, i32* %9
%31 = icmp slt i32 %30, 0
br i1 %31, label %32, label %38
32: ; preds = %28
%33 = load i64, i64* @errno
%34 = load i64, i64* @ENOENT
%35 = icmp ne i64 %33, %34
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %141
38: ; preds = %32, %28
%39 = load i32, i32* @O_CREAT
%40 = load i32, i32* @O_RDWR
%41 = or i32 %39, %40
%42 = load i32, i32* @O_EXCL
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IRUSR
%45 = load i32, i32* @S_IWUSR
%46 = or i32 %44, %45
%47 = load i32, i32* @S_IRGRP
%48 = or i32 %46, %47
%49 = load i32, i32* @S_IWGRP
%50 = or i32 %48, %49
%51 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %43, i32 %50)
store i32 %51, i32* %8
%52 = load i32, i32* %8
%53 = icmp slt i32 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %38
%55 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %141
56: ; preds = %38
%57 = load i32, i32* %8
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%59 = call i32 @rw_file(i32 %57, i8* %58, i32 4096, i32 1)
store i32 %59, i32* %7
%60 = load i32, i32* %7
%61 = icmp ne i32 %60, 4096
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %141
64: ; preds = %56
%65 = load i32, i32* %8
%66 = load i32, i32* @SEEK_SET
%67 = call i32 @lseek(i32 %65, i32 0, i32 %66)
store i32 %67, i32* %9
%68 = load i32, i32* %9
%69 = icmp slt i32 %68, 0
br i1 %69, label %70, label %72
70: ; preds = %64
%71 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
store i32 1, i32* %3
br label %141
72: ; preds = %64
%73 = load i32, i32* %8
%74 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%75 = call i32 @rw_file(i32 %73, i8* %74, i32 1, i32 1)
store i32 %75, i32* %7
%76 = load i32, i32* %7
%77 = icmp ne i32 %76, 1
br i1 %77, label %78, label %80
78: ; preds = %72
%79 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %141
80: ; preds = %72
%81 = load i32, i32* %8
%82 = load i32, i32* @SEEK_SET
%83 = call i32 @lseek(i32 %81, i32 8192, i32 %82)
store i32 %83, i32* %9
%84 = load i32, i32* %9
%85 = icmp slt i32 %84, 0
br i1 %85, label %86, label %88
86: ; preds = %80
%87 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %141
88: ; preds = %80
%89 = load i32, i32* %8
%90 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%91 = call i32 @rw_file(i32 %89, i8* %90, i32 4096, i32 1)
store i32 %91, i32* %7
%92 = load i32, i32* %7
%93 = icmp ne i32 %92, 4096
br i1 %93, label %94, label %96
94: ; preds = %88
%95 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %141
96: ; preds = %88
%97 = load i32, i32* %8
%98 = call i32 @close(i32 %97)
store i32 %98, i32* %9
%99 = load i32, i32* %9
%100 = icmp slt i32 %99, 0
br i1 %100, label %101, label %103
101: ; preds = %96
%102 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
br label %141
103: ; preds = %96
%104 = load i32, i32* @O_RDONLY
%105 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %104)
store i32 %105, i32* %8
%106 = load i32, i32* %8
%107 = icmp slt i32 %106, 0
br i1 %107, label %108, label %110
108: ; preds = %103
%109 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %141
110: ; preds = %103
%111 = load i32, i32* %8
%112 = load i32, i32* @SEEK_SET
%113 = call i32 @lseek(i32 %111, i32 8192, i32 %112)
store i32 %113, i32* %9
%114 = load i32, i32* %9
%115 = icmp slt i32 %114, 0
br i1 %115, label %116, label %118
116: ; preds = %110
%117 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %3
br label %141
118: ; preds = %110
%119 = load i32, i32* %8
%120 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%121 = call i32 @rw_file(i32 %119, i8* %120, i32 4096, i32 0)
store i32 %121, i32* %7
%122 = load i32, i32* %7
%123 = icmp ne i32 %122, 4096
br i1 %123, label %124, label %126
124: ; preds = %118
%125 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.10, i64 0, i64 0))
store i32 1, i32* %3
br label %141
126: ; preds = %118
%127 = load i32, i32* %8
%128 = call i32 @close(i32 %127)
store i32 %128, i32* %9
%129 = load i32, i32* %9
%130 = icmp slt i32 %129, 0
br i1 %130, label %131, label %133
131: ; preds = %126
%132 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
store i32 1, i32* %3
br label %141
133: ; preds = %126
%134 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 %134, i32* %9
%135 = load i32, i32* %9
%136 = icmp slt i32 %135, 0
br i1 %136, label %137, label %139
137: ; preds = %133
%138 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %141
139: ; preds = %133
%140 = call i32 @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.11, i64 0, i64 0))
store i32 0, i32* %3
br label %141
141: ; preds = %139, %137, %131, %124, %116, %108, %101, %94, %86, %78, %70, %62, %54, %36, %26
%142 = load i32, i32* %3
ret i32 %142
}

LLVM IR (-O2):
@.str = [4 x i8] c"tmp\00"
@S_IRWXU = global i32 0
@S_IRWXG = global i32 0
@S_IROTH = global i32 0
@S_IXOTH = global i32 0
@errno = global i64 0
@EEXIST = global i64 0
@.str.1 = [13 x i8] c"mkdir failed\00"
@.str.2 = [17 x i8] c"tmp/__testfile__\00"
@ENOENT = global i64 0
@.str.3 = [14 x i8] c"unlink failed\00"
@O_CREAT = global i32 0
@O_RDWR = global i32 0
@O_EXCL = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@S_IRGRP = global i32 0
@S_IWGRP = global i32 0
@.str.4 = [12 x i8] c"open failed\00"
@.str.5 = [39 x i8] c"writing 4096 bytes to test file failed\00"
@SEEK_SET = global i32 0
@.str.6 = [34 x i8] c"lseek to beginning of file failed\00"
@.str.7 = [37 x i8] c"writing one byte to test file failed\00"
@.str.8 = [28 x i8] c"lseek to 8192 offset failed\00"
@.str.9 = [13 x i8] c"close failed\00"
@O_RDONLY = global i32 0
@.str.10 = [41 x i8] c"reading 4096 bytes from test file failed\00"
@str = [28 x i8] c"test completed successfully\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%5 = call i32 @memset(i8* %4, i32 0, i32 4096)
%6 = load i32, i32* @S_IRWXU
%7 = load i32, i32* @S_IRWXG
%8 = or i32 %7, %6
%9 = load i32, i32* @S_IROTH
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IXOTH
%12 = or i32 %10, %11
%13 = call i32 @mkdir(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i64, i64* @errno
%17 = load i64, i64* @EEXIST
%18 = icmp eq i64 %16, %17
br i1 %18, label %21, label %19
19: ; preds = %15
%20 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
br label %108
21: ; preds = %15, %2
%22 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = load i64, i64* @errno
%26 = load i64, i64* @ENOENT
%27 = icmp eq i64 %25, %26
br i1 %27, label %30, label %28
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %108
30: ; preds = %24, %21
%31 = load i32, i32* @O_CREAT
%32 = load i32, i32* @O_RDWR
%33 = or i32 %32, %31
%34 = load i32, i32* @O_EXCL
%35 = or i32 %33, %34
%36 = load i32, i32* @S_IRUSR
%37 = load i32, i32* @S_IWUSR
%38 = or i32 %37, %36
%39 = load i32, i32* @S_IRGRP
%40 = or i32 %38, %39
%41 = load i32, i32* @S_IWGRP
%42 = or i32 %40, %41
%43 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %35, i32 %42)
%44 = icmp slt i32 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %30
%46 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %108
47: ; preds = %30
%48 = call i32 @rw_file(i32 %43, i8* %4, i32 4096, i32 1)
%49 = icmp eq i32 %48, 4096
br i1 %49, label %52, label %50
50: ; preds = %47
%51 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %108
52: ; preds = %47
%53 = load i32, i32* @SEEK_SET
%54 = call i32 @lseek(i32 %43, i32 0, i32 %53)
%55 = icmp slt i32 %54, 0
br i1 %55, label %56, label %58
56: ; preds = %52
%57 = call i32 @perror(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.6, i64 0, i64 0))
br label %108
58: ; preds = %52
%59 = call i32 @rw_file(i32 %43, i8* %4, i32 1, i32 1)
%60 = icmp eq i32 %59, 1
br i1 %60, label %63, label %61
61: ; preds = %58
%62 = call i32 @perror(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.7, i64 0, i64 0))
br label %108
63: ; preds = %58
%64 = load i32, i32* @SEEK_SET
%65 = call i32 @lseek(i32 %43, i32 8192, i32 %64)
%66 = icmp slt i32 %65, 0
br i1 %66, label %67, label %69
67: ; preds = %63
%68 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %108
69: ; preds = %63
%70 = call i32 @rw_file(i32 %43, i8* %4, i32 4096, i32 1)
%71 = icmp eq i32 %70, 4096
br i1 %71, label %74, label %72
72: ; preds = %69
%73 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %108
74: ; preds = %69
%75 = call i32 @close(i32 %43)
%76 = icmp slt i32 %75, 0
br i1 %76, label %77, label %79
77: ; preds = %74
%78 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
br label %108
79: ; preds = %74
%80 = load i32, i32* @O_RDONLY
%81 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0), i32 %80)
%82 = icmp slt i32 %81, 0
br i1 %82, label %83, label %85
83: ; preds = %79
%84 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.4, i64 0, i64 0))
br label %108
85: ; preds = %79
%86 = load i32, i32* @SEEK_SET
%87 = call i32 @lseek(i32 %81, i32 8192, i32 %86)
%88 = icmp slt i32 %87, 0
br i1 %88, label %89, label %91
89: ; preds = %85
%90 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.8, i64 0, i64 0))
br label %108
91: ; preds = %85
%92 = call i32 @rw_file(i32 %81, i8* %4, i32 4096, i32 0)
%93 = icmp eq i32 %92, 4096
br i1 %93, label %96, label %94
94: ; preds = %91
%95 = call i32 @perror(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.10, i64 0, i64 0))
br label %108
96: ; preds = %91
%97 = call i32 @close(i32 %81)
%98 = icmp slt i32 %97, 0
br i1 %98, label %99, label %101
99: ; preds = %96
%100 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
br label %108
101: ; preds = %96
%102 = call i32 @unlink(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%103 = icmp slt i32 %102, 0
br i1 %103, label %104, label %106
104: ; preds = %101
%105 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %108
106: ; preds = %101
%107 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str, i64 0, i64 0))
br label %108
108: ; preds = %106, %104, %99, %94, %89, %83, %77, %72, %67, %61, %56, %50, %45, %28, %19
%109 = phi i32 [ 1, %19 ], [ 1, %28 ], [ 1, %45 ], [ 1, %50 ], [ 1, %56 ], [ 1, %61 ], [ 1, %67 ], [ 1, %72 ], [ 1, %77 ], [ 1, %83 ], [ 1, %89 ], [ 1, %94 ], [ 1, %99 ], [ 1, %104 ], [ 0, %106 ]
ret i32 %109
}