#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*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ stderr;
int writematrixf(double *x, int n, int p, char *file) {
  int i, j;
  FILE *out;
  if (!(out = fopen(file, "wt"))) {
    fprintf(stderr, "can't open file %s\n", file);
    return 0;
  }
  for (i = 0; i < n; i++) {
    for (j = 0; j < p; j++) {
      fprintf(out, "%.10f", x[j * n + i]);
      if (j < p - 1)
        fprintf(out, ",");
      else
        fprintf(out, "\n");
    }
  }
  fflush(out);
  fclose(out);
  return 1;
}


x86 assembly (clang, -O0):
<writematrixf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    119c <writematrixf+0x5c>
mov    0x2e65(%rip),%rax
movslq (%rax),%rdi
mov    -0x20(%rbp),%rdx
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1263 <writematrixf+0x123>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    124a <writematrixf+0x10a>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1237 <writematrixf+0xf7>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    -0x24(%rbp),%ecx
movslq %ecx,%rcx
movsd  (%rax,%rcx,8),%xmm0
lea    0xe34(%rip),%rsi
mov    $0x1,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1212 <writematrixf+0xd2>
mov    -0x30(%rbp),%rdi
lea    0xe17(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1224 <writematrixf+0xe4>
mov    -0x30(%rbp),%rdi
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1229 <writematrixf+0xe9>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11b6 <writematrixf+0x76>
jmp    123c <writematrixf+0xfc>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a3 <writematrixf+0x63>
mov    -0x30(%rbp),%rdi
call   1050 <fflush@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<writematrixf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%r12
mov    %rdx,(%rsp)
mov    %esi,%ebp
mov    %rdi,0x8(%rsp)
lea    0xe8d(%rip),%rsi
mov    %rcx,%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     1242 <writematrixf+0xf2>
mov    %rax,%rbx
test   %ebp,%ebp
jle    122b <writematrixf+0xdb>
mov    (%rsp),%rcx
lea    -0x1(%rcx),%eax
mov    %ebp,%edx
cltq
mov    %rax,0x20(%rsp)
mov    %ecx,%r13d
mov    %rdx,0x10(%rsp)
lea    0x0(,%rdx,8),%r14
xor    %eax,%eax
lea    0xe5f(%rip),%rbp
jmp    11d6 <writematrixf+0x86>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rax
add    $0x1,%rax
addq   $0x8,0x8(%rsp)
cmp    0x10(%rsp),%rax
je     122b <writematrixf+0xdb>
mov    %rax,0x18(%rsp)
cmpl   $0x0,(%rsp)
jle    11c0 <writematrixf+0x70>
mov    0x8(%rsp),%r12
xor    %r15d,%r15d
nopl   0x0(%rax)
movsd  (%r12),%xmm0
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x1,%al
call   1050 <fprintf@plt>
cmp    0x20(%rsp),%r15
mov    $0xa,%edi
mov    $0x2c,%eax
cmovl  %eax,%edi
mov    %rbx,%rsi
call   1040 <fputc@plt>
add    $0x1,%r15
add    %r14,%r12
cmp    %r15,%r13
jne    11f0 <writematrixf+0xa0>
jmp    11c0 <writematrixf+0x70>
mov    %rbx,%rdi
call   1060 <fflush@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%ebx
jmp    125f <writematrixf+0x10f>
mov    0x2d97(%rip),%rax
movslq (%rax),%rdi
lea    0xdb0(%rip),%rsi
xor    %ebx,%ebx
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"wt\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"can't open file %s\0A\00"
@.str.2 = [6 x i8] c"%.10f\00"
@.str.3 = [2 x i8] c",\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @writematrixf(double* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca double*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store double* %0, double** %6
store i32 %1, i32* %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = load i8*, i8** %9
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %12
%15 = icmp ne i32* %14, null
br i1 %15, label %22, label %16
16: ; preds = %4
%17 = load i32, i32* @stderr
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i32*
%20 = load i8*, i8** %9
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %20)
store i32 0, i32* %5
br label %67
22: ; preds = %4
store i32 0, i32* %10
br label %23
23: ; preds = %59, %22
%24 = load i32, i32* %10
%25 = load i32, i32* %7
%26 = icmp slt i32 %24, %25
br i1 %26, label %27, label %62
27: ; preds = %23
store i32 0, i32* %11
br label %28
28: ; preds = %55, %27
%29 = load i32, i32* %11
%30 = load i32, i32* %8
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %58
32: ; preds = %28
%33 = load i32*, i32** %12
%34 = load double*, double** %6
%35 = load i32, i32* %11
%36 = load i32, i32* %7
%37 = mul nsw i32 %35, %36
%38 = load i32, i32* %10
%39 = add nsw i32 %37, %38
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds double, double* %34, i64 %40
%42 = load double, double* %41
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %42)
%44 = load i32, i32* %11
%45 = load i32, i32* %8
%46 = sub nsw i32 %45, 1
%47 = icmp slt i32 %44, %46
br i1 %47, label %48, label %51
48: ; preds = %32
%49 = load i32*, i32** %12
%50 = call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
br label %54
51: ; preds = %32
%52 = load i32*, i32** %12
%53 = call i32 (i32*, i8*, ...) @fprintf(i32* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %54
54: ; preds = %51, %48
br label %55
55: ; preds = %54
%56 = load i32, i32* %11
%57 = add nsw i32 %56, 1
store i32 %57, i32* %11
br label %28
58: ; preds = %28
br label %59
59: ; preds = %58
%60 = load i32, i32* %10
%61 = add nsw i32 %60, 1
store i32 %61, i32* %10
br label %23
62: ; preds = %23
%63 = load i32*, i32** %12
%64 = call i32 @fflush(i32* %63)
%65 = load i32*, i32** %12
%66 = call i32 @fclose(i32* %65)
store i32 1, i32* %5
br label %67
67: ; preds = %62, %16
%68 = load i32, i32* %5
ret i32 %68
}

