#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*/ SEEK_CUR;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fread(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, int *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, char *);
void inputimgpm(char *fname, unsigned char **img, int *ny, int *nx) {
  FILE *fimg;
  int i, c, bufsize = 1000, imagesize;
  char buf[1000];
  fimg = fopen(fname, "rb");
  if (!fimg) {
    printf("unable to read %s\n", fname);
    exit(-1);
  }
  fgets(buf, bufsize, fimg);
  if (buf[0] == 'P') {
    if (buf[1] == '5')
      imagesize = 1;
    else if (buf[1] == '6')
      imagesize = 3;
    else {
      printf("input image %s, unknown type\n", fname);
      exit(-1);
    }
  } else {
    printf("input image %s, unknown type\n", fname);
    exit(-1);
  }
  c = fgetc(fimg);
  while (c == '#') {
    fgets(buf, bufsize, fimg);
    c = fgetc(fimg);
  }
  fseek(fimg, -1, SEEK_CUR);
  fscanf(fimg, "%d %d\n", nx, ny);
  fgets(buf, bufsize, fimg);
  imagesize = imagesize * (*ny) * (*nx);
  *img = (unsigned char *)malloc(imagesize * sizeof(unsigned char));
  fread(*img, sizeof(unsigned char), imagesize, fimg);
  fclose(fimg);
}


