#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*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void save_int_matrix(int *data, char *filename, int N, int D) {
  FILE *fd = fopen(filename, "w+");
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < D; j++) {
      fprintf(fd, "%d\t", data[i * D + j]);
    }
    fprintf(fd, "\n");
  }
  fclose(fd);
}


x86 assembly (clang, -O0):
<save_int_matrix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11d4 <save_int_matrix+0xa4>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    11b4 <save_int_matrix+0x84>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
imul   -0x18(%rbp),%ecx
add    -0x28(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe64(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    1174 <save_int_matrix+0x44>
mov    -0x20(%rbp),%rdi
lea    0xe48(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1161 <save_int_matrix+0x31>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_int_matrix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,0x4(%rsp)
mov    %edx,%r15d
mov    %rsi,%rax
mov    %rdi,%rbx
lea    0xe9e(%rip),%rsi
mov    %rax,%rdi
call   1060 <fopen@plt>
mov    %rax,%rbp
test   %r15d,%r15d
jle    11ec <save_int_matrix+0xac>
movslq 0x4(%rsp),%rax
mov    %r15d,%ecx
mov    %rcx,0x8(%rsp)
mov    %eax,%r14d
shl    $0x2,%rax
mov    %rax,0x10(%rsp)
lea    0xe71(%rip),%r15
xor    %r13d,%r13d
jmp    11bd <save_int_matrix+0x7d>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
add    $0x1,%r13
add    0x10(%rsp),%rbx
cmp    0x8(%rsp),%r13
je     11ec <save_int_matrix+0xac>
cmpl   $0x0,0x4(%rsp)
jle    11a0 <save_int_matrix+0x60>
xor    %r12d,%r12d
nopw   0x0(%rax,%rax,1)
mov    (%rbx,%r12,4),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%r14
jne    11d0 <save_int_matrix+0x90>
jmp    11a0 <save_int_matrix+0x60>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"w+\00"
@.str.1 = [4 x i8] c"%d\09\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @save_int_matrix(i32* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
store i32* %0, i32** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %9
store i32 0, i32* %10
br label %14
14: ; preds = %41, %4
%15 = load i32, i32* %10
%16 = load i32, i32* %7
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %44
18: ; preds = %14
store i32 0, i32* %11
br label %19
19: ; preds = %35, %18
%20 = load i32, i32* %11
%21 = load i32, i32* %8
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %38
23: ; preds = %19
%24 = load i32*, i32** %9
%25 = load i32*, i32** %5
%26 = load i32, i32* %10
%27 = load i32, i32* %8
%28 = mul nsw i32 %26, %27
%29 = load i32, i32* %11
%30 = add nsw i32 %28, %29
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %25, i64 %31
%33 = load i32, i32* %32
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %33)
br label %35
35: ; preds = %23
%36 = load i32, i32* %11
%37 = add nsw i32 %36, 1
store i32 %37, i32* %11
br label %19
38: ; preds = %19
%39 = load i32*, i32** %9
%40 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %38
%42 = load i32, i32* %10
%43 = add nsw i32 %42, 1
store i32 %43, i32* %10
br label %14
44: ; preds = %14
%45 = load i32*, i32** %9
%46 = call i32 @fclose(i32* %45)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"w+\00"
@.str.1 = [4 x i8] c"%d\09\00"
define void @save_int_matrix(i32* nocapture %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i32* @fopen(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%6 = icmp sgt i32 %2, 0
br i1 %6, label %7, label %16
7: ; preds = %4
%8 = icmp sgt i32 %3, 0
%9 = sext i32 %3 to i64
%10 = zext i32 %2 to i64
%11 = zext i32 %3 to i64
br label %12
12: ; preds = %7, %18
%13 = phi i64 [ 0, %7 ], [ %20, %18 ]
br i1 %8, label %14, label %18
14: ; preds = %12
%15 = mul nsw i64 %13, %9
br label %22
16: ; preds = %18, %4
%17 = tail call i32 @fclose(i32* %5)
ret void
18: ; preds = %22, %12
%19 = tail call i32 @fputc(i32 10, i32* %5)
%20 = add nuw nsw i64 %13, 1
%21 = icmp eq i64 %20, %10
br i1 %21, label %16, label %12
22: ; preds = %14, %22
%23 = phi i64 [ 0, %14 ], [ %28, %22 ]
%24 = add nsw i64 %23, %15
%25 = getelementptr inbounds i32, i32* %0, i64 %24
%26 = load i32, i32* %25
%27 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = add nuw nsw i64 %23, 1
%29 = icmp eq i64 %28, %11
br i1 %29, label %18, label %22
}