#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(void const *, int, size_t, int /*<<< orphan*/ *);
int put_file_contents(const char *filename, const void *buf, size_t len) {
  size_t ret;
  FILE *f;
  if (!(f = fopen(filename, "wb"))) {
    return (-1);
  }
  ret = fwrite(buf, 1, len, f);
  fclose(f);
  return (len == ret ? 0 : -1);
}


x86 assembly (clang, -O0):
<put_file_contents>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    116e <put_file_contents+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11a9 <put_file_contents+0x79>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    %rax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rsi
mov    $0xffffffff,%eax
xor    %ecx,%ecx
cmp    %rsi,%rdx
cmove  %ecx,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<put_file_contents>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r15
mov    %rsi,%r14
lea    0xebe(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1179 <put_file_contents+0x49>
mov    %rax,%rbx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r15,%rdx
mov    %rax,%rcx
call   1050 <fwrite@plt>
mov    %rax,%r14
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmp    %r15,%r14
setne  %al
neg    %eax
jmp    117e <put_file_contents+0x4e>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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