#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*/ fail(int, char *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
void write_array(char *filename, int dim, int **arr) {
  FILE *fp = fopen(filename, "wb");
  if (fp == NULL) {
    fail(1, "write_array", "Cannot open file.");
  }
  fprintf(fp, "%i\n", dim);
  for (int i = 0; i < dim; i++) {
    fprintf(fp, "%i", arr[i][0]);
    for (int j = 1; j < dim; j++) {
      fprintf(fp, " %i", arr[i][j]);
    }
    fprintf(fp, "\n");
  }
  fclose(fp);
}


x86 assembly (clang, -O0):
<write_array>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    118a <write_array+0x4a>
mov    $0x1,%edi
lea    0xe85(%rip),%rsi
lea    0xe8a(%rip),%rdx
call   1030 <fail@plt>
mov    -0x20(%rbp),%rdi
mov    -0xc(%rbp),%edx
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1238 <write_array+0xf8>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    (%rax),%edx
lea    0xe5b(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1218 <write_array+0xd8>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x28(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe22(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11d9 <write_array+0x99>
mov    -0x20(%rbp),%rdi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a6 <write_array+0x66>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_array>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r13
mov    %esi,%r14d
lea    0xe98(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    118d <write_array+0x3d>
lea    0xe87(%rip),%rsi
lea    0xe8c(%rip),%rdx
mov    $0x1,%edi
call   1030 <fail@plt>
lea    0xe8d(%rip),%rsi
mov    %rbp,%rdi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
test   %r14d,%r14d
jle    1221 <write_array+0xd1>
mov    %r14d,%ebx
lea    0xe75(%rip),%r12
xor    %r15d,%r15d
mov    %r14d,0x4(%rsp)
jmp    11db <write_array+0x8b>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1050 <fputc@plt>
add    $0x1,%r15
cmp    %rbx,%r15
mov    0x4(%rsp),%r14d
je     1221 <write_array+0xd1>
mov    0x0(%r13,%r15,8),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
lea    0xe3a(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
cmp    $0x1,%r14d
je     11c0 <write_array+0x70>
mov    $0x1,%r14d
nop
mov    0x0(%r13,%r15,8),%rax
mov    (%rax,%r14,4),%edx
mov    %rbp,%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
add    $0x1,%r14
cmp    %r14,%rbx
jne    1200 <write_array+0xb0>
jmp    11c0 <write_array+0x70>
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [12 x i8] c"write_array\00"
@.str.2 = [18 x i8] c"Cannot open file.\00"
@.str.3 = [4 x i8] c"%i\0A\00"
@.str.4 = [3 x i8] c"%i\00"
@.str.5 = [4 x i8] c" %i\00"
@.str.6 = [2 x i8] c"\0A\00"
define void @write_array(i8* %0, i32 %1, i32** %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32**
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32** %2, i32*** %6
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = call i32 @fail(i32 1, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
%17 = load i32*, i32** %7
%18 = load i32, i32* %5
%19 = call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 %18)
store i32 0, i32* %8
br label %20
20: ; preds = %56, %16
%21 = load i32, i32* %8
%22 = load i32, i32* %5
%23 = icmp slt i32 %21, %22
br i1 %23, label %24, label %59
24: ; preds = %20
%25 = load i32*, i32** %7
%26 = load i32**, i32*** %6
%27 = load i32, i32* %8
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i32*, i32** %26, i64 %28
%30 = load i32*, i32** %29
%31 = getelementptr inbounds i32, i32* %30, i64 0
%32 = load i32, i32* %31
%33 = call i32 (i32*, i8*, ...) @fprintf(i32* %25, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i32 %32)
store i32 1, i32* %9
br label %34
34: ; preds = %50, %24
%35 = load i32, i32* %9
%36 = load i32, i32* %5
%37 = icmp slt i32 %35, %36
br i1 %37, label %38, label %53
38: ; preds = %34
%39 = load i32*, i32** %7
%40 = load i32**, i32*** %6
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds i32*, i32** %40, i64 %42
%44 = load i32*, i32** %43
%45 = load i32, i32* %9
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i32, i32* %44, i64 %46
%48 = load i32, i32* %47
%49 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.5, i64 0, i64 0), i32 %48)
br label %50
50: ; preds = %38
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %34
53: ; preds = %34
%54 = load i32*, i32** %7
%55 = call i32 (i32*, i8*, ...) @fprintf(i32* %54, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
br label %56
56: ; preds = %53
%57 = load i32, i32* %8
%58 = add nsw i32 %57, 1
store i32 %58, i32* %8
br label %20
59: ; preds = %20
%60 = load i32*, i32** %7
%61 = call i32 @fclose(i32* %60)
ret void
}

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