#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*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int /*<<< orphan*/ stderr;
void write_pgm(int **img, int W, int H, char *pfile) {
  int i, j;
  FILE *fd;
  fd = fopen(pfile, "w");
  if (!fd) {
    fprintf(stderr, "Error creando archivo '%s'\n", pfile);
    exit(-1);
  }
  fprintf(fd, "P2\n%d %d\n255\n", W, H);
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++)
      fprintf(fd, " %3d", img[i][j]);
    fprintf(fd, "\n");
  }
  fclose(fd);
}


x86 assembly (clang, -O0):
<write_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe9f(%rip),%rsi
call   1050 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    119b <write_pgm+0x5b>
mov    0x2e64(%rip),%rax
movslq (%rax),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe78(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    $0xffffffff,%edi
call   1060 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x10(%rbp),%eax
jge    122c <write_pgm+0xec>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    120c <write_pgm+0xcc>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rax
movslq -0x20(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe35(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11cd <write_pgm+0x8d>
mov    -0x28(%rbp),%rdi
lea    0xe05(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11ba <write_pgm+0x7a>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<write_pgm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,%r12
mov    %edx,%r15d
mov    %esi,%ebx
mov    %rdi,%r13
lea    0xe90(%rip),%rsi
mov    %rcx,%rdi
call   1060 <fopen@plt>
test   %rax,%rax
je     1227 <write_pgm+0xd7>
mov    %rax,%rbp
lea    0xe93(%rip),%rsi
mov    %rax,%rdi
mov    %ebx,%edx
mov    %r15d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
test   %r15d,%r15d
jle    1211 <write_pgm+0xc1>
mov    %ebx,%eax
mov    %r15d,%ecx
mov    %rcx,0x10(%rsp)
mov    %ebx,%ebx
lea    0xe7a(%rip),%r15
xor    %r14d,%r14d
mov    %eax,0xc(%rsp)
jmp    11dc <write_pgm+0x8c>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
mov    %rbp,%rsi
call   1040 <fputc@plt>
add    $0x1,%r14
cmp    0x10(%rsp),%r14
mov    0xc(%rsp),%eax
je     1211 <write_pgm+0xc1>
test   %eax,%eax
jle    11c0 <write_pgm+0x70>
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x0(%r13,%r14,8),%rax
mov    (%rax,%r12,4),%edx
mov    %rbp,%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%rbx
jne    11f0 <write_pgm+0xa0>
jmp    11c0 <write_pgm+0x70>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <fclose@plt>
mov    0x2db2(%rip),%rax
movslq (%rax),%rdi
lea    0xdca(%rip),%rsi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Error creando archivo '%s'\0A\00"
@.str.2 = [14 x i8] c"P2\0A%d %d\0A255\0A\00"
@.str.3 = [5 x i8] c" %3d\00"
@.str.4 = [2 x i8] c"\0A\00"
define void @write_pgm(i32** %0, i32 %1, i32 %2, i8* %3) {
%5 = alloca i32**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i32** %0, i32*** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%12 = load i8*, i8** %8
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %11
%14 = load i32*, i32** %11
%15 = icmp ne i32* %14, null
br i1 %15, label %23, 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** %8
%21 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %4
%24 = load i32*, i32** %11
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = call i32 (i32*, i8*, ...) @fprintf(i32* %24, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %25, i32 %26)
store i32 0, i32* %9
br label %28
28: ; preds = %55, %23
%29 = load i32, i32* %9
%30 = load i32, i32* %7
%31 = icmp slt i32 %29, %30
br i1 %31, label %32, label %58
32: ; preds = %28
store i32 0, i32* %10
br label %33
33: ; preds = %49, %32
%34 = load i32, i32* %10
%35 = load i32, i32* %6
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %52
37: ; preds = %33
%38 = load i32*, i32** %11
%39 = load i32**, i32*** %5
%40 = load i32, i32* %9
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i32*, i32** %39, i64 %41
%43 = load i32*, i32** %42
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i32, i32* %43, i64 %45
%47 = load i32, i32* %46
%48 = call i32 (i32*, i8*, ...) @fprintf(i32* %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %47)
br label %49
49: ; preds = %37
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %33
52: ; preds = %33
%53 = load i32*, i32** %11
%54 = call i32 (i32*, i8*, ...) @fprintf(i32* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
br label %55
55: ; preds = %52
%56 = load i32, i32* %9
%57 = add nsw i32 %56, 1
store i32 %57, i32* %9
br label %28
58: ; preds = %28
%59 = load i32*, i32** %11
%60 = call i32 @fclose(i32* %59)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@stderr = global i32 0
@.str.1 = [28 x i8] c"Error creando archivo '%s'\0A\00"
@.str.2 = [14 x i8] c"P2\0A%d %d\0A255\0A\00"
@.str.3 = [5 x i8] c" %3d\00"
define void @write_pgm(i32** nocapture %0, i32 %1, i32 %2, i8* %3) {
%5 = tail call i32* @fopen(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %13
7: ; preds = %4
%8 = load i32, i32* @stderr
%9 = sext i32 %8 to i64
%10 = inttoptr i64 %9 to i32*
%11 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %10, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %4
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %2)
%15 = icmp sgt i32 %2, 0
br i1 %15, label %16, label %36
16: ; preds = %13
%17 = icmp sgt i32 %1, 0
%18 = zext i32 %2 to i64
%19 = zext i32 %1 to i64
br label %20
20: ; preds = %16, %32
%21 = phi i64 [ 0, %16 ], [ %34, %32 ]
br i1 %17, label %22, label %32
22: ; preds = %20
%23 = getelementptr inbounds i32*, i32** %0, i64 %21
br label %24
24: ; preds = %22, %24
%25 = phi i64 [ 0, %22 ], [ %30, %24 ]
%26 = load i32*, i32** %23
%27 = getelementptr inbounds i32, i32* %26, i64 %25
%28 = load i32, i32* %27
%29 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i32 %28)
%30 = add nuw nsw i64 %25, 1
%31 = icmp eq i64 %30, %19
br i1 %31, label %32, label %24
32: ; preds = %24, %20
%33 = tail call i32 @fputc(i32 10, i32* %5)
%34 = add nuw nsw i64 %21, 1
%35 = icmp eq i64 %34, %18
br i1 %35, label %36, label %20
36: ; preds = %32, %13
%37 = tail call i32 @fclose(i32* %5)
ret void
}