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

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ close(int);
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int getFileSize(char *filename) {
  int fd;
  struct stat StatBuf;
  if ((fd = open(filename, O_RDONLY, 0666)) == -1) {
    perror("getFileSize");
  }
  if (fstat(fd, &StatBuf) == -1) {
    close(fd);
    return -1;
  }
  close(fd);
  return StatBuf.st_size;
}


x86 assembly (clang, -O0):
<getFileSize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    0x2e91(%rip),%rax
mov    (%rax),%esi
mov    $0x1b6,%edx
call   1040 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    117b <getFileSize+0x3b>
lea    0xe8a(%rip),%rdi
call   1050 <perror@plt>
mov    -0x14(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1060 <fstat@plt>
cmp    $0xffffffff,%eax
jne    11a4 <getFileSize+0x64>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b2 <getFileSize+0x72>
mov    -0x14(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getFileSize>:
push   %rbp
push   %rbx
push   %rax
mov    0x2e9e(%rip),%rax
mov    (%rax),%esi
mov    $0x1b6,%edx
call   1040 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
je     1181 <getFileSize+0x41>
mov    %rsp,%rsi
mov    %ebx,%edi
call   1060 <fstat@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   1030 <close@plt>
cmp    $0xffffffff,%ebp
je     1178 <getFileSize+0x38>
mov    (%rsp),%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe78(%rip),%rdi
call   1050 <perror@plt>
jmp    115d <getFileSize+0x1d>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [12 x i8] c"getFileSize\00"
define i32 @getFileSize(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.stat
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = load i32, i32* @O_RDONLY
%8 = call i32 @open(i8* %6, i32 %7, i32 438)
store i32 %8, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %10, %1
%13 = load i32, i32* %4
%14 = call i32 @fstat(i32 %13, %struct.stat* %5)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %12
%17 = load i32, i32* %4
%18 = call i32 @close(i32 %17)
store i32 -1, i32* %2
br label %24
19: ; preds = %12
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%23 = load i32, i32* %22
store i32 %23, i32* %2
br label %24
24: ; preds = %19, %16
%25 = load i32, i32* %2
ret i32 %25
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [12 x i8] c"getFileSize\00"
define i32 @getFileSize(i8* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = load i32, i32* @O_RDONLY
%5 = tail call i32 @open(i8* %0, i32 %4, i32 438)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %1
%8 = tail call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %9
9: ; preds = %7, %1
%10 = call i32 @fstat(i32 %5, %struct.stat* %2)
%11 = icmp eq i32 %10, -1
%12 = tail call i32 @close(i32 %5)
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%14 = load i32, i32* %13
%15 = select i1 %11, i32 -1, i32 %14
ret i32 %15
}