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

/* Variables and functions */
int /*<<< orphan*/ S_ISREG(int /*<<< orphan*/);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int fstat(int /*<<< orphan*/, struct stat *);
int file_size(FILE *f) {
  struct stat buf;
  if (fstat(fileno(f), &buf) == -1) {
    return -1;
  } else {
    if (!S_ISREG(buf.st_mode))
      return -1;
    else
      return buf.st_size;
  }
}


x86 assembly (clang, -O0):
<file_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <fileno@plt>
mov    %eax,%edi
lea    -0x18(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
jne    1165 <file_size+0x35>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <file_size+0x58>
mov    -0x14(%rbp),%edi
call   1040 <S_ISREG@plt>
cmp    $0x0,%eax
jne    1182 <file_size+0x52>
movl   $0xffffffff,-0x4(%rbp)
jmp    1188 <file_size+0x58>
mov    -0x18(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_size>:
push   %rbx
sub    $0x10,%rsp
call   1030 <fileno@plt>
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1050 <fstat@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     1161 <file_size+0x31>
mov    0xc(%rsp),%edi
call   1040 <S_ISREG@plt>
test   %eax,%eax
je     1161 <file_size+0x31>
mov    0x8(%rsp),%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32, i32 }
define i32 @file_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 i32 @fileno(i32* %5)
%7 = call i32 @fstat(i32 %6, %struct.stat* %4)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %1
store i32 -1, i32* %2
br label %19
10: ; preds = %1
%11 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 1
%12 = load i32, i32* %11
%13 = call i32 @S_ISREG(i32 %12)
%14 = icmp ne i32 %13, 0
br i1 %14, label %16, label %15
15: ; preds = %10
store i32 -1, i32* %2
br label %19
16: ; preds = %10
%17 = getelementptr inbounds %struct.stat, %struct.stat* %4, i32 0, i32 0
%18 = load i32, i32* %17
store i32 %18, i32* %2
br label %19
19: ; preds = %16, %15, %9
%20 = load i32, i32* %2
ret i32 %20
}

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
define i32 @file_size(i32* nocapture %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = tail call i32 @fileno(i32* %0)
%5 = call i32 @fstat(i32 %4, %struct.stat* %2)
%6 = icmp eq i32 %5, -1
br i1 %6, label %15, label %7
7: ; preds = %1
%8 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 1
%9 = load i32, i32* %8
%10 = tail call i32 @S_ISREG(i32 %9)
%11 = icmp eq i32 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %7
%13 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%14 = load i32, i32* %13
br label %15
15: ; preds = %7, %1, %12
%16 = phi i32 [ %14, %12 ], [ -1, %1 ], [ -1, %7 ]
ret i32 %16
}