#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 */
typedef int /*<<< orphan*/ uint64_t;
struct stat {
  int /*<<< orphan*/ st_size;
  int /*<<< orphan*/ st_mode;
};

/* Variables and functions */
scalar_t__ S_ISBLK(int /*<<< orphan*/);
scalar_t__ S_ISCHR(int /*<<< orphan*/);
scalar_t__ S_ISLNK(int /*<<< orphan*/);
scalar_t__ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ dev_get_size(int);
scalar_t__ fstat(int, struct stat *);
uint64_t file_get_size(int fd) {
  struct stat st;
  if (fstat(fd, &st) != 0) {
    return 0;
  }
  if (S_ISLNK(st.st_mode)) {
    return 0;
  }
  if (S_ISBLK(st.st_mode) || S_ISCHR(st.st_mode)) {
    return dev_get_size(fd);
  } else if (S_ISREG(st.st_mode)) {
    return st.st_size;
  }
  return 0;
}


x86 assembly (clang, -O0):
<file_get_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x10(%rbp),%rsi
call   1080 <fstat@plt>
cmp    $0x0,%rax
je     118d <file_get_size+0x2d>
movl   $0x0,-0x4(%rbp)
jmp    1208 <file_get_size+0xa8>
mov    -0xc(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     11ab <file_get_size+0x4b>
movl   $0x0,-0x4(%rbp)
jmp    1208 <file_get_size+0xa8>
mov    -0xc(%rbp),%edi
call   1070 <S_ISBLK@plt>
cmp    $0x0,%rax
jne    11cf <file_get_size+0x6f>
mov    -0xc(%rbp),%edi
call   1060 <S_ISCHR@plt>
cmp    $0x0,%rax
je     11df <file_get_size+0x7f>
mov    -0x8(%rbp),%edi
call   1040 <dev_get_size@plt>
mov    %eax,-0x4(%rbp)
jmp    1208 <file_get_size+0xa8>
mov    -0xc(%rbp),%edi
call   1050 <S_ISREG@plt>
cmp    $0x0,%rax
je     11fc <file_get_size+0x9c>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1208 <file_get_size+0xa8>
jmp    1201 <file_get_size+0xa1>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_get_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
lea    0x8(%rsp),%rsi
call   1080 <fstat@plt>
xor    %ebx,%ebx
test   %rax,%rax
jne    118d <file_get_size+0x2d>
mov    0xc(%rsp),%r14d
mov    %r14d,%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
je     1198 <file_get_size+0x38>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14d,%edi
call   1070 <S_ISBLK@plt>
test   %rax,%rax
jne    11b2 <file_get_size+0x52>
mov    %r14d,%edi
call   1060 <S_ISCHR@plt>
test   %rax,%rax
je     11c1 <file_get_size+0x61>
mov    %ebp,%edi
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
jmp    1040 <dev_get_size@plt>
mov    %r14d,%edi
call   1050 <S_ISREG@plt>
test   %rax,%rax
je     118d <file_get_size+0x2d>
mov    0x8(%rsp),%ebx
jmp    118d <file_get_size+0x2d>

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
define i32 @file_get_size(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.stat
store i32 %0, i32* %3
%5 = load i32, i32* %3
%6 = call i64 @fstat(i32 %5, %struct.stat* %4)
%7 = icmp ne i64 %6, 0
br i1 %7, label %8, label %9
8: ; preds = %1
store i32 0, i32* %2
br label %38
9: ; preds = %1
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%11 = load i32, i32* %10
%12 = call i64 @S_ISLNK(i32 %11)
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %15
14: ; preds = %9
store i32 0, i32* %2
br label %38
15: ; preds = %9
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%17 = load i32, i32* %16
%18 = call i64 @S_ISBLK(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %25, label %20
20: ; preds = %15
%21 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%22 = load i32, i32* %21
%23 = call i64 @S_ISCHR(i32 %22)
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %20, %15
%26 = load i32, i32* %3
%27 = call i32 @dev_get_size(i32 %26)
store i32 %27, i32* %2
br label %38
28: ; preds = %20
%29 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%30 = load i32, i32* %29
%31 = call i64 @S_ISREG(i32 %30)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %28
%34 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%35 = load i32, i32* %34
store i32 %35, i32* %2
br label %38
36: ; preds = %28
br label %37
37: ; preds = %36
store i32 0, i32* %2
br label %38
38: ; preds = %37, %33, %25, %14, %8
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
define i32 @file_get_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 %6, label %25
6: ; preds = %1
%7 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%8 = load i32, i32* %7
%9 = tail call i64 @S_ISLNK(i32 %8)
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %25
11: ; preds = %6
%12 = tail call i64 @S_ISBLK(i32 %8)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %11
%15 = tail call i64 @S_ISCHR(i32 %8)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %14, %11
%18 = tail call i32 @dev_get_size(i32 %0)
br label %25
19: ; preds = %14
%20 = tail call i64 @S_ISREG(i32 %8)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %19
%23 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%24 = load i32, i32* %23
br label %25
25: ; preds = %19, %6, %1, %22, %17
%26 = phi i32 [ %18, %17 ], [ %24, %22 ], [ 0, %1 ], [ 0, %6 ], [ 0, %19 ]
ret i32 %26
}