#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 *, ...);
int /*<<< orphan*/ printf(char *, ...);
int pgmWrite_ASCII(char *filename, int rows, int cols, int **image,
                   char *comment_string) {
  FILE *file;
  long nwritten = 0;
  long x, y;
  if (rows > 4500 || cols > 4500) {
    printf("ERROR: row/col specifications larger than image array:\n");
    return (0);
  }
  if ((file = fopen(filename, "w")) == NULL) {
    printf("ERROR: file open failed\n");
    return (0);
  }
  fprintf(file, "P2\n");
  if (comment_string != NULL)
    fprintf(file, "# %s \n", comment_string);
  fprintf(file, "%i %i \n", cols, rows);
  fprintf(file, "%d\n", (int)65535);
  for (y = 0; y < rows; y++) {
    for (x = 0; x < cols; x++) {
      fprintf(file, "%i ", image[y][x]);
      nwritten++;
    }
    fprintf(file, "\n");
  }
  fprintf(file, "\n");
  printf("\nNumber of pixels total(from rows*cols): %i\n", rows * cols);
  printf("Number of pixels written in file %s: %ld\n\n", filename, nwritten);
  fclose(file);
  return (1);
}


x86 assembly (clang, -O0):
<pgmWrite_ASCII>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movq   $0x0,-0x38(%rbp)
cmpl   $0x1194,-0x14(%rbp)
jg     117c <pgmWrite_ASCII+0x3c>
cmpl   $0x1194,-0x18(%rbp)
jle    1196 <pgmWrite_ASCII+0x56>
lea    0xe7d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1314 <pgmWrite_ASCII+0x1d4>
mov    -0x10(%rbp),%rdi
lea    0xe97(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
jne    11ce <pgmWrite_ASCII+0x8e>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1314 <pgmWrite_ASCII+0x1d4>
mov    -0x30(%rbp),%rdi
lea    0xe7a(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
cmpq   $0x0,-0x28(%rbp)
je     1201 <pgmWrite_ASCII+0xc1>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rdx
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x30(%rbp),%rdi
lea    0xe42(%rip),%rsi
mov    $0xffff,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jge    12c7 <pgmWrite_ASCII+0x187>
movq   $0x0,-0x40(%rbp)
mov    -0x40(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmp    %rcx,%rax
jge    12a4 <pgmWrite_ASCII+0x164>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rax
mov    -0x48(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
mov    -0x40(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xdea(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x40(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
jmp    1251 <pgmWrite_ASCII+0x111>
mov    -0x30(%rbp),%rdi
lea    0xe15(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x48(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
jmp    1238 <pgmWrite_ASCII+0xf8>
mov    -0x30(%rbp),%rdi
lea    0xdf2(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x14(%rbp),%esi
imul   -0x18(%rbp),%esi
lea    0xd87(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    0xd9e(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgmWrite_ASCII>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
cmp    $0x1194,%esi
jg     12b1 <pgmWrite_ASCII+0x141>
mov    %edx,%r15d
cmp    $0x1195,%edx
jge    12b1 <pgmWrite_ASCII+0x141>
mov    %r8,%r13
mov    %rcx,%rbp
mov    %esi,%r12d
mov    %rdi,%r14
lea    0xe54(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1319 <pgmWrite_ASCII+0x1a9>
mov    %rax,%rbx
lea    0xe3e(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1090 <fwrite@plt>
test   %r13,%r13
je     11ef <pgmWrite_ASCII+0x7f>
lea    0xe24(%rip),%rsi
mov    %rbx,%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xe17(%rip),%rsi
xor    %r13d,%r13d
mov    %rbx,%rdi
mov    %r15d,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1070 <fprintf@plt>
lea    0xe05(%rip),%rsi
mov    %rbx,%rdi
mov    $0xffff,%edx
xor    %eax,%eax
call   1070 <fprintf@plt>
test   %r12d,%r12d
jle    12d4 <pgmWrite_ASCII+0x164>
mov    %r14,0x20(%rsp)
mov    %r12d,0xc(%rsp)
movslq %r12d,%rax
mov    %rax,0x10(%rsp)
mov    %r15d,0x8(%rsp)
movslq %r15d,%r15
test   %r15,%r15
mov    $0x0,%eax
cmovg  %r15,%rax
mov    %rax,0x18(%rsp)
lea    0xdbf(%rip),%r14
xor    %r12d,%r12d
jmp    1278 <pgmWrite_ASCII+0x108>
nop
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
add    $0x1,%r12
cmp    0x10(%rsp),%r12
je     12ba <pgmWrite_ASCII+0x14a>
cmpl   $0x0,0x8(%rsp)
jle    1260 <pgmWrite_ASCII+0xf0>
xor    %r13d,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x0(%rbp,%r12,8),%rax
mov    (%rax,%r13,4),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1070 <fprintf@plt>
add    $0x1,%r13
cmp    %r13,%r15
jne    1290 <pgmWrite_ASCII+0x120>
jmp    1260 <pgmWrite_ASCII+0xf0>
lea    0xdd5(%rip),%rdi
jmp    1320 <pgmWrite_ASCII+0x1b0>
mov    0x18(%rsp),%r13
imul   0x10(%rsp),%r13
mov    0x20(%rsp),%r14
mov    0x8(%rsp),%r15d
mov    0xc(%rsp),%r12d
mov    $0xa,%edi
mov    %rbx,%rsi
call   1060 <fputc@plt>
imul   %r12d,%r15d
lea    0xd31(%rip),%rdi
mov    %r15d,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xd4d(%rip),%rdi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    1327 <pgmWrite_ASCII+0x1b7>
lea    0xd55(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [56 x i8] c"ERROR: row/col specifications larger than image array:\0A\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [25 x i8] c"ERROR: file open failed\0A\00"
@.str.3 = [4 x i8] c"P2\0A\00"
@.str.4 = [7 x i8] c"# %s \0A\00"
@.str.5 = [8 x i8] c"%i %i \0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
@.str.7 = [4 x i8] c"%i \00"
@.str.8 = [2 x i8] c"\0A\00"
@.str.9 = [45 x i8] c"\0ANumber of pixels total(from rows*cols): %i\0A\00"
@.str.10 = [43 x i8] c"Number of pixels written in file %s: %ld\0A\0A\00"
define i32 @pgmWrite_ASCII(i8* %0, i32 %1, i32 %2, i32** %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32**
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i64
%15 = alloca i64
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32** %3, i32*** %10
store i8* %4, i8** %11
store i64 0, i64* %13
%16 = load i32, i32* %8
%17 = icmp sgt i32 %16, 4500
br i1 %17, label %21, label %18
18: ; preds = %5
%19 = load i32, i32* %9
%20 = icmp sgt i32 %19, 4500
br i1 %20, label %21, label %23
21: ; preds = %18, %5
%22 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %6
br label %89
23: ; preds = %18
%24 = load i8*, i8** %7
%25 = call i32* @fopen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %25, i32** %12
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %29
27: ; preds = %23
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %6
br label %89
29: ; preds = %23
%30 = load i32*, i32** %12
%31 = call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0))
%32 = load i8*, i8** %11
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %38
34: ; preds = %29
%35 = load i32*, i32** %12
%36 = load i8*, i8** %11
%37 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %36)
br label %38
38: ; preds = %34, %29
%39 = load i32*, i32** %12
%40 = load i32, i32* %9
%41 = load i32, i32* %8
%42 = call i32 (i32*, i8*, ...) @fprintf(i32* %39, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %40, i32 %41)
%43 = load i32*, i32** %12
%44 = call i32 (i32*, i8*, ...) @fprintf(i32* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 65535)
store i64 0, i64* %15
br label %45
45: ; preds = %74, %38
%46 = load i64, i64* %15
%47 = load i32, i32* %8
%48 = sext i32 %47 to i64
%49 = icmp slt i64 %46, %48
br i1 %49, label %50, label %77
50: ; preds = %45
store i64 0, i64* %14
br label %51
51: ; preds = %68, %50
%52 = load i64, i64* %14
%53 = load i32, i32* %9
%54 = sext i32 %53 to i64
%55 = icmp slt i64 %52, %54
br i1 %55, label %56, label %71
56: ; preds = %51
%57 = load i32*, i32** %12
%58 = load i32**, i32*** %10
%59 = load i64, i64* %15
%60 = getelementptr inbounds i32*, i32** %58, i64 %59
%61 = load i32*, i32** %60
%62 = load i64, i64* %14
%63 = getelementptr inbounds i32, i32* %61, i64 %62
%64 = load i32, i32* %63
%65 = call i32 (i32*, i8*, ...) @fprintf(i32* %57, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %64)
%66 = load i64, i64* %13
%67 = add nsw i64 %66, 1
store i64 %67, i64* %13
br label %68
68: ; preds = %56
%69 = load i64, i64* %14
%70 = add nsw i64 %69, 1
store i64 %70, i64* %14
br label %51
71: ; preds = %51
%72 = load i32*, i32** %12
%73 = call i32 (i32*, i8*, ...) @fprintf(i32* %72, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
br label %74
74: ; preds = %71
%75 = load i64, i64* %15
%76 = add nsw i64 %75, 1
store i64 %76, i64* %15
br label %45
77: ; preds = %45
%78 = load i32*, i32** %12
%79 = call i32 (i32*, i8*, ...) @fprintf(i32* %78, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.8, i64 0, i64 0))
%80 = load i32, i32* %8
%81 = load i32, i32* %9
%82 = mul nsw i32 %80, %81
%83 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.9, i64 0, i64 0), i32 %82)
%84 = load i8*, i8** %7
%85 = load i64, i64* %13
%86 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.10, i64 0, i64 0), i8* %84, i64 %85)
%87 = load i32*, i32** %12
%88 = call i32 @fclose(i32* %87)
store i32 1, i32* %6
br label %89
89: ; preds = %77, %27, %21
%90 = load i32, i32* %6
ret i32 %90
}

LLVM IR (-O2):
@.str.1 = [2 x i8] c"w\00"
@.str.3 = [4 x i8] c"P2\0A\00"
@.str.4 = [7 x i8] c"# %s \0A\00"
@.str.5 = [8 x i8] c"%i %i \0A\00"
@.str.6 = [4 x i8] c"%d\0A\00"
@.str.7 = [4 x i8] c"%i \00"
@.str.9 = [45 x i8] c"\0ANumber of pixels total(from rows*cols): %i\0A\00"
@.str.10 = [43 x i8] c"Number of pixels written in file %s: %ld\0A\0A\00"
@str = [24 x i8] c"ERROR: file open failed\00"
@str.11 = [55 x i8] c"ERROR: row/col specifications larger than image array:\00"
define i32 @pgmWrite_ASCII(i8* %0, i32 %1, i32 %2, i32** nocapture %3, i8* %4) {
%6 = icmp sgt i32 %1, 4500
%7 = icmp sgt i32 %2, 4500
%8 = or i1 %6, %7
br i1 %8, label %9, label %11
9: ; preds = %5
%10 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([55 x i8], [55 x i8]* @str.11, i64 0, i64 0))
br label %55
11: ; preds = %5
%12 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str, i64 0, i64 0))
br label %55
16: ; preds = %11
%17 = tail call i64 @fwrite(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i64 3, i64 1, i32* %12)
%18 = icmp eq i8* %4, null
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8* %4)
br label %21
21: ; preds = %19, %16
%22 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.5, i64 0, i64 0), i32 %2, i32 %1)
%23 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.6, i64 0, i64 0), i32 65535)
%24 = sext i32 %1 to i64
%25 = icmp sgt i32 %1, 0
br i1 %25, label %26, label %48
26: ; preds = %21
%27 = sext i32 %2 to i64
%28 = icmp sgt i32 %2, 0
%29 = call i64 @llvm.smax.i64(i64 %27, i64 0)
br label %30
30: ; preds = %26, %42
%31 = phi i64 [ 0, %26 ], [ %44, %42 ]
br i1 %28, label %32, label %42
32: ; preds = %30
%33 = getelementptr inbounds i32*, i32** %3, i64 %31
br label %34
34: ; preds = %32, %34
%35 = phi i64 [ 0, %32 ], [ %40, %34 ]
%36 = load i32*, i32** %33
%37 = getelementptr inbounds i32, i32* %36, i64 %35
%38 = load i32, i32* %37
%39 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %12, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.7, i64 0, i64 0), i32 %38)
%40 = add nuw nsw i64 %35, 1
%41 = icmp eq i64 %40, %27
br i1 %41, label %42, label %34
42: ; preds = %34, %30
%43 = tail call i32 @fputc(i32 10, i32* %12)
%44 = add nuw nsw i64 %31, 1
%45 = icmp eq i64 %44, %24
br i1 %45, label %46, label %30
46: ; preds = %42
%47 = mul i64 %29, %24
br label %48
48: ; preds = %46, %21
%49 = phi i64 [ 0, %21 ], [ %47, %46 ]
%50 = tail call i32 @fputc(i32 10, i32* %12)
%51 = mul nsw i32 %2, %1
%52 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([45 x i8], [45 x i8]* @.str.9, i64 0, i64 0), i32 %51)
%53 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([43 x i8], [43 x i8]* @.str.10, i64 0, i64 0), i8* %0, i64 %49)
%54 = tail call i32 @fclose(i32* %12)
br label %55
55: ; preds = %48, %14, %9
%56 = phi i32 [ 0, %9 ], [ 0, %14 ], [ 1, %48 ]
ret i32 %56
}