#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_IFBLK 134
#define S_IFCHR 133
#define S_IFDIR 132
#define S_IFIFO 131
#define S_IFLNK 130
int S_IFMT;
#define S_IFREG 129
#define S_IFSOCK 128
int stat(char *, struct stat *);
int file_stats(char *filename) {
  struct stat sb;
  if (stat(filename, &sb) == -1) {
    return -1;
  }
  switch (sb.st_mode & S_IFMT) {
  case S_IFBLK:
    return 0;
    break;
  case S_IFCHR:
    return 0;
    break;
  case S_IFDIR:
    return 1;
    break;
  case S_IFIFO:
    return 0;
    break;
  case S_IFLNK:
    return 2;
    break;
  case S_IFREG:
    return 3;
    break;
  case S_IFSOCK:
    return 0;
    break;
  default:
    return 0;
    break;
  }
}


x86 assembly (clang, -O0):
<file_stats>:
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    $0xffffffff,%eax
jne    113e <file_stats+0x2e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cd <file_stats+0xbd>
mov    -0x18(%rbp),%eax
mov    0x2e98(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
add    $0xffffff80,%eax
mov    %eax,%ecx
mov    %rcx,-0x20(%rbp)
sub    $0x6,%eax
ja     11c6 <file_stats+0xb6>
mov    -0x20(%rbp),%rax
lea    0xe97(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x0,-0x4(%rbp)
jmp    11cd <file_stats+0xbd>
movl   $0x0,-0x4(%rbp)
jmp    11cd <file_stats+0xbd>
movl   $0x1,-0x4(%rbp)
jmp    11cd <file_stats+0xbd>
movl   $0x0,-0x4(%rbp)
jmp    11cd <file_stats+0xbd>
movl   $0x2,-0x4(%rbp)
jmp    11cd <file_stats+0xbd>
movl   $0x3,-0x4(%rbp)
jmp    11cd <file_stats+0xbd>
movl   $0x0,-0x4(%rbp)
jmp    11cd <file_stats+0xbd>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_stats>:
push   %rax
mov    %rsp,%rsi
call   1030 <stat@plt>
cmp    $0xffffffff,%eax
je     1146 <file_stats+0x36>
mov    0x2ebb(%rip),%rax
mov    (%rax),%ecx
and    (%rsp),%ecx
add    $0xffffff7f,%ecx
xor    %eax,%eax
cmp    $0x3,%ecx
ja     1144 <file_stats+0x34>
movslq %ecx,%rax
lea    0xebf(%rip),%rcx
mov    (%rcx,%rax,4),%eax
pop    %rcx
ret
mov    $0xffffffff,%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
define i32 @file_stats(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32 @stat(i8* %5, %struct.stat* %4)
%7 = icmp eq i32 %6, -1
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 -1, i32* %2
br label %22
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 %21 [
i32 134, label %14
i32 133, label %15
i32 132, label %16
i32 131, label %17
i32 130, label %18
i32 129, label %19
i32 128, label %20
]
14: ; preds = %9
store i32 0, i32* %2
br label %22
15: ; preds = %9
store i32 0, i32* %2
br label %22
16: ; preds = %9
store i32 1, i32* %2
br label %22
17: ; preds = %9
store i32 0, i32* %2
br label %22
18: ; preds = %9
store i32 2, i32* %2
br label %22
19: ; preds = %9
store i32 3, i32* %2
br label %22
20: ; preds = %9
store i32 0, i32* %2
br label %22
21: ; preds = %9
store i32 0, i32* %2
br label %22
22: ; preds = %21, %20, %19, %18, %17, %16, %15, %14, %8
%23 = load i32, i32* %2
ret i32 %23
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@S_IFMT = global i32 0
@switch.table.file_stats = [4 x i32] [i32 3, i32 2, i32 0, i32 1]
define i32 @file_stats(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i32 %4, -1
br i1 %5, label %17, label %6
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 = add i32 %10, -129
%12 = icmp ult i32 %11, 4
br i1 %12, label %13, label %17
13: ; preds = %6
%14 = sext i32 %11 to i64
%15 = getelementptr inbounds [4 x i32], [4 x i32]* @switch.table.file_stats, i64 0, i64 %14
%16 = load i32, i32* %15
br label %17
17: ; preds = %13, %6, %1
%18 = phi i32 [ -1, %1 ], [ 0, %6 ], [ %16, %13 ]
ret i32 %18
}