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

/* Variables and functions */
int EOF;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ printf(char *);
int optima_fopen(char *dst, int32_t dst_len, char *file_name) {
  FILE *fp = fopen(file_name, "rt");
  if (fp == NULL) {
    printf("optimaize err: optima_fopen(), file_name\n");
    exit(EXIT_FAILURE);
  }
  int i;
  for (i = 0; i < dst_len; i++) {
    int c = fgetc(fp);
    if (c == EOF)
      break;
    *dst++ = c;
  }
  fclose(fp);
  *dst = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<optima_fopen>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    119c <optima_fopen+0x4c>
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f6 <optima_fopen+0xa6>
mov    -0x20(%rbp),%rdi
call   1050 <fgetc@plt>
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%eax
jne    11d2 <optima_fopen+0x82>
jmp    11f6 <optima_fopen+0xa6>
mov    -0x28(%rbp),%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a3 <optima_fopen+0x53>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x8(%rbp),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optima_fopen>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe9d(%rip),%rsi
mov    %rdx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11b0 <optima_fopen+0x60>
mov    %rax,%r14
test   %ebp,%ebp
jle    1198 <optima_fopen+0x48>
mov    0x2e6a(%rip),%r15
xchg   %ax,%ax
mov    %r14,%rdi
call   1050 <fgetc@plt>
cmp    (%r15),%eax
je     1198 <optima_fopen+0x48>
mov    %al,(%rbx)
add    $0x1,%rbx
add    $0xffffffff,%ebp
jne    1180 <optima_fopen+0x30>
mov    %r14,%rdi
call   1040 <fclose@plt>
movb   $0x0,(%rbx)
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4c(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rt\00"
@.str.1 = [42 x i8] c"optimaize err: optima_fopen(), file_name\0A\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
define i32 @optima_fopen(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%10 = load i8*, i8** %6
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %3
%15 = call i32 @printf(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
%16 = load i32, i32* @EXIT_FAILURE
%17 = call i32 @exit(i32 %16)
unreachable
18: ; preds = %3
store i32 0, i32* %8
br label %19
19: ; preds = %35, %18
%20 = load i32, i32* %8
%21 = load i32, i32* %5
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %38
23: ; preds = %19
%24 = load i32*, i32** %7
%25 = call i32 @fgetc(i32* %24)
store i32 %25, i32* %9
%26 = load i32, i32* %9
%27 = load i32, i32* @EOF
%28 = icmp eq i32 %26, %27
br i1 %28, label %29, label %30
29: ; preds = %23
br label %38
30: ; preds = %23
%31 = load i32, i32* %9
%32 = trunc i32 %31 to i8
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %4
store i8 %32, i8* %33
br label %35
35: ; preds = %30
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %19
38: ; preds = %29, %19
%39 = load i32*, i32** %7
%40 = call i32 @fclose(i32* %39)
%41 = load i8*, i8** %4
store i8 0, i8* %41
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"rt\00"
@EXIT_FAILURE = global i32 0
@EOF = global i32 0
@str = [41 x i8] c"optimaize err: optima_fopen(), file_name\00"
define i32 @optima_fopen(i8* nocapture writeonly %0, i32 %1, i8* nocapture %2) {
%4 = tail call i32* @fopen(i8* %2, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = icmp sgt i32 %1, 0
br i1 %7, label %12, label %23
8: ; preds = %3
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([41 x i8], [41 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %6, %18
%13 = phi i8* [ %20, %18 ], [ %0, %6 ]
%14 = phi i32 [ %21, %18 ], [ 0, %6 ]
%15 = tail call i32 @fgetc(i32* %4)
%16 = load i32, i32* @EOF
%17 = icmp eq i32 %15, %16
br i1 %17, label %23, label %18
18: ; preds = %12
%19 = trunc i32 %15 to i8
%20 = getelementptr inbounds i8, i8* %13, i64 1
store i8 %19, i8* %13
%21 = add nuw nsw i32 %14, 1
%22 = icmp eq i32 %21, %1
br i1 %22, label %23, label %12
23: ; preds = %18, %12, %6
%24 = phi i8* [ %0, %6 ], [ %13, %12 ], [ %20, %18 ]
%25 = tail call i32 @fclose(i32* %4)
store i8 0, i8* %24
ret i32 0
}