#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;
};
typedef int ssize_t;

/* Variables and functions */
int EINVAL;
int errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int stat(char const *, struct stat *);
int /*<<< orphan*/ stderr;
ssize_t size_file(const char *filename) {
  struct stat st;
  int status = 0;
  ssize_t size = -1;
  if (filename == NULL) {
    errno = EINVAL;
    fprintf(stderr, "Error (size_file): invalid args\n");
  } else {
    status = stat(filename, &st);
    if (status != 0) {
      fprintf(stderr, "Error: unable to stat '%s' - (err %d)\n", filename,
              errno);
    } else {
      size = st.st_size;
    }
  }
  return size;
}


x86 assembly (clang, -O0):
<size_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1173 <size_file+0x53>
mov    0x2e84(%rip),%rax
mov    (%rax),%ecx
mov    0x2e83(%rip),%rax
mov    %ecx,(%rax)
mov    0x2e82(%rip),%rax
mov    (%rax),%edi
lea    0xe99(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    11c1 <size_file+0xa1>
mov    -0x8(%rbp),%rdi
lea    -0x10(%rbp),%rsi
call   1040 <stat@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
je     11b6 <size_file+0x96>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
mov    0x2e37(%rip),%rax
mov    (%rax),%ecx
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1030 <fprintf@plt>
jmp    11bc <size_file+0x9c>
mov    -0x10(%rbp),%eax
mov    %eax,-0x18(%rbp)
jmp    11c1 <size_file+0xa1>
mov    -0x18(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<size_file>:
push   %rbx
sub    $0x10,%rsp
test   %rdi,%rdi
je     1165 <size_file+0x45>
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1040 <stat@plt>
test   %eax,%eax
je     1195 <size_file+0x75>
mov    0x2e9e(%rip),%rax
mov    (%rax),%edi
mov    0x2e8d(%rip),%rax
mov    (%rax),%ecx
lea    0xecd(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1199 <size_file+0x79>
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    0x2e63(%rip),%rcx
mov    %eax,(%rcx)
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    0xe79(%rip),%rsi
xor    %eax,%eax
call   1030 <fprintf@plt>
mov    $0xffffffff,%eax
jmp    1199 <size_file+0x79>
mov    0x8(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Error (size_file): invalid args\0A\00"
@.str.1 = [39 x i8] c"Error: unable to stat '%s' - (err %d)\0A\00"
define i32 @size_file(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.stat
%4 = alloca i32
%5 = alloca i32
store i8* %0, i8** %2
store i32 0, i32* %4
store i32 -1, i32* %5
%6 = load i8*, i8** %2
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @EINVAL
store i32 %9, i32* @errno
%10 = load i32, i32* @stderr
%11 = call i32 (i32, i8*, ...) @fprintf(i32 %10, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %26
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = call i32 @stat(i8* %13, %struct.stat* %3)
store i32 %14, i32* %4
%15 = load i32, i32* %4
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %12
%18 = load i32, i32* @stderr
%19 = load i8*, i8** %2
%20 = load i32, i32* @errno
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %18, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %19, i32 %20)
br label %25
22: ; preds = %12
%23 = getelementptr inbounds %struct.stat, %struct.stat* %3, i32 0, i32 0
%24 = load i32, i32* %23
store i32 %24, i32* %5
br label %25
25: ; preds = %22, %17
br label %26
26: ; preds = %25, %8
%27 = load i32, i32* %5
ret i32 %27
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@stderr = global i32 0
@.str = [33 x i8] c"Error (size_file): invalid args\0A\00"
@.str.1 = [39 x i8] c"Error: unable to stat '%s' - (err %d)\0A\00"
define i32 @size_file(i8* %0) {
%2 = alloca %struct.stat
%3 = bitcast %struct.stat* %2 to i8*
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %9
5: ; preds = %1
%6 = load i32, i32* @EINVAL
store i32 %6, i32* @errno
%7 = load i32, i32* @stderr
%8 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str, i64 0, i64 0))
br label %19
9: ; preds = %1
%10 = call i32 @stat(i8* %0, %struct.stat* %2)
%11 = icmp eq i32 %10, 0
br i1 %11, label %16, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %13, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %14)
br label %19
16: ; preds = %9
%17 = getelementptr inbounds %struct.stat, %struct.stat* %2, i64 0, i32 0
%18 = load i32, i32* %17
br label %19
19: ; preds = %12, %16, %5
%20 = phi i32 [ -1, %5 ], [ -1, %12 ], [ %18, %16 ]
ret i32 %20
}