#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 */
struct stat {
  int /*<<< orphan*/ member_0;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, double, ...);
char *malloc(int);
int /*<<< orphan*/ mkdir(char const *, int);
int /*<<< orphan*/ sprintf(char *, char *, char const *, int, int, int, int);
int stat(char const *, struct stat *);
int strlen(char const *);
int saveMatrixBlock(int i0, int i1, int j0, int j1, double *mat,
                    const char *outputLocation) {
  struct stat st = {0};
  if (stat(outputLocation, &st) == -1) {
    mkdir(outputLocation, 0700);
  }
  char *file_name = malloc(sizeof(char) * (strlen(outputLocation) + 300));
  sprintf(file_name, "%s/block%d-%d-%d-%d.txt", outputLocation, i0, i1, j0, j1);
  long i;
  FILE *fp;
  fp = fopen(file_name, "w");
  if (fp == NULL)
    return -1;
  fprintf(fp, "#i0=%d,i1=%d,j0=%d,j1=%d\n", i0, i1, j0, j1);
  int size = (i1 - i0) * (j1 - j0);
  for (i = 0; i < size; i++) {
    fprintf(fp, "%f\n", mat[i]);
  }
  fclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<saveMatrixBlock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    %r8,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1060 <memset@plt>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0xffffffff,%eax
jne    11e0 <saveMatrixBlock+0x50>
mov    -0x28(%rbp),%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
add    $0x12c,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x10(%rbp),%r9d
mov    -0x14(%rbp),%eax
lea    0xde4(%rip),%rsi
mov    %eax,(%rsp)
call   10b0 <sprintf@plt>
mov    -0x38(%rbp),%rdi
lea    0xde9(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    124f <saveMatrixBlock+0xbf>
movl   $0xffffffff,-0x4(%rbp)
jmp    12db <saveMatrixBlock+0x14b>
mov    -0x48(%rbp),%rdi
cvtsi2sdl -0x8(%rbp),%xmm0
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x14(%rbp),%r8d
lea    0xdb1(%rip),%rsi
mov    $0x1,%al
call   1070 <fprintf@plt>
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    -0x10(%rbp),%ecx
imul   %ecx,%eax
mov    %eax,-0x4c(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x4c(%rbp),%rcx
cmp    %rcx,%rax
jge    12cb <saveMatrixBlock+0x13b>
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x40(%rbp),%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xd81(%rip),%rsi
mov    $0x1,%al
call   1070 <fprintf@plt>
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    128a <saveMatrixBlock+0xfa>
mov    -0x48(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<saveMatrixBlock>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r9,%rbp
mov    %r8,%r15
mov    %ecx,%r13d
mov    %edx,%r12d
mov    %esi,%r14d
mov    %edi,0x14(%rsp)
movl   $0x0,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %r9,%rdi
call   1070 <stat@plt>
cmp    $0xffffffff,%eax
jne    11c8 <saveMatrixBlock+0x48>
mov    %rbp,%rdi
mov    $0x1c0,%esi
call   1030 <mkdir@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
lea    0x12c(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %r13d,(%rsp)
lea    0xe17(%rip),%rsi
mov    %rax,%rdi
mov    %rbp,%rdx
mov    0x14(%rsp),%ebp
mov    %ebp,%ecx
mov    %r14d,%r8d
mov    %r12d,%r9d
call   10a0 <sprintf@plt>
lea    0xe11(%rip),%rsi
mov    %rbx,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     1288 <saveMatrixBlock+0x108>
mov    %r12d,%ebx
mov    %rax,%r12
cvtsi2sd %ebp,%xmm0
lea    0xdf5(%rip),%rsi
mov    %rax,%rdi
mov    %r14d,%edx
mov    %ebx,%ecx
mov    %r13d,%r8d
mov    $0x1,%al
call   1060 <fprintf@plt>
sub    %ebp,%r14d
sub    %ebx,%r13d
imul   %r14d,%r13d
test   %r13d,%r13d
jle    127c <saveMatrixBlock+0xfc>
movslq %r13d,%rbx
xor    %ebp,%ebp
lea    0xde2(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movsd  (%r15,%rbp,8),%xmm0
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x1,%al
call   1060 <fprintf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
jne    1260 <saveMatrixBlock+0xe0>
mov    %r12,%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
jmp    128d <saveMatrixBlock+0x10d>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [24 x i8] c"%s/block%d-%d-%d-%d.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [26 x i8] c"#i0=%d,i1=%d,j0=%d,j1=%d\0A\00"
@.str.3 = [4 x i8] c"%f\0A\00"
define i32 @saveMatrixBlock(i32 %0, i32 %1, i32 %2, i32 %3, double* %4, i8* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca double*
%13 = alloca i8*
%14 = alloca %struct.stat
%15 = alloca i8*
%16 = alloca i64
%17 = alloca i32*
%18 = alloca i32
store i32 %0, i32* %8
store i32 %1, i32* %9
store i32 %2, i32* %10
store i32 %3, i32* %11
store double* %4, double** %12
store i8* %5, i8** %13
%19 = bitcast %struct.stat* %14 to i8*
%20 = load i8*, i8** %13
%21 = call i32 @stat(i8* %20, %struct.stat* %14)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %26
23: ; preds = %6
%24 = load i8*, i8** %13
%25 = call i32 @mkdir(i8* %24, i32 448)
br label %26
26: ; preds = %23, %6
%27 = load i8*, i8** %13
%28 = call i32 @strlen(i8* %27)
%29 = add nsw i32 %28, 300
%30 = sext i32 %29 to i64
%31 = mul i64 1, %30
%32 = trunc i64 %31 to i32
%33 = call i8* @malloc(i32 %32)
store i8* %33, i8** %15
%34 = load i8*, i8** %15
%35 = load i8*, i8** %13
%36 = load i32, i32* %8
%37 = load i32, i32* %9
%38 = load i32, i32* %10
%39 = load i32, i32* %11
%40 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %35, i32 %36, i32 %37, i32 %38, i32 %39)
%41 = load i8*, i8** %15
%42 = call i32* @fopen(i8* %41, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %42, i32** %17
%43 = load i32*, i32** %17
%44 = icmp eq i32* %43, null
br i1 %44, label %45, label %46
45: ; preds = %26
store i32 -1, i32* %7
br label %79
46: ; preds = %26
%47 = load i32*, i32** %17
%48 = load i32, i32* %8
%49 = sitofp i32 %48 to double
%50 = load i32, i32* %9
%51 = load i32, i32* %10
%52 = load i32, i32* %11
%53 = call i32 (i32*, i8*, double, ...) @fprintf(i32* %47, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), double %49, i32 %50, i32 %51, i32 %52)
%54 = load i32, i32* %9
%55 = load i32, i32* %8
%56 = sub nsw i32 %54, %55
%57 = load i32, i32* %11
%58 = load i32, i32* %10
%59 = sub nsw i32 %57, %58
%60 = mul nsw i32 %56, %59
store i32 %60, i32* %18
store i64 0, i64* %16
br label %61
61: ; preds = %73, %46
%62 = load i64, i64* %16
%63 = load i32, i32* %18
%64 = sext i32 %63 to i64
%65 = icmp slt i64 %62, %64
br i1 %65, label %66, label %76
66: ; preds = %61
%67 = load i32*, i32** %17
%68 = load double*, double** %12
%69 = load i64, i64* %16
%70 = getelementptr inbounds double, double* %68, i64 %69
%71 = load double, double* %70
%72 = call i32 (i32*, i8*, double, ...) @fprintf(i32* %67, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double %71)
br label %73
73: ; preds = %66
%74 = load i64, i64* %16
%75 = add nsw i64 %74, 1
store i64 %75, i64* %16
br label %61
76: ; preds = %61
%77 = load i32*, i32** %17
%78 = call i32 @fclose(i32* %77)
store i32 0, i32* %7
br label %79
79: ; preds = %76, %45
%80 = load i32, i32* %7
ret i32 %80
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [24 x i8] c"%s/block%d-%d-%d-%d.txt\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [26 x i8] c"#i0=%d,i1=%d,j0=%d,j1=%d\0A\00"
@.str.3 = [4 x i8] c"%f\0A\00"
define i32 @saveMatrixBlock(i32 %0, i32 %1, i32 %2, i32 %3, double* nocapture %4, i8* %5) {
%7 = alloca %struct.stat
%8 = bitcast %struct.stat* %7 to i8*
%9 = getelementptr inbounds %struct.stat, %struct.stat* %7, i64 0, i32 0
store i32 0, i32* %9
%10 = call i32 @stat(i8* %5, %struct.stat* %7)
%11 = icmp eq i32 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %6
%13 = tail call i32 @mkdir(i8* %5, i32 448)
br label %14
14: ; preds = %12, %6
%15 = tail call i32 @strlen(i8* %5)
%16 = add nsw i32 %15, 300
%17 = tail call i8* @malloc(i32 %16)
%18 = tail call i32 @sprintf(i8* dereferenceable(1) %17, i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %5, i32 %0, i32 %1, i32 %2, i32 %3)
%19 = tail call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br i1 %20, label %38, label %21
21: ; preds = %14
%22 = sitofp i32 %0 to double
%23 = tail call i32 (i32*, i8*, double, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), double %22, i32 %1, i32 %2, i32 %3)
%24 = sub nsw i32 %1, %0
%25 = sub nsw i32 %3, %2
%26 = mul nsw i32 %25, %24
%27 = sext i32 %26 to i64
%28 = icmp sgt i32 %26, 0
br i1 %28, label %29, label %36
29: ; preds = %21, %29
%30 = phi i64 [ %34, %29 ], [ 0, %21 ]
%31 = getelementptr inbounds double, double* %4, i64 %30
%32 = load double, double* %31
%33 = tail call i32 (i32*, i8*, double, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), double %32)
%34 = add nuw nsw i64 %30, 1
%35 = icmp eq i64 %34, %27
br i1 %35, label %36, label %29
36: ; preds = %29, %21
%37 = tail call i32 @fclose(i32* %19)
br label %38
38: ; preds = %14, %36
%39 = phi i32 [ 0, %36 ], [ -1, %14 ]
ret i32 %39
}