#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*/ uint8_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, int, size_t,
                          int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int write_buffer(const char *file_path, uint8_t *buffer, size_t buf_size) {
  int ret = 0;
  FILE *fp = NULL;
  fp = fopen(file_path, "wb");
  if (fp == NULL) {
    printf("Error opening .encr file\n");
    ret = 3;
    goto err;
  }
  fwrite(buffer, 1, buf_size, fp);
  fclose(fp);
err:
  fclose(fp);
  return ret;
}


x86 assembly (clang, -O0):
<write_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe92(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119a <write_buffer+0x5a>
lea    0xe7a(%rip),%rdi
call   1040 <printf@plt>
movl   $0x3,-0x1c(%rbp)
jmp    11b9 <write_buffer+0x79>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1060 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_buffer>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xeac(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
je     1180 <write_buffer+0x40>
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r14,%rdx
mov    %rbx,%rcx
call   1060 <fwrite@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
xor    %ebp,%ebp
jmp    1191 <write_buffer+0x51>
lea    0xe7c(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%ebp
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@str = [25 x i8] c"Error opening .encr file\00"
define i32 @write_buffer(i8* nocapture %0, i32* 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 %6, label %8
6: ; preds = %3
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
br label %11
8: ; preds = %3
%9 = tail call i32 @fwrite(i32* %1, i32 1, i64 %2, i32* %4)
%10 = tail call i32 @fclose(i32* %4)
br label %11
11: ; preds = %8, %6
%12 = phi i32 [ 3, %6 ], [ 0, %8 ]
%13 = tail call i32 @fclose(i32* %4)
ret i32 %12
}