#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_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ ENOENT;
scalar_t__ S_ISDIR(int /*<<< orphan*/);
scalar_t__ errno;
int stat(char *, struct stat *);
int file_size(char *filename) {
  struct stat filestat;
  if (stat(filename, &filestat) == -1 && errno == ENOENT) {
    return -1;
  }
  if (S_ISDIR(filestat.st_mode)) {
    return -1;
  }
  return filestat.st_size;
}


x86 assembly (clang, -O0):
<file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    1168 <file_size+0x48>
mov    0x2e97(%rip),%rax
mov    (%rax),%rax
mov    0x2e7d(%rip),%rcx
cmp    (%rcx),%rax
jne    1168 <file_size+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    118c <file_size+0x6c>
mov    -0x14(%rbp),%edi
call   1030 <S_ISDIR@plt>
cmp    $0x0,%rax
je     1186 <file_size+0x66>
movl   $0xffffffff,-0x4(%rbp)
jmp    118c <file_size+0x6c>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_size>:
push   %rax
mov    %rsp,%rsi
call   1040 <stat@plt>
cmp    $0xffffffff,%eax
jne    1149 <file_size+0x29>
mov    0x2eab(%rip),%rax
mov    (%rax),%rcx
mov    0x2e91(%rip),%rdx
mov    $0xffffffff,%eax
cmp    (%rdx),%rcx
je     115f <file_size+0x3f>
mov    0x4(%rsp),%edi
call   1030 <S_ISDIR@plt>
mov    %rax,%rcx
mov    $0xffffffff,%eax
test   %rcx,%rcx
je     1161 <file_size+0x41>
pop    %rcx
ret
mov    (%rsp),%eax
pop    %rcx
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
@errno = global i64 0
@ENOENT = global i64 0
define i32 @file_size(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 %13
8: ; preds = %1
%9 = load i64, i64* @errno
%10 = load i64, i64* @ENOENT
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %13
12: ; preds = %8
store i32 -1, i32* %2
br label %22
13: ; preds = %8, %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i64 @S_ISDIR(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %2
br label %22
19: ; preds = %13
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%21 = load i32, i32* %20
store i32 %21, i32* %2
br label %22
22: ; preds = %19, %18, %12
%23 = load i32, i32* %2
ret i32 %23
}

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