#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 *);
scalar_t__ fwrite(char const *, size_t const, int, int /*<<< orphan*/ *);
int open_and_write(const char *file, const char *buf, const size_t size) {
  FILE *fd;
  if ((fd = fopen(file, "w")) == NULL) {
    return -1;
  }
  if (fwrite(buf, size, 1, fd) <= 0) {
    return -2;
  }
  fclose(fd);
  return 0;
}


x86 assembly (clang, -O0):
<open_and_write>:
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,-0x28(%rbp)
cmp    $0x0,%rax
jne    116e <open_and_write+0x3e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11aa <open_and_write+0x7a>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rcx
mov    $0x1,%edx
call   1050 <fwrite@plt>
cmp    $0x0,%rax
jg     119a <open_and_write+0x6a>
movl   $0xfffffffe,-0x4(%rbp)
jmp    11aa <open_and_write+0x7a>
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):
<open_and_write>:
push   %r15
push   %r14
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xebe(%rip),%rsi
call   1040 <fopen@plt>
test   %rax,%rax
je     1173 <open_and_write+0x43>
mov    %rax,%rbx
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rax,%rcx
call   1050 <fwrite@plt>
test   %rax,%rax
jle    117a <open_and_write+0x4a>
mov    %rbx,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
jmp    117f <open_and_write+0x4f>
mov    $0xffffffff,%eax
jmp    117f <open_and_write+0x4f>
mov    $0xfffffffe,%eax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
define i32 @open_and_write(i8* nocapture %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 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, i64 %2, i32 1, i32* %4)
%8 = icmp slt i64 %7, 1
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @fclose(i32* %4)
br label %11
11: ; preds = %6, %3, %9
%12 = phi i32 [ 0, %9 ], [ -1, %3 ], [ -2, %6 ]
ret i32 %12
}