#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 */
scalar_t__ EOF;
int /*<<< orphan*/ ErrorLine(char *, ...);
int /*<<< orphan*/ FindAndOpenFile(char *, char *, int /*<<< orphan*/ **);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ free(int /*<<< orphan*/);
scalar_t__ fscanf(int /*<<< orphan*/ *, char *, int *, int *, int *);
void ParsePaletteFile(char *pchFileName, int p[256][3], int *nColors) {
  FILE *fh;
  int i = 0;
  int r, g, b;
  free(FindAndOpenFile(pchFileName, "rb", &fh));
  if (fh == NULL)
    ErrorLine("Unable to open palette file %s", pchFileName);
  while (EOF != fscanf(fh, "%d %d %d", &r, &g, &b)) {
    if (r > 255) {
      ErrorLine("Red color component must be BYTE (0..255)");
    }
    if (g > 255) {
      ErrorLine("Green color component must be BYTE (0..255)");
    }
    if (b > 255) {
      ErrorLine("Blue color component must be BYTE (0..255)");
    }
    p[i][0] = r;
    p[i][1] = g;
    p[i][2] = b;
    i++;
    if (i == 257) {
      break;
    }
  }
  fclose(fh);
  *nColors = i;
}


x86 assembly (clang, -O0):
<ParsePaletteFile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe8a(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   1070 <FindAndOpenFile@plt>
mov    %eax,%edi
call   1030 <free@plt>
cmpq   $0x0,-0x20(%rbp)
jne    11a3 <ParsePaletteFile+0x53>
mov    -0x8(%rbp),%rsi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1050 <ErrorLine@plt>
jmp    11a8 <ParsePaletteFile+0x58>
mov    0x2e39(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe61(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rcx
lea    -0x30(%rbp),%r8
call   1060 <fscanf@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
cmp    %rcx,%rax
je     1291 <ParsePaletteFile+0x141>
cmpl   $0xff,-0x28(%rbp)
jle    11fd <ParsePaletteFile+0xad>
lea    0xe35(%rip),%rdi
mov    $0x0,%al
call   1050 <ErrorLine@plt>
cmpl   $0xff,-0x2c(%rbp)
jle    1218 <ParsePaletteFile+0xc8>
lea    0xe44(%rip),%rdi
mov    $0x0,%al
call   1050 <ErrorLine@plt>
cmpl   $0xff,-0x30(%rbp)
jle    1233 <ParsePaletteFile+0xe3>
lea    0xe55(%rip),%rdi
mov    $0x0,%al
call   1050 <ErrorLine@plt>
mov    -0x28(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,0x4(%rax)
mov    -0x30(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq -0x24(%rbp),%rdx
imul   $0xc,%rdx,%rdx
add    %rdx,%rax
mov    %ecx,0x8(%rax)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cmpl   $0x101,-0x24(%rbp)
jne    128c <ParsePaletteFile+0x13c>
jmp    1291 <ParsePaletteFile+0x141>
jmp    11a8 <ParsePaletteFile+0x58>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
mov    -0x24(%rbp),%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ParsePaletteFile>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,0x20(%rsp)
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe90(%rip),%rsi
lea    0x18(%rsp),%rdx
call   1070 <FindAndOpenFile@plt>
mov    %eax,%edi
call   1030 <free@plt>
cmpq   $0x0,0x18(%rsp)
jne    119a <ParsePaletteFile+0x4a>
lea    0xe73(%rip),%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <ErrorLine@plt>
add    $0x8,%rbx
lea    0x14(%rsp),%r12
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%rbp
xor    %r14d,%r14d
jmp    11e1 <ParsePaletteFile+0x91>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    0x14(%rsp),%ecx
mov    %ecx,-0x8(%rbx)
mov    0x10(%rsp),%ecx
mov    %ecx,-0x4(%rbx)
mov    %eax,(%rbx)
add    $0x1,%r14
add    $0xc,%rbx
cmp    $0x101,%r14
je     1260 <ParsePaletteFile+0x110>
mov    0x2e00(%rip),%rax
mov    (%rax),%r15
mov    0x18(%rsp),%rdi
lea    0xe2b(%rip),%rsi
mov    %r12,%rdx
mov    %r13,%rcx
mov    %rbp,%r8
call   1060 <fscanf@plt>
cmp    %rax,%r15
je     1266 <ParsePaletteFile+0x116>
cmpl   $0x100,0x14(%rsp)
jl     1222 <ParsePaletteFile+0xd2>
lea    0xe10(%rip),%rdi
xor    %eax,%eax
call   1050 <ErrorLine@plt>
cmpl   $0x100,0x10(%rsp)
jl     123a <ParsePaletteFile+0xea>
lea    0xe22(%rip),%rdi
xor    %eax,%eax
call   1050 <ErrorLine@plt>
mov    0xc(%rsp),%eax
cmp    $0x100,%eax
jl     11c0 <ParsePaletteFile+0x70>
lea    0xe31(%rip),%rdi
xor    %eax,%eax
call   1050 <ErrorLine@plt>
mov    0xc(%rsp),%eax
jmp    11c0 <ParsePaletteFile+0x70>
mov    $0x101,%r14d
mov    0x20(%rsp),%rbx
mov    0x18(%rsp),%rdi
call   1040 <fclose@plt>
mov    %r14d,(%rbx)
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"rb\00"
@.str.1 = [31 x i8] c"Unable to open palette file %s\00"
@EOF = global i64 0
@.str.2 = [9 x i8] c"%d %d %d\00"
@.str.3 = [42 x i8] c"Red color component must be BYTE (0..255)\00"
@.str.4 = [44 x i8] c"Green color component must be BYTE (0..255)\00"
@.str.5 = [43 x i8] c"Blue color component must be BYTE (0..255)\00"
define void @ParsePaletteFile(i8* %0, [3 x i32]* %1, i32* %2) {
%4 = alloca i8*
%5 = alloca [3 x i32]*
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store [3 x i32]* %1, [3 x i32]** %5
store i32* %2, i32** %6
store i32 0, i32* %8
%12 = load i8*, i8** %4
%13 = call i32 @FindAndOpenFile(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32** %7)
%14 = call i32 @free(i32 %13)
%15 = load i32*, i32** %7
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %3
%18 = load i8*, i8** %4
%19 = call i32 (i8*, ...) @ErrorLine(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %18)
br label %20
20: ; preds = %17, %3
br label %21
21: ; preds = %65, %20
%22 = load i64, i64* @EOF
%23 = load i32*, i32** %7
%24 = call i64 @fscanf(i32* %23, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %9, i32* %10, i32* %11)
%25 = icmp ne i64 %22, %24
br i1 %25, label %26, label %66
26: ; preds = %21
%27 = load i32, i32* %9
%28 = icmp sgt i32 %27, 255
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 (i8*, ...) @ErrorLine(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %31
31: ; preds = %29, %26
%32 = load i32, i32* %10
%33 = icmp sgt i32 %32, 255
br i1 %33, label %34, label %36
34: ; preds = %31
%35 = call i32 (i8*, ...) @ErrorLine(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
br label %36
36: ; preds = %34, %31
%37 = load i32, i32* %11
%38 = icmp sgt i32 %37, 255
br i1 %38, label %39, label %41
39: ; preds = %36
%40 = call i32 (i8*, ...) @ErrorLine(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
br label %41
41: ; preds = %39, %36
%42 = load i32, i32* %9
%43 = load [3 x i32]*, [3 x i32]** %5
%44 = load i32, i32* %8
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [3 x i32], [3 x i32]* %43, i64 %45
%47 = getelementptr inbounds [3 x i32], [3 x i32]* %46, i64 0, i64 0
store i32 %42, i32* %47
%48 = load i32, i32* %10
%49 = load [3 x i32]*, [3 x i32]** %5
%50 = load i32, i32* %8
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [3 x i32], [3 x i32]* %49, i64 %51
%53 = getelementptr inbounds [3 x i32], [3 x i32]* %52, i64 0, i64 1
store i32 %48, i32* %53
%54 = load i32, i32* %11
%55 = load [3 x i32]*, [3 x i32]** %5
%56 = load i32, i32* %8
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [3 x i32], [3 x i32]* %55, i64 %57
%59 = getelementptr inbounds [3 x i32], [3 x i32]* %58, i64 0, i64 2
store i32 %54, i32* %59
%60 = load i32, i32* %8
%61 = add nsw i32 %60, 1
store i32 %61, i32* %8
%62 = load i32, i32* %8
%63 = icmp eq i32 %62, 257
br i1 %63, label %64, label %65
64: ; preds = %41
br label %66
65: ; preds = %41
br label %21
66: ; preds = %64, %21
%67 = load i32*, i32** %7
%68 = call i32 @fclose(i32* %67)
%69 = load i32, i32* %8
%70 = load i32*, i32** %6
store i32 %69, i32* %70
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [31 x i8] c"Unable to open palette file %s\00"
@EOF = global i64 0
@.str.2 = [9 x i8] c"%d %d %d\00"
@.str.3 = [42 x i8] c"Red color component must be BYTE (0..255)\00"
@.str.4 = [44 x i8] c"Green color component must be BYTE (0..255)\00"
@.str.5 = [43 x i8] c"Blue color component must be BYTE (0..255)\00"
define void @ParsePaletteFile(i8* %0, [3 x i32]* nocapture writeonly %1, i32* nocapture writeonly %2) {
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = bitcast i32** %4 to i8*
%9 = bitcast i32* %5 to i8*
%10 = bitcast i32* %6 to i8*
%11 = bitcast i32* %7 to i8*
%12 = call i32 @FindAndOpenFile(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32** %4)
%13 = call i32 @free(i32 %12)
%14 = load i32*, i32** %4
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = call i32 (i8*, ...) @ErrorLine(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %0)
br label %18
18: ; preds = %16, %3
br label %19
19: ; preds = %18, %41
%20 = phi i64 [ %48, %41 ], [ 0, %18 ]
%21 = load i64, i64* @EOF
%22 = load i32*, i32** %4
%23 = call i64 @fscanf(i32* %22, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %5, i32* %6, i32* %7)
%24 = icmp eq i64 %21, %23
br i1 %24, label %50, label %25
25: ; preds = %19
%26 = load i32, i32* %5
%27 = icmp sgt i32 %26, 255
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 (i8*, ...) @ErrorLine(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 255
br i1 %32, label %33, label %35
33: ; preds = %30
%34 = call i32 (i8*, ...) @ErrorLine(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.4, i64 0, i64 0))
br label %35
35: ; preds = %33, %30
%36 = load i32, i32* %7
%37 = icmp sgt i32 %36, 255
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 (i8*, ...) @ErrorLine(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.5, i64 0, i64 0))
%40 = load i32, i32* %7
br label %41
41: ; preds = %38, %35
%42 = phi i32 [ %40, %38 ], [ %36, %35 ]
%43 = load i32, i32* %5
%44 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 %20, i64 0
store i32 %43, i32* %44
%45 = load i32, i32* %6
%46 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 %20, i64 1
store i32 %45, i32* %46
%47 = getelementptr inbounds [3 x i32], [3 x i32]* %1, i64 %20, i64 2
store i32 %42, i32* %47
%48 = add nuw nsw i64 %20, 1
%49 = icmp eq i64 %48, 257
br i1 %49, label %52, label %19
50: ; preds = %19
%51 = trunc i64 %20 to i32
br label %52
52: ; preds = %41, %50
%53 = phi i32 [ %51, %50 ], [ 257, %41 ]
%54 = load i32*, i32** %4
%55 = call i32 @fclose(i32* %54)
store i32 %53, i32* %2
ret void
}