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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
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: %ld\n", err < 0 ? (long)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    $0x30,%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    115d <get_filesize+0x3d>
movslq -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    1165 <get_filesize+0x45>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x28(%rbp),%rdx
lea    0xe8d(%rip),%rsi
call   1030 <fprintf@plt>
cmpl   $0x0,-0x10(%rbp)
jge    118f <get_filesize+0x6f>
movslq -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1197 <get_filesize+0x77>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_filesize>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1040 <fstat@plt>
mov    0x2eaa(%rip),%rcx
mov    (%rcx),%edi
test   %eax,%eax
js     1143 <get_filesize+0x23>
mov    0x8(%rsp),%rbx
jmp    1146 <get_filesize+0x26>
movslq %eax,%rbx
lea    0xeb3(%rip),%rsi
mov    %rbx,%rdx
call   1030 <fprintf@plt>
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
ret

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

LLVM IR (-O2):
%struct.stat = type { i64 }
@stderr = global i32 0
@.str = [15 x i8] c"filesize: %ld\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 = sext i32 %4 to i64
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%9 = load i64, i64* %8
%10 = select i1 %6, i64 %7, i64 %9
%11 = tail call i32 @fprintf(i32 %5, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i64 %10)
ret i64 %10
}