#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 *);
size_t fread(unsigned char *, int, size_t, int /*<<< orphan*/ *);
int get_pgm_header(int /*<<< orphan*/ *, size_t *, size_t *, int *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int import_pgm_8bit(unsigned char *buf, size_t *pwidth, size_t *pheight,
                    int *pmaxval, char *filename) {
  FILE *f;
  size_t size;
  int r;
  if (!(f = fopen(filename, "r"))) {
    printf("ERR:8bit_pgm\n");
    return 1;
  }
  if ((r = get_pgm_header(f, pwidth, pheight, pmaxval))) {
    printf("ERR:8bit_pgm:get_pgm_header:%d\n", r);
    return 2;
  }
  if ((*pmaxval) > 256) {
    printf("ERR: pgm not <8 bit.\n");
    return 4;
  }
  size = (*pwidth) * (*pheight);
  if (fread(buf, 1, size, f) != size) {
    fprintf(stderr, "ERROR: couldn't load pgm data\n");
    return 5;
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<import_pgm_8bit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe79(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
jne    11b4 <import_pgm_8bit+0x54>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1280 <import_pgm_8bit+0x120>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <get_pgm_header@plt>
mov    %eax,-0x44(%rbp)
cmp    $0x0,%eax
je     11f2 <import_pgm_8bit+0x92>
mov    -0x44(%rbp),%esi
lea    0xe31(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1280 <import_pgm_8bit+0x120>
mov    -0x28(%rbp),%rax
cmpl   $0x100,(%rax)
jle    121c <import_pgm_8bit+0xbc>
lea    0xe27(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x4,-0x4(%rbp)
jmp    1280 <import_pgm_8bit+0x120>
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
imul   (%rcx),%rax
mov    %rax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    $0x1,%esi
call   1030 <fread@plt>
cmp    -0x40(%rbp),%rax
je     1270 <import_pgm_8bit+0x110>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
lea    0xde7(%rip),%rsi
call   1070 <fprintf@plt>
movl   $0x5,-0x4(%rbp)
jmp    1280 <import_pgm_8bit+0x120>
mov    -0x38(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<import_pgm_8bit>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r13
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%r14
lea    0xe74(%rip),%rsi
mov    %r8,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11c8 <import_pgm_8bit+0x58>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %r13,%rcx
call   1060 <get_pgm_header@plt>
test   %eax,%eax
je     11db <import_pgm_8bit+0x6b>
lea    0xe4a(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
jmp    123e <import_pgm_8bit+0xce>
lea    0xe72(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    123e <import_pgm_8bit+0xce>
cmpl   $0x101,0x0(%r13)
jl     11f8 <import_pgm_8bit+0x88>
lea    0xe62(%rip),%rdi
call   1030 <puts@plt>
mov    $0x4,%eax
jmp    123e <import_pgm_8bit+0xce>
mov    (%r12),%r12
imul   (%r15),%r12
mov    %r14,%rdi
mov    $0x1,%esi
mov    %r12,%rdx
mov    %rbx,%rcx
call   1040 <fread@plt>
cmp    %r12,%rax
jne    1224 <import_pgm_8bit+0xb4>
mov    %rbx,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    123e <import_pgm_8bit+0xce>
mov    0x2db5(%rip),%rax
mov    (%rax),%edi
lea    0xdee(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x5,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [14 x i8] c"ERR:8bit_pgm\0A\00"
@.str.2 = [32 x i8] c"ERR:8bit_pgm:get_pgm_header:%d\0A\00"
@.str.3 = [22 x i8] c"ERR: pgm not <8 bit.\0A\00"
@stderr = global i32 0
@.str.4 = [31 x i8] c"ERROR: couldn't load pgm data\0A\00"
define i32 @import_pgm_8bit(i8* %0, i64* %1, i64* %2, i32* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64*
%9 = alloca i64*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i64
%14 = alloca i32
store i8* %0, i8** %7
store i64* %1, i64** %8
store i64* %2, i64** %9
store i32* %3, i32** %10
store i8* %4, i8** %11
%15 = load i8*, i8** %11
%16 = call i32* @fopen(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %16, i32** %12
%17 = icmp ne i32* %16, null
br i1 %17, label %20, label %18
18: ; preds = %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %6
br label %54
20: ; preds = %5
%21 = load i32*, i32** %12
%22 = load i64*, i64** %8
%23 = load i64*, i64** %9
%24 = load i32*, i32** %10
%25 = call i32 @get_pgm_header(i32* %21, i64* %22, i64* %23, i32* %24)
store i32 %25, i32* %14
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %20
%28 = load i32, i32* %14
%29 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %28)
store i32 2, i32* %6
br label %54
30: ; preds = %20
%31 = load i32*, i32** %10
%32 = load i32, i32* %31
%33 = icmp sgt i32 %32, 256
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.3, i64 0, i64 0))
store i32 4, i32* %6
br label %54
36: ; preds = %30
%37 = load i64*, i64** %8
%38 = load i64, i64* %37
%39 = load i64*, i64** %9
%40 = load i64, i64* %39
%41 = mul i64 %38, %40
store i64 %41, i64* %13
%42 = load i8*, i8** %7
%43 = load i64, i64* %13
%44 = load i32*, i32** %12
%45 = call i64 @fread(i8* %42, i32 1, i64 %43, i32* %44)
%46 = load i64, i64* %13
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %51
48: ; preds = %36
%49 = load i32, i32* @stderr
%50 = call i32 @fprintf(i32 %49, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
store i32 5, i32* %6
br label %54
51: ; preds = %36
%52 = load i32*, i32** %12
%53 = call i32 @fclose(i32* %52)
store i32 0, i32* %6
br label %54
54: ; preds = %51, %48, %34, %27, %18
%55 = load i32, i32* %6
ret i32 %55
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.2 = [32 x i8] c"ERR:8bit_pgm:get_pgm_header:%d\0A\00"
@stderr = global i32 0
@.str.4 = [31 x i8] c"ERROR: couldn't load pgm data\0A\00"
@str = [13 x i8] c"ERR:8bit_pgm\00"
@str.5 = [21 x i8] c"ERR: pgm not <8 bit.\00"
define i32 @import_pgm_8bit(i8* nocapture %0, i64* %1, i64* %2, i32* %3, i8* nocapture %4) {
%6 = tail call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %10
8: ; preds = %5
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0))
br label %31
10: ; preds = %5
%11 = tail call i32 @get_pgm_header(i32* %6, i64* %1, i64* %2, i32* %3)
%12 = icmp eq i32 %11, 0
br i1 %12, label %15, label %13
13: ; preds = %10
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %11)
br label %31
15: ; preds = %10
%16 = load i32, i32* %3
%17 = icmp sgt i32 %16, 256
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @str.5, i64 0, i64 0))
br label %31
20: ; preds = %15
%21 = load i64, i64* %1
%22 = load i64, i64* %2
%23 = mul i64 %22, %21
%24 = tail call i64 @fread(i8* %0, i32 1, i64 %23, i32* %6)
%25 = icmp eq i64 %24, %23
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = load i32, i32* @stderr
%28 = tail call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %31
29: ; preds = %20
%30 = tail call i32 @fclose(i32* %6)
br label %31
31: ; preds = %29, %26, %18, %13, %8
%32 = phi i32 [ 2, %13 ], [ 4, %18 ], [ 5, %26 ], [ 0, %29 ], [ 1, %8 ]
ret i32 %32
}