#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 {
  unsigned long long st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
int /*<<< orphan*/ BLKGETSIZE;
scalar_t__ S_ISBLK(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ fstat(int, struct stat *);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned long *);
int /*<<< orphan*/ stderr;
unsigned long long fsize(int in_fd) {
  struct stat dm_stat;
  unsigned long long size;
  if (fstat(in_fd, &dm_stat)) {
    fprintf(stderr, "Can not fstat\n");
    exit(20);
  }
  if (S_ISBLK(dm_stat.st_mode)) {
    unsigned long ls;
    if (ioctl(in_fd, BLKGETSIZE, &ls)) {
      fprintf(stderr, "Can not ioctl(BLKGETSIZE)\n");
      exit(20);
    }
    size = ((unsigned long long)ls) * 512;
  } else if (S_ISREG(dm_stat.st_mode)) {
    size = dm_stat.st_size;
  } else
    size = -1;
  return size;
}


x86 assembly (clang, -O0):
<fsize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     11a0 <fsize+0x40>
mov    0x2e58(%rip),%rax
mov    (%rax),%edi
lea    0xe6f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x14,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%edi
call   1060 <S_ISBLK@plt>
cmp    $0x0,%rax
je     1201 <fsize+0xa1>
mov    -0x4(%rbp),%edi
mov    0x2e1c(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11f0 <fsize+0x90>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
lea    0xe2e(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x14,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rax
shl    $0x9,%rax
mov    %rax,-0x20(%rbp)
jmp    122d <fsize+0xcd>
mov    -0x10(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     1220 <fsize+0xc0>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1228 <fsize+0xc8>
movq   $0xffffffffffffffff,-0x20(%rbp)
ff
jmp    122d <fsize+0xcd>
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fsize>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
lea    0x8(%rsp),%rsi
call   1080 <fstat@plt>
test   %rax,%rax
jne    11ca <fsize+0x6a>
mov    0x10(%rsp),%ebp
mov    %ebp,%edi
call   1060 <S_ISBLK@plt>
test   %rax,%rax
je     11a9 <fsize+0x49>
mov    0x2e4a(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    11dc <fsize+0x7c>
mov    (%rsp),%rax
shl    $0x9,%rax
jmp    11c3 <fsize+0x63>
mov    %ebp,%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
je     11bc <fsize+0x5c>
mov    0x8(%rsp),%rax
jmp    11c3 <fsize+0x63>
mov    $0xffffffffffffffff,%rax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe26(%rip),%rsi
jmp    11ec <fsize+0x8c>
mov    0x2dfd(%rip),%rax
mov    (%rax),%edi
lea    0xe23(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x14,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
@stderr = global i32 0
@.str = [15 x i8] c"Can not fstat\0A\00"
@BLKGETSIZE = global i32 0
@.str.1 = [27 x i8] c"Can not ioctl(BLKGETSIZE)\0A\00"
define i64 @fsize(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.stat
%4 = alloca i64
%5 = alloca i64
store i32 %0, i32* %2
%6 = load i32, i32* %2
%7 = call i64 @fstat(i32 %6, %struct.stat* %3)
%8 = icmp ne i64 %7, 0
br i1 %8, label %9, label %13
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @exit(i32 20)
unreachable
13: ; preds = %1
%14 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%15 = load i32, i32* %14
%16 = call i64 @S_ISBLK(i32 %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %30
18: ; preds = %13
%19 = load i32, i32* %2
%20 = load i32, i32* @BLKGETSIZE
%21 = call i64 @ioctl(i32 %19, i32 %20, i64* %5)
%22 = icmp ne i64 %21, 0
br i1 %22, label %23, label %27
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 20)
unreachable
27: ; preds = %18
%28 = load i64, i64* %5
%29 = mul i64 %28, 512
store i64 %29, i64* %4
br label %40
30: ; preds = %13
%31 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 1
%32 = load i32, i32* %31
%33 = call i64 @S_ISREG(i32 %32)
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%37 = load i64, i64* %36
store i64 %37, i64* %4
br label %39
38: ; preds = %30
store i64 -1, i64* %4
br label %39
39: ; preds = %38, %35
br label %40
40: ; preds = %39, %27
%41 = load i64, i64* %4
ret i64 %41
}

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