#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 const *, char *);
size_t fwrite(char const *, size_t, int, int /*<<< orphan*/ *);
int write_content_to_file(const char *filename, const char *buffer,
                          size_t len) {
  if (filename == NULL || buffer == NULL || len == 0) {
    return -1;
  }
  FILE *file = fopen(filename, "w");
  if (!file) {
    return -1;
  }
  size_t ret = fwrite(buffer, len, 1, file);
  if (ret != 1) {
    fclose(file);
    return -1;
  }
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<write_content_to_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1165 <write_content_to_file+0x35>
cmpq   $0x0,-0x18(%rbp)
je     1165 <write_content_to_file+0x35>
cmpq   $0x0,-0x20(%rbp)
jne    1171 <write_content_to_file+0x41>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <write_content_to_file+0xb6>
mov    -0x10(%rbp),%rdi
lea    0xe84(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119c <write_content_to_file+0x6c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <write_content_to_file+0xb6>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fwrite@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x1,-0x30(%rbp)
je     11d6 <write_content_to_file+0xa6>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <write_content_to_file+0xb6>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_content_to_file>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0xffffffff,%ebx
test   %rdi,%rdi
je     118c <write_content_to_file+0x5c>
mov    %rsi,%r15
test   %rsi,%rsi
je     118c <write_content_to_file+0x5c>
mov    %rdx,%r14
test   %rdx,%rdx
je     118c <write_content_to_file+0x5c>
lea    0xea7(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     118c <write_content_to_file+0x5c>
mov    %rax,%r12
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <fwrite@plt>
xor    %ebx,%ebx
cmp    $0x1,%rax
setne  %bl
mov    %r12,%rdi
call   1030 <fclose@plt>
neg    %ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

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