#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*/ *);
char *fm_abs(char *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ free(char *);
int fwrite(char const *, long, int, int /*<<< orphan*/ *);
int fm_write(char *filename, const char *buf, long size) {
  FILE *fh;
  char *fa = NULL;
  int rv;
  fa = fm_abs(filename);
  if (!(fh = fopen(fa, "w")))
    return (0);
  rv = fwrite(buf, size, 1, fh);
  fclose(fh);
  do {
    if ((fa) != NULL) {
      free(fa);
      (fa) = NULL;
    }
  } while (0);
  return (rv);
}


x86 assembly (clang, -O0):
<fm_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <fm_abs@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe7c(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11a3 <fm_write+0x53>
movl   $0x0,-0x4(%rbp)
jmp    11ec <fm_write+0x9c>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1070 <fwrite@plt>
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
cmpq   $0x0,-0x30(%rbp)
je     11e1 <fm_write+0x91>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x30(%rbp)
jmp    11e6 <fm_write+0x96>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fm_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
call   1040 <fm_abs@plt>
mov    %rax,%rbx
lea    0xe94(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     11a9 <fm_write+0x59>
mov    %rax,%rbp
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
mov    %eax,%r14d
mov    %rbp,%rdi
call   1050 <fclose@plt>
test   %rbx,%rbx
je     11ac <fm_write+0x5c>
mov    %rbx,%rdi
call   1030 <free@plt>
jmp    11ac <fm_write+0x5c>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
define i32 @fm_write(i8* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i32*
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8* null, i8** %9
%11 = load i8*, i8** %5
%12 = call i8* @fm_abs(i8* %11)
store i8* %12, i8** %9
%13 = load i8*, i8** %9
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %17, label %16
16: ; preds = %3
store i32 0, i32* %4
br label %33
17: ; preds = %3
%18 = load i8*, i8** %6
%19 = load i64, i64* %7
%20 = load i32*, i32** %8
%21 = call i32 @fwrite(i8* %18, i64 %19, i32 1, i32* %20)
store i32 %21, i32* %10
%22 = load i32*, i32** %8
%23 = call i32 @fclose(i32* %22)
br label %24
24: ; preds = %17
%25 = load i8*, i8** %9
%26 = icmp ne i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %24
%28 = load i8*, i8** %9
%29 = call i32 @free(i8* %28)
store i8* null, i8** %9
br label %30
30: ; preds = %27, %24
br label %31
31: ; preds = %30
%32 = load i32, i32* %10
store i32 %32, i32* %4
br label %33
33: ; preds = %31, %16
%34 = load i32, i32* %4
ret i32 %34
}

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