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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int fread(scalar_t__ *, int, int, int /*<<< orphan*/ *);
int get_pgm_header(int /*<<< orphan*/ *, size_t *, size_t *, int *);
scalar_t__ ntohs(scalar_t__);
int /*<<< orphan*/ printf(char *, ...);
int import_pgm_16bit(uint16_t *buf, size_t *pwidth, size_t *pheight,
                     int *pmaxval, char *filename) {
  FILE *f;
  int i, bpp;
  size_t size;
  int r;
  if (!(f = fopen(filename, "r"))) {
    printf("ERR:16bit_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) > 65535) {
    printf("ERR: pgm not <16 bit.\n");
    return 3;
  }
  bpp = *pmaxval < 256 ? 1 : 2;
  size = (*pwidth) * (*pheight);
  for (i = 0; i < size; i++) {
    buf[i] = 0;
    if (fread(buf + i, bpp, 1, f) != 1) {
      printf("ERR: pgm data\n");
      return 4;
    }
    if (bpp > 1)
      buf[i] = ntohs(buf[i]);
  }
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<import_pgm_16bit>:
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_16bit+0x54>
lea    0xe61(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1300 <import_pgm_16bit+0x1a0>
mov    -0x38(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <get_pgm_header@plt>
mov    %eax,-0x4c(%rbp)
cmp    $0x0,%eax
je     11f2 <import_pgm_16bit+0x92>
mov    -0x4c(%rbp),%esi
lea    0xe32(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x2,-0x4(%rbp)
jmp    1300 <import_pgm_16bit+0x1a0>
mov    -0x28(%rbp),%rax
cmpl   $0xffff,(%rax)
jle    121c <import_pgm_16bit+0xbc>
lea    0xe28(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x3,-0x4(%rbp)
jmp    1300 <import_pgm_16bit+0x1a0>
mov    -0x28(%rbp),%rax
mov    (%rax),%edx
mov    $0x2,%eax
mov    $0x1,%ecx
cmp    $0x100,%edx
cmovl  %ecx,%eax
mov    %eax,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    -0x20(%rbp),%rcx
imul   (%rcx),%rax
mov    %rax,-0x48(%rbp)
movl   $0x0,-0x3c(%rbp)
movslq -0x3c(%rbp),%rax
cmp    -0x48(%rbp),%rax
jae    12f0 <import_pgm_16bit+0x190>
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
movq   $0x0,(%rax,%rcx,8)
mov    -0x10(%rbp),%rdi
movslq -0x3c(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rdi
mov    -0x40(%rbp),%esi
mov    -0x38(%rbp),%rcx
mov    $0x1,%edx
call   1030 <fread@plt>
cmp    $0x1,%eax
je     12b3 <import_pgm_16bit+0x153>
lea    0xda8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x4,-0x4(%rbp)
jmp    1300 <import_pgm_16bit+0x1a0>
cmpl   $0x1,-0x40(%rbp)
jle    12dd <import_pgm_16bit+0x17d>
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1070 <ntohs@plt>
mov    %rax,%rdx
mov    -0x10(%rbp),%rax
movslq -0x3c(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
jmp    12e2 <import_pgm_16bit+0x182>
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1252 <import_pgm_16bit+0xf2>
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_16bit>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%r12
mov    %rsi,%r15
mov    %rdi,%rbx
lea    0xe72(%rip),%rsi
mov    %r8,%rdi
call   1090 <fopen@plt>
test   %rax,%rax
je     11cd <import_pgm_16bit+0x5d>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    %rbp,%rcx
call   1060 <get_pgm_header@plt>
test   %eax,%eax
je     11e3 <import_pgm_16bit+0x73>
lea    0xe48(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x2,%eax
jmp    128c <import_pgm_16bit+0x11c>
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%eax
jmp    128c <import_pgm_16bit+0x11c>
mov    0x0(%rbp),%r13d
cmp    $0x10000,%r13d
jl     1206 <import_pgm_16bit+0x96>
lea    0xe47(%rip),%rdi
call   1030 <puts@plt>
mov    $0x3,%eax
jmp    128c <import_pgm_16bit+0x11c>
xor    %eax,%eax
cmp    $0x100,%r13d
setl   %cl
mov    (%r12),%rbp
imul   (%r15),%rbp
test   %rbp,%rbp
je     126f <import_pgm_16bit+0xff>
mov    %cl,%al
mov    $0x2,%r15d
sub    %eax,%r15d
jmp    123a <import_pgm_16bit+0xca>
nopl   0x0(%rax)
add    $0x8,%rbx
add    $0xffffffffffffffff,%rbp
je     126f <import_pgm_16bit+0xff>
movq   $0x0,(%rbx)
mov    %rbx,%rdi
mov    %r15d,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   1040 <fread@plt>
cmp    $0x1,%eax
jne    127b <import_pgm_16bit+0x10b>
cmp    $0x100,%r13d
jl     1230 <import_pgm_16bit+0xc0>
mov    (%rbx),%rdi
call   1080 <ntohs@plt>
mov    %rax,(%rbx)
jmp    1230 <import_pgm_16bit+0xc0>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    128c <import_pgm_16bit+0x11c>
lea    0xdae(%rip),%rdi
call   1030 <puts@plt>
mov    $0x4,%eax
add    $0x8,%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 = [15 x i8] c"ERR:16bit_pgm\0A\00"
@.str.2 = [32 x i8] c"ERR:8bit_pgm:get_pgm_header:%d\0A\00"
@.str.3 = [23 x i8] c"ERR: pgm not <16 bit.\0A\00"
@.str.4 = [15 x i8] c"ERR: pgm data\0A\00"
define i32 @import_pgm_16bit(i64* %0, i64* %1, i64* %2, i32* %3, i8* %4) {
%6 = alloca i32
%7 = alloca i64*
%8 = alloca i64*
%9 = alloca i64*
%10 = alloca i32*
%11 = alloca i8*
%12 = alloca i32*
%13 = alloca i32
%14 = alloca i32
%15 = alloca i64
%16 = alloca i32
store i64* %0, i64** %7
store i64* %1, i64** %8
store i64* %2, i64** %9
store i32* %3, i32** %10
store i8* %4, i8** %11
%17 = load i8*, i8** %11
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %12
%19 = icmp ne i32* %18, null
br i1 %19, label %22, label %20
20: ; preds = %5
%21 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %6
br label %90
22: ; preds = %5
%23 = load i32*, i32** %12
%24 = load i64*, i64** %8
%25 = load i64*, i64** %9
%26 = load i32*, i32** %10
%27 = call i32 @get_pgm_header(i32* %23, i64* %24, i64* %25, i32* %26)
store i32 %27, i32* %16
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %22
%30 = load i32, i32* %16
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i32 %30)
store i32 2, i32* %6
br label %90
32: ; preds = %22
%33 = load i32*, i32** %10
%34 = load i32, i32* %33
%35 = icmp sgt i32 %34, 65535
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
store i32 3, i32* %6
br label %90
38: ; preds = %32
%39 = load i32*, i32** %10
%40 = load i32, i32* %39
%41 = icmp slt i32 %40, 256
%42 = zext i1 %41 to i64
%43 = select i1 %41, i32 1, i32 2
store i32 %43, i32* %14
%44 = load i64*, i64** %8
%45 = load i64, i64* %44
%46 = load i64*, i64** %9
%47 = load i64, i64* %46
%48 = mul i64 %45, %47
store i64 %48, i64* %15
store i32 0, i32* %13
br label %49
49: ; preds = %84, %38
%50 = load i32, i32* %13
%51 = sext i32 %50 to i64
%52 = load i64, i64* %15
%53 = icmp ult i64 %51, %52
br i1 %53, label %54, label %87
54: ; preds = %49
%55 = load i64*, i64** %7
%56 = load i32, i32* %13
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds i64, i64* %55, i64 %57
store i64 0, i64* %58
%59 = load i64*, i64** %7
%60 = load i32, i32* %13
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i64, i64* %59, i64 %61
%63 = load i32, i32* %14
%64 = load i32*, i32** %12
%65 = call i32 @fread(i64* %62, i32 %63, i32 1, i32* %64)
%66 = icmp ne i32 %65, 1
br i1 %66, label %67, label %69
67: ; preds = %54
%68 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
store i32 4, i32* %6
br label %90
69: ; preds = %54
%70 = load i32, i32* %14
%71 = icmp sgt i32 %70, 1
br i1 %71, label %72, label %83
72: ; preds = %69
%73 = load i64*, i64** %7
%74 = load i32, i32* %13
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds i64, i64* %73, i64 %75
%77 = load i64, i64* %76
%78 = call i64 @ntohs(i64 %77)
%79 = load i64*, i64** %7
%80 = load i32, i32* %13
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds i64, i64* %79, i64 %81
store i64 %78, i64* %82
br label %83
83: ; preds = %72, %69
br label %84
84: ; preds = %83
%85 = load i32, i32* %13
%86 = add nsw i32 %85, 1
store i32 %86, i32* %13
br label %49
87: ; preds = %49
%88 = load i32*, i32** %12
%89 = call i32 @fclose(i32* %88)
store i32 0, i32* %6
br label %90
90: ; preds = %87, %67, %36, %29, %20
%91 = load i32, i32* %6
ret i32 %91
}

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"
@str = [14 x i8] c"ERR:16bit_pgm\00"
@str.5 = [14 x i8] c"ERR: pgm data\00"
@str.6 = [22 x i8] c"ERR: pgm not <16 bit.\00"
define i32 @import_pgm_16bit(i64* 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 ([14 x i8], [14 x i8]* @str, i64 0, i64 0))
br label %43
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 %43
15: ; preds = %10
%16 = load i32, i32* %3
%17 = icmp sgt i32 %16, 65535
br i1 %17, label %18, label %20
18: ; preds = %15
%19 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str.6, i64 0, i64 0))
br label %43
20: ; preds = %15
%21 = icmp slt i32 %16, 256
%22 = select i1 %21, i32 1, i32 2
%23 = load i64, i64* %1
%24 = load i64, i64* %2
%25 = mul i64 %24, %23
%26 = icmp eq i64 %25, 0
br i1 %26, label %41, label %27
27: ; preds = %20, %38
%28 = phi i64 [ %39, %38 ], [ 0, %20 ]
%29 = getelementptr inbounds i64, i64* %0, i64 %28
store i64 0, i64* %29
%30 = tail call i32 @fread(i64* %29, i32 %22, i32 1, i32* %6)
%31 = icmp eq i32 %30, 1
br i1 %31, label %34, label %32
32: ; preds = %27
%33 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.5, i64 0, i64 0))
br label %43
34: ; preds = %27
br i1 %21, label %38, label %35
35: ; preds = %34
%36 = load i64, i64* %29
%37 = tail call i64 @ntohs(i64 %36)
store i64 %37, i64* %29
br label %38
38: ; preds = %34, %35
%39 = add nuw i64 %28, 1
%40 = icmp eq i64 %39, %25
br i1 %40, label %41, label %27
41: ; preds = %38, %20
%42 = tail call i32 @fclose(i32* %6)
br label %43
43: ; preds = %41, %32, %18, %13, %8
%44 = phi i32 [ 2, %13 ], [ 3, %18 ], [ 4, %32 ], [ 0, %41 ], [ 1, %8 ]
ret i32 %44
}