#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*/ uint8_t;
typedef int /*<<< orphan*/ uint16_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ *calloc(int, int);
int /*<<< orphan*/ errx(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int nextToken(int /*<<< orphan*/ *, char *, int);
scalar_t__ strcmp(char *, char *);
int pgm_load(const char *name, int *w, int *h, uint8_t **img) {
  int n, maxval = -1;
  FILE *fp = fopen(name, "r");
  if (fp) {
    char buf[32];
    n = nextToken(fp, buf, 32);
    if (strcmp(buf, "P5")) {
      errx(0, "File %s has signature \"%s\"; expected \"P5\"", name, buf);
      return -1;
    }
    nextToken(fp, buf, 32);
    *w = atoi(buf);
    nextToken(fp, buf, 32);
    *h = atoi(buf);
    nextToken(fp, buf, 32);
    maxval = atoi(buf);
    n = (*w) * (*h);
    if (maxval < 256) {
      *img = calloc(n, sizeof(uint8_t));
      if (fread(*img, sizeof(uint8_t), n, fp) != n) {
        free(*img);
        return -1;
      }
    } else {
      *img = calloc(n, sizeof(uint16_t));
      if (fread(*img, sizeof(uint16_t), n, fp) != n) {
        free(*img);
        return -1;
      }
    }
    fclose(fp);
  }
  return maxval;
}


x86 assembly (clang, -O0):
<pgm_load>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
movl   $0xffffffff,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe46(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     1353 <pgm_load+0x1c3>
mov    -0x38(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x20,%edx
call   1060 <nextToken@plt>
mov    %eax,-0x2c(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe14(%rip),%rsi
call   1090 <strcmp@plt>
cmp    $0x0,%rax
je     121f <pgm_load+0x8f>
mov    -0x10(%rbp),%rdx
lea    -0x60(%rbp),%rcx
xor    %edi,%edi
lea    0xdf7(%rip),%rsi
call   1070 <errx@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1359 <pgm_load+0x1c9>
mov    -0x38(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x20,%edx
call   1060 <nextToken@plt>
lea    -0x60(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x20,%edx
call   1060 <nextToken@plt>
lea    -0x60(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x20,%edx
call   1060 <nextToken@plt>
lea    -0x60(%rbp),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    -0x20(%rbp),%rcx
imul   (%rcx),%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x100,-0x30(%rbp)
jge    12f5 <pgm_load+0x165>
mov    -0x2c(%rbp),%edi
mov    $0x4,%esi
call   1080 <calloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x4,%esi
call   1040 <fread@plt>
cmp    -0x2c(%rbp),%eax
je     12f0 <pgm_load+0x160>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1359 <pgm_load+0x1c9>
jmp    134a <pgm_load+0x1ba>
mov    -0x2c(%rbp),%edi
mov    $0x4,%esi
call   1080 <calloc@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%edx
mov    -0x38(%rbp),%rcx
mov    $0x4,%esi
call   1040 <fread@plt>
cmp    -0x2c(%rbp),%eax
je     1345 <pgm_load+0x1b5>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
call   1030 <free@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1359 <pgm_load+0x1c9>
jmp    134a <pgm_load+0x1ba>
mov    -0x38(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pgm_load>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,%rbx
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,%rbp
lea    0xe4f(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,%r15
mov    $0xffffffff,%eax
test   %r15,%r15
je     12ac <pgm_load+0x11c>
mov    %rsp,%r14
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x20,%edx
call   1060 <nextToken@plt>
lea    0xe21(%rip),%rsi
mov    %r14,%rdi
call   1090 <strcmp@plt>
test   %rax,%rax
je     1207 <pgm_load+0x77>
lea    0xe10(%rip),%rsi
mov    %rsp,%rcx
xor    %edi,%edi
mov    %rbp,%rdx
call   1070 <errx@plt>
jmp    12a7 <pgm_load+0x117>
mov    %rsp,%rbp
mov    %r15,%rdi
mov    %rbp,%rsi
mov    $0x20,%edx
call   1060 <nextToken@plt>
mov    %rbp,%rdi
call   10b0 <atoi@plt>
mov    %eax,(%r12)
mov    %r15,%rdi
mov    %rbp,%rsi
mov    $0x20,%edx
call   1060 <nextToken@plt>
mov    %rbp,%rdi
call   10b0 <atoi@plt>
mov    %eax,0x0(%r13)
mov    %r15,%rdi
mov    %rbp,%rsi
mov    $0x20,%edx
call   1060 <nextToken@plt>
mov    %rbp,%rdi
call   10b0 <atoi@plt>
mov    %eax,%r14d
mov    0x0(%r13),%ebp
imul   (%r12),%ebp
mov    %ebp,%edi
mov    $0x4,%esi
call   1080 <calloc@plt>
mov    %rax,(%rbx)
mov    %rax,%rdi
mov    $0x4,%esi
mov    %ebp,%edx
mov    %r15,%rcx
call   1040 <fread@plt>
cmp    $0xff,%r14d
cmp    %ebp,%eax
jne    129f <pgm_load+0x10f>
mov    %r15,%rdi
call   1050 <fclose@plt>
mov    %r14d,%eax
jmp    12ac <pgm_load+0x11c>
mov    (%rbx),%rdi
call   1030 <free@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"P5\00"
@.str.2 = [42 x i8] c"File %s has signature \22%s\22; expected \22P5\22\00"
define i32 @pgm_load(i8* %0, i32* %1, i32* %2, i32** %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32**
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca [32 x i8]
store i8* %0, i8** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i32** %3, i32*** %9
store i32 -1, i32* %11
%14 = load i8*, i8** %6
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %12
%16 = load i32*, i32** %12
%17 = icmp ne i32* %16, null
br i1 %17, label %18, label %89
18: ; preds = %4
%19 = load i32*, i32** %12
%20 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%21 = call i32 @nextToken(i32* %19, i8* %20, i32 32)
store i32 %21, i32* %10
%22 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%24 = icmp ne i64 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %18
%26 = load i8*, i8** %6
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%28 = call i32 @errx(i32 0, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %26, i8* %27)
store i32 -1, i32* %5
br label %91
29: ; preds = %18
%30 = load i32*, i32** %12
%31 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%32 = call i32 @nextToken(i32* %30, i8* %31, i32 32)
%33 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%34 = call i32 @atoi(i8* %33)
%35 = load i32*, i32** %7
store i32 %34, i32* %35
%36 = load i32*, i32** %12
%37 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%38 = call i32 @nextToken(i32* %36, i8* %37, i32 32)
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%40 = call i32 @atoi(i8* %39)
%41 = load i32*, i32** %8
store i32 %40, i32* %41
%42 = load i32*, i32** %12
%43 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%44 = call i32 @nextToken(i32* %42, i8* %43, i32 32)
%45 = getelementptr inbounds [32 x i8], [32 x i8]* %13, i64 0, i64 0
%46 = call i32 @atoi(i8* %45)
store i32 %46, i32* %11
%47 = load i32*, i32** %7
%48 = load i32, i32* %47
%49 = load i32*, i32** %8
%50 = load i32, i32* %49
%51 = mul nsw i32 %48, %50
store i32 %51, i32* %10
%52 = load i32, i32* %11
%53 = icmp slt i32 %52, 256
br i1 %53, label %54, label %70
54: ; preds = %29
%55 = load i32, i32* %10
%56 = call i32* @calloc(i32 %55, i32 4)
%57 = load i32**, i32*** %9
store i32* %56, i32** %57
%58 = load i32**, i32*** %9
%59 = load i32*, i32** %58
%60 = load i32, i32* %10
%61 = load i32*, i32** %12
%62 = call i32 @fread(i32* %59, i32 4, i32 %60, i32* %61)
%63 = load i32, i32* %10
%64 = icmp ne i32 %62, %63
br i1 %64, label %65, label %69
65: ; preds = %54
%66 = load i32**, i32*** %9
%67 = load i32*, i32** %66
%68 = call i32 @free(i32* %67)
store i32 -1, i32* %5
br label %91
69: ; preds = %54
br label %86
70: ; preds = %29
%71 = load i32, i32* %10
%72 = call i32* @calloc(i32 %71, i32 4)
%73 = load i32**, i32*** %9
store i32* %72, i32** %73
%74 = load i32**, i32*** %9
%75 = load i32*, i32** %74
%76 = load i32, i32* %10
%77 = load i32*, i32** %12
%78 = call i32 @fread(i32* %75, i32 4, i32 %76, i32* %77)
%79 = load i32, i32* %10
%80 = icmp ne i32 %78, %79
br i1 %80, label %81, label %85
81: ; preds = %70
%82 = load i32**, i32*** %9
%83 = load i32*, i32** %82
%84 = call i32 @free(i32* %83)
store i32 -1, i32* %5
br label %91
85: ; preds = %70
br label %86
86: ; preds = %85, %69
%87 = load i32*, i32** %12
%88 = call i32 @fclose(i32* %87)
br label %89
89: ; preds = %86, %4
%90 = load i32, i32* %11
store i32 %90, i32* %5
br label %91
91: ; preds = %89, %81, %65, %25
%92 = load i32, i32* %5
ret i32 %92
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [3 x i8] c"P5\00"
@.str.2 = [42 x i8] c"File %s has signature \22%s\22; expected \22P5\22\00"
define i32 @pgm_load(i8* %0, i32* nocapture %1, i32* nocapture %2, i32** nocapture %3) {
%5 = alloca [32 x i8]
%6 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %40, label %8
8: ; preds = %4
%9 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%10 = call i32 @nextToken(i32* %6, i8* %9, i32 32)
%11 = call i64 @strcmp(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i64 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %8
%14 = call i32 @errx(i32 0, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %9)
br label %37
15: ; preds = %8
%16 = call i32 @nextToken(i32* %6, i8* %9, i32 32)
%17 = call i32 @atoi(i8* %9)
store i32 %17, i32* %1
%18 = call i32 @nextToken(i32* %6, i8* %9, i32 32)
%19 = call i32 @atoi(i8* %9)
store i32 %19, i32* %2
%20 = call i32 @nextToken(i32* %6, i8* %9, i32 32)
%21 = call i32 @atoi(i8* %9)
%22 = load i32, i32* %1
%23 = load i32, i32* %2
%24 = mul nsw i32 %23, %22
%25 = icmp slt i32 %21, 256
%26 = call i32* @calloc(i32 %24, i32 4)
store i32* %26, i32** %3
%27 = call i32 @fread(i32* %26, i32 4, i32 %24, i32* %6)
%28 = icmp eq i32 %27, %24
br i1 %25, label %29, label %33
29: ; preds = %15
br i1 %28, label %38, label %30
30: ; preds = %29
%31 = load i32*, i32** %3
%32 = call i32 @free(i32* %31)
br label %37
33: ; preds = %15
br i1 %28, label %38, label %34
34: ; preds = %33
%35 = load i32*, i32** %3
%36 = call i32 @free(i32* %35)
br label %37
37: ; preds = %13, %30, %34
br label %40
38: ; preds = %29, %33
%39 = call i32 @fclose(i32* %6)
br label %40
40: ; preds = %4, %38, %37
%41 = phi i32 [ -1, %37 ], [ %21, %38 ], [ -1, %4 ]
ret i32 %41
}