#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 ENOENT;
int errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ printf(char *, char const *, ...);
scalar_t__ stat(char const *, struct stat *);
char *strerror(int);
void expect_type(const char *path, int mode) {
  struct stat st_buf;
  if (stat(path, &st_buf) < 0) {
    int err = errno;
    if (err == ENOENT) {
      if (mode == 0) {
        return;
      }
      printf("expect_type(%s): stat failed unexpectedly: %s\n", path,
             strerror(err));
      exit(1);
    }
  }
  if (mode == 0) {
    printf("expect_type(%s): we expected the file to be gone, but it "
           "existed.\n",
           path);
    exit(1);
  }
  if (!(st_buf.st_mode & mode)) {
    printf("expect_type(%s): the file existed, but it had mode 0%4o, "
           "which didn't have bit 0%4o\n",
           path, st_buf.st_mode, mode);
    exit(1);
  }
}


x86 assembly (clang, -O0):
<expect_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0x0,%rax
jge    11c7 <expect_type+0x87>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    0x2e54(%rip),%rcx
cmp    (%rcx),%eax
jne    11c2 <expect_type+0x82>
cmpl   $0x0,-0xc(%rbp)
jne    1193 <expect_type+0x53>
jmp    121e <expect_type+0xde>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1060 <strerror@plt>
mov    -0x20(%rbp),%rsi
mov    %rax,%rdx
lea    0xe4f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
jmp    11c7 <expect_type+0x87>
cmpl   $0x0,-0xc(%rbp)
jne    11ed <expect_type+0xad>
mov    -0x8(%rbp),%rsi
lea    0xe53(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%eax
and    -0xc(%rbp),%eax
cmp    $0x0,%eax
jne    121e <expect_type+0xde>
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%edx
mov    -0xc(%rbp),%ecx
lea    0xe65(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expect_type>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rsi
call   1040 <stat@plt>
test   %rax,%rax
jns    1190 <expect_type+0x50>
mov    0x2e84(%rip),%rax
mov    (%rax),%edi
mov    0x2e6b(%rip),%rax
cmp    (%rax),%edi
jne    1190 <expect_type+0x50>
test   %ebp,%ebp
je     119b <expect_type+0x5b>
call   1060 <strerror@plt>
lea    0xe87(%rip),%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
test   %ebp,%ebp
je     11a2 <expect_type+0x62>
mov    (%rsp),%edx
test   %ebp,%edx
je     11bd <expect_type+0x7d>
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe86(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
lea    0xeae(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%ecx
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@errno = global i32 0
@ENOENT = global i32 0
@.str = [47 x i8] c"expect_type(%s): stat failed unexpectedly: %s\0A\00"
@.str.1 = [67 x i8] c"expect_type(%s): we expected the file to be gone, but it existed.\0A\00"
@.str.2 = [85 x i8] c"expect_type(%s): the file existed, but it had mode 0%4o, which didn't have bit 0%4o\0A\00"
define void @expect_type(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
%6 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i64 @stat(i8* %7, %struct.stat* %5)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %26
10: ; preds = %2
%11 = load i32, i32* @errno
store i32 %11, i32* %6
%12 = load i32, i32* %6
%13 = load i32, i32* @ENOENT
%14 = icmp eq i32 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %10
%16 = load i32, i32* %4
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %15
br label %46
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = load i32, i32* %6
%22 = call i8* @strerror(i32 %21)
%23 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %20, i8* %22)
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %10
br label %26
26: ; preds = %25, %2
%27 = load i32, i32* %4
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i8*, i8** %3
%31 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i8* %30)
%32 = call i32 @exit(i32 1)
unreachable
33: ; preds = %26
%34 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%35 = load i32, i32* %34
%36 = load i32, i32* %4
%37 = and i32 %35, %36
%38 = icmp ne i32 %37, 0
br i1 %38, label %46, label %39
39: ; preds = %33
%40 = load i8*, i8** %3
%41 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%42 = load i32, i32* %41
%43 = load i32, i32* %4
%44 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.2, i64 0, i64 0), i8* %40, i32 %42, i32 %43)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %18, %33
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@errno = global i32 0
@ENOENT = global i32 0
@.str = [47 x i8] c"expect_type(%s): stat failed unexpectedly: %s\0A\00"
@.str.1 = [67 x i8] c"expect_type(%s): we expected the file to be gone, but it existed.\0A\00"
@.str.2 = [85 x i8] c"expect_type(%s): the file existed, but it had mode 0%4o, which didn't have bit 0%4o\0A\00"
define void @expect_type(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i64 @stat(i8* %0, %struct.stat* %3)
%6 = icmp slt i64 %5, 0
br i1 %6, label %7, label %17
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = load i32, i32* @ENOENT
%10 = icmp eq i32 %8, %9
br i1 %10, label %11, label %17
11: ; preds = %7
%12 = icmp eq i32 %1, 0
br i1 %12, label %30, label %13
13: ; preds = %11
%14 = call i8* @strerror(i32 %8)
%15 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %14)
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %7, %2
%18 = icmp eq i32 %1, 0
br i1 %18, label %19, label %22
19: ; preds = %17
%20 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([67 x i8], [67 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %17
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%24 = load i32, i32* %23
%25 = and i32 %24, %1
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %22
%28 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([85 x i8], [85 x i8]* @.str.2, i64 0, i64 0), i8* %0, i32 %24, i32 %1)
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %11, %22
ret void
}