#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*/ assert(void const *);
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 *path, const void *buf, size_t len) {
  assert(path);
  assert(buf);
  size_t ret;
  FILE *f;
  if (!(f = fopen(path, "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
call   1040 <assert@plt>
mov    -0x18(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x10(%rbp),%rdi
lea    0xe8f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    1190 <put_file_contents+0x50>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <put_file_contents+0x8b>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1060 <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,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <assert@plt>
mov    %r15,%rdi
call   1040 <assert@plt>
lea    0xe9e(%rip),%rsi
mov    %rbx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     119c <put_file_contents+0x5c>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rax,%rcx
call   1060 <fwrite@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
cmp    %r14,%r15
setne  %al
neg    %eax
jmp    11a1 <put_file_contents+0x61>
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 @assert(i8* %10)
%12 = load i8*, i8** %6
%13 = call i32 @assert(i8* %12)
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %9
%16 = icmp ne i32* %15, null
br i1 %16, label %18, label %17
17: ; preds = %3
store i32 -1, i32* %4
br label %30
18: ; preds = %3
%19 = load i8*, i8** %6
%20 = load i64, i64* %7
%21 = load i32*, i32** %9
%22 = call i64 @fwrite(i8* %19, i32 1, i64 %20, i32* %21)
store i64 %22, i64* %8
%23 = load i32*, i32** %9
%24 = call i32 @fclose(i32* %23)
%25 = load i64, i64* %7
%26 = load i64, i64* %8
%27 = icmp eq i64 %25, %26
%28 = zext i1 %27 to i64
%29 = select i1 %27, i32 0, i32 -1
store i32 %29, i32* %4
br label %30
30: ; preds = %18, %17
%31 = load i32, i32* %4
ret i32 %31
}

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