#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_mode;
  scalar_t__ st_size;
};
typedef scalar_t__ off_t;

/* Variables and functions */
int /*<<< orphan*/ SEEK_CUR;
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ SEEK_SET;
int S_IFBLK;
scalar_t__ S_ISREG(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
scalar_t__ fstat(int, struct stat *);
scalar_t__ lseek(int, scalar_t__, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
off_t filesize(int fd) {
  struct stat stat;
  off_t size = 0;
  if (fstat(fd, &stat) < 0) {
    perror("stat");
    exit(errno);
  }
  do {
    if (stat.st_mode & S_IFBLK) {
      off_t old_pos = lseek(fd, 0, SEEK_CUR);
      if (old_pos < 0) {
        break;
      }
      size = lseek(fd, 0, SEEK_END);
      if (size < 0) {
        break;
      }
      if (lseek(fd, old_pos, SEEK_SET) < 0) {
        break;
      }
    } else if (S_ISREG(stat.st_mode)) {
      size = stat.st_size;
    }
  } while (0);
  return size;
}


x86 assembly (clang, -O0):
<filesize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%rax
jge    1193 <filesize+0x43>
lea    0xe80(%rip),%rdi
call   1050 <perror@plt>
mov    0x2e3c(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>
jmp    1198 <filesize+0x48>
mov    -0x18(%rbp),%eax
mov    0x2e2e(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1228 <filesize+0xd8>
mov    -0x4(%rbp),%edi
mov    0x2e29(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <lseek@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    11d6 <filesize+0x86>
jmp    124c <filesize+0xfc>
mov    -0x4(%rbp),%edi
mov    0x2e08(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1030 <lseek@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11ff <filesize+0xaf>
jmp    124c <filesize+0xfc>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
call   1030 <lseek@plt>
cmp    $0x0,%rax
jge    1223 <filesize+0xd3>
jmp    124c <filesize+0xfc>
jmp    1247 <filesize+0xf7>
mov    -0x18(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%rax
je     1242 <filesize+0xf2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1247 <filesize+0xf7>
jmp    124c <filesize+0xfc>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<filesize>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    %rsp,%rsi
call   1070 <fstat@plt>
test   %rax,%rax
js     11df <filesize+0x8f>
mov    (%rsp),%edi
mov    0x2e5f(%rip),%rax
test   %edi,(%rax)
je     11c0 <filesize+0x70>
mov    0x2e64(%rip),%rax
mov    (%rax),%edx
xor    %ebx,%ebx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
test   %rax,%rax
js     11d3 <filesize+0x83>
mov    %rax,%r14
mov    0x2e50(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
xor    %esi,%esi
call   1030 <lseek@plt>
mov    %rax,%rbx
test   %rax,%rax
js     11d3 <filesize+0x83>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <lseek@plt>
jmp    11d3 <filesize+0x83>
call   1040 <S_ISREG@plt>
test   %rax,%rax
je     11d1 <filesize+0x81>
mov    0x8(%rsp),%rbx
jmp    11d3 <filesize+0x83>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe1a(%rip),%rdi
call   1050 <perror@plt>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   1060 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32, i64 }
@.str = [5 x i8] c"stat\00"
@errno = global i32 0
@S_IFBLK = global i32 0
@SEEK_CUR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @filesize(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i64
%5 = alloca i64
store i32 %0, i32* %2
store i64 0, i64* %4
%6 = load i32, i32* %2
%7 = call i64 @fstat(i32 %6, %struct.stat* %3)
%8 = icmp slt i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%11 = load i32, i32* @errno
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %1
br label %14
14: ; preds = %13
%15 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%16 = load i32, i32* %15
%17 = load i32, i32* @S_IFBLK
%18 = and i32 %16, %17
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %42
20: ; preds = %14
%21 = load i32, i32* %2
%22 = load i32, i32* @SEEK_CUR
%23 = call i64 @lseek(i32 %21, i64 0, i32 %22)
store i64 %23, i64* %5
%24 = load i64, i64* %5
%25 = icmp slt i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %20
br label %52
27: ; preds = %20
%28 = load i32, i32* %2
%29 = load i32, i32* @SEEK_END
%30 = call i64 @lseek(i32 %28, i64 0, i32 %29)
store i64 %30, i64* %4
%31 = load i64, i64* %4
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %27
br label %52
34: ; preds = %27
%35 = load i32, i32* %2
%36 = load i64, i64* %5
%37 = load i32, i32* @SEEK_SET
%38 = call i64 @lseek(i32 %35, i64 %36, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %34
br label %52
41: ; preds = %34
br label %51
42: ; preds = %14
%43 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @S_ISREG(i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%49 = load i64, i64* %48
store i64 %49, i64* %4
br label %50
50: ; preds = %47, %42
br label %51
51: ; preds = %50, %41
br label %52
52: ; preds = %51, %40, %33, %26
%53 = load i64, i64* %4
ret i64 %53
}

LLVM IR (-O2):
%struct.stat = type { i32, i64 }
@.str = [5 x i8] c"stat\00"
@errno = global i32 0
@S_IFBLK = global i32 0
@SEEK_CUR = global i32 0
@SEEK_END = global i32 0
@SEEK_SET = global i32 0
define i64 @filesize(i32 %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @fstat(i32 %0, %struct.stat* %2)
%5 = icmp slt i64 %4, 0
br i1 %5, label %6, label %10
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%8 = load i32, i32* @errno
%9 = tail call i32 @exit(i32 %8)
unreachable
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = load i32, i32* @S_IFBLK
%14 = and i32 %13, %12
%15 = icmp eq i32 %14, 0
br i1 %15, label %27, label %16
16: ; preds = %10
%17 = load i32, i32* @SEEK_CUR
%18 = tail call i64 @lseek(i32 %0, i64 0, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %33, label %20
20: ; preds = %16
%21 = load i32, i32* @SEEK_END
%22 = tail call i64 @lseek(i32 %0, i64 0, i32 %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %20
%25 = load i32, i32* @SEEK_SET
%26 = tail call i64 @lseek(i32 %0, i64 %18, i32 %25)
br label %33
27: ; preds = %10
%28 = tail call i64 @S_ISREG(i32 %12)
%29 = icmp eq i64 %28, 0
br i1 %29, label %33, label %30
30: ; preds = %27
%31 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%32 = load i64, i64* %31
br label %33
33: ; preds = %16, %20, %24, %27, %30
%34 = phi i64 [ %32, %30 ], [ 0, %27 ], [ 0, %16 ], [ %22, %20 ], [ %22, %24 ]
ret i64 %34
}