#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_mode;
};

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_EXCL;
int O_RDONLY;
int O_RDWR;
int O_TRUNC;
int O_WRONLY;
int creat(char *, int);
int errno;
int /*<<< orphan*/ memset(struct stat *, int /*<<< orphan*/, int);
int open(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stat(char *, struct stat *);
int main() {
  struct stat s;
  int modes[] = {O_RDONLY, O_WRONLY, O_RDWR};
  char nonexistent_name[] = "/noexist-##";
  for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 16; j++) {
      int flags = modes[i];
      if (j & 0x1)
        flags |= O_CREAT;
      if (j & 0x2)
        flags |= O_EXCL;
      if (j & 0x4)
        flags |= O_TRUNC;
      if (j & 0x8)
        flags |= O_APPEND;
      printf("EXISTING FILE %d,%d\n", i, j);
      printf("success: %d\n", open("/test-file", flags, 0777) != -1);
      printf("errno: %d\n", errno);
      stat("/test-file", &s);
      printf("st_mode: 0%o\n", s.st_mode & 037777777000);
      memset(&s, 0, sizeof s);
      printf("\n");
      errno = 0;
      printf("EXISTING FOLDER %d,%d\n", i, j);
      printf("success: %d\n", open("/test-folder", flags, 0777) != -1);
      printf("errno: %d\n", errno);
      stat("/test-folder", &s);
      printf("st_mode: 0%o\n", s.st_mode & 037777777000);
      memset(&s, 0, sizeof s);
      printf("\n");
      errno = 0;
      nonexistent_name[9] = 'a' + i;
      nonexistent_name[10] = 'a' + j;
      printf("NON-EXISTING %d,%d\n", i, j);
      printf("success: %d\n", open(nonexistent_name, flags, 0777) != -1);
      printf("errno: %d\n", errno);
      stat(nonexistent_name, &s);
      printf("st_mode: 0%o\n", s.st_mode & 037777777000);
      memset(&s, 0, sizeof s);
      printf("\n");
      errno = 0;
    }
  }
  printf("CREAT\n");
  printf("success: %d\n", creat("/creat-me", 0777) != -1);
  printf("errno: %d\n", errno);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e6a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
