#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*/ fprintf(int /*<<< orphan*/, char *, int);
int fstat(int, struct stat *);
int /*<<< orphan*/ stderr;
size_t get_filesize(int fd) {
  int err;
  struct stat stat;
  err = fstat(fd, &stat);
  fprintf(stderr, "filesize: %d\n", err < 0 ? err : stat.st_size);
  if (err < 0)
    return err;
  return (size_t)stat.st_size;
}


x86 assembly (clang, -O0):
<get_filesize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1040 <fstat@plt>
mov    %eax,-0x10(%rbp)
mov    0x2e9f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x10(%rbp)
jge    115b <get_filesize+0x3b>
mov    -0x10(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    1161 <get_filesize+0x41>
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x20(%rbp),%edx
lea    0xe92(%rip),%rsi
call   1030 <fprintf@plt>
cmpl   $0x0,-0x10(%rbp)
jge    118a <get_filesize+0x6a>
movslq -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1192 <get_filesize+0x72>
movslq -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filesize>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1040 <fstat@plt>
mov    %eax,%ebx
mov    0x2ea8(%rip),%rax
mov    (%rax),%edi
test   %ebx,%ebx
js     1142 <get_filesize+0x22>
mov    0x8(%rsp),%ebx
lea    0xeb7(%rip),%rsi
mov    %ebx,%edx
call   1030 <fprintf@plt>
movslq %ebx,%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [14 x i8] c"filesize: %d\0A\00"
define i64 @get_filesize(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.stat
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = call i32 @fstat(i32 %6, %struct.stat* %5)
store i32 %7, i32* %4
%8 = load i32, i32* @stderr
%9 = load i32, i32* %4
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i32, i32* %4
br label %16
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%15 = load i32, i32* %14
br label %16
16: ; preds = %13, %11
%17 = phi i32 [ %12, %11 ], [ %15, %13 ]
%18 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i32, i32* %4
%23 = sext i32 %22 to i64
store i64 %23, i64* %2
br label %28
24: ; preds = %16
%25 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%26 = load i32, i32* %25
%27 = sext i32 %26 to i64
store i64 %27, i64* %2
br label %28
28: ; preds = %24, %21
%29 = load i64, i64* %2
ret i64 %29
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@stderr = global i32 0
@.str = [14 x i8] c"filesize: %d\0A\00"
define i64 @get_filesize(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i32 @fstat(i32 %0, %struct.stat* %2)
%5 = load i32, i32* @stderr
%6 = icmp slt i32 %4, 0
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%8 = load i32, i32* %7
%9 = select i1 %6, i32 %4, i32 %8
%10 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = sext i32 %9 to i64
ret i64 %11
}