#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*/ uint8_t;

/* Variables and functions */
int get_file_size(char const *);
scalar_t__ readfile(char const *, int /*<<< orphan*/ *, int);
int load_external_palette(uint8_t *palette, const char *filename) {
  int size;
  if (!palette || !filename)
    return -1;
  size = get_file_size(filename);
  if (size < 192)
    return -1;
  if (size < 1536)
    size = 192;
  else
    size = 1536;
  if (readfile(filename, palette, size) != 0) {
    return -1;
  }
  return size / 3;
}


x86 assembly (clang, -O0):
<load_external_palette>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1146 <load_external_palette+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    1152 <load_external_palette+0x32>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <load_external_palette+0xab>
mov    -0x18(%rbp),%rdi
call   1030 <get_file_size@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xc0,-0x1c(%rbp)
jge    1177 <load_external_palette+0x57>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <load_external_palette+0xab>
cmpl   $0x600,-0x1c(%rbp)
jge    1190 <load_external_palette+0x70>
movl   $0xc0,-0x1c(%rbp)
jmp    1197 <load_external_palette+0x77>
movl   $0x600,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <readfile@plt>
cmp    $0x0,%rax
je     11bd <load_external_palette+0x9d>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cb <load_external_palette+0xab>
mov    -0x1c(%rbp),%eax
mov    $0x3,%ecx
cltd
idiv   %ecx
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_external_palette>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    $0xffffffff,%r14d
test   %rdi,%rdi
je     117e <load_external_palette+0x5e>
mov    %rsi,%rbp
test   %rsi,%rsi
je     117e <load_external_palette+0x5e>
mov    %rdi,%rbx
mov    %rbp,%rdi
call   1030 <get_file_size@plt>
cmp    $0xc0,%eax
jl     117e <load_external_palette+0x5e>
cmp    $0x600,%eax
mov    $0xc0,%eax
mov    $0x600,%r15d
cmovb  %eax,%r15d
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <readfile@plt>
test   %rax,%rax
jne    117e <load_external_palette+0x5e>
imul   $0xaaab,%r15d,%r14d
shr    $0x11,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @load_external_palette(i32* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
%7 = load i32*, i32** %4
%8 = icmp ne i32* %7, null
br i1 %8, label %9, label %12
9: ; preds = %2
%10 = load i8*, i8** %5
%11 = icmp ne i8* %10, null
br i1 %11, label %13, label %12
12: ; preds = %9, %2
store i32 -1, i32* %3
br label %34
13: ; preds = %9
%14 = load i8*, i8** %5
%15 = call i32 @get_file_size(i8* %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 192
br i1 %17, label %18, label %19
18: ; preds = %13
store i32 -1, i32* %3
br label %34
19: ; preds = %13
%20 = load i32, i32* %6
%21 = icmp slt i32 %20, 1536
br i1 %21, label %22, label %23
22: ; preds = %19
store i32 192, i32* %6
br label %24
23: ; preds = %19
store i32 1536, i32* %6
br label %24
24: ; preds = %23, %22
%25 = load i8*, i8** %5
%26 = load i32*, i32** %4
%27 = load i32, i32* %6
%28 = call i64 @readfile(i8* %25, i32* %26, i32 %27)
%29 = icmp ne i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
store i32 -1, i32* %3
br label %34
31: ; preds = %24
%32 = load i32, i32* %6
%33 = sdiv i32 %32, 3
store i32 %33, i32* %3
br label %34
34: ; preds = %31, %30, %18, %12
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
define i32 @load_external_palette(i32* %0, i8* %1) {
%3 = icmp ne i32* %0, null
%4 = icmp ne i8* %1, null
%5 = and i1 %3, %4
br i1 %5, label %6, label %18
6: ; preds = %2
%7 = tail call i32 @get_file_size(i8* %1)
%8 = icmp slt i32 %7, 192
br i1 %8, label %18, label %9
9: ; preds = %6
%10 = icmp ult i32 %7, 1536
%11 = select i1 %10, i32 192, i32 1536
%12 = tail call i64 @readfile(i8* %1, i32* %0, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = trunc i32 %11 to i16
%16 = udiv i16 %15, 3
%17 = zext i16 %16 to i32
br label %18
18: ; preds = %9, %6, %2, %14
%19 = phi i32 [ %17, %14 ], [ -1, %2 ], [ -1, %6 ], [ -1, %9 ]
ret i32 %19
}