#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*/ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
scalar_t__ fstat(int, struct stat *);
int /*<<< orphan*/ stdout;
char *strerror(int /*<<< orphan*/);
unsigned long file_size(int fd) {
  struct stat statbuff;
  int filesize = -1;
  if (fstat(fd, &statbuff)) {
    fprintf(stdout, "fstat error: %s\r\n", strerror(errno));
    return filesize;
  }
  filesize = statbuff.st_size;
  return filesize;
}


x86 assembly (clang, -O0):
<file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movl   $0xffffffff,-0x14(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0x0,%rax
je     1191 <file_size+0x61>
mov    0x2e79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
mov    -0x18(%rbp),%edi
mov    %rax,%rdx
lea    0xe81(%rip),%rsi
call   1030 <fprintf@plt>
movslq -0x14(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    119f <file_size+0x6f>
mov    -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
movslq -0x14(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_size>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1050 <fstat@plt>
test   %rax,%rax
je     1175 <file_size+0x45>
mov    0x2e8d(%rip),%rax
mov    (%rax),%ebx
mov    0x2e8c(%rip),%rax
mov    (%rax),%edi
call   1040 <strerror@plt>
lea    0xe9e(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
call   1030 <fprintf@plt>
mov    $0xffffffffffffffff,%rax
jmp    117a <file_size+0x4a>
movslq 0x8(%rsp),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stdout = global i32 0
@.str = [18 x i8] c"fstat error: %s\0D\0A\00"
@errno = global i32 0
define i64 @file_size(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca %struct.stat
%5 = alloca i32
store i32 %0, i32* %3
store i32 -1, i32* %5
%6 = load i32, i32* %3
%7 = call i64 @fstat(i32 %6, %struct.stat* %4)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %16
9: ; preds = %1
%10 = load i32, i32* @stdout
%11 = load i32, i32* @errno
%12 = call i8* @strerror(i32 %11)
%13 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = load i32, i32* %5
%15 = sext i32 %14 to i64
store i64 %15, i64* %2
br label %21
16: ; preds = %1
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
store i64 %20, i64* %2
br label %21
21: ; preds = %16, %9
%22 = load i64, i64* %2
ret i64 %22
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stdout = global i32 0
@.str = [18 x i8] c"fstat error: %s\0D\0A\00"
@errno = global i32 0
define i64 @file_size(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @fstat(i32 %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
br i1 %5, label %11, label %6
6: ; preds = %1
%7 = load i32, i32* @stdout
%8 = load i32, i32* @errno
%9 = tail call i8* @strerror(i32 %8)
%10 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %9)
br label %15
11: ; preds = %1
%12 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%13 = load i32, i32* %12
%14 = sext i32 %13 to i64
br label %15
15: ; preds = %11, %6
%16 = phi i64 [ -1, %6 ], [ %14, %11 ]
ret i64 %16
}