x86 assembly (clang, -O0):
<inputimgpm>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movl   $0x3e8,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe33(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11fb <inputimgpm+0x5b>
mov    -0x8(%rbp),%rsi
lea    0xe17(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
lea    -0x420(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1080 <fgets@plt>
movsbl -0x420(%rbp),%eax
cmp    $0x50,%eax
jne    127a <inputimgpm+0xda>
movsbl -0x41f(%rbp),%eax
cmp    $0x35,%eax
jne    123a <inputimgpm+0x9a>
movl   $0x1,-0x38(%rbp)
jmp    1275 <inputimgpm+0xd5>
movsbl -0x41f(%rbp),%eax
cmp    $0x36,%eax
jne    1256 <inputimgpm+0xb6>
movl   $0x3,-0x38(%rbp)
jmp    1270 <inputimgpm+0xd0>
mov    -0x8(%rbp),%rsi
lea    0xdb5(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
jmp    1275 <inputimgpm+0xd5>
jmp    1294 <inputimgpm+0xf4>
mov    -0x8(%rbp),%rsi
lea    0xd91(%rip),%rdi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0x30(%rbp)
cmpl   $0x23,-0x30(%rbp)
jne    12ce <inputimgpm+0x12e>
lea    -0x420(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1080 <fgets@plt>
mov    -0x28(%rbp),%rdi
call   1070 <fgetc@plt>
mov    %eax,-0x30(%rbp)
jmp    12a0 <inputimgpm+0x100>
mov    -0x28(%rbp),%rdi
mov    0x2d0f(%rip),%rax
mov    (%rax),%edx
mov    $0xffffffff,%esi
call   10a0 <fseek@plt>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%rcx
lea    0xd3c(%rip),%rsi
call   1060 <fscanf@plt>
lea    -0x420(%rbp),%rdi
mov    -0x34(%rbp),%esi
mov    -0x28(%rbp),%rdx
call   1080 <fgets@plt>
mov    -0x38(%rbp),%eax
mov    -0x18(%rbp),%rcx
imul   (%rcx),%eax
mov    -0x20(%rbp),%rcx
imul   (%rcx),%eax
mov    %eax,-0x38(%rbp)
movslq -0x38(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x38(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
mov    -0x28(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<inputimgpm>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
mov    %rcx,%r15
mov    %rdx,%r12
mov    %rsi,%rbp
mov    %rdi,%r13
lea    0xe3c(%rip),%rsi
call   10b0 <fopen@plt>
test   %rax,%rax
je     12d9 <inputimgpm+0x139>
mov    %rax,%rbx
mov    %rbp,0x8(%rsp)
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
cmpb   $0x50,0x10(%rsp)
jne    12d0 <inputimgpm+0x130>
mov    $0x1,%r14d
mov    0x11(%rsp),%al
cmp    $0x35,%al
je     1213 <inputimgpm+0x73>
cmp    $0x36,%al
jne    12d0 <inputimgpm+0x130>
mov    $0x3,%r14d
mov    %rbx,%rdi
call   1070 <fgetc@plt>
cmp    $0x23,%eax
jne    124d <inputimgpm+0xad>
lea    0x10(%rsp),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbp,%rdi
mov    $0x3e8,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
mov    %rbx,%rdi
call   1070 <fgetc@plt>
cmp    $0x23,%eax
je     1230 <inputimgpm+0x90>
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0xffffffff,%esi
call   10a0 <fseek@plt>
lea    0xdca(%rip),%rsi
mov    %rbx,%rdi
mov    %r15,%rdx
mov    %r12,%rcx
call   1060 <fscanf@plt>
lea    0x10(%rsp),%rdi
mov    $0x3e8,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
imul   (%r12),%r14d
imul   (%r15),%r14d
mov    %r14d,%edi
call   1090 <malloc@plt>
mov    0x8(%rsp),%rcx
mov    %rax,(%rcx)
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r14d,%edx
mov    %rbx,%rcx
call   1030 <fread@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x3f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd3f(%rip),%rdi
jmp    12e0 <inputimgpm+0x140>
lea    0xd23(%rip),%rdi
mov    %r13,%rsi
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"unable to read %s\0A\00"
@.str.2 = [30 x i8] c"input image %s, unknown type\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [7 x i8] c"%d %d\0A\00"
define void @inputimgpm(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 i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca [1000 x i8]
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i32* %2, i32** %7
store i32* %3, i32** %8
store i32 1000, i32* %12
%15 = load i8*, i8** %5
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %9
%17 = load i32*, i32** %9
%18 = icmp ne i32* %17, null
br i1 %18, label %23, label %19
19: ; preds = %4
%20 = load i8*, i8** %5
%21 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %20)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %4
%24 = getelementptr inbounds [1000 x i8], [1000 x i8]* %14, i64 0, i64 0
%25 = load i32, i32* %12
%26 = load i32*, i32** %9
%27 = call i32 @fgets(i8* %24, i32 %25, i32* %26)
%28 = getelementptr inbounds [1000 x i8], [1000 x i8]* %14, i64 0, i64 0
%29 = load i8, i8* %28
%30 = sext i8 %29 to i32
%31 = icmp eq i32 %30, 80
br i1 %31, label %32, label %50
32: ; preds = %23
%33 = getelementptr inbounds [1000 x i8], [1000 x i8]* %14, i64 0, i64 1
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 53
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 1, i32* %13
br label %49
38: ; preds = %32
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %14, i64 0, i64 1
%40 = load i8, i8* %39
%41 = sext i8 %40 to i32
%42 = icmp eq i32 %41, 54
br i1 %42, label %43, label %44
43: ; preds = %38
store i32 3, i32* %13
br label %48
44: ; preds = %38
%45 = load i8*, i8** %5
%46 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %45)
%47 = call i32 @exit(i32 -1)
unreachable
48: ; preds = %43
br label %49
49: ; preds = %48, %37
br label %54
50: ; preds = %23
%51 = load i8*, i8** %5
%52 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %51)
%53 = call i32 @exit(i32 -1)
unreachable
54: ; preds = %49
%55 = load i32*, i32** %9
%56 = call i32 @fgetc(i32* %55)
store i32 %56, i32* %11
br label %57
57: ; preds = %60, %54
%58 = load i32, i32* %11
%59 = icmp eq i32 %58, 35
br i1 %59, label %60, label %67
60: ; preds = %57
%61 = getelementptr inbounds [1000 x i8], [1000 x i8]* %14, i64 0, i64 0
%62 = load i32, i32* %12
%63 = load i32*, i32** %9
%64 = call i32 @fgets(i8* %61, i32 %62, i32* %63)
%65 = load i32*, i32** %9
%66 = call i32 @fgetc(i32* %65)
store i32 %66, i32* %11
br label %57
67: ; preds = %57
%68 = load i32*, i32** %9
%69 = load i32, i32* @SEEK_CUR
%70 = call i32 @fseek(i32* %68, i32 -1, i32 %69)
%71 = load i32*, i32** %9
%72 = load i32*, i32** %8
%73 = load i32*, i32** %7
%74 = call i32 @fscanf(i32* %71, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32* %72, i32* %73)
%75 = getelementptr inbounds [1000 x i8], [1000 x i8]* %14, i64 0, i64 0
%76 = load i32, i32* %12
%77 = load i32*, i32** %9
%78 = call i32 @fgets(i8* %75, i32 %76, i32* %77)
%79 = load i32, i32* %13
%80 = load i32*, i32** %7
%81 = load i32, i32* %80
%82 = mul nsw i32 %79, %81
%83 = load i32*, i32** %8
%84 = load i32, i32* %83
%85 = mul nsw i32 %82, %84
store i32 %85, i32* %13
%86 = load i32, i32* %13
%87 = sext i32 %86 to i64
%88 = mul i64 %87, 1
%89 = trunc i64 %88 to i32
%90 = call i64 @malloc(i32 %89)
%91 = inttoptr i64 %90 to i8*
%92 = load i8**, i8*** %6
store i8* %91, i8** %92
%93 = load i8**, i8*** %6
%94 = load i8*, i8** %93
%95 = load i32, i32* %13
%96 = load i32*, i32** %9
%97 = call i32 @fread(i8* %94, i32 1, i32 %95, i32* %96)
%98 = load i32*, i32** %9
%99 = call i32 @fclose(i32* %98)
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [19 x i8] c"unable to read %s\0A\00"
@.str.2 = [30 x i8] c"input image %s, unknown type\0A\00"
@SEEK_CUR = global i32 0
@.str.3 = [7 x i8] c"%d %d\0A\00"
define void @inputimgpm(i8* %0, i8** nocapture writeonly %1, i32* %2, i32* %3) {
%5 = alloca [1000 x i8]
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 0
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%11 = tail call i32 @exit(i32 -1)
unreachable
12: ; preds = %4
%13 = call i32 @fgets(i8* %6, i32 1000, i32* %7)
%14 = load i8, i8* %6
%15 = icmp eq i8 %14, 80
br i1 %15, label %16, label %23
16: ; preds = %12
%17 = getelementptr inbounds [1000 x i8], [1000 x i8]* %5, i64 0, i64 1
%18 = load i8, i8* %17
switch i8 %18, label %20 [
i8 53, label %26
i8 54, label %19
]
19: ; preds = %16
br label %26
20: ; preds = %16
%21 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%22 = call i32 @exit(i32 -1)
unreachable
23: ; preds = %12
%24 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%25 = call i32 @exit(i32 -1)
unreachable
26: ; preds = %16, %19
%27 = phi i32 [ 3, %19 ], [ 1, %16 ]
%28 = call i32 @fgetc(i32* %7)
%29 = icmp eq i32 %28, 35
br i1 %29, label %30, label %34
30: ; preds = %26, %30
%31 = call i32 @fgets(i8* %6, i32 1000, i32* %7)
%32 = call i32 @fgetc(i32* %7)
%33 = icmp eq i32 %32, 35
br i1 %33, label %30, label %34
34: ; preds = %30, %26
%35 = load i32, i32* @SEEK_CUR
%36 = call i32 @fseek(i32* %7, i32 -1, i32 %35)
%37 = call i32 @fscanf(i32* %7, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0), i32* %3, i32* %2)
%38 = call i32 @fgets(i8* %6, i32 1000, i32* %7)
%39 = load i32, i32* %2
%40 = mul nsw i32 %39, %27
%41 = load i32, i32* %3
%42 = mul nsw i32 %40, %41
%43 = call i64 @malloc(i32 %42)
%44 = inttoptr i64 %43 to i8*
store i8* %44, i8** %1
%45 = call i32 @fread(i8* %44, i32 1, i32 %42, i32* %7)
%46 = call i32 @fclose(i32* %7)
ret void
}