#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*/ SEEK_CUR;
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
int /*<<< orphan*/ ftell(int /*<<< orphan*/ *);
int /*<<< orphan*/ ftruncate(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
long rand();
int /*<<< orphan*/ stdout;
int matrixGenerator(long matrixHeight, long matrixWidth, long randMin,
                    long randMax, char *fileName) {
  FILE *file = NULL;
  if ((file = fopen(fileName, "wb")) < 0) {
    perror("fopen: ");
    fprintf(stdout, "error open '%s' wb mode\n", fileName);
    return -1;
  }
  long i, j;
  double v;
  for (i = 0; i < matrixHeight; i++) {
    for (j = 0; j < matrixWidth; j++) {
      v = randMin + rand() % (randMax - randMin);
      fprintf(file, "%ld ", (long)v);
    }
    fseek(file, -1, SEEK_CUR);
    fputc('\n', file);
  }
  fseek(file, -1, SEEK_CUR);
  ftruncate(fileno(file), ftell(file));
  fclose(file);
  return 0;
}


x86 assembly (clang, -O0):
<matrixGenerator>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe31(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jae    1216 <matrixGenerator+0x76>
lea    0xe1a(%rip),%rdi
call   10b0 <perror@plt>
mov    0x2de3(%rip),%rax
movslq (%rax),%rdi
mov    -0x30(%rbp),%rdx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1314 <matrixGenerator+0x174>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x10(%rbp),%rax
jge    12ce <matrixGenerator+0x12e>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    1298 <matrixGenerator+0xf8>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    $0x0,%al
call   10c0 <rand@plt>
mov    -0x28(%rbp),%rcx
sub    -0x20(%rbp),%rcx
cqto
idiv   %rcx
mov    -0x58(%rbp),%rax
add    %rdx,%rax
cvtsi2sd %rax,%xmm0
movsd  %xmm0,-0x50(%rbp)
mov    -0x38(%rbp),%rdi
cvttsd2si -0x50(%rbp),%rdx
lea    0xda4(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1234 <matrixGenerator+0x94>
mov    -0x38(%rbp),%rdi
mov    0x2d45(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1090 <fseek@plt>
mov    -0x38(%rbp),%rsi
mov    $0xa,%edi
call   1050 <fputc@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    121e <matrixGenerator+0x7e>
mov    -0x38(%rbp),%rdi
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   1090 <fseek@plt>
mov    -0x38(%rbp),%rdi
call   1080 <fileno@plt>
mov    %eax,-0x5c(%rbp)
mov    -0x38(%rbp),%rdi
call   1070 <ftell@plt>
mov    -0x5c(%rbp),%edi
mov    %eax,%esi
call   1040 <ftruncate@plt>
mov    -0x38(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<matrixGenerator>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%rbx
mov    %rdx,%r15
mov    %rsi,0x10(%rsp)
mov    %rdi,%rbp
lea    0xe4d(%rip),%rsi
mov    %r8,%rdi
call   10a0 <fopen@plt>
mov    %rax,%r14
mov    %rbp,0x8(%rsp)
test   %rbp,%rbp
jle    1261 <matrixGenerator+0xd1>
sub    %r15,%rbx
lea    0xe2d(%rip),%rbp
xor    %r13d,%r13d
jmp    120e <matrixGenerator+0x7e>
nopl   0x0(%rax,%rax,1)
mov    0x2e01(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0xffffffff,%esi
call   1090 <fseek@plt>
mov    $0xa,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
add    $0x1,%r13
cmp    0x8(%rsp),%r13
je     1261 <matrixGenerator+0xd1>
mov    0x10(%rsp),%rax
mov    %rax,%r12
test   %rax,%rax
jg     1248 <matrixGenerator+0xb8>
jmp    11e0 <matrixGenerator+0x50>
nopl   (%rax)
cqto
idiv   %rbx
add    %r15,%rdx
xorps  %xmm0,%xmm0
cvtsi2sd %rdx,%xmm0
cvttsd2si %xmm0,%rdx
mov    %r14,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0xffffffffffffffff,%r12
je     11e0 <matrixGenerator+0x50>
xor    %eax,%eax
call   10b0 <rand@plt>
mov    %rax,%rcx
or     %rbx,%rcx
shr    $0x20,%rcx
jne    1220 <matrixGenerator+0x90>
xor    %edx,%edx
div    %ebx
jmp    1225 <matrixGenerator+0x95>
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
mov    $0xffffffff,%esi
call   1090 <fseek@plt>
mov    %r14,%rdi
call   1080 <fileno@plt>
mov    %eax,%ebx
mov    %r14,%rdi
call   1070 <ftell@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1040 <ftruncate@plt>
mov    %r14,%rdi
call   1030 <fclose@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [8 x i8] c"fopen: \00"
@stdout = global i32 0
@.str.2 = [25 x i8] c"error open '%s' wb mode\0A\00"
@.str.3 = [5 x i8] c"%ld \00"
@SEEK_CUR = global i32 0
define i32 @matrixGenerator(i64 %0, i64 %1, i64 %2, i64 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
%9 = alloca i64
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i64
%15 = alloca double
store i64 %0, i64* %7
store i64 %1, i64* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i8* %4, i8** %11
store i32* null, i32** %12
%16 = load i8*, i8** %11
%17 = call i32* @fopen(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %17, i32** %12
%18 = icmp ult i32* %17, null
br i1 %18, label %19, label %26
19: ; preds = %5
%20 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%21 = load i32, i32* @stdout
%22 = sext i32 %21 to i64
%23 = inttoptr i64 %22 to i32*
%24 = load i8*, i8** %11
%25 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i8* %24)
store i32 -1, i32* %6
br label %72
26: ; preds = %5
store i64 0, i64* %13
br label %27
27: ; preds = %58, %26
%28 = load i64, i64* %13
%29 = load i64, i64* %7
%30 = icmp slt i64 %28, %29
br i1 %30, label %31, label %61
31: ; preds = %27
store i64 0, i64* %14
br label %32
32: ; preds = %49, %31
%33 = load i64, i64* %14
%34 = load i64, i64* %8
%35 = icmp slt i64 %33, %34
br i1 %35, label %36, label %52
36: ; preds = %32
%37 = load i64, i64* %9
%38 = call i64 (...) @rand()
%39 = load i64, i64* %10
%40 = load i64, i64* %9
%41 = sub nsw i64 %39, %40
%42 = srem i64 %38, %41
%43 = add nsw i64 %37, %42
%44 = sitofp i64 %43 to double
store double %44, double* %15
%45 = load i32*, i32** %12
%46 = load double, double* %15
%47 = fptosi double %46 to i64
%48 = call i32 (i32*, i8*, ...) @fprintf(i32* %45, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %47)
br label %49
49: ; preds = %36
%50 = load i64, i64* %14
%51 = add nsw i64 %50, 1
store i64 %51, i64* %14
br label %32
52: ; preds = %32
%53 = load i32*, i32** %12
%54 = load i32, i32* @SEEK_CUR
%55 = call i32 @fseek(i32* %53, i32 -1, i32 %54)
%56 = load i32*, i32** %12
%57 = call i32 @fputc(i8 signext 10, i32* %56)
br label %58
58: ; preds = %52
%59 = load i64, i64* %13
%60 = add nsw i64 %59, 1
store i64 %60, i64* %13
br label %27
61: ; preds = %27
%62 = load i32*, i32** %12
%63 = load i32, i32* @SEEK_CUR
%64 = call i32 @fseek(i32* %62, i32 -1, i32 %63)
%65 = load i32*, i32** %12
%66 = call i32 @fileno(i32* %65)
%67 = load i32*, i32** %12
%68 = call i32 @ftell(i32* %67)
%69 = call i32 @ftruncate(i32 %66, i32 %68)
%70 = load i32*, i32** %12
%71 = call i32 @fclose(i32* %70)
store i32 0, i32* %6
br label %72
72: ; preds = %61, %19
%73 = load i32, i32* %6
ret i32 %73
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@stdout = global i32 0
@.str.3 = [5 x i8] c"%ld \00"
@SEEK_CUR = global i32 0
define i32 @matrixGenerator(i64 %0, i64 %1, i64 %2, i64 %3, i8* nocapture %4) {
%6 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%7 = icmp sgt i64 %0, 0
br i1 %7, label %8, label %29
8: ; preds = %5
%9 = icmp sgt i64 %1, 0
%10 = sub nsw i64 %3, %2
br label %11
11: ; preds = %8, %23
%12 = phi i64 [ 0, %8 ], [ %27, %23 ]
br i1 %9, label %13, label %23
13: ; preds = %11, %13
%14 = phi i64 [ %21, %13 ], [ 0, %11 ]
%15 = tail call i64 (...) @rand()
%16 = srem i64 %15, %10
%17 = add nsw i64 %16, %2
%18 = sitofp i64 %17 to double
%19 = fptosi double %18 to i64
%20 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 %19)
%21 = add nuw nsw i64 %14, 1
%22 = icmp eq i64 %21, %1
br i1 %22, label %23, label %13
23: ; preds = %13, %11
%24 = load i32, i32* @SEEK_CUR
%25 = tail call i32 @fseek(i32* %6, i32 -1, i32 %24)
%26 = tail call i32 @fputc(i8 signext 10, i32* %6)
%27 = add nuw nsw i64 %12, 1
%28 = icmp eq i64 %27, %0
br i1 %28, label %29, label %11
29: ; preds = %23, %5
%30 = load i32, i32* @SEEK_CUR
%31 = tail call i32 @fseek(i32* %6, i32 -1, i32 %30)
%32 = tail call i32 @fileno(i32* %6)
%33 = tail call i32 @ftell(i32* %6)
%34 = tail call i32 @ftruncate(i32 %32, i32 %33)
%35 = tail call i32 @fclose(i32* %6)
ret i32 0
}