#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 print_matrix_in_file(char *file_name, int ***matrix, int width,
                          int height) {
  FILE *output;
  output = fopen(file_name, "w");
  fprintf(output, "P3\n");
  fprintf(output, "%d %d\n", width, height);
  for (size_t i = 0; i < width; i++) {
    for (size_t j = 0; j < height; j++) {
      for (size_t k = 0; k < 3; k++) {
        fprintf(output, "%3d ", matrix[i][j][k]);
      }
      fprintf(output, "  ");
    }
    fprintf(output, "\n");
  }
  fclose(output);
}


x86 assembly (clang, -O0):
<print_matrix_in_file>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeaf(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe9d(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xe89(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jae    124d <print_matrix_in_file+0x11d>
movq   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmp    %rcx,%rax
jae    122a <print_matrix_in_file+0xfa>
movq   $0x0,-0x38(%rbp)
cmpq   $0x3,-0x38(%rbp)
jae    1207 <print_matrix_in_file+0xd7>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x30(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x38(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe1e(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    11be <print_matrix_in_file+0x8e>
mov    -0x20(%rbp),%rdi
lea    0xe00(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    11a5 <print_matrix_in_file+0x75>
mov    -0x20(%rbp),%rdi
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    118c <print_matrix_in_file+0x5c>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<print_matrix_in_file>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %rsi,%rbx
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbp
lea    0xe85(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe70(%rip),%rsi
mov    %rbp,%rdi
mov    %r15d,%edx
mov    %r14d,0xc(%rsp)
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r15d,%r15d
je     127e <print_matrix_in_file+0x12e>
mov    0xc(%rsp),%ecx
cmp    $0x2,%ecx
mov    $0x1,%eax
cmovae %ecx,%eax
movslq %eax,%r13
movslq %r15d,%rax
mov    %rax,0x10(%rsp)
lea    0xe38(%rip),%r15
xor    %r14d,%r14d
jmp    11fc <print_matrix_in_file+0xac>
nopw   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
add    $0x1,%r14
cmp    0x10(%rsp),%r14
je     127e <print_matrix_in_file+0x12e>
cmpl   $0x0,0xc(%rsp)
je     11e0 <print_matrix_in_file+0x90>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
mov    (%rbx,%r14,8),%rax
mov    (%rax,%r12,8),%rax
mov    (%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx,%r14,8),%rax
mov    (%rax,%r12,8),%rax
mov    0x4(%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx,%r14,8),%rax
mov    (%rax,%r12,8),%rax
mov    0x8(%rax),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x2,%esi
mov    $0x1,%edx
lea    0xdaa(%rip),%rdi
mov    %rbp,%rcx
call   1070 <fwrite@plt>
add    $0x1,%r12
cmp    %r12,%r13
jne    1210 <print_matrix_in_file+0xc0>
jmp    11e0 <print_matrix_in_file+0x90>
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 = [2 x i8] c"w\00"
@.str.1 = [4 x i8] c"P3\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [5 x i8] c"%3d \00"
@.str.4 = [3 x i8] c" \00"
@.str.5 = [2 x i8] c"\0A\00"
define void @print_matrix_in_file(i8* %0, i32*** %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32***
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
store i8* %0, i8** %5
store i32*** %1, i32**** %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%13 = load i8*, i8** %5
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %9
%15 = load i32*, i32** %9
%16 = call i32 (i32*, i8*, ...) @fprintf(i32* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%17 = load i32*, i32** %9
%18 = load i32, i32* %7
%19 = load i32, i32* %8
%20 = call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %18, i32 %19)
store i64 0, i64* %10
br label %21
21: ; preds = %61, %4
%22 = load i64, i64* %10
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = icmp ult i64 %22, %24
br i1 %25, label %26, label %64
26: ; preds = %21
store i64 0, i64* %11
br label %27
27: ; preds = %55, %26
%28 = load i64, i64* %11
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = icmp ult i64 %28, %30
br i1 %31, label %32, label %58
32: ; preds = %27
store i64 0, i64* %12
br label %33
33: ; preds = %49, %32
%34 = load i64, i64* %12
%35 = icmp ult i64 %34, 3
br i1 %35, label %36, label %52
36: ; preds = %33
%37 = load i32*, i32** %9
%38 = load i32***, i32**** %6
%39 = load i64, i64* %10
%40 = getelementptr inbounds i32**, i32*** %38, i64 %39
%41 = load i32**, i32*** %40
%42 = load i64, i64* %11
%43 = getelementptr inbounds i32*, i32** %41, i64 %42
%44 = load i32*, i32** %43
%45 = load i64, i64* %12
%46 = getelementptr inbounds i32, i32* %44, i64 %45
%47 = load i32, i32* %46
%48 = call i32 (i32*, i8*, ...) @fprintf(i32* %37, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %36
%50 = load i64, i64* %12
%51 = add i64 %50, 1
store i64 %51, i64* %12
br label %33
52: ; preds = %33
%53 = load i32*, i32** %9
%54 = call i32 (i32*, i8*, ...) @fprintf(i32* %53, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %52
%56 = load i64, i64* %11
%57 = add i64 %56, 1
store i64 %57, i64* %11
br label %27
58: ; preds = %27
%59 = load i32*, i32** %9
%60 = call i32 (i32*, i8*, ...) @fprintf(i32* %59, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
br label %61
61: ; preds = %58
%62 = load i64, i64* %10
%63 = add i64 %62, 1
store i64 %63, i64* %10
br label %21
64: ; preds = %21
%65 = load i32*, i32** %9
%66 = call i32 @fclose(i32* %65)
ret void
}

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