#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 */
scalar_t__ ENOENT;
int /*<<< orphan*/ O_CREAT;
int creat(char *, int /*<<< orphan*/);
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int stat(char *, struct stat *);
int main() {
  struct stat st;
  int i = stat("a.txt", &st);
  if (i == -1) {
    perror("stat");
    if (errno == ENOENT) {
      if (creat("a.txt", O_CREAT) == -1) {
        perror("create");
        return errno;
      }
      stat("a.txt", &st);
    } else {
      return errno;
    }
  }
  printf("%x\n", st.st_mode);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xeaa(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    11fa <main+0xba>
lea    0xe93(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e59(%rip),%rax
mov    (%rax),%rax
mov    0x2e3f(%rip),%rcx
cmp    (%rcx),%rax
jne    11e3 <main+0xa3>
mov    0x2e5f(%rip),%rax
mov    (%rax),%esi
lea    0xe5e(%rip),%rdi
call   1060 <creat@plt>
cmp    $0xffffffff,%eax
jne    11ce <main+0x8e>
lea    0xe54(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    1210 <main+0xd0>
lea    0xe2b(%rip),%rdi
lea    -0x8(%rbp),%rsi
call   1040 <stat@plt>
jmp    11f5 <main+0xb5>
mov    0x2dee(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    1210 <main+0xd0>
jmp    11fa <main+0xba>
mov    -0x8(%rbp),%esi
lea    0xe0e(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
lea    0xeb4(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     1173 <main+0x33>
mov    0x8(%rsp),%esi
lea    0xeac(%rip),%rdi
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe8c(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e52(%rip),%rbx
mov    (%rbx),%rax
mov    0x2e38(%rip),%rcx
cmp    (%rcx),%rax
jne    116d <main+0x2d>
mov    0x2e5c(%rip),%rax
mov    (%rax),%esi
lea    0xe5b(%rip),%rdi
call   1060 <creat@plt>
cmp    $0xffffffff,%eax
je     11c2 <main+0x82>
lea    0xe4a(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
jmp    115b <main+0x1b>
lea    0xe42(%rip),%rdi
call   1050 <perror@plt>
mov    (%rbx),%eax
jmp    116d <main+0x2d>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [6 x i8] c"a.txt\00"
@.str.1 = [5 x i8] c"stat\00"
@errno = global i64 0
@ENOENT = global i64 0
@O_CREAT = global i32 0
@.str.2 = [7 x i8] c"create\00"
@.str.3 = [4 x i8] c"%x\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca %struct.stat
%3 = alloca i32
store i32 0, i32* %1
%4 = call i32 @stat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
store i32 %4, i32* %3
%5 = load i32, i32* %3
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %26
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %22
12: ; preds = %7
%13 = load i32, i32* @O_CREAT
%14 = call i32 @creat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %20
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%18 = load i64, i64* @errno
%19 = trunc i64 %18 to i32
store i32 %19, i32* %1
br label %30
20: ; preds = %12
%21 = call i32 @stat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.stat* %2)
br label %25
22: ; preds = %7
%23 = load i64, i64* @errno
%24 = trunc i64 %23 to i32
store i32 %24, i32* %1
br label %30
25: ; preds = %20
br label %26
26: ; preds = %25, %0
%27 = getelementptr inbounds %struct.stat, %struct.stat* %2, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %28)
store i32 0, i32* %1
br label %30
30: ; preds = %26, %22, %16
%31 = load i32, i32* %1
ret i32 %31
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [6 x i8] c"a.txt\00"
@.str.1 = [5 x i8] c"stat\00"
@errno = global i64 0
@ENOENT = global i64 0
@O_CREAT = global i32 0
@.str.2 = [7 x i8] c"create\00"
@.str.3 = [4 x i8] c"%x\0A\00"
define i32 @main() {
%1 = alloca %struct.stat
%2 = bitcast %struct.stat* %1 to i8*
%3 = call i32 @stat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
%4 = icmp eq i32 %3, -1
br i1 %4, label %5, label %22
5: ; preds = %0
%6 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%7 = load i64, i64* @errno
%8 = load i64, i64* @ENOENT
%9 = icmp eq i64 %7, %8
br i1 %9, label %10, label %20
10: ; preds = %5
%11 = load i32, i32* @O_CREAT
%12 = tail call i32 @creat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%16 = load i64, i64* @errno
%17 = trunc i64 %16 to i32
br label %26
18: ; preds = %10
%19 = call i32 @stat(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), %struct.stat* %1)
br label %22
20: ; preds = %5
%21 = trunc i64 %7 to i32
br label %26
22: ; preds = %18, %0
%23 = getelementptr inbounds %struct.stat, %struct.stat* %1, i64 0, i32 0
%24 = load i32, i32* %23
%25 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %24)
br label %26
26: ; preds = %22, %20, %14
%27 = phi i32 [ %17, %14 ], [ 0, %22 ], [ %21, %20 ]
ret i32 %27
}