#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 *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int fwrite(int *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ stderr;
void writeArrayBin(int *v, int n, char *filename) {
  FILE *fp;
  if ((fp = fopen(filename, "wb")) == NULL) {
    fprintf(stderr, "Error opening the file %s for writing.\n", filename);
    return;
  }
  if (fwrite(v, sizeof(int), n, fp) != n) {
    fprintf(stderr, "Error writing the file %s.\n", filename);
    return;
  }
  fclose(fp);
  return;
}


x86 assembly (clang, -O0):
<writeArrayBin>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    118f <writeArrayBin+0x4f>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe7e(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11d4 <writeArrayBin+0x94>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x20(%rbp),%rcx
mov    $0x4,%esi
call   1060 <fwrite@plt>
cmp    -0xc(%rbp),%eax
je     11cb <writeArrayBin+0x8b>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rdx
lea    0xe6a(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11d4 <writeArrayBin+0x94>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writeArrayBin>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%r15
lea    0xeaa(%rip),%rsi
mov    %rdx,%rdi
call   1050 <fopen@plt>
test   %rax,%rax
je     118e <writeArrayBin+0x4e>
mov    %rax,%rbx
mov    %r15,%rdi
mov    $0x4,%esi
mov    %ebp,%edx
mov    %rax,%rcx
call   1060 <fwrite@plt>
cmp    %ebp,%eax
jne    11a0 <writeArrayBin+0x60>
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe65(%rip),%rsi
jmp    11b0 <writeArrayBin+0x70>
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe7b(%rip),%rsi
mov    %r14,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fprintf@plt>

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

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stderr = global i32 0
@.str.1 = [40 x i8] c"Error opening the file %s for writing.\0A\00"
@.str.2 = [28 x i8] c"Error writing the file %s.\0A\00"
define void @writeArrayBin(i32* nocapture %0, i32 %1, i8* %2) {
%4 = tail call i32* @fopen(i8* %2, 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 %9
6: ; preds = %3
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.1, i64 0, i64 0), i8* %2)
br label %17
9: ; preds = %3
%10 = tail call i32 @fwrite(i32* %0, i32 4, i32 %1, i32* %4)
%11 = icmp eq i32 %10, %1
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* %2)
br label %17
15: ; preds = %9
%16 = tail call i32 @fclose(i32* %4)
br label %17
17: ; preds = %15, %12, %6
ret void
}