#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*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
void outputimgpm(char *fname, unsigned char *img, int ny, int nx, int dim) {
  FILE *fimg;
  fimg = fopen(fname, "wb");
  if (dim == 3) {
    fprintf(fimg, "P6\n");
    fprintf(fimg, "%d %d\n", nx, ny);
    fprintf(fimg, "255\n");
  } else if (dim == 1) {
    fprintf(fimg, "P5\n");
    fprintf(fimg, "%d %d\n", nx, ny);
    fprintf(fimg, "255\n");
  } else {
    printf("output image, unknown type\n");
    exit(-1);
  }
  fwrite(img, sizeof(unsigned char), ny * nx * dim, fimg);
  fclose(fimg);
}


x86 assembly (clang, -O0):
<outputimgpm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpl   $0x3,-0x1c(%rbp)
jne    11d9 <outputimgpm+0x79>
mov    -0x28(%rbp),%rdi
lea    0xe60(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe4c(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xe41(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    123f <outputimgpm+0xdf>
cmpl   $0x1,-0x1c(%rbp)
jne    1224 <outputimgpm+0xc4>
mov    -0x28(%rbp),%rdi
lea    0xe25(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
lea    0xe01(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x28(%rbp),%rdi
lea    0xdf6(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
jmp    123a <outputimgpm+0xda>
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
jmp    123f <outputimgpm+0xdf>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%edx
imul   -0x18(%rbp),%edx
imul   -0x1c(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1080 <fwrite@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<outputimgpm>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r15d
mov    %ecx,%ebx
mov    %edx,%r12d
mov    %rsi,%r14
lea    0xe86(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,%rbp
cmp    $0x3,%r15d
je     1197 <outputimgpm+0x37>
cmp    $0x1,%r15d
jne    1209 <outputimgpm+0xa9>
lea    0xe7e(%rip),%rdi
jmp    119e <outputimgpm+0x3e>
lea    0xe65(%rip),%rdi
mov    $0x3,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
lea    0xe50(%rip),%rsi
mov    %rbp,%rdi
mov    %ebx,%edx
mov    %r12d,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
lea    0xe41(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
imul   %r12d,%ebx
imul   %r15d,%ebx
mov    %r14,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %rbp,%rcx
call   1080 <fwrite@plt>
mov    %rbp,%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <fclose@plt>
lea    0xe07(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"wb\00"
@.str.1 = [4 x i8] c"P6\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [5 x i8] c"255\0A\00"
@.str.4 = [4 x i8] c"P5\0A\00"
@.str.5 = [28 x i8] c"output image, unknown type\0A\00"
define void @outputimgpm(i8* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32*
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%12 = load i8*, i8** %6
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %11
%14 = load i32, i32* %10
%15 = icmp eq i32 %14, 3
br i1 %15, label %16, label %25
16: ; preds = %5
%17 = load i32*, i32** %11
%18 = call i32 (i32*, i8*, ...) @fprintf(i32* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%19 = load i32*, i32** %11
%20 = load i32, i32* %9
%21 = load i32, i32* %8
%22 = call i32 (i32*, i8*, ...) @fprintf(i32* %19, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %20, i32 %21)
%23 = load i32*, i32** %11
%24 = call i32 (i32*, i8*, ...) @fprintf(i32* %23, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %41
25: ; preds = %5
%26 = load i32, i32* %10
%27 = icmp eq i32 %26, 1
br i1 %27, label %28, label %37
28: ; preds = %25
%29 = load i32*, i32** %11
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0))
%31 = load i32*, i32** %11
%32 = load i32, i32* %9
%33 = load i32, i32* %8
%34 = call i32 (i32*, i8*, ...) @fprintf(i32* %31, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %32, i32 %33)
%35 = load i32*, i32** %11
%36 = call i32 (i32*, i8*, ...) @fprintf(i32* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %40
37: ; preds = %25
%38 = call i32 @printf(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.5, i64 0, i64 0))
%39 = call i32 @exit(i32 -1)
unreachable
40: ; preds = %28
br label %41
41: ; preds = %40, %16
%42 = load i8*, i8** %7
%43 = load i32, i32* %8
%44 = load i32, i32* %9
%45 = mul nsw i32 %43, %44
%46 = load i32, i32* %10
%47 = mul nsw i32 %45, %46
%48 = load i32*, i32** %11
%49 = call i32 @fwrite(i8* %42, i32 1, i32 %47, i32* %48)
%50 = load i32*, i32** %11
%51 = call i32 @fclose(i32* %50)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"wb\00"
@.str.1 = [4 x i8] c"P6\0A\00"
@.str.2 = [7 x i8] c"%d %d\0A\00"
@.str.3 = [5 x i8] c"255\0A\00"
@.str.4 = [4 x i8] c"P5\0A\00"
@str = [27 x i8] c"output image, unknown type\00"
define void @outputimgpm(i8* nocapture %0, i8* nocapture %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
switch i32 %4, label %8 [
i32 3, label %11
i32 1, label %7
]
7: ; preds = %5
br label %11
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([27 x i8], [27 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 -1)
unreachable
11: ; preds = %5, %7
%12 = phi i8* [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), %7 ], [ getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), %5 ]
%13 = tail call i64 bitcast (i32 (i8*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* %12, i64 3, i64 1, i32* %6)
%14 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %3, i32 %2)
%15 = tail call i64 bitcast (i32 (i8*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 4, i64 1, i32* %6)
%16 = mul nsw i32 %3, %2
%17 = mul nsw i32 %16, %4
%18 = tail call i32 @fwrite(i8* %1, i32 1, i32 %17, i32* %6)
%19 = tail call i32 @fclose(i32* %6)
ret void
}