#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 /*<<< orphan*/ st_size;
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ off_t;

/* Variables and functions */
int S_ISREG(int /*<<< orphan*/);
scalar_t__ stat(char const *, struct stat *);
off_t ace_fs_get_file_size(const char *filepath) {
  struct stat sb;
  bool does_file_exist = stat(filepath, &sb) == 0;
  bool is_regular_file = S_ISREG(sb.st_mode);
  if (does_file_exist && is_regular_file) {
    return sb.st_size;
  } else
    return 0;
}


x86 assembly (clang, -O0):
<ace_fs_get_file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <stat@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x14(%rbp),%edi
call   1040 <S_ISREG@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1172 <ace_fs_get_file_size+0x52>
cmpl   $0x0,-0x20(%rbp)
je     1172 <ace_fs_get_file_size+0x52>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1179 <ace_fs_get_file_size+0x59>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ace_fs_get_file_size>:
push   %rbx
sub    $0x10,%rsp
lea    0x8(%rsp),%rsi
call   1030 <stat@plt>
mov    %rax,%rbx
mov    0xc(%rsp),%edi
call   1040 <S_ISREG@plt>
test   %eax,%eax
je     1143 <ace_fs_get_file_size+0x23>
mov    0x8(%rsp),%eax
xor    %ecx,%ecx
test   %rbx,%rbx
cmove  %eax,%ecx
mov    %ecx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
define i32 @ace_fs_get_file_size(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.stat
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = call i64 @stat(i8* %7, %struct.stat* %4)
%9 = icmp eq i64 %8, 0
%10 = zext i1 %9 to i32
store i32 %10, i32* %5
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%12 = load i32, i32* %11
%13 = call i32 @S_ISREG(i32 %12)
store i32 %13, i32* %6
%14 = load i32, i32* %5
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %22
16: ; preds = %1
%17 = load i32, i32* %6
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%21 = load i32, i32* %20
store i32 %21, i32* %2
br label %23
22: ; preds = %16, %1
store i32 0, i32* %2
br label %23
23: ; preds = %22, %19
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
define i32 @ace_fs_get_file_size(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = call i64 @stat(i8* %0, %struct.stat* %2)
%5 = icmp eq i64 %4, 0
%6 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%7 = load i32, i32* %6
%8 = call i32 @S_ISREG(i32 %7)
%9 = icmp ne i32 %8, 0
%10 = select i1 %5, i1 %9, i1 false
%11 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%12 = load i32, i32* %11
%13 = select i1 %10, i32 %12, i32 0
ret i32 %13
}