#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*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int fseek(int /*<<< orphan*/ *, long, int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
FILE *secure_open(const char *filename) {
  FILE *fr = fopen(filename, "rw+");
  if (fr == NULL) {
    fr = fopen(filename, "w+");
    if (fr == NULL) {
      fprintf(stderr, "File %s not open!/n", filename);
      return NULL;
    }
  };
  if (fseek(fr, 0L, SEEK_END) != -1) {
    printf("File size:%ld\n", ftell(fr));
  } else {
    fprintf(stderr, "File error!/n");
    return NULL;
  }
  return fr;
}


x86 assembly (clang, -O0):
<secure_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe99(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c7 <secure_open+0x77>
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11c2 <secure_open+0x72>
mov    0x2e37(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe56(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    122e <secure_open+0xde>
jmp    11c7 <secure_open+0x77>
mov    -0x18(%rbp),%rdi
mov    0x2e16(%rip),%rax
mov    (%rax),%edx
xor    %eax,%eax
mov    %eax,%esi
call   1060 <fseek@plt>
cmp    $0xffffffff,%eax
je     1202 <secure_open+0xb2>
mov    -0x18(%rbp),%rdi
call   1050 <ftell@plt>
mov    %eax,%esi
lea    0xe20(%rip),%rdi
call   1030 <printf@plt>
jmp    1226 <secure_open+0xd6>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    122e <secure_open+0xde>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<secure_open>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xea2(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1182 <secure_open+0x32>
lea    0xe8f(%rip),%rsi
mov    %r14,%rdi
call   1070 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11cd <secure_open+0x7d>
mov    0x2e5f(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1060 <fseek@plt>
cmp    $0xffffffff,%eax
je     11b2 <secure_open+0x62>
mov    %rbx,%rdi
call   1050 <ftell@plt>
lea    0xe6f(%rip),%rdi
mov    %eax,%esi
call   1030 <printf@plt>
jmp    11e9 <secure_open+0x99>
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
xor    %ebx,%ebx
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    11e9 <secure_open+0x99>
mov    0x2e04(%rip),%rax
mov    (%rax),%edi
lea    0xe27(%rip),%rsi
xor    %ebx,%ebx
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [4 x i8] c"rw+\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"File %s not open!/n\00"
@SEEK_END = global i32 0
@.str.3 = [15 x i8] c"File size:%ld\0A\00"
@.str.4 = [14 x i8] c"File error!/n\00"
define i32* @secure_open(i8* %0) {
%2 = alloca i32*
%3 = alloca i8*
%4 = alloca i32*
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store i32* %6, i32** %4
%7 = load i32*, i32** %4
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %19
9: ; preds = %1
%10 = load i8*, i8** %3
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %11, i32** %4
%12 = load i32*, i32** %4
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = load i8*, i8** %3
%17 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %16)
store i32* null, i32** %2
br label %33
18: ; preds = %9
br label %19
19: ; preds = %18, %1
%20 = load i32*, i32** %4
%21 = load i32, i32* @SEEK_END
%22 = call i32 @fseek(i32* %20, i64 0, i32 %21)
%23 = icmp ne i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32*, i32** %4
%26 = call i32 @ftell(i32* %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %26)
br label %31
28: ; preds = %19
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
store i32* null, i32** %2
br label %33
31: ; preds = %24
%32 = load i32*, i32** %4
store i32* %32, i32** %2
br label %33
33: ; preds = %31, %28, %14
%34 = load i32*, i32** %2
ret i32* %34
}

LLVM IR (-O2):
@.str = [4 x i8] c"rw+\00"
@.str.1 = [3 x i8] c"w+\00"
@stderr = global i32 0
@.str.2 = [20 x i8] c"File %s not open!/n\00"
@SEEK_END = global i32 0
@.str.3 = [15 x i8] c"File size:%ld\0A\00"
@.str.4 = [14 x i8] c"File error!/n\00"
define noalias i32* @secure_open(i8* %0) {
%2 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%3 = icmp eq i32* %2, null
br i1 %3, label %4, label %10
4: ; preds = %1
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0)
br label %21
10: ; preds = %4, %1
%11 = phi i32* [ %5, %4 ], [ %2, %1 ]
%12 = load i32, i32* @SEEK_END
%13 = tail call i32 @fseek(i32* %11, i64 0, i32 %12)
%14 = icmp eq i32 %13, -1
br i1 %14, label %18, label %15
15: ; preds = %10
%16 = tail call i32 @ftell(i32* %11)
%17 = tail call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %16)
br label %21
18: ; preds = %10
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
br label %21
21: ; preds = %15, %18, %7
%22 = phi i32* [ null, %7 ], [ %11, %15 ], [ null, %18 ]
ret i32* %22
}