#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 /*<<< orphan*/, int /*<<< orphan*/ *);
int /*<<< orphan*/ strlen(char *);
int write_file_text(char *data, char *file) {
  int r;
  FILE *fp;
  if ((fp = fopen(file, "a")) == NULL)
    return 0;
  r = fwrite(data, 1, strlen(data), fp);
  fclose(fp);
  return r;
}


x86 assembly (clang, -O0):
<write_file_text>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea5(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    117a <write_file_text+0x3a>
movl   $0x0,-0x4(%rbp)
jmp    11b1 <write_file_text+0x71>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    %eax,%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_file_text>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xeaf(%rip),%rsi
mov    %rax,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     1189 <write_file_text+0x49>
mov    %rax,%rbx
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %r14,%rdi
mov    $0x1,%esi
mov    %eax,%edx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
jmp    118b <write_file_text+0x4b>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"a\00"
define i32 @write_file_text(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
store i8* %0, i8** %4
store i8* %1, i8** %5
%8 = load i8*, i8** %5
%9 = call i32* @fopen(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %9, i32** %7
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %21
12: ; preds = %2
%13 = load i8*, i8** %4
%14 = load i8*, i8** %4
%15 = call i32 @strlen(i8* %14)
%16 = load i32*, i32** %7
%17 = call i32 @fwrite(i8* %13, i32 1, i32 %15, i32* %16)
store i32 %17, i32* %6
%18 = load i32*, i32** %7
%19 = call i32 @fclose(i32* %18)
%20 = load i32, i32* %6
store i32 %20, i32* %3
br label %21
21: ; preds = %12, %11
%22 = load i32, i32* %3
ret i32 %22
}

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