#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 {
  scalar_t__ st_size;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
scalar_t__ EBADF;
scalar_t__ errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fstat(int, struct stat *);
int /*<<< orphan*/ printf(char *, ...);
int main(int argc, char **argv) {
  struct stat st;
  FILE *file;
  int fd;
  if (2 != argc) {
    printf("Usage: sel_ldr test_fstat.nexe test_stat_data\n");
    return 1;
  }
  file = fopen(argv[1], "r");
  if (file == NULL)
    return 2;
  fd = fileno(file);
  errno = 0;
  if (fstat(fd, &st))
    return 3;
  printf("%d\n", (int)st.st_size);
  if (errno != 0)
    return 4;
  if (fclose(file))
    return 5;
  errno = 0;
  if (fstat(-1, &st) != -1)
    return 6;
  if (errno != EBADF)
    return 7;
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x2,%eax
cmp    -0x8(%rbp),%eax
je     118e <main+0x3e>
lea    0xe85(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12a6 <main+0x156>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11bd <main+0x6d>
movl   $0x2,-0x4(%rbp)
jmp    12a6 <main+0x156>
mov    -0x20(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,-0x24(%rbp)
mov    0x2e10(%rip),%rax
movq   $0x0,(%rax)
mov    -0x24(%rbp),%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0x0,%eax
je     11f8 <main+0xa8>
movl   $0x3,-0x4(%rbp)
jmp    12a6 <main+0x156>
mov    -0x18(%rbp),%rax
mov    %eax,%esi
lea    0xe2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2dcd(%rip),%rax
cmpq   $0x0,(%rax)
je     1229 <main+0xd9>
movl   $0x4,-0x4(%rbp)
jmp    12a6 <main+0x156>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
cmp    $0x0,%rax
je     1248 <main+0xf8>
movl   $0x5,-0x4(%rbp)
jmp    12a6 <main+0x156>
mov    0x2d91(%rip),%rax
movq   $0x0,(%rax)
mov    $0xffffffff,%edi
lea    -0x18(%rbp),%rsi
call   1070 <fstat@plt>
cmp    $0xffffffff,%eax
je     1279 <main+0x129>
movl   $0x6,-0x4(%rbp)
jmp    12a6 <main+0x156>
mov    0x2d60(%rip),%rax
mov    (%rax),%rax
mov    0x2d4e(%rip),%rcx
cmp    (%rcx),%rax
je     129f <main+0x14f>
movl   $0x7,-0x4(%rbp)
jmp    12a6 <main+0x156>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
cmp    $0x2,%edi
jne    1222 <main+0xc2>
mov    0x8(%rsi),%rdi
lea    0xe88(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     1235 <main+0xd5>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <fileno@plt>
mov    0x2e48(%rip),%r14
movq   $0x0,(%r14)
mov    %rsp,%rsi
mov    %eax,%edi
call   1080 <fstat@plt>
mov    %eax,%ecx
mov    $0x3,%eax
test   %ecx,%ecx
jne    123a <main+0xda>
mov    (%rsp),%esi
lea    0xe40(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0x4,%eax
cmpq   $0x0,(%r14)
jne    123a <main+0xda>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %rax,%rcx
mov    $0x5,%eax
test   %rcx,%rcx
jne    123a <main+0xda>
movq   $0x0,(%r14)
mov    %rsp,%rsi
mov    $0xffffffff,%edi
call   1080 <fstat@plt>
mov    %eax,%ecx
mov    $0x6,%eax
cmp    $0xffffffff,%ecx
jne    123a <main+0xda>
mov    (%r14),%rax
mov    0x2dc5(%rip),%rcx
xor    %edx,%edx
cmp    (%rcx),%rax
mov    $0x7,%eax
cmove  %edx,%eax
jmp    123a <main+0xda>
lea    0xddd(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    123a <main+0xda>
mov    $0x2,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.stat = type { i64 }
@.str = [47 x i8] c"Usage: sel_ldr test_fstat.nexe test_stat_data\0A\00"
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@.str.2 = [4 x i8] c"%d\0A\00"
@EBADF = global i64 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.stat
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 2, %9
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %51
13: ; preds = %2
%14 = load i8**, i8*** %5
%15 = getelementptr inbounds i8*, i8** %14, i64 1
%16 = load i8*, i8** %15
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %17, i32** %7
%18 = load i32*, i32** %7
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %21
20: ; preds = %13
store i32 2, i32* %3
br label %51
21: ; preds = %13
%22 = load i32*, i32** %7
%23 = call i32 @fileno(i32* %22)
store i32 %23, i32* %8
store i64 0, i64* @errno
%24 = load i32, i32* %8
%25 = call i32 @fstat(i32 %24, %struct.stat* %6)
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %21
store i32 3, i32* %3
br label %51
28: ; preds = %21
%29 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%30 = load i64, i64* %29
%31 = trunc i64 %30 to i32
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %31)
%33 = load i64, i64* @errno
%34 = icmp ne i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %28
store i32 4, i32* %3
br label %51
36: ; preds = %28
%37 = load i32*, i32** %7
%38 = call i64 @fclose(i32* %37)
%39 = icmp ne i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %36
store i32 5, i32* %3
br label %51
41: ; preds = %36
store i64 0, i64* @errno
%42 = call i32 @fstat(i32 -1, %struct.stat* %6)
%43 = icmp ne i32 %42, -1
br i1 %43, label %44, label %45
44: ; preds = %41
store i32 6, i32* %3
br label %51
45: ; preds = %41
%46 = load i64, i64* @errno
%47 = load i64, i64* @EBADF
%48 = icmp ne i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45
store i32 7, i32* %3
br label %51
50: ; preds = %45
store i32 0, i32* %3
br label %51
51: ; preds = %50, %49, %44, %40, %35, %27, %20, %11
%52 = load i32, i32* %3
ret i32 %52
}

LLVM IR (-O2):
%struct.stat = type { i64 }
@.str.1 = [2 x i8] c"r\00"
@errno = global i64 0
@.str.2 = [4 x i8] c"%d\0A\00"
@EBADF = global i64 0
@str = [46 x i8] c"Usage: sel_ldr test_fstat.nexe test_stat_data\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %0, 2
br i1 %5, label %8, label %6
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([46 x i8], [46 x i8]* @str, i64 0, i64 0))
br label %35
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = tail call i32* @fopen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i32* %11, null
br i1 %12, label %35, label %13
13: ; preds = %8
%14 = tail call i32 @fileno(i32* %11)
store i64 0, i64* @errno
%15 = call i32 @fstat(i32 %14, %struct.stat* %3)
%16 = icmp eq i32 %15, 0
br i1 %16, label %17, label %35
17: ; preds = %13
%18 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%19 = load i64, i64* %18
%20 = trunc i64 %19 to i32
%21 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %20)
%22 = load i64, i64* @errno
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %35
24: ; preds = %17
%25 = tail call i64 @fclose(i32* %11)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %24
store i64 0, i64* @errno
%28 = call i32 @fstat(i32 -1, %struct.stat* %3)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %35
30: ; preds = %27
%31 = load i64, i64* @errno
%32 = load i64, i64* @EBADF
%33 = icmp eq i64 %31, %32
%34 = select i1 %33, i32 0, i32 7
br label %35
35: ; preds = %30, %27, %24, %17, %13, %8, %6
%36 = phi i32 [ 1, %6 ], [ 2, %8 ], [ 3, %13 ], [ 4, %17 ], [ 5, %24 ], [ 6, %27 ], [ %34, %30 ]
ret i32 %36
}