#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 */
#define S_IFDIR 129
int S_IFMT;
#define S_IFREG 128
scalar_t__ stat(char *, struct stat *);
int jnb_io_exists(char *filename) {
  struct stat s;
  if (stat(filename, &s))
    return -1;
  switch (s.st_mode & S_IFMT) {
  case S_IFREG:
    return 1;
  case S_IFDIR:
    return 2;
  default:
    return 3;
  }
}


x86 assembly (clang, -O0):
<jnb_io_exists>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0x0,%rax
je     113f <jnb_io_exists+0x2f>
movl   $0xffffffff,-0x4(%rbp)
jmp    1192 <jnb_io_exists+0x82>
mov    -0x18(%rbp),%eax
mov    0x2e97(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x1c(%rbp)
sub    $0x80,%eax
je     1173 <jnb_io_exists+0x63>
jmp    1160 <jnb_io_exists+0x50>
mov    -0x1c(%rbp),%eax
sub    $0x81,%eax
je     117f <jnb_io_exists+0x6f>
jmp    118b <jnb_io_exists+0x7b>
movl   $0x1,-0x4(%rbp)
jmp    1192 <jnb_io_exists+0x82>
movl   $0x2,-0x4(%rbp)
jmp    1192 <jnb_io_exists+0x82>
movl   $0x3,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<jnb_io_exists>:
push   %rax
mov    %rsp,%rsi
call   1030 <stat@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     1128 <jnb_io_exists+0x18>
pop    %rcx
ret
mov    0x2eb1(%rip),%rax
mov    (%rax),%eax
and    (%rsp),%eax
xor    %ecx,%ecx
cmp    $0x81,%eax
sete   %cl
xor    $0x3,%ecx
cmp    $0x80,%eax
mov    $0x1,%eax
cmovne %ecx,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
define i32 @jnb_io_exists(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i64 @stat(i8* %5, %struct.stat* %4)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %17
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%11 = load i32, i32* %10
%12 = load i32, i32* @S_IFMT
%13 = and i32 %11, %12
switch i32 %13, label %16 [
i32 128, label %14
i32 129, label %15
]
14: ; preds = %9
store i32 1, i32* %2
br label %17
15: ; preds = %9
store i32 2, i32* %2
br label %17
16: ; preds = %9
store i32 3, i32* %2
br label %17
17: ; preds = %16, %15, %14, %8
%18 = load i32, i32* %2
ret i32 %18
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
define i32 @jnb_io_exists(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %15
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = load i32, i32* @S_IFMT
%10 = and i32 %9, %8
%11 = icmp eq i32 %10, 129
%12 = select i1 %11, i32 2, i32 3
%13 = icmp eq i32 %10, 128
%14 = select i1 %13, i32 1, i32 %12
br label %15
15: ; preds = %6, %1
%16 = phi i32 [ -1, %1 ], [ %14, %6 ]
ret i32 %16
}