#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;
  int st_size;
};

/* Variables and functions */
int S_IFDIR;
int S_IFMT;
scalar_t__ stat(char const *, struct stat *);
int GetFileLength(const char *filename) {
  struct stat statbuf;
  if (stat(filename, &statbuf) != 0)
    return (0);
  if ((statbuf.st_mode & S_IFMT) == S_IFDIR)
    return (-2);
  return (statbuf.st_size);
}


x86 assembly (clang, -O0):
<GetFileLength>:
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 <GetFileLength+0x2f>
movl   $0x0,-0x4(%rbp)
jmp    116c <GetFileLength+0x5c>
mov    -0x18(%rbp),%eax
mov    0x2e97(%rip),%rcx
and    (%rcx),%eax
mov    0x2e7e(%rip),%rcx
cmp    (%rcx),%eax
jne    1166 <GetFileLength+0x56>
movl   $0xfffffffe,-0x4(%rbp)
jmp    116c <GetFileLength+0x5c>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<GetFileLength>:
push   %rax
mov    %rsp,%rsi
call   1030 <stat@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
je     1125 <GetFileLength+0x15>
pop    %rcx
ret
mov    0x2eb4(%rip),%rax
mov    (%rax),%ecx
and    (%rsp),%ecx
mov    0x2e98(%rip),%rdx
mov    $0xfffffffe,%eax
cmp    (%rdx),%ecx
je     1123 <GetFileLength+0x13>
mov    0x4(%rsp),%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
define i32 @GetFileLength(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 0, i32* %2
br label %20
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
%14 = load i32, i32* @S_IFDIR
%15 = icmp eq i32 %13, %14
br i1 %15, label %16, label %17
16: ; preds = %9
store i32 -2, i32* %2
br label %20
17: ; preds = %9
%18 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%19 = load i32, i32* %18
store i32 %19, i32* %2
br label %20
20: ; preds = %17, %16, %8
%21 = load i32, i32* %2
ret i32 %21
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@S_IFMT = global i32 0
@S_IFDIR = global i32 0
define i32 @GetFileLength(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 %16
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 = load i32, i32* @S_IFDIR
%12 = icmp eq i32 %10, %11
br i1 %12, label %16, label %13
13: ; preds = %6
%14 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%15 = load i32, i32* %14
br label %16
16: ; preds = %6, %1, %13
%17 = phi i32 [ %15, %13 ], [ 0, %1 ], [ -2, %6 ]
ret i32 %17
}