#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*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fwrite(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ logger(int, char *, char *);
int file_write(char *file_name, char *buf, int buf_len, char *mode) {
  FILE *fp;
  int n_writed;
  fp = fopen(file_name, mode);
  if (NULL == fp) {
    logger(3, "file_write  file open failed filename=%s", file_name);
    return -1;
  }
  n_writed = fwrite(buf, sizeof(unsigned char), buf_len, fp);
  fclose(fp);
  return n_writed;
}


x86 assembly (clang, -O0):
<file_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
xor    %eax,%eax
cmp    -0x30(%rbp),%rax
jne    1195 <file_write+0x55>
mov    -0x10(%rbp),%rdx
mov    $0x3,%edi
lea    0xe7c(%rip),%rsi
call   1040 <logger@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11bc <file_write+0x7c>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%edx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<file_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbx
mov    %rcx,%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     1181 <file_write+0x41>
mov    %rax,%rbp
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
jmp    119a <file_write+0x5a>
lea    0xe78(%rip),%rsi
mov    $0x3,%edi
mov    %rbx,%rdx
call   1040 <logger@plt>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [41 x i8] c"file_write file open failed filename=%s\00"
define i32 @file_write(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%12 = load i8*, i8** %6
%13 = load i8*, i8** %9
%14 = call i32* @fopen(i8* %12, i8* %13)
store i32* %14, i32** %10
%15 = load i32*, i32** %10
%16 = icmp eq i32* null, %15
br i1 %16, label %17, label %20
17: ; preds = %4
%18 = load i8*, i8** %6
%19 = call i32 @logger(i32 3, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %18)
store i32 -1, i32* %5
br label %28
20: ; preds = %4
%21 = load i8*, i8** %7
%22 = load i32, i32* %8
%23 = load i32*, i32** %10
%24 = call i32 @fwrite(i8* %21, i32 1, i32 %22, i32* %23)
store i32 %24, i32* %11
%25 = load i32*, i32** %10
%26 = call i32 @fclose(i32* %25)
%27 = load i32, i32* %11
store i32 %27, i32* %5
br label %28
28: ; preds = %20, %17
%29 = load i32, i32* %5
ret i32 %29
}

LLVM IR (-O2):
@.str = [41 x i8] c"file_write file open failed filename=%s\00"
define i32 @file_write(i8* %0, i8* nocapture %1, i32 %2, i8* nocapture %3) {
%5 = tail call i32* @fopen(i8* %0, i8* %3)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %9
7: ; preds = %4
%8 = tail call i32 @logger(i32 3, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %12
9: ; preds = %4
%10 = tail call i32 @fwrite(i8* %1, i32 1, i32 %2, i32* %5)
%11 = tail call i32 @fclose(i32* %5)
br label %12
12: ; preds = %9, %7
%13 = phi i32 [ -1, %7 ], [ %10, %9 ]
ret i32 %13
}