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

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int fread(int /*<<< orphan*/ *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
scalar_t__ read_ppm_header(int /*<<< orphan*/ *, int *, int *);
int /*<<< orphan*/ stderr;
uint16_t *read_ppm16(const char *filename, int *wd, int *ht) {
  FILE *f = NULL;
  uint16_t *p = NULL;
  f = fopen(filename, "rb");
  if (!f) {
    goto exit;
  }
  if (read_ppm_header(f, wd, ht)) {
    goto exit;
  }
  p = (uint16_t *)malloc(sizeof(uint16_t) * 3 * (*wd) * (*ht));
  int rd = fread(p, sizeof(uint16_t) * 3, (*wd) * (*ht), f);
  if (rd != (*wd) * (*ht)) {
    fprintf(stderr,
            "[read_ppm] unexpected end of file! maybe you're loading an 8-bit "
            "ppm here instead of a 16-bit one? (%s)\n",
            filename);
    free(p);
    p = NULL;
  }
exit:
  if (f) {
    fclose(f);
    f = NULL;
  }
  return p;
}


x86 assembly (clang, -O0):
<read_ppm16>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe61(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11b8 <read_ppm16+0x48>
jmp    1262 <read_ppm16+0xf2>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1090 <read_ppm_header@plt>
cmp    $0x0,%rax
je     11d8 <read_ppm16+0x68>
jmp    1262 <read_ppm16+0xf2>
mov    -0x10(%rbp),%rax
movslq (%rax),%rax
imul   $0xc,%rax,%rax
mov    -0x18(%rbp),%rcx
movslq (%rcx),%rcx
imul   %rcx,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%edx
mov    -0x18(%rbp),%rax
imul   (%rax),%edx
mov    -0x20(%rbp),%rcx
mov    $0xc,%esi
call   1040 <fread@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    -0x10(%rbp),%rcx
mov    (%rcx),%ecx
mov    -0x18(%rbp),%rdx
imul   (%rdx),%ecx
cmp    %ecx,%eax
je     125d <read_ppm16+0xed>
mov    0x2da6(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdbc(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x28(%rbp)
jmp    1262 <read_ppm16+0xf2>
cmpq   $0x0,-0x20(%rbp)
je     127e <read_ppm16+0x10e>
mov    -0x20(%rbp),%rdi
call   1050 <fclose@plt>
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_ppm16>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,%r14
lea    0xe77(%rip),%rsi
call   1080 <fopen@plt>
test   %rax,%rax
je     1217 <read_ppm16+0xa7>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1090 <read_ppm_header@plt>
test   %rax,%rax
jne    120a <read_ppm16+0x9a>
mov    (%r12),%eax
imul   0x0(%r13),%eax
shl    $0x2,%eax
lea    (%rax,%rax,2),%edi
call   1070 <malloc@plt>
mov    %rax,%r15
mov    0x0(%r13),%edx
imul   (%r12),%edx
mov    %rax,%rdi
mov    $0xc,%esi
mov    %rbx,%rcx
call   1040 <fread@plt>
mov    0x0(%r13),%ecx
imul   (%r12),%ecx
cmp    %ecx,%eax
je     120d <read_ppm16+0x9d>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
lea    0xe09(%rip),%rsi
mov    %r14,%rdx
call   1060 <fprintf@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %rbx,%rdi
call   1050 <fclose@plt>
jmp    121a <read_ppm16+0xaa>
xor    %r15d,%r15d
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [105 x i8] c"[read_ppm] unexpected end of file! maybe you're loading an 8-bit ppm here instead of a 16-bit one? (%s)\0A\00"
define i32* @read_ppm16(i8* %0, i32* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i32
store i8* %0, i8** %4
store i32* %1, i32** %5
store i32* %2, i32** %6
store i32* null, i32** %7
store i32* null, i32** %8
%10 = load i8*, i8** %4
%11 = call i32* @fopen(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %7
%12 = load i32*, i32** %7
%13 = icmp ne i32* %12, null
br i1 %13, label %15, label %14
14: ; preds = %3
br label %56
15: ; preds = %3
%16 = load i32*, i32** %7
%17 = load i32*, i32** %5
%18 = load i32*, i32** %6
%19 = call i64 @read_ppm_header(i32* %16, i32* %17, i32* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
br label %56
22: ; preds = %15
%23 = load i32*, i32** %5
%24 = load i32, i32* %23
%25 = sext i32 %24 to i64
%26 = mul i64 12, %25
%27 = load i32*, i32** %6
%28 = load i32, i32* %27
%29 = sext i32 %28 to i64
%30 = mul i64 %26, %29
%31 = trunc i64 %30 to i32
%32 = call i64 @malloc(i32 %31)
%33 = inttoptr i64 %32 to i32*
store i32* %33, i32** %8
%34 = load i32*, i32** %8
%35 = load i32*, i32** %5
%36 = load i32, i32* %35
%37 = load i32*, i32** %6
%38 = load i32, i32* %37
%39 = mul nsw i32 %36, %38
%40 = load i32*, i32** %7
%41 = call i32 @fread(i32* %34, i32 12, i32 %39, i32* %40)
store i32 %41, i32* %9
%42 = load i32, i32* %9
%43 = load i32*, i32** %5
%44 = load i32, i32* %43
%45 = load i32*, i32** %6
%46 = load i32, i32* %45
%47 = mul nsw i32 %44, %46
%48 = icmp ne i32 %42, %47
br i1 %48, label %49, label %55
49: ; preds = %22
%50 = load i32, i32* @stderr
%51 = load i8*, i8** %4
%52 = call i32 @fprintf(i32 %50, i8* getelementptr inbounds ([105 x i8], [105 x i8]* @.str.1, i64 0, i64 0), i8* %51)
%53 = load i32*, i32** %8
%54 = call i32 @free(i32* %53)
store i32* null, i32** %8
br label %55
55: ; preds = %49, %22
br label %56
56: ; preds = %55, %21, %14
%57 = load i32*, i32** %7
%58 = icmp ne i32* %57, null
br i1 %58, label %59, label %62
59: ; preds = %56
%60 = load i32*, i32** %7
%61 = call i32 @fclose(i32* %60)
store i32* null, i32** %7
br label %62
62: ; preds = %59, %56
%63 = load i32*, i32** %8
ret i32* %63
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@stderr = global i32 0
@.str.1 = [105 x i8] c"[read_ppm] unexpected end of file! maybe you're loading an 8-bit ppm here instead of a 16-bit one? (%s)\0A\00"
define i32* @read_ppm16(i8* %0, i32* %1, i32* %2) {
%4 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %31, label %6
6: ; preds = %3
%7 = tail call i64 @read_ppm_header(i32* %4, i32* %1, i32* %2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %28
9: ; preds = %6
%10 = load i32, i32* %1
%11 = mul i32 %10, 12
%12 = load i32, i32* %2
%13 = mul i32 %11, %12
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i32*
%16 = load i32, i32* %1
%17 = load i32, i32* %2
%18 = mul nsw i32 %17, %16
%19 = tail call i32 @fread(i32* %15, i32 12, i32 %18, i32* %4)
%20 = load i32, i32* %1
%21 = load i32, i32* %2
%22 = mul nsw i32 %21, %20
%23 = icmp eq i32 %19, %22
br i1 %23, label %28, label %24
24: ; preds = %9
%25 = load i32, i32* @stderr
%26 = tail call i32 @fprintf(i32 %25, i8* getelementptr inbounds ([105 x i8], [105 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%27 = tail call i32 @free(i32* %15)
br label %28
28: ; preds = %9, %24, %6
%29 = phi i32* [ %15, %9 ], [ null, %24 ], [ null, %6 ]
%30 = tail call i32 @fclose(i32* %4)
br label %31
31: ; preds = %3, %28
%32 = phi i32* [ %29, %28 ], [ null, %3 ]
ret i32* %32
}