#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 *);
int fwrite(char const *, size_t, int, int /*<<< orphan*/ *);
size_t strlen(char const *);
int save_file(const char *filename, const char *blob, size_t length) {
  FILE *fh;
  int ret;
  if (length == 0) {
    length = strlen(blob);
  }
  fh = fopen(filename, "w");
  if (!fh) {
    return -1;
  }
  ret = fwrite(blob, length, 1, fh);
  if (!ret) {
    fclose(fh);
    return -1;
  }
  fclose(fh);
  return 0;
}


x86 assembly (clang, -O0):
<save_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    116c <save_file+0x2c>
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1197 <save_file+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <save_file+0x9f>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1060 <fwrite@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11cf <save_file+0x8f>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <save_file+0x9f>
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):
<save_file>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdx,%r12
mov    %rsi,%r14
mov    %rdi,%r15
test   %rdx,%rdx
jne    1161 <save_file+0x21>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
lea    0xe98(%rip),%rsi
mov    %r15,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     119c <save_file+0x5c>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1030 <fclose@plt>
cmp    $0x1,%ebp
sbb    %eax,%eax
jmp    11a1 <save_file+0x61>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

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

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