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

/* Variables and functions */
scalar_t__ EEXIST;
int O_APPEND;
int O_CREAT;
int O_EXCL;
int O_TRUNC;
int O_WRONLY;
int S_IRUSR;
int S_IWUSR;
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ perror(char *);
int stat(char *, struct stat *);
int /*<<< orphan*/ stderr;
int unlink(char *);
int write(int, char *, int);
int main(void) {
  char buf[4096];
  int fd;
  int res;
  ssize_t wres;
  struct stat s;
  (void)unlink("./mnt/testopen");
  fd = open("./mnt/testopen", O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
  if (fd < 0) {
    perror("Unable to create testopen");
    return 1;
  }
  memset(buf, 0, 4096);
  wres = write(fd, buf, 4096);
  if (wres != 4096) {
    perror("Error writing");
    (void)close(fd);
    return 1;
  }
  (void)close(fd);
  res = stat("./mnt/testopen", &s);
  if (res < 0) {
    perror("Error stating testopen");
    return 1;
  }
  if (s.st_size == 4096) {
  } else {
    fprintf(stderr, "Size of file should be 4096, but is %jd\n", s.st_size);
    return 1;
  }
  fd = open("./mnt/testopen", O_CREAT | O_EXCL | O_WRONLY, S_IRUSR | S_IWUSR);
  if (fd < 0 && errno == EEXIST) {
  } else {
    fprintf(stderr, "Second create succedded and should not");
    return 1;
  }
  fd = open("./mnt/testopen", O_CREAT | O_WRONLY | O_APPEND, S_IRUSR | S_IWUSR);
  res = stat("./mnt/testopen", &s);
  if (res < 0) {
    perror("Error stating testopen");
    return 1;
  }
  if (s.st_size == 4096) {
  } else {
    fprintf(stderr, "Size of file should be 4096, but is %jd\n", s.st_size);
    return 1;
  }
  wres = write(fd, buf, 4096);
  if (wres != 4096) {
    perror("Error writing");
    (void)close(fd);
    return 1;
  }
  (void)close(fd);
  res = stat("./mnt/testopen", &s);
  if (res < 0) {
    perror("Error stating testopen");
    return 1;
  }
  if (s.st_size == 8192) {
  } else {
    fprintf(stderr, "Size of file should be 8192, but is %jd\n", s.st_size);
    return 1;
  }
  fd = open("./mnt/testopen", O_CREAT | O_WRONLY | O_TRUNC, S_IRUSR | S_IWUSR);
  (void)close(fd);
  res = stat("./mnt/testopen", &s);
  if (res < 0) {
    perror("Error stating testopen");
    return 1;
  }
  if (s.st_size == 0) {
  } else {
    fprintf(stderr, "Size of file should be 0, but is %jd\n", s.st_size);
    return 1;
  }
  res = unlink("./mnt/testopen");
  if (res < 0) {
    perror("Error removing testopen");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1020,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe67(%rip),%rdi
call   1030 <unlink@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%esi
mov    0x2e0a(%rip),%rax
or     (%rax),%esi
mov    0x2de1(%rip),%rax
or     (%rax),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
mov    0x2e0f(%rip),%rax
or     (%rax),%edx
lea    0xe2e(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    1202 <main+0x82>
lea    0xe1e(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
lea    -0x1010(%rbp),%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x1000,-0x101c(%rbp)
je     1265 <main+0xe5>
lea    0xde0(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
lea    0xd89(%rip),%rdi
lea    -0x1020(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    12ae <main+0x12e>
lea    0xd9a(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
cmpl   $0x1000,-0x1020(%rbp)
jne    12c3 <main+0x143>
jmp    12ec <main+0x16c>
mov    0x2cf6(%rip),%rax
mov    (%rax),%edi
mov    -0x1020(%rbp),%edx
lea    0xd75(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
mov    0x2cbc(%rip),%rax
or     (%rax),%esi
mov    0x2c93(%rip),%rax
or     (%rax),%esi
mov    0x2c9a(%rip),%rax
mov    (%rax),%edx
mov    0x2cc1(%rip),%rax
or     (%rax),%edx
lea    0xce0(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1014(%rbp)
cmpl   $0x0,-0x1014(%rbp)
jge    1357 <main+0x1d7>
mov    0x2c71(%rip),%rax
mov    (%rax),%rax
mov    0x2c7f(%rip),%rcx
cmp    (%rcx),%rax
jne    1357 <main+0x1d7>
jmp    137a <main+0x1fa>
mov    0x2c62(%rip),%rax
mov    (%rax),%edi
lea    0xd10(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
mov    0x2c77(%rip),%rax
mov    (%rax),%esi
mov    0x2c0e(%rip),%rax
or     (%rax),%esi
mov    0x2bfd(%rip),%rax
or     (%rax),%esi
mov    0x2c0c(%rip),%rax
mov    (%rax),%edx
mov    0x2c33(%rip),%rax
or     (%rax),%edx
lea    0xc52(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1014(%rbp)
lea    0xc40(%rip),%rdi
lea    -0x1020(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    13f7 <main+0x277>
lea    0xc51(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
cmpl   $0x1000,-0x1020(%rbp)
jne    140c <main+0x28c>
jmp    1435 <main+0x2b5>
mov    0x2bad(%rip),%rax
mov    (%rax),%edi
mov    -0x1020(%rbp),%edx
lea    0xc2c(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
mov    -0x1014(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
mov    %eax,-0x101c(%rbp)
cmpl   $0x1000,-0x101c(%rbp)
je     1485 <main+0x305>
lea    0xbc0(%rip),%rdi
call   10a0 <perror@plt>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
lea    0xb69(%rip),%rdi
lea    -0x1020(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    14ce <main+0x34e>
lea    0xb7a(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
cmpl   $0x2000,-0x1020(%rbp)
jne    14e3 <main+0x363>
jmp    150c <main+0x38c>
mov    0x2ad6(%rip),%rax
mov    (%rax),%edi
mov    -0x1020(%rbp),%edx
lea    0xba5(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
mov    0x2ae5(%rip),%rax
mov    (%rax),%esi
mov    0x2a7c(%rip),%rax
or     (%rax),%esi
mov    0x2acb(%rip),%rax
or     (%rax),%esi
mov    0x2a7a(%rip),%rax
mov    (%rax),%edx
mov    0x2aa1(%rip),%rax
or     (%rax),%edx
lea    0xac0(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x1014(%rbp)
mov    -0x1014(%rbp),%edi
call   1060 <close@plt>
lea    0xaa3(%rip),%rdi
lea    -0x1020(%rbp),%rsi
call   1080 <stat@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    1594 <main+0x414>
lea    0xab4(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
cmpl   $0x0,-0x1020(%rbp)
jne    15a6 <main+0x426>
jmp    15cf <main+0x44f>
mov    0x2a13(%rip),%rax
mov    (%rax),%edi
mov    -0x1020(%rbp),%edx
lea    0xb0b(%rip),%rsi
mov    $0x0,%al
call   1070 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
lea    0xa2a(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x1018(%rbp)
cmpl   $0x0,-0x1018(%rbp)
jge    1606 <main+0x486>
lea    0xaf8(%rip),%rdi
call   10a0 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    160d <main+0x48d>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1020,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1018,%rsp
lea    0xe68(%rip),%rbx
mov    %rbx,%rdi
call   1030 <unlink@plt>
mov    0x2e51(%rip),%rax
mov    0x2e0a(%rip),%r15
mov    (%r15),%esi
or     (%rax),%esi
mov    0x2dde(%rip),%rax
mov    %rax,%rbp
or     (%rax),%esi
mov    0x2de2(%rip),%rax
mov    0x2e0b(%rip),%rcx
mov    %rcx,%r13
mov    (%rcx),%edx
mov    %rax,%r12
or     (%rax),%edx
mov    %rbx,%rdi
call   1090 <open@plt>
test   %eax,%eax
js     13bd <main+0x23d>
mov    %eax,%ebx
lea    0x10(%rsp),%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x1000,%edx
call   1050 <memset@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1040 <write@plt>
cmp    $0x1000,%eax
jne    13cb <main+0x24b>
mov    %ebx,%edi
call   1060 <close@plt>
lea    0xddb(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %eax,%eax
js     13b4 <main+0x234>
mov    0x8(%rsp),%edx
cmp    $0x1000,%edx
jne    1382 <main+0x202>
mov    (%r15),%esi
mov    0x2da7(%rip),%rbx
or     (%rbx),%esi
or     0x0(%rbp),%esi
mov    0x0(%r13),%edx
or     (%r12),%edx
lea    0xd9b(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
jns    139b <main+0x21b>
mov    0x2d37(%rip),%rax
mov    (%rax),%rax
mov    0x2d45(%rip),%rcx
cmp    (%rcx),%rax
jne    139b <main+0x21b>
mov    0x0(%rbp),%esi
or     (%rbx),%esi
mov    0x2cf8(%rip),%rax
or     (%rax),%esi
mov    0x0(%r13),%edx
or     (%r12),%edx
lea    0xd57(%rip),%r15
mov    %r15,%rdi
call   1090 <open@plt>
mov    %eax,%r14d
lea    0x8(%rsp),%rsi
mov    %r15,%rdi
call   1080 <stat@plt>
test   %eax,%eax
js     13b4 <main+0x234>
mov    0x8(%rsp),%edx
cmp    $0x1000,%edx
jne    1382 <main+0x202>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0x1000,%edx
call   1040 <write@plt>
cmp    $0x1000,%eax
jne    1424 <main+0x2a4>
mov    %r14d,%edi
call   1060 <close@plt>
lea    0xcfb(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %eax,%eax
js     13b4 <main+0x234>
mov    0x8(%rsp),%edx
cmp    $0x2000,%edx
jne    13f5 <main+0x275>
mov    0x0(%rbp),%esi
or     (%rbx),%esi
mov    0x2cbd(%rip),%rax
or     (%rax),%esi
mov    0x0(%r13),%edx
or     (%r12),%edx
lea    0xcbc(%rip),%rbx
mov    %rbx,%rdi
call   1090 <open@plt>
mov    %eax,%edi
call   1060 <close@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <stat@plt>
test   %eax,%eax
js     13b4 <main+0x234>
mov    0x8(%rsp),%edx
test   %edx,%edx
je     1407 <main+0x287>
mov    0x2c49(%rip),%rax
mov    (%rax),%edi
lea    0xd47(%rip),%rsi
jmp    1392 <main+0x212>
mov    0x2c37(%rip),%rax
mov    (%rax),%edi
lea    0xcbc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    13de <main+0x25e>
mov    0x2c1e(%rip),%rax
mov    (%rax),%edi
lea    0xccc(%rip),%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
jmp    13de <main+0x25e>
lea    0xc7c(%rip),%rdi
jmp    13c4 <main+0x244>
lea    0xc4b(%rip),%rdi
call   10a0 <perror@plt>
jmp    13de <main+0x25e>
lea    0xc57(%rip),%rdi
call   10a0 <perror@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    $0x1,%eax
add    $0x1018,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2bc4(%rip),%rax
mov    (%rax),%edi
lea    0xc99(%rip),%rsi
jmp    1392 <main+0x212>
lea    0xbf2(%rip),%rdi
call   1030 <unlink@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
jns    13e3 <main+0x263>
lea    0xccb(%rip),%rdi
jmp    13c4 <main+0x244>
lea    0xbfe(%rip),%rdi
call   10a0 <perror@plt>
mov    %r14d,%edi
jmp    13d9 <main+0x259>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [15 x i8] c"./mnt/testopen\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [26 x i8] c"Unable to create testopen\00"
@.str.2 = [14 x i8] c"Error writing\00"
@.str.3 = [23 x i8] c"Error stating testopen\00"
@stderr = global i32 0
@.str.4 = [41 x i8] c"Size of file should be 4096, but is %jd\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.5 = [39 x i8] c"Second create succedded and should not\00"
@O_APPEND = global i32 0
@.str.6 = [41 x i8] c"Size of file should be 8192, but is %jd\0A\00"
@O_TRUNC = global i32 0
@.str.7 = [38 x i8] c"Size of file should be 0, but is %jd\0A\00"
@.str.8 = [24 x i8] c"Error removing testopen\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [4096 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca %struct.stat
store i32 0, i32* %1
%7 = call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @O_CREAT
%9 = load i32, i32* @O_EXCL
%10 = or i32 %8, %9
%11 = load i32, i32* @O_WRONLY
%12 = or i32 %10, %11
%13 = load i32, i32* @S_IRUSR
%14 = load i32, i32* @S_IWUSR
%15 = or i32 %13, %14
%16 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %15)
store i32 %16, i32* %3
%17 = load i32, i32* %3
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %0
%20 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %1
br label %158
21: ; preds = %0
%22 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%23 = call i32 @memset(i8* %22, i32 0, i32 4096)
%24 = load i32, i32* %3
%25 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%26 = call i32 @write(i32 %24, i8* %25, i32 4096)
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = icmp ne i32 %27, 4096
br i1 %28, label %29, label %33
29: ; preds = %21
%30 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%31 = load i32, i32* %3
%32 = call i32 @close(i32 %31)
store i32 1, i32* %1
br label %158
33: ; preds = %21
%34 = load i32, i32* %3
%35 = call i32 @close(i32 %34)
%36 = call i32 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %6)
store i32 %36, i32* %4
%37 = load i32, i32* %4
%38 = icmp slt i32 %37, 0
br i1 %38, label %39, label %41
39: ; preds = %33
%40 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %158
41: ; preds = %33
%42 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%43 = load i32, i32* %42
%44 = icmp eq i32 %43, 4096
br i1 %44, label %45, label %46
45: ; preds = %41
br label %51
46: ; preds = %41
%47 = load i32, i32* @stderr
%48 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%49 = load i32, i32* %48
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %49)
store i32 1, i32* %1
br label %158
51: ; preds = %45
%52 = load i32, i32* @O_CREAT
%53 = load i32, i32* @O_EXCL
%54 = or i32 %52, %53
%55 = load i32, i32* @O_WRONLY
%56 = or i32 %54, %55
%57 = load i32, i32* @S_IRUSR
%58 = load i32, i32* @S_IWUSR
%59 = or i32 %57, %58
%60 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %56, i32 %59)
store i32 %60, i32* %3
%61 = load i32, i32* %3
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %68
63: ; preds = %51
%64 = load i64, i64* @errno
%65 = load i64, i64* @EEXIST
%66 = icmp eq i64 %64, %65
br i1 %66, label %67, label %68
67: ; preds = %63
br label %71
68: ; preds = %63, %51
%69 = load i32, i32* @stderr
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %69, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %1
br label %158
71: ; preds = %67
%72 = load i32, i32* @O_CREAT
%73 = load i32, i32* @O_WRONLY
%74 = or i32 %72, %73
%75 = load i32, i32* @O_APPEND
%76 = or i32 %74, %75
%77 = load i32, i32* @S_IRUSR
%78 = load i32, i32* @S_IWUSR
%79 = or i32 %77, %78
%80 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %76, i32 %79)
store i32 %80, i32* %3
%81 = call i32 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %6)
store i32 %81, i32* %4
%82 = load i32, i32* %4
%83 = icmp slt i32 %82, 0
br i1 %83, label %84, label %86
84: ; preds = %71
%85 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %158
86: ; preds = %71
%87 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%88 = load i32, i32* %87
%89 = icmp eq i32 %88, 4096
br i1 %89, label %90, label %91
90: ; preds = %86
br label %96
91: ; preds = %86
%92 = load i32, i32* @stderr
%93 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%94 = load i32, i32* %93
%95 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %94)
store i32 1, i32* %1
br label %158
96: ; preds = %90
%97 = load i32, i32* %3
%98 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%99 = call i32 @write(i32 %97, i8* %98, i32 4096)
store i32 %99, i32* %5
%100 = load i32, i32* %5
%101 = icmp ne i32 %100, 4096
br i1 %101, label %102, label %106
102: ; preds = %96
%103 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%104 = load i32, i32* %3
%105 = call i32 @close(i32 %104)
store i32 1, i32* %1
br label %158
106: ; preds = %96
%107 = load i32, i32* %3
%108 = call i32 @close(i32 %107)
%109 = call i32 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %6)
store i32 %109, i32* %4
%110 = load i32, i32* %4
%111 = icmp slt i32 %110, 0
br i1 %111, label %112, label %114
112: ; preds = %106
%113 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %158
114: ; preds = %106
%115 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%116 = load i32, i32* %115
%117 = icmp eq i32 %116, 8192
br i1 %117, label %118, label %119
118: ; preds = %114
br label %124
119: ; preds = %114
%120 = load i32, i32* @stderr
%121 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%122 = load i32, i32* %121
%123 = call i32 (i32, i8*, ...) @fprintf(i32 %120, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i32 %122)
store i32 1, i32* %1
br label %158
124: ; preds = %118
%125 = load i32, i32* @O_CREAT
%126 = load i32, i32* @O_WRONLY
%127 = or i32 %125, %126
%128 = load i32, i32* @O_TRUNC
%129 = or i32 %127, %128
%130 = load i32, i32* @S_IRUSR
%131 = load i32, i32* @S_IWUSR
%132 = or i32 %130, %131
%133 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %129, i32 %132)
store i32 %133, i32* %3
%134 = load i32, i32* %3
%135 = call i32 @close(i32 %134)
%136 = call i32 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %6)
store i32 %136, i32* %4
%137 = load i32, i32* %4
%138 = icmp slt i32 %137, 0
br i1 %138, label %139, label %141
139: ; preds = %124
%140 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %1
br label %158
141: ; preds = %124
%142 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%143 = load i32, i32* %142
%144 = icmp eq i32 %143, 0
br i1 %144, label %145, label %146
145: ; preds = %141
br label %151
146: ; preds = %141
%147 = load i32, i32* @stderr
%148 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%149 = load i32, i32* %148
%150 = call i32 (i32, i8*, ...) @fprintf(i32 %147, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0), i32 %149)
store i32 1, i32* %1
br label %158
151: ; preds = %145
%152 = call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 %152, i32* %4
%153 = load i32, i32* %4
%154 = icmp slt i32 %153, 0
br i1 %154, label %155, label %157
155: ; preds = %151
%156 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
store i32 1, i32* %1
br label %158
157: ; preds = %151
store i32 0, i32* %1
br label %158
158: ; preds = %157, %155, %146, %139, %119, %112, %102, %91, %84, %68, %46, %39, %29, %19
%159 = load i32, i32* %1
ret i32 %159
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [15 x i8] c"./mnt/testopen\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_WRONLY = global i32 0
@S_IRUSR = global i32 0
@S_IWUSR = global i32 0
@.str.1 = [26 x i8] c"Unable to create testopen\00"
@.str.2 = [14 x i8] c"Error writing\00"
@.str.3 = [23 x i8] c"Error stating testopen\00"
@stderr = global i32 0
@.str.4 = [41 x i8] c"Size of file should be 4096, but is %jd\0A\00"
@errno = global i64 0
@EEXIST = global i64 0
@.str.5 = [39 x i8] c"Second create succedded and should not\00"
@O_APPEND = global i32 0
@.str.6 = [41 x i8] c"Size of file should be 8192, but is %jd\0A\00"
@O_TRUNC = global i32 0
@.str.7 = [38 x i8] c"Size of file should be 0, but is %jd\0A\00"
@.str.8 = [24 x i8] c"Error removing testopen\00"
define i32 @main() {
%1 = alloca [4096 x i8]
%2 = alloca %struct.stat
%3 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.stat* %2 to i8*
%5 = tail call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = load i32, i32* @O_CREAT
%7 = load i32, i32* @O_EXCL
%8 = or i32 %7, %6
%9 = load i32, i32* @O_WRONLY
%10 = or i32 %8, %9
%11 = load i32, i32* @S_IRUSR
%12 = load i32, i32* @S_IWUSR
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %10, i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %0
%17 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %120
18: ; preds = %0
%19 = call i32 @memset(i8* %3, i32 0, i32 4096)
%20 = call i32 @write(i32 %14, i8* %3, i32 4096)
%21 = icmp eq i32 %20, 4096
br i1 %21, label %25, label %22
22: ; preds = %18
%23 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%24 = call i32 @close(i32 %14)
br label %120
25: ; preds = %18
%26 = call i32 @close(i32 %14)
%27 = call i32 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %25
%30 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %120
31: ; preds = %25
%32 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%33 = load i32, i32* %32
%34 = icmp eq i32 %33, 4096
br i1 %34, label %38, label %35
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %33)
br label %120
38: ; preds = %31
%39 = load i32, i32* @O_CREAT
%40 = load i32, i32* @O_EXCL
%41 = or i32 %40, %39
%42 = load i32, i32* @O_WRONLY
%43 = or i32 %41, %42
%44 = load i32, i32* @S_IRUSR
%45 = load i32, i32* @S_IWUSR
%46 = or i32 %45, %44
%47 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %43, i32 %46)
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %53
49: ; preds = %38
%50 = load i64, i64* @errno
%51 = load i64, i64* @EEXIST
%52 = icmp eq i64 %50, %51
br i1 %52, label %56, label %53
53: ; preds = %49, %38
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.5, i64 0, i64 0))
br label %120
56: ; preds = %49
%57 = load i32, i32* @O_CREAT
%58 = load i32, i32* @O_WRONLY
%59 = or i32 %58, %57
%60 = load i32, i32* @O_APPEND
%61 = or i32 %59, %60
%62 = load i32, i32* @S_IRUSR
%63 = load i32, i32* @S_IWUSR
%64 = or i32 %63, %62
%65 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %61, i32 %64)
%66 = call i32 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %56
%69 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %120
70: ; preds = %56
%71 = load i32, i32* %32
%72 = icmp eq i32 %71, 4096
br i1 %72, label %76, label %73
73: ; preds = %70
%74 = load i32, i32* @stderr
%75 = call i32 (i32, i8*, ...) @fprintf(i32 %74, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i32 %71)
br label %120
76: ; preds = %70
%77 = call i32 @write(i32 %65, i8* %3, i32 4096)
%78 = icmp eq i32 %77, 4096
br i1 %78, label %82, label %79
79: ; preds = %76
%80 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
%81 = call i32 @close(i32 %65)
br label %120
82: ; preds = %76
%83 = call i32 @close(i32 %65)
%84 = call i32 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%85 = icmp slt i32 %84, 0
br i1 %85, label %86, label %88
86: ; preds = %82
%87 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %120
88: ; preds = %82
%89 = load i32, i32* %32
%90 = icmp eq i32 %89, 8192
br i1 %90, label %94, label %91
91: ; preds = %88
%92 = load i32, i32* @stderr
%93 = call i32 (i32, i8*, ...) @fprintf(i32 %92, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0), i32 %89)
br label %120
94: ; preds = %88
%95 = load i32, i32* @O_CREAT
%96 = load i32, i32* @O_WRONLY
%97 = or i32 %96, %95
%98 = load i32, i32* @O_TRUNC
%99 = or i32 %97, %98
%100 = load i32, i32* @S_IRUSR
%101 = load i32, i32* @S_IWUSR
%102 = or i32 %101, %100
%103 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %99, i32 %102)
%104 = call i32 @close(i32 %103)
%105 = call i32 @stat(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
%106 = icmp slt i32 %105, 0
br i1 %106, label %107, label %109
107: ; preds = %94
%108 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
br label %120
109: ; preds = %94
%110 = load i32, i32* %32
%111 = icmp eq i32 %110, 0
br i1 %111, label %115, label %112
112: ; preds = %109
%113 = load i32, i32* @stderr
%114 = call i32 (i32, i8*, ...) @fprintf(i32 %113, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.7, i64 0, i64 0), i32 %110)
br label %120
115: ; preds = %109
%116 = call i32 @unlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%117 = icmp slt i32 %116, 0
br i1 %117, label %118, label %120
118: ; preds = %115
%119 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.8, i64 0, i64 0))
br label %120
120: ; preds = %115, %118, %112, %107, %91, %86, %79, %73, %68, %53, %35, %29, %22, %16
%121 = phi i32 [ 1, %16 ], [ 1, %22 ], [ 1, %29 ], [ 1, %68 ], [ 1, %79 ], [ 1, %86 ], [ 1, %107 ], [ 1, %118 ], [ 1, %112 ], [ 1, %91 ], [ 1, %73 ], [ 1, %53 ], [ 1, %35 ], [ 0, %115 ]
ret i32 %121
}