#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 /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ EACCES;
int O_CREAT;
int O_RDONLY;
int /*<<< orphan*/ S_IRUSR;
int S_ISDIR(int /*<<< orphan*/);
int S_ISREG(int /*<<< orphan*/);
int chmod(char *, int /*<<< orphan*/);
int creat(char *, int);
int dirfd(int /*<<< orphan*/ *);
scalar_t__ errno;
int fstat(int, struct stat *);
int open(char *, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char *argv[]) {
  int fd = creat("test.txt", O_CREAT | O_RDONLY);
  if (fd == -1) {
    perror("creat");
    if (errno == EACCES) {
      printf("EACCES\n");
      int ret = chmod("test.txt", S_IRUSR);
      if (ret == -1) {
        perror("chmod");
        return errno;
      }
      fd = open("test.txt", O_RDONLY);
      if (fd == -1) {
        perror("open");
        return errno;
      }
    }
  }
  struct stat st;
  int ret = fstat(fd, &st);
  if (ret == -1) {
    perror("fstat");
    return errno;
  }
  printf("S_ISREG %d\n", S_ISREG(st.st_mode));
  DIR *dir = opendir(".");
  if (dir == NULL) {
    perror("opendir");
    return errno;
  }
  int dfd = dirfd(dir);
  if (dfd == -1) {
    perror("dirfd");
    return errno;
  }
  struct stat st2;
  ret = fstat(dfd, &st2);
  if (ret == -1) {
    perror("fstat");
    return errno;
  }
  printf("S_ISREG %d\n", S_ISREG(st2.st_mode));
  printf("S_ISDIR %d\n", S_ISDIR(st2.st_mode));
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
or     (%rax),%esi
lea    0xe31(%rip),%rdi
call   10b0 <creat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    129f <main+0xff>
lea    0xe21(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%rax
mov    0x2dda(%rip),%rcx
cmp    (%rcx),%rax
jne    129a <main+0xfa>
lea    0xe01(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
lea    0xddb(%rip),%rdi
call   1080 <chmod@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1255 <main+0xb5>
lea    0xdd9(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d7e(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    13c9 <main+0x229>
mov    0x2d84(%rip),%rax
mov    (%rax),%esi
lea    0xd9b(%rip),%rdi
call   1090 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1295 <main+0xf5>
lea    0xd9f(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    13c9 <main+0x229>
jmp    129a <main+0xfa>
jmp    129f <main+0xff>
mov    -0x14(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   10c0 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    12d6 <main+0x136>
lea    0xd63(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cfd(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    13c9 <main+0x229>
mov    -0x20(%rbp),%edi
call   1070 <S_ISREG@plt>
mov    %eax,%esi
lea    0xd41(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xd3f(%rip),%rdi
call   1030 <opendir@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1327 <main+0x187>
lea    0xd26(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2cac(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    13c9 <main+0x229>
mov    -0x30(%rbp),%rdi
call   1060 <dirfd@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffffff,-0x34(%rbp)
jne    135b <main+0x1bb>
lea    0xcfa(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c78(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    13c9 <main+0x229>
mov    -0x34(%rbp),%edi
lea    -0x38(%rbp),%rsi
call   10c0 <fstat@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    1392 <main+0x1f2>
lea    0xca7(%rip),%rdi
call   10a0 <perror@plt>
mov    0x2c41(%rip),%rax
mov    (%rax),%rax
mov    %eax,-0x4(%rbp)
jmp    13c9 <main+0x229>
mov    -0x38(%rbp),%edi
call   1070 <S_ISREG@plt>
mov    %eax,%esi
lea    0xc85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%edi
call   1050 <S_ISDIR@plt>
mov    %eax,%esi
lea    0xc89(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    0x2e3b(%rip),%rax
mov    0x2e1c(%rip),%rbp
mov    0x0(%rbp),%esi
or     (%rax),%esi
lea    0xe30(%rip),%rdi
call   10c0 <creat@plt>
cmp    $0xffffffff,%eax
je     1288 <main+0xd8>
lea    0x10(%rsp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
je     127c <main+0xcc>
mov    0x10(%rsp),%edi
call   1080 <S_ISREG@plt>
lea    0xe1d(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe19(%rip),%rdi
call   1040 <opendir@plt>
test   %rax,%rax
je     12fa <main+0x14a>
mov    %rax,%rdi
call   1070 <dirfd@plt>
cmp    $0xffffffff,%eax
je     1303 <main+0x153>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10d0 <fstat@plt>
cmp    $0xffffffff,%eax
je     127c <main+0xcc>
mov    0x8(%rsp),%ebp
mov    %ebp,%edi
call   1080 <S_ISREG@plt>
lea    0xdcb(%rip),%rdi
xor    %ebx,%ebx
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebp,%edi
call   1060 <S_ISDIR@plt>
lea    0xdce(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <printf@plt>
jmp    1318 <main+0x168>
lea    0xd97(%rip),%rdi
jmp    130a <main+0x15a>
lea    0xd7a(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2d2d(%rip),%rbx
mov    (%rbx),%rcx
mov    0x2d33(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
jne    11de <main+0x2e>
lea    0xd8e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%esi
lea    0xd31(%rip),%rdi
call   1090 <chmod@plt>
cmp    $0xffffffff,%eax
je     1321 <main+0x171>
mov    0x0(%rbp),%esi
lea    0xd1d(%rip),%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
jne    11de <main+0x2e>
lea    0xd1d(%rip),%rdi
jmp    1328 <main+0x178>
lea    0xd2d(%rip),%rdi
jmp    130a <main+0x15a>
lea    0xd2c(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2cb2(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xce7(%rip),%rdi
call   10b0 <perror@plt>
mov    (%rbx),%ebx
jmp    1318 <main+0x168>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [6 x i8] c"creat\00"
@errno = global i64 0
@EACCES = global i64 0
@.str.2 = [8 x i8] c"EACCES\0A\00"
@S_IRUSR = global i32 0
@.str.3 = [6 x i8] c"chmod\00"
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [6 x i8] c"fstat\00"
@.str.6 = [12 x i8] c"S_ISREG %d\0A\00"
@.str.7 = [2 x i8] c".\00"
@.str.8 = [8 x i8] c"opendir\00"
@.str.9 = [6 x i8] c"dirfd\00"
@.str.10 = [12 x i8] c"S_ISDIR %d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.stat
%9 = alloca i32
%10 = alloca i32*
%11 = alloca i32
%12 = alloca %struct.stat
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* @O_CREAT
%14 = load i32, i32* @O_RDONLY
%15 = or i32 %13, %14
%16 = call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %15)
store i32 %16, i32* %6
%17 = load i32, i32* %6
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %45
19: ; preds = %2
%20 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%21 = load i64, i64* @errno
%22 = load i64, i64* @EACCES
%23 = icmp eq i64 %21, %22
br i1 %23, label %24, label %44
24: ; preds = %19
%25 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* @S_IRUSR
%27 = call i32 @chmod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %26)
store i32 %27, i32* %7
%28 = load i32, i32* %7
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %34
30: ; preds = %24
%31 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%32 = load i64, i64* @errno
%33 = trunc i64 %32 to i32
store i32 %33, i32* %3
br label %93
34: ; preds = %24
%35 = load i32, i32* @O_RDONLY
%36 = call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %35)
store i32 %36, i32* %6
%37 = load i32, i32* %6
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %43
39: ; preds = %34
%40 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%41 = load i64, i64* @errno
%42 = trunc i64 %41 to i32
store i32 %42, i32* %3
br label %93
43: ; preds = %34
br label %44
44: ; preds = %43, %19
br label %45
45: ; preds = %44, %2
%46 = load i32, i32* %6
%47 = call i32 @fstat(i32 %46, %struct.stat* %8)
store i32 %47, i32* %9
%48 = load i32, i32* %9
%49 = icmp eq i32 %48, -1
br i1 %49, label %50, label %54
50: ; preds = %45
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%52 = load i64, i64* @errno
%53 = trunc i64 %52 to i32
store i32 %53, i32* %3
br label %93
54: ; preds = %45
%55 = getelementptr inbounds %struct.stat, %struct.stat* %8, i32 0, i32 0
%56 = load i32, i32* %55
%57 = call i32 @S_ISREG(i32 %56)
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %57)
%59 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
store i32* %59, i32** %10
%60 = load i32*, i32** %10
%61 = icmp eq i32* %60, null
br i1 %61, label %62, label %66
62: ; preds = %54
%63 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
%64 = load i64, i64* @errno
%65 = trunc i64 %64 to i32
store i32 %65, i32* %3
br label %93
66: ; preds = %54
%67 = load i32*, i32** %10
%68 = call i32 @dirfd(i32* %67)
store i32 %68, i32* %11
%69 = load i32, i32* %11
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %75
71: ; preds = %66
%72 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%73 = load i64, i64* @errno
%74 = trunc i64 %73 to i32
store i32 %74, i32* %3
br label %93
75: ; preds = %66
%76 = load i32, i32* %11
%77 = call i32 @fstat(i32 %76, %struct.stat* %12)
store i32 %77, i32* %9
%78 = load i32, i32* %9
%79 = icmp eq i32 %78, -1
br i1 %79, label %80, label %84
80: ; preds = %75
%81 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%82 = load i64, i64* @errno
%83 = trunc i64 %82 to i32
store i32 %83, i32* %3
br label %93
84: ; preds = %75
%85 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%86 = load i32, i32* %85
%87 = call i32 @S_ISREG(i32 %86)
%88 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %87)
%89 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%90 = load i32, i32* %89
%91 = call i32 @S_ISDIR(i32 %90)
%92 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i32 %91)
store i32 0, i32* %3
br label %93
93: ; preds = %84, %80, %71, %62, %50, %39, %30
%94 = load i32, i32* %3
ret i32 %94
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [9 x i8] c"test.txt\00"
@O_CREAT = global i32 0
@O_RDONLY = global i32 0
@.str.1 = [6 x i8] c"creat\00"
@errno = global i64 0
@EACCES = global i64 0
@S_IRUSR = global i32 0
@.str.3 = [6 x i8] c"chmod\00"
@.str.4 = [5 x i8] c"open\00"
@.str.5 = [6 x i8] c"fstat\00"
@.str.6 = [12 x i8] c"S_ISREG %d\0A\00"
@.str.7 = [2 x i8] c".\00"
@.str.8 = [8 x i8] c"opendir\00"
@.str.9 = [6 x i8] c"dirfd\00"
@.str.10 = [12 x i8] c"S_ISDIR %d\0A\00"
@str = [7 x i8] c"EACCES\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.stat
%4 = alloca %struct.stat
%5 = load i32, i32* @O_CREAT
%6 = load i32, i32* @O_RDONLY
%7 = or i32 %6, %5
%8 = tail call i32 @creat(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %7)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %32
10: ; preds = %2
%11 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%12 = load i64, i64* @errno
%13 = load i64, i64* @EACCES
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %32
15: ; preds = %10
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
%17 = load i32, i32* @S_IRUSR
%18 = tail call i32 @chmod(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %24
20: ; preds = %15
%21 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%22 = load i64, i64* @errno
%23 = trunc i64 %22 to i32
br label %78
24: ; preds = %15
%25 = load i32, i32* @O_RDONLY
%26 = tail call i32 @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %25)
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
%30 = load i64, i64* @errno
%31 = trunc i64 %30 to i32
br label %78
32: ; preds = %24, %10, %2
%33 = phi i32 [ -1, %10 ], [ %8, %2 ], [ %26, %24 ]
%34 = bitcast %struct.stat* %3 to i8*
%35 = call i32 @fstat(i32 %33, %struct.stat* %3)
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%39 = load i64, i64* @errno
%40 = trunc i64 %39 to i32
br label %76
41: ; preds = %32
%42 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%43 = load i32, i32* %42
%44 = tail call i32 @S_ISREG(i32 %43)
%45 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %44)
%46 = tail call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.7, i64 0, i64 0))
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %52
48: ; preds = %41
%49 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.8, i64 0, i64 0))
%50 = load i64, i64* @errno
%51 = trunc i64 %50 to i32
br label %76
52: ; preds = %41
%53 = tail call i32 @dirfd(i32* %46)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.9, i64 0, i64 0))
%57 = load i64, i64* @errno
%58 = trunc i64 %57 to i32
br label %76
59: ; preds = %52
%60 = bitcast %struct.stat* %4 to i8*
%61 = call i32 @fstat(i32 %53, %struct.stat* %4)
%62 = icmp eq i32 %61, -1
br i1 %62, label %63, label %67
63: ; preds = %59
%64 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%65 = load i64, i64* @errno
%66 = trunc i64 %65 to i32
br label %74
67: ; preds = %59
%68 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%69 = load i32, i32* %68
%70 = tail call i32 @S_ISREG(i32 %69)
%71 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.6, i64 0, i64 0), i32 %70)
%72 = tail call i32 @S_ISDIR(i32 %69)
%73 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.10, i64 0, i64 0), i32 %72)
br label %74
74: ; preds = %67, %63
%75 = phi i32 [ %66, %63 ], [ 0, %67 ]
br label %76
76: ; preds = %48, %74, %55, %37
%77 = phi i32 [ %40, %37 ], [ %51, %48 ], [ %58, %55 ], [ %75, %74 ]
br label %78
78: ; preds = %20, %28, %76
%79 = phi i32 [ %77, %76 ], [ %23, %20 ], [ %31, %28 ]
ret i32 %79
}