#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 writematrixl(int *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, "%d", x[j * n + i]);
      if (j < p - 1)
        fprintf(out, ",");
      else
        fprintf(out, "\n");
    }
  }
  fflush(out);
  fclose(out);
  return 1;
}


x86 assembly (clang, -O0):
<writematrixl>:
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 <writematrixl+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    1261 <writematrixl+0x121>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1248 <writematrixl+0x108>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1235 <writematrixl+0xf5>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%ecx
imul   -0x14(%rbp),%ecx
add    -0x24(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%eax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1210 <writematrixl+0xd0>
mov    -0x30(%rbp),%rdi
lea    0xe16(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1222 <writematrixl+0xe2>
mov    -0x30(%rbp),%rdi
lea    0xdfa(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1227 <writematrixl+0xe7>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11b6 <writematrixl+0x76>
jmp    123a <writematrixl+0xfa>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a3 <writematrixl+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):
<writematrixl>:
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     1240 <writematrixl+0xf0>
mov    %rax,%rbx
test   %ebp,%ebp
jle    1229 <writematrixl+0xd9>
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,4),%r14
lea    0xe61(%rip),%rbp
xor    %edx,%edx
jmp    11d6 <writematrixl+0x86>
nopw   0x0(%rax,%rax,1)
mov    0x18(%rsp),%rdx
add    $0x1,%rdx
addq   $0x4,0x8(%rsp)
cmp    0x10(%rsp),%rdx
je     1229 <writematrixl+0xd9>
mov    %rdx,0x18(%rsp)
cmpl   $0x0,(%rsp)
jle    11c0 <writematrixl+0x70>
mov    0x8(%rsp),%r12
xor    %r15d,%r15d
nopl   0x0(%rax)
mov    (%r12),%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
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 <writematrixl+0xa0>
jmp    11c0 <writematrixl+0x70>
mov    %rbx,%rdi
call   1060 <fflush@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%ebx
jmp    125d <writematrixl+0x10d>
mov    0x2d99(%rip),%rax
movslq (%rax),%rdi
lea    0xdb2(%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 = [3 x i8] c"%d\00"
@.str.3 = [2 x i8] c",\00"
@.str.4 = [2 x i8] c"\0A\00"
define i32 @writematrixl(i32* %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
store i32* %0, i32** %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 i32*, i32** %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 i32, i32* %34, i64 %40
%42 = load i32, i32* %41
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %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 = [3 x i8] c"%d\00"
define i32 @writematrixl(i32* 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 i32, i32* %0, i64 %26
%28 = load i32, i32* %27
%29 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %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
}