#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 /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fputc(int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int optima_fwrite(char *src, int32_t src_len, char *file_name) {
  FILE *fp = fopen(file_name, "wt");
  if (fp == NULL) {
    printf("optimaize err: optima_fwrite(), file_name\n");
    exit(EXIT_FAILURE);
  }
  int i;
  for (i = 0; i < src_len; i++) {
    int c = *src++;
    if (c == '\0')
      break;
    fputc(c, fp);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<optima_fwrite>:
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_fwrite+0x4c>
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11ed <optima_fwrite+0x9d>
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x8(%rbp)
movsbl (%rax),%eax
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
jne    11d3 <optima_fwrite+0x83>
jmp    11ed <optima_fwrite+0x9d>
mov    -0x28(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1050 <fputc@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a3 <optima_fwrite+0x53>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optima_fwrite>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xe9d(%rip),%rsi
mov    %rdx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11af <optima_fwrite+0x5f>
mov    %rax,%rbx
test   %ebp,%ebp
jle    119a <optima_fwrite+0x4a>
mov    %ebp,%r15d
xor    %ebp,%ebp
nopl   0x0(%rax)
movsbl (%r14,%rbp,1),%edi
test   %edi,%edi
je     119a <optima_fwrite+0x4a>
mov    %rbx,%rsi
call   1050 <fputc@plt>
add    $0x1,%rbp
cmp    %ebp,%r15d
jne    1180 <optima_fwrite+0x30>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe4d(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wt\00"
@.str.1 = [43 x i8] c"optimaize err: optima_fwrite(), file_name\0A\00"
@EXIT_FAILURE = global i32 0
define i32 @optima_fwrite(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 ([43 x i8], [43 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 i8*, i8** %4
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %4
%26 = load i8, i8* %24
%27 = sext i8 %26 to i32
store i32 %27, i32* %9
%28 = load i32, i32* %9
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %23
br label %38
31: ; preds = %23
%32 = load i32, i32* %9
%33 = load i32*, i32** %7
%34 = call i32 @fputc(i32 %32, i32* %33)
br label %35
35: ; preds = %31
%36 = load i32, i32* %8
%37 = add nsw i32 %36, 1
store i32 %37, i32* %8
br label %19
38: ; preds = %30, %19
%39 = load i32*, i32** %7
%40 = call i32 @fclose(i32* %39)
ret i32 0
}

LLVM IR (-O2):
@.str = [3 x i8] c"wt\00"
@EXIT_FAILURE = global i32 0
@str = [42 x i8] c"optimaize err: optima_fwrite(), file_name\00"
define i32 @optima_fwrite(i8* nocapture %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 ([42 x i8], [42 x i8]* @str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = tail call i32 @exit(i32 %10)
unreachable
12: ; preds = %6, %17
%13 = phi i8* [ %18, %17 ], [ %0, %6 ]
%14 = phi i32 [ %21, %17 ], [ 0, %6 ]
%15 = load i8, i8* %13
%16 = icmp eq i8 %15, 0
br i1 %16, label %23, label %17
17: ; preds = %12
%18 = getelementptr inbounds i8, i8* %13, i64 1
%19 = sext i8 %15 to i32
%20 = tail call i32 @fputc(i32 %19, i32* %4)
%21 = add nuw nsw i32 %14, 1
%22 = icmp eq i32 %21, %1
br i1 %22, label %23, label %12
23: ; preds = %17, %12, %6
%24 = tail call i32 @fclose(i32* %4)
ret i32 0
}