#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 {
  size_t st_size;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ err(int, char *, char *);
int fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int opensize(char *name, size_t *size) {
  struct stat s;
  int fd = open(name, O_RDONLY);
  if (fd < 0) {
    err(1, "cannot open \"%s\"", name);
  }
  if (fstat(fd, &s) == -1) {
    err(1, "cannot stat \"%s\"", name);
  }
  *size = s.st_size;
  return fd;
}


x86 assembly (clang, -O0):
<opensize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e9d(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1174 <opensize+0x44>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe91(%rip),%rsi
call   1030 <err@plt>
mov    -0x1c(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
jne    119e <opensize+0x6e>
mov    -0x8(%rbp),%rdx
mov    $0x1,%edi
lea    0xe78(%rip),%rsi
call   1030 <err@plt>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x1c(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opensize>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
mov    0x2ea3(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,%ebp
test   %eax,%eax
jns    1166 <opensize+0x36>
lea    0xea7(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1030 <err@plt>
lea    0x8(%rsp),%rsi
mov    %ebp,%edi
call   1050 <fstat@plt>
cmp    $0xffffffff,%eax
jne    118b <opensize+0x5b>
lea    0xe93(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
call   1030 <err@plt>
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [17 x i8] c"cannot open \22%s\22\00"
@.str.1 = [17 x i8] c"cannot stat \22%s\22\00"
define i32 @opensize(i8* %0, i64* %1) {
%3 = alloca i8*
%4 = alloca i64*
%5 = alloca %struct.stat
%6 = alloca i32
store i8* %0, i8** %3
store i64* %1, i64** %4
%7 = load i8*, i8** %3
%8 = load i32, i32* @O_RDONLY
%9 = call i32 @open(i8* %7, i32 %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = load i8*, i8** %3
%14 = call i32 @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %13)
br label %15
15: ; preds = %12, %2
%16 = load i32, i32* %6
%17 = call i32 @fstat(i32 %16, %struct.stat* %5)
%18 = icmp eq i32 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i8*, i8** %3
%21 = call i32 @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %20)
br label %22
22: ; preds = %19, %15
%23 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%24 = load i64, i64* %23
%25 = load i64*, i64** %4
store i64 %24, i64* %25
%26 = load i32, i32* %6
ret i32 %26
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@O_RDONLY = global i32 0
@.str = [17 x i8] c"cannot open \22%s\22\00"
@.str.1 = [17 x i8] c"cannot stat \22%s\22\00"
define i32 @opensize(i8* %0, i64* nocapture writeonly %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @O_RDONLY
%6 = tail call i32 @open(i8* %0, i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %10
8: ; preds = %2
%9 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %10
10: ; preds = %8, %2
%11 = call i32 @fstat(i32 %6, %struct.stat* %3)
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = tail call i32 @err(i32 1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %15
15: ; preds = %13, %10
%16 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%17 = load i64, i64* %16
store i64 %17, i64* %1
ret i32 %6
}