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

/* Variables and functions */
int /*<<< orphan*/ SEEK_END;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fseek(int /*<<< orphan*/ *, int /*<<< orphan*/, int /*<<< orphan*/);
long ftell(int /*<<< orphan*/ *);
FILE *vi_openAtEnd(char *filename, long *len) {
  FILE *fp = NULL;
  if ((fp = fopen(filename, "rb")) == NULL)
    goto err;
  if ((fseek(fp, 0, SEEK_END)) == -1)
    goto err;
  if ((*len = ftell(fp)) == -1)
    goto err;
  return fp;
err:
  if (fp != NULL)
    fclose(fp);
  return NULL;
}


x86 assembly (clang, -O0):
<vi_openAtEnd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe9d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    117b <vi_openAtEnd+0x3b>
jmp    11c9 <vi_openAtEnd+0x89>
mov    -0x20(%rbp),%rdi
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   1050 <fseek@plt>
cmp    $0xffffffff,%eax
jne    119d <vi_openAtEnd+0x5d>
jmp    11c9 <vi_openAtEnd+0x89>
mov    -0x20(%rbp),%rdi
call   1040 <ftell@plt>
mov    -0x18(%rbp),%rcx
mov    %rax,(%rcx)
cmp    $0xffffffffffffffff,%rax
jne    11bc <vi_openAtEnd+0x7c>
jmp    11c9 <vi_openAtEnd+0x89>
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11e5 <vi_openAtEnd+0xa5>
cmpq   $0x0,-0x20(%rbp)
je     11dd <vi_openAtEnd+0x9d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vi_openAtEnd>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%r14
lea    0xeb2(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     118c <vi_openAtEnd+0x4c>
mov    %rax,%rbx
mov    0x2e86(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1050 <fseek@plt>
cmp    $0xffffffff,%eax
je     1184 <vi_openAtEnd+0x44>
mov    %rbx,%rdi
call   1040 <ftell@plt>
mov    %rax,(%r14)
cmp    $0xffffffffffffffff,%rax
jne    118e <vi_openAtEnd+0x4e>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32* @vi_openAtEnd(i8* %0, i64* %1) {
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca i32*
store i8* %0, i8** %4
store i64* %1, i64** %5
store i32* null, i32** %6
%7 = load i8*, i8** %4
%8 = call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %8, i32** %6
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %11
10: ; preds = %2
br label %25
11: ; preds = %2
%12 = load i32*, i32** %6
%13 = load i32, i32* @SEEK_END
%14 = call i32 @fseek(i32* %12, i32 0, i32 %13)
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %11
br label %25
17: ; preds = %11
%18 = load i32*, i32** %6
%19 = call i64 @ftell(i32* %18)
%20 = load i64*, i64** %5
store i64 %19, i64* %20
%21 = icmp eq i64 %19, -1
br i1 %21, label %22, label %23
22: ; preds = %17
br label %25
23: ; preds = %17
%24 = load i32*, i32** %6
store i32* %24, i32** %3
br label %32
25: ; preds = %22, %16, %10
%26 = load i32*, i32** %6
%27 = icmp ne i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %25
%29 = load i32*, i32** %6
%30 = call i32 @fclose(i32* %29)
br label %31
31: ; preds = %28, %25
store i32* null, i32** %3
br label %32
32: ; preds = %31, %23
%33 = load i32*, i32** %3
ret i32* %33
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@SEEK_END = global i32 0
define i32* @vi_openAtEnd(i8* nocapture %0, i64* nocapture writeonly %1) {
%3 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %14, label %5
5: ; preds = %2
%6 = load i32, i32* @SEEK_END
%7 = tail call i32 @fseek(i32* %3, i32 0, i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %12, label %9
9: ; preds = %5
%10 = tail call i64 @ftell(i32* %3)
store i64 %10, i64* %1
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %5, %9
%13 = tail call i32 @fclose(i32* %3)
br label %14
14: ; preds = %2, %12, %9
%15 = phi i32* [ %3, %9 ], [ null, %2 ], [ null, %12 ]
ret i32* %15
}