#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 /*<<< orphan*/ dr_setRGB8multi(int /*<<< orphan*/, int, unsigned char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, ...);
int /*<<< orphan*/ stderr;
int load_palette(const char *fname, unsigned char *palette) {
  FILE *file = fopen(fname, "rb");
  if (file) {
    int x;
    int anzahl = 256;
    int r, g, b;
    fscanf(file, "%d", &anzahl);
    for (x = 0; x < anzahl; x++) {
      fscanf(file, "%d %d %d", &r, &g, &b);
      palette[x * 3 + 0] = r;
      palette[x * 3 + 1] = g;
      palette[x * 3 + 2] = b;
    }
    dr_setRGB8multi(0, anzahl, palette);
    fclose(file);
    return 1;
  } else {
    fprintf(stderr, "Error: can't open file '%s' for reading\n", fname);
    return 0;
  }
}


x86 assembly (clang, -O0):
<load_palette>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1240 <load_palette+0xf0>
movl   $0x100,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe78(%rip),%rsi
lea    -0x28(%rbp),%rdx
mov    $0x0,%al
call   1050 <fscanf@plt>
movl   $0x0,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x28(%rbp),%eax
jge    121d <load_palette+0xcd>
mov    -0x20(%rbp),%rdi
lea    0xe49(%rip),%rsi
lea    -0x2c(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x34(%rbp),%r8
mov    $0x0,%al
call   1050 <fscanf@plt>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
imul   $0x3,-0x24(%rbp),%ecx
add    $0x0,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
imul   $0x3,-0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x34(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rax
imul   $0x3,-0x24(%rbp),%ecx
add    $0x2,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11a3 <load_palette+0x53>
mov    -0x28(%rbp),%esi
mov    -0x18(%rbp),%rdx
xor    %edi,%edi
call   1030 <dr_setRGB8multi@plt>
mov    -0x20(%rbp),%rdi
call   1040 <fclose@plt>
movl   $0x1,-0x4(%rbp)
jmp    1260 <load_palette+0x110>
mov    0x2d99(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xdb8(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<load_palette>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe95(%rip),%rsi
call   1070 <fopen@plt>
test   %rax,%rax
je     122a <load_palette+0xda>
mov    %rax,%r14
movl   $0x100,0x8(%rsp)
lea    0xe7e(%rip),%rsi
lea    0x8(%rsp),%rdx
mov    %rax,%rdi
xor    %eax,%eax
call   1050 <fscanf@plt>
mov    0x8(%rsp),%esi
test   %esi,%esi
jle    1211 <load_palette+0xc1>
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%rbp
xor    %r15d,%r15d
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
lea    0xe39(%rip),%rsi
lea    0x14(%rsp),%rdx
mov    %r13,%rcx
mov    %rbp,%r8
xor    %eax,%eax
call   1050 <fscanf@plt>
movzbl 0x14(%rsp),%eax
mov    %r15d,%ecx
mov    %al,(%rbx,%rcx,1)
movzbl 0x10(%rsp),%eax
lea    0x1(%r15),%ecx
mov    %al,(%rbx,%rcx,1)
movzbl 0xc(%rsp),%eax
lea    0x2(%r15),%ecx
mov    %al,(%rbx,%rcx,1)
add    $0x1,%r12
movslq 0x8(%rsp),%rsi
add    $0x3,%r15d
cmp    %rsi,%r12
jl     11c0 <load_palette+0x70>
xor    %edi,%edi
mov    %rbx,%rdx
call   1030 <dr_setRGB8multi@plt>
mov    %r14,%rdi
call   1040 <fclose@plt>
mov    $0x1,%eax
jmp    1244 <load_palette+0xf4>
mov    0x2daf(%rip),%rax
mov    (%rax),%edi
lea    0xdd2(%rip),%rsi
mov    %rbp,%rdx
call   1060 <fprintf@plt>
xor    %eax,%eax
add    $0x18,%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 = [3 x i8] c"%d\00"
@.str.2 = [9 x i8] c"%d %d %d\00"
@stderr = global i32 0
@.str.3 = [41 x i8] c"Error: can't open file '%s' for reading\0A\00"
define i32 @load_palette(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%12 = load i8*, i8** %4
%13 = call i32* @fopen(i8* %12, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %6
%14 = load i32*, i32** %6
%15 = icmp ne i32* %14, null
br i1 %15, label %16, label %59
16: ; preds = %2
store i32 256, i32* %8
%17 = load i32*, i32** %6
%18 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %8)
store i32 0, i32* %7
br label %19
19: ; preds = %50, %16
%20 = load i32, i32* %7
%21 = load i32, i32* %8
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %53
23: ; preds = %19
%24 = load i32*, i32** %6
%25 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %24, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %9, i32* %10, i32* %11)
%26 = load i32, i32* %9
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** %5
%29 = load i32, i32* %7
%30 = mul nsw i32 %29, 3
%31 = add nsw i32 %30, 0
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %28, i64 %32
store i8 %27, i8* %33
%34 = load i32, i32* %10
%35 = trunc i32 %34 to i8
%36 = load i8*, i8** %5
%37 = load i32, i32* %7
%38 = mul nsw i32 %37, 3
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %36, i64 %40
store i8 %35, i8* %41
%42 = load i32, i32* %11
%43 = trunc i32 %42 to i8
%44 = load i8*, i8** %5
%45 = load i32, i32* %7
%46 = mul nsw i32 %45, 3
%47 = add nsw i32 %46, 2
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %44, i64 %48
store i8 %43, i8* %49
br label %50
50: ; preds = %23
%51 = load i32, i32* %7
%52 = add nsw i32 %51, 1
store i32 %52, i32* %7
br label %19
53: ; preds = %19
%54 = load i32, i32* %8
%55 = load i8*, i8** %5
%56 = call i32 @dr_setRGB8multi(i32 0, i32 %54, i8* %55)
%57 = load i32*, i32** %6
%58 = call i32 @fclose(i32* %57)
store i32 1, i32* %3
br label %63
59: ; preds = %2
%60 = load i32, i32* @stderr
%61 = load i8*, i8** %4
%62 = call i32 @fprintf(i32 %60, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %61)
store i32 0, i32* %3
br label %63
63: ; preds = %59, %53
%64 = load i32, i32* %3
ret i32 %64
}

LLVM IR (-O2):
@.str = [3 x i8] c"rb\00"
@.str.1 = [3 x i8] c"%d\00"
@.str.2 = [9 x i8] c"%d %d %d\00"
@stderr = global i32 0
@.str.3 = [41 x i8] c"Error: can't open file '%s' for reading\0A\00"
define i32 @load_palette(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %44, label %9
9: ; preds = %2
%10 = bitcast i32* %3 to i8*
store i32 256, i32* %3
%11 = bitcast i32* %4 to i8*
%12 = bitcast i32* %5 to i8*
%13 = bitcast i32* %6 to i8*
%14 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i32* %3)
%15 = load i32, i32* %3
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %40
17: ; preds = %9, %17
%18 = phi i64 [ %36, %17 ], [ 0, %9 ]
%19 = call i32 (i32*, i8*, i32*, ...) @fscanf(i32* %7, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), i32* %4, i32* %5, i32* %6)
%20 = load i32, i32* %4
%21 = trunc i32 %20 to i8
%22 = trunc i64 %18 to i32
%23 = mul nsw i32 %22, 3
%24 = zext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 %21, i8* %25
%26 = load i32, i32* %5
%27 = trunc i32 %26 to i8
%28 = add nuw nsw i32 %23, 1
%29 = zext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %1, i64 %29
store i8 %27, i8* %30
%31 = load i32, i32* %6
%32 = trunc i32 %31 to i8
%33 = add nuw nsw i32 %23, 2
%34 = zext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %1, i64 %34
store i8 %32, i8* %35
%36 = add nuw nsw i64 %18, 1
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = icmp slt i64 %36, %38
br i1 %39, label %17, label %40
40: ; preds = %17, %9
%41 = phi i32 [ %15, %9 ], [ %37, %17 ]
%42 = call i32 @dr_setRGB8multi(i32 0, i32 %41, i8* %1)
%43 = call i32 @fclose(i32* %7)
br label %47
44: ; preds = %2
%45 = load i32, i32* @stderr
%46 = tail call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i8* %0)
br label %47
47: ; preds = %44, %40
%48 = phi i32 [ 1, %40 ], [ 0, %44 ]
ret i32 %48
}