LLVM IR (-O2):
@.str = [3 x i8] c"wt\00"
@stderr = global i32 0
@.str.1 = [20 x i8] c"can't open file %s\0A\00"
@.str.2 = [6 x i8] c"%.10f\00"
define i32 @writematrixf(double* nocapture %0, i32 %1, i32 %2, i8* %3) {
%5 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %16, label %7
7: ; preds = %4
%8 = icmp sgt i32 %1, 0
br i1 %8, label %9, label %38
9: ; preds = %7
%10 = icmp sgt i32 %2, 0
%11 = add nsw i32 %2, -1
%12 = zext i32 %1 to i64
%13 = sext i32 %11 to i64
%14 = zext i32 %1 to i64
%15 = zext i32 %2 to i64
br label %21
16: ; preds = %4
%17 = load i32, i32* @stderr
%18 = sext i32 %17 to i64
%19 = inttoptr i64 %18 to i32*
%20 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %3)
br label %41
21: ; preds = %9, %35
%22 = phi i64 [ 0, %9 ], [ %36, %35 ]
br i1 %10, label %23, label %35
23: ; preds = %21, %23
%24 = phi i64 [ %33, %23 ], [ 0, %21 ]
%25 = mul nsw i64 %24, %12
%26 = add nuw nsw i64 %25, %22
%27 = getelementptr inbounds double, double* %0, i64 %26
%28 = load double, double* %27
%29 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), double %28)
%30 = icmp slt i64 %24, %13
%31 = select i1 %30, i32 44, i32 10
%32 = tail call i32 @fputc(i32 %31, i32* %5)
%33 = add nuw nsw i64 %24, 1
%34 = icmp eq i64 %33, %15
br i1 %34, label %35, label %23
35: ; preds = %23, %21
%36 = add nuw nsw i64 %22, 1
%37 = icmp eq i64 %36, %14
br i1 %37, label %38, label %21
38: ; preds = %35, %7
%39 = tail call i32 @fflush(i32* %5)
%40 = tail call i32 @fclose(i32* %5)
br label %41
41: ; preds = %38, %16
%42 = phi i32 [ 1, %38 ], [ 0, %16 ]
ret i32 %42
}