#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;
};

/* Variables and functions */
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fstat(int, struct stat *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, char *, ...);
void open_file(char *fn, int *fd, int *sz) {
  struct stat buf;
  *fd = open(fn, O_RDONLY);
  if (fd < 0) {
    printf("cannot open %s\n", fn);
    exit(1);
  }
  fstat(*fd, &buf);
  if (buf.st_size <= 10 * 1024) {
    printf("Is this a right file %s, size = %ld\n", fn, buf.st_size);
    exit(1);
  }
  *sz = (int)buf.st_size;
}


x86 assembly (clang, -O0):
<open_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
mov    0x2e89(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
cmpq   $0x0,-0x10(%rbp)
jae    1195 <open_file+0x55>
mov    -0x8(%rbp),%rsi
lea    0xe7c(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%edi
lea    -0x20(%rbp),%rsi
call   1060 <fstat@plt>
cmpl   $0x2800,-0x20(%rbp)
jg     11d0 <open_file+0x90>
mov    -0x8(%rbp),%rsi
mov    -0x20(%rbp),%edx
lea    0xe51(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>
mov    -0x20(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_file>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    0x2e8f(%rip),%rax
mov    (%rax),%esi
call   1040 <open@plt>
mov    %eax,(%rbx)
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1060 <fstat@plt>
mov    0x8(%rsp),%edx
cmp    $0x2800,%edx
jle    1187 <open_file+0x47>
mov    %edx,(%r15)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xe72(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
mov    $0x1,%edi
call   1050 <exit@plt>

LLVM IR (-O0):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str = [16 x i8] c"cannot open %s\0A\00"
@.str.1 = [37 x i8] c"Is this a right file %s, size = %ld\0A\00"
define void @open_file(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca %struct.stat
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
%8 = load i8*, i8** %4
%9 = load i32, i32* @O_RDONLY
%10 = call i32 @open(i8* %8, i32 %9)
%11 = load i32*, i32** %5
store i32 %10, i32* %11
%12 = load i32*, i32** %5
%13 = icmp ult i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = load i8*, i8** %4
%16 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %15)
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %3
%19 = load i32*, i32** %5
%20 = load i32, i32* %19
%21 = call i32 @fstat(i32 %20, %struct.stat* %7)
%22 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%23 = load i32, i32* %22
%24 = icmp sle i32 %23, 10240
br i1 %24, label %25, label %31
25: ; preds = %18
%26 = load i8*, i8** %4
%27 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%28 = load i32, i32* %27
%29 = call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %26, i32 %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %18
%32 = getelementptr inbounds %struct.stat, %struct.stat* %7, i32 0, i32 0
%33 = load i32, i32* %32
%34 = load i32*, i32** %6
store i32 %33, i32* %34
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@O_RDONLY = global i32 0
@.str.1 = [37 x i8] c"Is this a right file %s, size = %ld\0A\00"
define void @open_file(i8* %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca %struct.stat
%5 = bitcast %struct.stat* %4 to i8*
%6 = load i32, i32* @O_RDONLY
%7 = tail call i32 @open(i8* %0, i32 %6)
store i32 %7, i32* %1
%8 = call i32 @fstat(i32 %7, %struct.stat* %4)
%9 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%10 = load i32, i32* %9
%11 = icmp slt i32 %10, 10241
br i1 %11, label %12, label %15
12: ; preds = %3
%13 = tail call i32 (i8*, i8*, ...) @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %10)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %3
store i32 %10, i32* %2
ret void
}