#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;
};
struct dirent {
  int dummy;
};
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
scalar_t__ errno;
int stat(char *, struct stat *);
int exists(char *fname, int t) {
  struct dirent *e;
  DIR *d;
  char cmd[200];
  struct stat s;
  int err = stat(fname, &s);
  if (err == -1) {
    if (errno == ENOENT) {
    }
  } else {
    if (t == 2) {
      if (S_ISDIR(s.st_mode)) {
        return 1;
      }
    }
    if (t == 1) {
      if (S_ISREG(s.st_mode)) {
        return 1;
      }
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0xf8(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0xfc(%rbp)
cmpl   $0xffffffff,-0xfc(%rbp)
jne    1189 <exists+0x59>
mov    0x2e74(%rip),%rax
mov    (%rax),%rax
mov    0x2e5a(%rip),%rcx
cmp    (%rcx),%rax
jne    1184 <exists+0x54>
jmp    1184 <exists+0x54>
jmp    11ee <exists+0xbe>
cmpl   $0x2,-0x14(%rbp)
jne    11b9 <exists+0x89>
mov    -0xf8(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     11b4 <exists+0x84>
movl   $0x1,-0x4(%rbp)
jmp    11f5 <exists+0xc5>
jmp    11b9 <exists+0x89>
cmpl   $0x1,-0x14(%rbp)
jne    11e9 <exists+0xb9>
mov    -0xf8(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     11e4 <exists+0xb4>
movl   $0x1,-0x4(%rbp)
jmp    11f5 <exists+0xc5>
jmp    11e9 <exists+0xb9>
jmp    11ee <exists+0xbe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x100,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exists>:
push   %rbx
sub    $0x10,%rsp
mov    %esi,%ebx
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
je     1171 <exists+0x41>
cmp    $0x1,%ebx
je     115b <exists+0x2b>
cmp    $0x2,%ebx
jne    1171 <exists+0x41>
mov    0x8(%rsp),%edi
call   1030 <S_ISDIR@plt>
jmp    1164 <exists+0x34>
mov    0x8(%rsp),%edi
call   1050 <S_ISREG@plt>
mov    %rax,%rcx
mov    $0x1,%eax
test   %rcx,%rcx
jne    1173 <exists+0x43>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
%struct.stat = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @exists(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.dirent*
%7 = alloca i32*
%8 = alloca [200 x i8]
%9 = alloca %struct.stat
%10 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%11 = load i8*, i8** %4
%12 = call i32 @stat(i8* %11, %struct.stat* %9)
store i32 %12, i32* %10
%13 = load i32, i32* %10
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = load i64, i64* @errno
%17 = load i64, i64* @ENOENT
%18 = icmp eq i64 %16, %17
br i1 %18, label %19, label %20
19: ; preds = %15
br label %20
20: ; preds = %19, %15
br label %42
21: ; preds = %2
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 2
br i1 %23, label %24, label %31
24: ; preds = %21
%25 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%26 = load i32, i32* %25
%27 = call i64 @S_ISDIR(i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %24
store i32 1, i32* %3
br label %43
30: ; preds = %24
br label %31
31: ; preds = %30, %21
%32 = load i32, i32* %5
%33 = icmp eq i32 %32, 1
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = getelementptr inbounds %struct.stat, %struct.stat* %9, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @S_ISREG(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 1, i32* %3
br label %43
40: ; preds = %34
br label %41
41: ; preds = %40, %31
br label %42
42: ; preds = %41, %20
store i32 0, i32* %3
br label %43
43: ; preds = %42, %39, %29
%44 = load i32, i32* %3
ret i32 %44
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.dirent = type { i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @exists(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = call i32 @stat(i8* %0, %struct.stat* %3)
%6 = icmp eq i32 %5, -1
br i1 %6, label %18, label %7
7: ; preds = %2
switch i32 %1, label %18 [
i32 2, label %8
i32 1, label %13
]
8: ; preds = %7
%9 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%10 = load i32, i32* %9
%11 = tail call i64 @S_ISDIR(i32 %10)
%12 = icmp eq i64 %11, 0
br i1 %12, label %18, label %19
13: ; preds = %7
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%15 = load i32, i32* %14
%16 = tail call i64 @S_ISREG(i32 %15)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %8, %7, %2, %13
br label %19
19: ; preds = %13, %8, %18
%20 = phi i32 [ 0, %18 ], [ 1, %8 ], [ 1, %13 ]
ret i32 %20
}