#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 ReadBMP(char const *, unsigned char ***, int *, int *);
int ReadPPM(char const *, unsigned char ***, int *, int *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int getc(int /*<<< orphan*/ *);
int ReadGenRGB(const char *fname, unsigned char (**data)[3], int *w, int *h) {
  FILE *fp = fopen(fname, "rb");
  int b1, b2;
  *data = NULL;
  if (!fp)
    return -1;
  b1 = getc(fp);
  b2 = getc(fp);
  fclose(fp);
  if (b1 == 'P' && ('1' <= b2 && b2 <= '6'))
    return ReadPPM(fname, data, w, h);
  else if (b1 == 'B' && b2 == 'M')
    return ReadBMP(fname, data, w, h);
  else
    return -4;
}


x86 assembly (clang, -O0):
<ReadGenRGB>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rax
movq   $0x0,(%rax)
cmpq   $0x0,-0x30(%rbp)
jne    119e <ReadGenRGB+0x4e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1236 <ReadGenRGB+0xe6>
mov    -0x30(%rbp),%rdi
call   1070 <getc@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1070 <getc@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <fclose@plt>
cmpl   $0x50,-0x34(%rbp)
jne    11fe <ReadGenRGB+0xae>
mov    $0x31,%eax
cmp    -0x38(%rbp),%eax
jg     11fe <ReadGenRGB+0xae>
cmpl   $0x36,-0x38(%rbp)
jg     11fe <ReadGenRGB+0xae>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <ReadPPM@plt>
mov    %eax,-0x4(%rbp)
jmp    1236 <ReadGenRGB+0xe6>
cmpl   $0x42,-0x34(%rbp)
jne    122f <ReadGenRGB+0xdf>
cmpl   $0x4d,-0x38(%rbp)
jne    122f <ReadGenRGB+0xdf>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1040 <ReadBMP@plt>
mov    %eax,-0x4(%rbp)
jmp    1236 <ReadGenRGB+0xe6>
movl   $0xfffffffc,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ReadGenRGB>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%rbx
lea    0xe92(%rip),%rsi
call   1060 <fopen@plt>
movq   $0x0,(%r12)
test   %rax,%rax
je     11d2 <ReadGenRGB+0x82>
mov    %rax,%r14
mov    %rbp,(%rsp)
mov    %rax,%rdi
call   1070 <getc@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1070 <getc@plt>
mov    %eax,%r13d
mov    %r14,%rdi
call   1030 <fclose@plt>
cmp    $0x50,%ebp
jne    11d9 <ReadGenRGB+0x89>
lea    -0x31(%r13),%eax
cmp    $0x5,%eax
ja     11d9 <ReadGenRGB+0x89>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    (%rsp),%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <ReadPPM@plt>
mov    $0xffffffff,%eax
jmp    1209 <ReadGenRGB+0xb9>
mov    $0xfffffffc,%eax
cmp    $0x42,%ebp
jne    1209 <ReadGenRGB+0xb9>
cmp    $0x4d,%r13d
jne    1209 <ReadGenRGB+0xb9>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rdx
mov    (%rsp),%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <ReadBMP@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
define i32 @ReadGenRGB(i8* %0, [3 x i8]** %1, i32* %2, i32* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca [3 x i8]**
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store [3 x i8]** %1, [3 x i8]*** %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%13 = load i8*, i8** %6
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %10
%15 = load [3 x i8]**, [3 x i8]*** %7
store [3 x i8]* null, [3 x i8]** %15
%16 = load i32*, i32** %10
%17 = icmp ne i32* %16, null
br i1 %17, label %19, label %18
18: ; preds = %4
store i32 -1, i32* %5
br label %55
19: ; preds = %4
%20 = load i32*, i32** %10
%21 = call i32 @getc(i32* %20)
store i32 %21, i32* %11
%22 = load i32*, i32** %10
%23 = call i32 @getc(i32* %22)
store i32 %23, i32* %12
%24 = load i32*, i32** %10
%25 = call i32 @fclose(i32* %24)
%26 = load i32, i32* %11
%27 = icmp eq i32 %26, 80
br i1 %27, label %28, label %41
28: ; preds = %19
%29 = load i32, i32* %12
%30 = icmp sle i32 49, %29
br i1 %30, label %31, label %41
31: ; preds = %28
%32 = load i32, i32* %12
%33 = icmp sle i32 %32, 54
br i1 %33, label %34, label %41
34: ; preds = %31
%35 = load i8*, i8** %6
%36 = load [3 x i8]**, [3 x i8]*** %7
%37 = bitcast [3 x i8]** %36 to i8***
%38 = load i32*, i32** %8
%39 = load i32*, i32** %9
%40 = call i32 @ReadPPM(i8* %35, i8*** %37, i32* %38, i32* %39)
store i32 %40, i32* %5
br label %55
41: ; preds = %31, %28, %19
%42 = load i32, i32* %11
%43 = icmp eq i32 %42, 66
br i1 %43, label %44, label %54
44: ; preds = %41
%45 = load i32, i32* %12
%46 = icmp eq i32 %45, 77
br i1 %46, label %47, label %54
47: ; preds = %44
%48 = load i8*, i8** %6
%49 = load [3 x i8]**, [3 x i8]*** %7
%50 = bitcast [3 x i8]** %49 to i8***
%51 = load i32*, i32** %8
%52 = load i32*, i32** %9
%53 = call i32 @ReadBMP(i8* %48, i8*** %50, i32* %51, i32* %52)
store i32 %53, i32* %5
br label %55
54: ; preds = %44, %41
store i32 -4, i32* %5
br label %55
55: ; preds = %54, %47, %34, %18
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
define i32 @ReadGenRGB(i8* %0, [3 x i8]** %1, i32* %2, i32* %3) {
%5 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store [3 x i8]* null, [3 x i8]** %1
%6 = icmp eq i32* %5, null
br i1 %6, label %25, label %7
7: ; preds = %4
%8 = tail call i32 @getc(i32* %5)
%9 = tail call i32 @getc(i32* %5)
%10 = tail call i32 @fclose(i32* %5)
%11 = icmp eq i32 %8, 80
%12 = add i32 %9, -49
%13 = icmp ult i32 %12, 6
%14 = and i1 %11, %13
br i1 %14, label %15, label %18
15: ; preds = %7
%16 = bitcast [3 x i8]** %1 to i8***
%17 = tail call i32 @ReadPPM(i8* %0, i8*** %16, i32* %2, i32* %3)
br label %25
18: ; preds = %7
%19 = icmp eq i32 %8, 66
%20 = icmp eq i32 %9, 77
%21 = and i1 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = bitcast [3 x i8]** %1 to i8***
%24 = tail call i32 @ReadBMP(i8* %0, i8*** %23, i32* %2, i32* %3)
br label %25
25: ; preds = %18, %4, %22, %15
%26 = phi i32 [ %17, %15 ], [ %24, %22 ], [ -1, %4 ], [ -4, %18 ]
ret i32 %26
}