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

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
size_t fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
size_t fwrite(unsigned int const *, size_t, size_t, int /*<<< orphan*/ *);
int hvecs_write(const char *file_in, const uint16_t *data, const unsigned int n,
                const unsigned int m) {
  size_t s1 = sizeof(uint16_t), s2 = sizeof(uint16_t), k = 0;
  unsigned int i;
  FILE *fid = fopen(file_in, "wb");
  if (fid == NULL)
    return EXIT_FAILURE;
  for (i = 0; i < n; i++) {
    k = fwrite(&m, s1, 1, fid);
    if (k != 1)
      return EXIT_FAILURE;
    k = fwrite(data + i * m, s2, (size_t)m, fid);
    if (k != (size_t)m)
      return EXIT_FAILURE;
  }
  k = fclose(fid);
  if (k != 0)
    return EXIT_FAILURE;
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<hvecs_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
movq   $0x4,-0x28(%rbp)
movq   $0x4,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe97(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    118e <hvecs_write+0x5e>
mov    0x2e5c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <hvecs_write+0x13a>
movl   $0x0,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    1235 <hvecs_write+0x105>
mov    -0x28(%rbp),%rsi
mov    -0x48(%rbp),%rcx
lea    -0x20(%rbp),%rdi
mov    $0x1,%edx
call   1050 <fwrite@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x1,-0x38(%rbp)
je     11d7 <hvecs_write+0xa7>
mov    0x2e13(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <hvecs_write+0x13a>
mov    -0x18(%rbp),%rdi
mov    -0x3c(%rbp),%eax
imul   -0x20(%rbp),%eax
mov    %eax,%eax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x20(%rbp),%eax
mov    %eax,%edx
mov    -0x48(%rbp),%rcx
call   1050 <fwrite@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    -0x20(%rbp),%ecx
cmp    %rcx,%rax
je     1222 <hvecs_write+0xf2>
mov    0x2dc8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <hvecs_write+0x13a>
jmp    1227 <hvecs_write+0xf7>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1195 <hvecs_write+0x65>
mov    -0x48(%rbp),%rdi
call   1030 <fclose@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     125e <hvecs_write+0x12e>
mov    0x2d8c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126a <hvecs_write+0x13a>
mov    0x2d73(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hvecs_write>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,%r14
mov    %ecx,0x4(%rsp)
lea    0xeb4(%rip),%rsi
call   1040 <fopen@plt>
mov    %rax,%rbx
mov    0x2e85(%rip),%rax
test   %rbx,%rbx
je     11d4 <hvecs_write+0xa4>
test   %r12d,%r12d
je     11b7 <hvecs_write+0x87>
xor    %r13d,%r13d
lea    0x4(%rsp),%r15
nopl   (%rax)
mov    $0x4,%esi
mov    $0x1,%edx
mov    %r15,%rdi
mov    %rbx,%rcx
call   1050 <fwrite@plt>
cmp    $0x1,%rax
jne    11cd <hvecs_write+0x9d>
mov    0x4(%rsp),%ebp
mov    %ebp,%eax
imul   %r13d,%eax
lea    (%r14,%rax,4),%rdi
mov    $0x4,%esi
mov    %rbp,%rdx
mov    %rbx,%rcx
call   1050 <fwrite@plt>
cmp    %rbp,%rax
jne    11cd <hvecs_write+0x9d>
add    $0x1,%r13d
cmp    %r13d,%r12d
jne    1170 <hvecs_write+0x40>
mov    %rbx,%rdi
call   1030 <fclose@plt>
test   %rax,%rax
jne    11cd <hvecs_write+0x9d>
mov    0x2e0d(%rip),%rax
jmp    11d4 <hvecs_write+0xa4>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @hvecs_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 i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32
%14 = alloca i32*
store i8* %0, i8** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i64 4, i64* %10
store i64 4, i64* %11
store i64 0, i64* %12
%15 = load i8*, i8** %6
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %14
%17 = load i32*, i32** %14
%18 = icmp eq i32* %17, null
br i1 %18, label %19, label %21
19: ; preds = %4
%20 = load i32, i32* @EXIT_FAILURE
store i32 %20, i32* %5
br label %65
21: ; preds = %4
store i32 0, i32* %13
br label %22
22: ; preds = %53, %21
%23 = load i32, i32* %13
%24 = load i32, i32* %8
%25 = icmp ult i32 %23, %24
br i1 %25, label %26, label %56
26: ; preds = %22
%27 = load i64, i64* %10
%28 = load i32*, i32** %14
%29 = call i64 @fwrite(i32* %9, i64 %27, i64 1, i32* %28)
store i64 %29, i64* %12
%30 = load i64, i64* %12
%31 = icmp ne i64 %30, 1
br i1 %31, label %32, label %34
32: ; preds = %26
%33 = load i32, i32* @EXIT_FAILURE
store i32 %33, i32* %5
br label %65
34: ; preds = %26
%35 = load i32*, i32** %7
%36 = load i32, i32* %13
%37 = load i32, i32* %9
%38 = mul i32 %36, %37
%39 = zext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %35, i64 %39
%41 = load i64, i64* %11
%42 = load i32, i32* %9
%43 = zext i32 %42 to i64
%44 = load i32*, i32** %14
%45 = call i64 @fwrite(i32* %40, i64 %41, i64 %43, i32* %44)
store i64 %45, i64* %12
%46 = load i64, i64* %12
%47 = load i32, i32* %9
%48 = zext i32 %47 to i64
%49 = icmp ne i64 %46, %48
br i1 %49, label %50, label %52
50: ; preds = %34
%51 = load i32, i32* @EXIT_FAILURE
store i32 %51, i32* %5
br label %65
52: ; preds = %34
br label %53
53: ; preds = %52
%54 = load i32, i32* %13
%55 = add i32 %54, 1
store i32 %55, i32* %13
br label %22
56: ; preds = %22
%57 = load i32*, i32** %14
%58 = call i64 @fclose(i32* %57)
store i64 %58, i64* %12
%59 = load i64, i64* %12
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %63
61: ; preds = %56
%62 = load i32, i32* @EXIT_FAILURE
store i32 %62, i32* %5
br label %65
63: ; preds = %56
%64 = load i32, i32* @EXIT_SUCCESS
store i32 %64, i32* %5
br label %65
65: ; preds = %63, %61, %50, %32, %19
%66 = load i32, i32* %5
ret i32 %66
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@EXIT_FAILURE = global i32 0
@EXIT_SUCCESS = global i32 0
define i32 @hvecs_write(i8* nocapture %0, i32* nocapture %1, i32 %2, i32 %3) {
%5 = alloca i32
store i32 %3, i32* %5
%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 %29, label %8
8: ; preds = %4
%9 = icmp eq i32 %2, 0
br i1 %9, label %25, label %12
10: ; preds = %16
%11 = icmp eq i32 %24, %2
br i1 %11, label %25, label %12
12: ; preds = %8, %10
%13 = phi i32 [ %24, %10 ], [ 0, %8 ]
%14 = call i64 @fwrite(i32* %5, i64 4, i64 1, i32* %6)
%15 = icmp eq i64 %14, 1
br i1 %15, label %16, label %29
16: ; preds = %12
%17 = load i32, i32* %5
%18 = mul i32 %17, %13
%19 = zext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %1, i64 %19
%21 = zext i32 %17 to i64
%22 = tail call i64 @fwrite(i32* %20, i64 4, i64 %21, i32* %6)
%23 = icmp eq i64 %22, %21
%24 = add nuw i32 %13, 1
br i1 %23, label %10, label %29
25: ; preds = %10, %8
%26 = tail call i64 @fclose(i32* %6)
%27 = icmp eq i64 %26, 0
%28 = select i1 %27, i32* @EXIT_SUCCESS, i32* @EXIT_FAILURE
br label %29
29: ; preds = %16, %12, %25, %4
%30 = phi i32* [ @EXIT_FAILURE, %4 ], [ %28, %25 ], [ @EXIT_FAILURE, %12 ], [ @EXIT_FAILURE, %16 ]
%31 = load i32, i32* %30
ret i32 %31
}