mov    0xe76(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0xe74(%rip),%eax
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
cmpl   $0x3,-0x24(%rbp)
jge    1467 <main+0x317>
movl   $0x0,-0x28(%rbp)
cmpl   $0x10,-0x28(%rbp)
jge    1454 <main+0x304>
movslq -0x24(%rbp),%rax
mov    -0x14(%rbp,%rax,4),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
and    $0x1,%eax
cmp    $0x0,%eax
je     11e2 <main+0x92>
mov    0x2e1e(%rip),%rax
mov    (%rax),%eax
or     -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     1200 <main+0xb0>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
or     -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
and    $0x4,%eax
cmp    $0x0,%eax
je     121e <main+0xce>
mov    0x2dda(%rip),%rax
mov    (%rax),%eax
or     -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x28(%rbp),%eax
and    $0x8,%eax
cmp    $0x0,%eax
je     123c <main+0xec>
mov    0x2d6c(%rip),%rax
mov    (%rax),%eax
or     -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xdc3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2c(%rbp),%esi
lea    0xdd4(%rip),%rdi
mov    $0x1ff,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%esi
lea    0xdab(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2d34(%rip),%rax
mov    (%rax),%esi
lea    0xdac(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd93(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x8(%rbp),%esi
and    $0xfffffe00,%esi
lea    0xd90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    0xdbd(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2cd8(%rip),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xd5f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x2c(%rbp),%esi
lea    0xd65(%rip),%rdi
mov    $0x1ff,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%esi
lea    0xd01(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2c8a(%rip),%rax
mov    (%rax),%esi
lea    0xd02(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xd24(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x8(%rbp),%esi
and    $0xfffffe00,%esi
lea    0xce6(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    0xd13(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2c2e(%rip),%rax
movl   $0x0,(%rax)
mov    -0x24(%rbp),%eax
add    $0x61,%eax
mov    %al,-0x17(%rbp)
mov    -0x28(%rbp),%eax
add    $0x61,%eax
mov    %al,-0x16(%rbp)
mov    -0x24(%rbp),%esi
mov    -0x28(%rbp),%edx
lea    0xcc7(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
mov    -0x2c(%rbp),%esi
mov    $0x1ff,%edx
call   1060 <open@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%esi
lea    0xc48(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2bd1(%rip),%rax
mov    (%rax),%esi
lea    0xc49(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x20(%rbp),%rdi
lea    -0x8(%rbp),%rsi
call   1050 <stat@plt>
mov    -0x8(%rbp),%esi
and    $0xfffffe00,%esi
lea    0xc30(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x8(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
lea    0xc5d(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2b78(%rip),%rax
movl   $0x0,(%rax)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11af <main+0x5f>
jmp    1459 <main+0x309>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    119e <main+0x4e>
lea    0xc1c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xc15(%rip),%rdi
mov    $0x1ff,%esi
call   1070 <creat@plt>
cmp    $0xffffffff,%eax
setne  %al
and    $0x1,%al
movzbl %al,%esi
lea    0xb89(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2b12(%rip),%rax
mov    (%rax),%esi
lea    0xb8a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    0x2e4b(%rip),%rax
mov    (%rax),%esi
mov    %esi,0x2c(%rsp)
mov    0x2e16(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x30(%rsp)
mov    0x2e39(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x34(%rsp)
movabs $0x74736978656f6e2f,%rax
69 73 74
mov    %rax,0x20(%rsp)
movl   $0x23232d,0x28(%rsp)
mov    0x2e35(%rip),%rcx
mov    0x2df6(%rip),%rdx
mov    0x2e1f(%rip),%rdi
mov    0x2dc8(%rip),%rbp
mov    0x2dd9(%rip),%rbx
xor    %eax,%eax
mov    %rax,0x10(%rsp)
cs nopw 0x0(%rax,%rax,1)
mov    0x10(%rsp),%rax
add    $0x61,%eax
mov    %eax,0x18(%rsp)
xor    %r15d,%r15d
mov    %esi,0x1c(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%eax
and    $0x1,%eax
neg    %eax
and    (%rcx),%eax
mov    %r15d,%ecx
shl    $0x1e,%ecx
sar    $0x1f,%ecx
and    (%rdx),%ecx
mov    %r15d,%edx
shl    $0x1d,%edx
sar    $0x1f,%edx
and    (%rdi),%edx
or     %esi,%eax
or     %ecx,%edx
or     %eax,%edx
mov    %r15d,%r12d
shl    $0x1c,%r12d
sar    $0x1f,%r12d
and    0x0(%rbp),%r12d
or     %edx,%r12d
lea    0xdbd(%rip),%rdi
mov    0x10(%rsp),%r13
mov    %r13d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xdc6(%rip),%r14
mov    %r14,%rdi
mov    %r12d,%esi
mov    $0x1ff,%edx
call   1080 <open@plt>
xor    %esi,%esi
cmp    $0xffffffff,%eax
setne  %sil
lea    0xd99(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0xda1(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
mov    0x8(%rsp),%esi
mov    $0xfffffe00,%eax
and    %eax,%esi
lea    0xd86(%rip),%r14
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x0,(%rbx)
lea    0xd62(%rip),%rdi
mov    %r13d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd65(%rip),%rbp
mov    %rbp,%rdi
mov    %r12d,%esi
mov    $0x1ff,%edx
call   1080 <open@plt>
xor    %esi,%esi
cmp    $0xffffffff,%eax
setne  %sil
lea    0xcfd(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0xd05(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
mov    0x8(%rsp),%esi
mov    $0xfffffe00,%eax
and    %eax,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x8(%rsp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
movl   $0x0,(%rbx)
mov    0x18(%rsp),%eax
mov    %al,0x29(%rsp)
lea    0x61(%r15),%eax
mov    %al,0x2a(%rsp)
lea    0xce1(%rip),%rdi
mov    %r13d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
lea    0x20(%rsp),%rbp
mov    %rbp,%rdi
mov    %r12d,%esi
lea    0xc6d(%rip),%r12
mov    $0x1ff,%edx
call   1080 <open@plt>
xor    %esi,%esi
cmp    $0xffffffff,%eax
setne  %sil
mov    %r12,%rdi
lea    0xc68(%rip),%r12
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%esi
mov    %r12,%rdi
lea    0x8(%rsp),%r12
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
call   1070 <stat@plt>
mov    0x8(%rsp),%esi
mov    $0xfffffe00,%eax
and    %eax,%esi
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    0x2b77(%rip),%rbp
mov    0x2bc0(%rip),%rdi
mov    0x2b89(%rip),%rdx
mov    0x2bba(%rip),%rcx
mov    0x1c(%rsp),%esi
movl   $0x0,(%rbx)
add    $0x1,%r15d
cmp    $0x10,%r15d
jne    1210 <main+0xa0>
mov    0x10(%rsp),%rsi
add    $0x1,%rsi
mov    %rsi,%rax
mov    %rsi,0x10(%rsp)
cmp    $0x3,%rsi
je     147b <main+0x30b>
mov    0x10(%rsp),%rax
mov    0x2c(%rsp,%rax,4),%esi
jmp    11f0 <main+0x80>
lea    0xc12(%rip),%rdi
call   1040 <puts@plt>
lea    0xbfc(%rip),%rdi
mov    $0x1ff,%esi
call   1090 <creat@plt>
xor    %esi,%esi
cmp    $0xffffffff,%eax
setne  %sil
lea    0xb79(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    (%rbx),%esi
lea    0xb81(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
xor    %eax,%eax
add    $0x38,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@__const.main.nonexistent_name = [12 x i8] c"/noexist-##\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@.str = [21 x i8] c"EXISTING FILE %d,%d\0A\00"
@.str.1 = [13 x i8] c"success: %d\0A\00"
@.str.2 = [11 x i8] c"/test-file\00"
@.str.3 = [11 x i8] c"errno: %d\0A\00"
@errno = global i32 0
@.str.4 = [14 x i8] c"st_mode: 0%o\0A\00"
@.str.5 = [2 x i8] c"\0A\00"
@.str.6 = [23 x i8] c"EXISTING FOLDER %d,%d\0A\00"
@.str.7 = [13 x i8] c"/test-folder\00"
@.str.8 = [20 x i8] c"NON-EXISTING %d,%d\0A\00"
@.str.9 = [7 x i8] c"CREAT\0A\00"
@.str.10 = [10 x i8] c"/creat-me\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca [3 x i32]
%4 = alloca [12 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %1
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 0
%9 = load i32, i32* @O_RDONLY
store i32 %9, i32* %8
%10 = getelementptr inbounds i32, i32* %8, i64 1
%11 = load i32, i32* @O_WRONLY
store i32 %11, i32* %10
%12 = getelementptr inbounds i32, i32* %10, i64 1
%13 = load i32, i32* @O_RDWR
store i32 %13, i32* %12
%14 = bitcast [12 x i8]* %4 to i8*
store i32 0, i32* %5
br label %15
15: ; preds = %124, %0
%16 = load i32, i32* %5
%17 = icmp slt i32 %16, 3
br i1 %17, label %18, label %127
18: ; preds = %15
store i32 0, i32* %6
br label %19
19: ; preds = %120, %18
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 16
br i1 %21, label %22, label %123
22: ; preds = %19
%23 = load i32, i32* %5
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds [3 x i32], [3 x i32]* %3, i64 0, i64 %24
%26 = load i32, i32* %25
store i32 %26, i32* %7
%27 = load i32, i32* %6
%28 = and i32 %27, 1
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %34
30: ; preds = %22
%31 = load i32, i32* @O_CREAT
%32 = load i32, i32* %7
%33 = or i32 %32, %31
store i32 %33, i32* %7
br label %34
34: ; preds = %30, %22
%35 = load i32, i32* %6
%36 = and i32 %35, 2
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* @O_EXCL
%40 = load i32, i32* %7
%41 = or i32 %40, %39
store i32 %41, i32* %7
br label %42
42: ; preds = %38, %34
%43 = load i32, i32* %6
%44 = and i32 %43, 4
%45 = icmp ne i32 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %42
%47 = load i32, i32* @O_TRUNC
%48 = load i32, i32* %7
%49 = or i32 %48, %47
store i32 %49, i32* %7
br label %50
50: ; preds = %46, %42
%51 = load i32, i32* %6
%52 = and i32 %51, 8
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %50
%55 = load i32, i32* @O_APPEND
%56 = load i32, i32* %7
%57 = or i32 %56, %55
store i32 %57, i32* %7
br label %58
58: ; preds = %54, %50
%59 = load i32, i32* %5
%60 = load i32, i32* %6
%61 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %59, i32 %60)
%62 = load i32, i32* %7
%63 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %62, i32 511)
%64 = icmp ne i32 %63, -1
%65 = zext i1 %64 to i32
%66 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %65)
%67 = load i32, i32* @errno
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %67)
%69 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %2)
%70 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%71 = load i32, i32* %70
%72 = and i32 %71, -512
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %72)
%74 = call i32 @memset(%struct.stat* %2, i32 0, i32 4)
%75 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* @errno
%76 = load i32, i32* %5
%77 = load i32, i32* %6
%78 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %76, i32 %77)
%79 = load i32, i32* %7
%80 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %79, i32 511)
%81 = icmp ne i32 %80, -1
%82 = zext i1 %81 to i32
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %82)
%84 = load i32, i32* @errno
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %84)
%86 = call i32 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), %struct.stat* %2)
%87 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%88 = load i32, i32* %87
%89 = and i32 %88, -512
%90 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %89)
%91 = call i32 @memset(%struct.stat* %2, i32 0, i32 4)
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* @errno
%93 = load i32, i32* %5
%94 = add nsw i32 97, %93
%95 = trunc i32 %94 to i8
%96 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 9
store i8 %95, i8* %96
%97 = load i32, i32* %6
%98 = add nsw i32 97, %97
%99 = trunc i32 %98 to i8
%100 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 10
store i8 %99, i8* %100
%101 = load i32, i32* %5
%102 = load i32, i32* %6
%103 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i32 %101, i32 %102)
%104 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%105 = load i32, i32* %7
%106 = call i32 @open(i8* %104, i32 %105, i32 511)
%107 = icmp ne i32 %106, -1
%108 = zext i1 %107 to i32
%109 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %108)
%110 = load i32, i32* @errno
%111 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %110)
%112 = getelementptr inbounds [12 x i8], [12 x i8]* %4, i64 0, i64 0
%113 = call i32 @stat(i8* %112, %struct.stat* %2)
%114 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%115 = load i32, i32* %114
%116 = and i32 %115, -512
%117 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %116)
%118 = call i32 @memset(%struct.stat* %2, i32 0, i32 4)
%119 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32 0, i32* @errno
br label %120
120: ; preds = %58
%121 = load i32, i32* %6
%122 = add nsw i32 %121, 1
store i32 %122, i32* %6
br label %19
123: ; preds = %19
br label %124
124: ; preds = %123
%125 = load i32, i32* %5
%126 = add nsw i32 %125, 1
store i32 %126, i32* %5
br label %15
127: ; preds = %15
%128 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.9, i64 0, i64 0))
%129 = call i32 @creat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i32 511)
%130 = icmp ne i32 %129, -1
%131 = zext i1 %130 to i32
%132 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %131)
%133 = load i32, i32* @errno
%134 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %133)
ret i32 0
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@O_WRONLY = global i32 0
@O_RDWR = global i32 0
@__const.main.nonexistent_name = [12 x i8] c"/noexist-##\00"
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@.str = [21 x i8] c"EXISTING FILE %d,%d\0A\00"
@.str.1 = [13 x i8] c"success: %d\0A\00"
@.str.2 = [11 x i8] c"/test-file\00"
@.str.3 = [11 x i8] c"errno: %d\0A\00"
@errno = global i32 0
@.str.4 = [14 x i8] c"st_mode: 0%o\0A\00"
@.str.6 = [23 x i8] c"EXISTING FOLDER %d,%d\0A\00"
@.str.7 = [13 x i8] c"/test-folder\00"
@.str.8 = [20 x i8] c"NON-EXISTING %d,%d\0A\00"
@.str.10 = [10 x i8] c"/creat-me\00"
@str = [6 x i8] c"CREAT\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = alloca [3 x i32]
%3 = alloca [12 x i8]
%4 = bitcast %struct.stat* %1 to i8*
%5 = bitcast [3 x i32]* %2 to i8*
%6 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 0
%7 = load i32, i32* @O_RDONLY
store i32 %7, i32* %6
%8 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 1
%9 = load i32, i32* @O_WRONLY
store i32 %9, i32* %8
%10 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 2
%11 = load i32, i32* @O_RDWR
store i32 %11, i32* %10
%12 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 0
%13 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%14 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 9
%15 = getelementptr inbounds [12 x i8], [12 x i8]* %3, i64 0, i64 10
br label %16
16: ; preds = %33, %0
%17 = phi i32 [ %7, %0 ], [ %35, %33 ]
%18 = phi i64 [ 0, %0 ], [ %31, %33 ]
%19 = trunc i64 %18 to i32
%20 = trunc i64 %18 to i8
%21 = add i8 %20, 97
br label %36
22: ; preds = %30
%23 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str, i64 0, i64 0))
%24 = call i32 @creat(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.10, i64 0, i64 0), i32 511)
%25 = icmp ne i32 %24, -1
%26 = zext i1 %25 to i32
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = load i32, i32* @errno
%29 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %28)
ret i32 0
30: ; preds = %36
%31 = add nuw nsw i64 %18, 1
%32 = icmp eq i64 %31, 3
br i1 %32, label %22, label %33
33: ; preds = %30
%34 = getelementptr inbounds [3 x i32], [3 x i32]* %2, i64 0, i64 %31
%35 = load i32, i32* %34
br label %16
36: ; preds = %16, %36
%37 = phi i32 [ 0, %16 ], [ %99, %36 ]
%38 = and i32 %37, 1
%39 = icmp eq i32 %38, 0
%40 = load i32, i32* @O_CREAT
%41 = select i1 %39, i32 0, i32 %40
%42 = or i32 %41, %17
%43 = and i32 %37, 2
%44 = icmp eq i32 %43, 0
%45 = load i32, i32* @O_EXCL
%46 = select i1 %44, i32 0, i32 %45
%47 = or i32 %42, %46
%48 = and i32 %37, 4
%49 = icmp eq i32 %48, 0
%50 = load i32, i32* @O_TRUNC
%51 = select i1 %49, i32 0, i32 %50
%52 = or i32 %47, %51
%53 = and i32 %37, 8
%54 = icmp eq i32 %53, 0
%55 = load i32, i32* @O_APPEND
%56 = select i1 %54, i32 0, i32 %55
%57 = or i32 %52, %56
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0), i32 %19, i32 %37)
%59 = call i32 @open(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), i32 %57, i32 511)
%60 = icmp ne i32 %59, -1
%61 = zext i1 %60 to i32
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %61)
%63 = load i32, i32* @errno
%64 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %63)
%65 = call i32 @stat(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0), %struct.stat* %1)
%66 = load i32, i32* %13
%67 = and i32 %66, -512
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %67)
%69 = call i32 @memset(%struct.stat* %1, i32 0, i32 4)
%70 = call i32 @putchar(i32 10)
store i32 0, i32* @errno
%71 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.6, i64 0, i64 0), i32 %19, i32 %37)
%72 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), i32 %57, i32 511)
%73 = icmp ne i32 %72, -1
%74 = zext i1 %73 to i32
%75 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %74)
%76 = load i32, i32* @errno
%77 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %76)
%78 = call i32 @stat(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.7, i64 0, i64 0), %struct.stat* %1)
%79 = load i32, i32* %13
%80 = and i32 %79, -512
%81 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %80)
%82 = call i32 @memset(%struct.stat* %1, i32 0, i32 4)
%83 = call i32 @putchar(i32 10)
store i32 0, i32* @errno
store i8 %21, i8* %14
%84 = trunc i32 %37 to i8
%85 = add nuw nsw i8 %84, 97
store i8 %85, i8* %15
%86 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i32 %19, i32 %37)
%87 = call i32 @open(i8* %12, i32 %57, i32 511)
%88 = icmp ne i32 %87, -1
%89 = zext i1 %88 to i32
%90 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %89)
%91 = load i32, i32* @errno
%92 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i32 %91)
%93 = call i32 @stat(i8* %12, %struct.stat* %1)
%94 = load i32, i32* %13
%95 = and i32 %94, -512
%96 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 %95)
%97 = call i32 @memset(%struct.stat* %1, i32 0, i32 4)
%98 = call i32 @putchar(i32 10)
store i32 0, i32* @errno
%99 = add nuw nsw i32 %37, 1
%100 = icmp eq i32 %99, 16
br i1 %100, label %30, label %36
}