#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, int);
int /*<<< orphan*/ fputc(unsigned char, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
char *malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
scalar_t__ strlen(char *);
void debug_dump_pgm(char *path_, unsigned char *luma, int w, int h) {
  FILE *out;
  char *path, *extension = ".dump.pgm";
  path = malloc(strlen(path_) + strlen(extension));
  strcpy(path, path_);
  strcat(path, extension);
  out = fopen(path, "wb");
  free(path);
  fprintf(out, "P5 %d %d 255\n", w, h);
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      fputc(luma[j + i * w], out);
  fclose(out);
}


x86 assembly (clang, -O0):
<debug_dump_pgm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x40(%rbp),%rdi
add    %rax,%rdi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   10b0 <strcat@plt>
mov    -0x28(%rbp),%rdi
lea    0xe0e(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
lea    0xdee(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    128d <debug_dump_pgm+0xfd>
movl   $0x0,-0x38(%rbp)
mov    -0x38(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    127a <debug_dump_pgm+0xea>
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%ecx
mov    -0x34(%rbp),%edx
imul   -0x14(%rbp),%edx
add    %edx,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%al
mov    -0x20(%rbp),%rsi
movzbl %al,%edi
call   1070 <fputc@plt>
mov    -0x38(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x38(%rbp)
jmp    123e <debug_dump_pgm+0xae>
jmp    127f <debug_dump_pgm+0xef>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    122b <debug_dump_pgm+0x9b>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<debug_dump_pgm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%ebx
mov    %rsi,%r13
mov    %rdi,%rbp
call   1060 <strlen@plt>
lea    0x9(%rax),%rdi
call   1090 <malloc@plt>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbp,%rsi
call   1040 <strcpy@plt>
lea    0xe34(%rip),%rsi
mov    %r12,%rdi
call   10b0 <strcat@plt>
lea    0xe2f(%rip),%rsi
mov    %r12,%rdi
call   10a0 <fopen@plt>
mov    %rax,%rbp
mov    %r12,%rdi
call   1030 <free@plt>
lea    0xe18(%rip),%rsi
mov    %rbp,%rdi
mov    %ebx,0xc(%rsp)
mov    %ebx,%edx
mov    %r15d,%ecx
call   1080 <fprintf@plt>
test   %r15d,%r15d
jle    1269 <debug_dump_pgm+0xd9>
mov    0xc(%rsp),%eax
movslq %eax,%rcx
mov    %rcx,0x10(%rsp)
mov    %r15d,%r15d
mov    %eax,%ebx
xor    %r14d,%r14d
jmp    123e <debug_dump_pgm+0xae>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
add    $0x1,%r14
add    0x10(%rsp),%r13
cmp    %r15,%r14
je     1269 <debug_dump_pgm+0xd9>
cmpl   $0x0,0xc(%rsp)
jle    1230 <debug_dump_pgm+0xa0>
xor    %r12d,%r12d
nopl   0x0(%rax,%rax,1)
movzbl 0x0(%r13,%r12,1),%edi
mov    %rbp,%rsi
call   1070 <fputc@plt>
add    $0x1,%r12
cmp    %r12,%rbx
jne    1250 <debug_dump_pgm+0xc0>
jmp    1230 <debug_dump_pgm+0xa0>
mov    %rbp,%rdi
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <fclose@plt>

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

LLVM IR (-O2):
@.str = [10 x i8] c".dump.pgm\00"
@.str.1 = [3 x i8] c"wb\00"
@.str.2 = [14 x i8] c"P5 %d %d 255\0A\00"
define void @debug_dump_pgm(i8* %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%6 = add nsw i64 %5, 9
%7 = tail call i8* @malloc(i64 %6)
%8 = tail call i32 @strcpy(i8* %7, i8* %0)
%9 = tail call i32 @strcat(i8* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32* @fopen(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%11 = tail call i32 @free(i8* %7)
%12 = tail call i32 @fprintf(i32* %10, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %2, i32 %3)
%13 = icmp sgt i32 %3, 0
br i1 %13, label %14, label %23
14: ; preds = %4
%15 = icmp sgt i32 %2, 0
%16 = sext i32 %2 to i64
%17 = zext i32 %3 to i64
%18 = zext i32 %2 to i64
br label %19
19: ; preds = %14, %25
%20 = phi i64 [ 0, %14 ], [ %26, %25 ]
br i1 %15, label %21, label %25
21: ; preds = %19
%22 = mul nsw i64 %20, %16
br label %28
23: ; preds = %25, %4
%24 = tail call i32 @fclose(i32* %10)
ret void
25: ; preds = %28, %19
%26 = add nuw nsw i64 %20, 1
%27 = icmp eq i64 %26, %17
br i1 %27, label %23, label %19
28: ; preds = %21, %28
%29 = phi i64 [ 0, %21 ], [ %34, %28 ]
%30 = add nsw i64 %29, %22
%31 = getelementptr inbounds i8, i8* %1, i64 %30
%32 = load i8, i8* %31
%33 = tail call i32 @fputc(i8 zeroext %32, i32* %10)
%34 = add nuw nsw i64 %29, 1
%35 = icmp eq i64 %34, %18
br i1 %35, label %25, label %28
}