#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 *);
int ivecs_fwrite(int /*<<< orphan*/ *, int, int, int const *);
int /*<<< orphan*/ perror(char *);
int ivecs_write(const char *fname, int d, int n, const int *v) {
  int ret = 0;
  FILE *f = fopen(fname, "w");
  if (!f) {
    perror("ivecs_write");
    return -1;
  }
  ret = ivecs_fwrite(f, d, n, v);
  fclose(f);
  return ret;
}


x86 assembly (clang, -O0):
<ivecs_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe98(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1194 <ivecs_write+0x54>
lea    0xe7f(%rip),%rdi
call   1060 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b9 <ivecs_write+0x79>
mov    -0x30(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x18(%rbp),%edx
mov    -0x20(%rbp),%rcx
call   1040 <ivecs_fwrite@plt>
mov    %eax,-0x24(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ivecs_write>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %edx,%r15d
mov    %esi,%ebx
lea    0xeaa(%rip),%rsi
call   1050 <fopen@plt>
test   %rax,%rax
je     118a <ivecs_write+0x4a>
mov    %rax,%rbp
mov    %rax,%rdi
mov    %ebx,%esi
mov    %r15d,%edx
mov    %r14,%rcx
call   1040 <ivecs_fwrite@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <fclose@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe71(%rip),%rdi
call   1060 <perror@plt>
mov    $0xffffffff,%eax
jmp    117f <ivecs_write+0x3f>

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

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