#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*/ bm_get_rgb(unsigned int, unsigned char *, unsigned char *,
                              unsigned char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, unsigned char, ...);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/ *);
int bm_save_palette(const char *filename, unsigned int *pal,
                    unsigned int npal) {
  unsigned i;
  FILE *f;
  if (!filename)
    return 0;
  f = fopen(filename, "w");
  if (!f)
    return 0;
  fputs("JASC-PAL\n", f);
  fputs("0100\n", f);
  fprintf(f, "%u\n", npal);
  for (i = 0; i < npal; i++) {
    unsigned char R, G, B;
    bm_get_rgb(pal[i], &R, &G, &B);
    fprintf(f, "%u %u %u\n", R, G, B);
  }
  fclose(f);
  return 1;
}


x86 assembly (clang, -O0):
<bm_save_palette>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    117a <bm_save_palette+0x2a>
movl   $0x0,-0x4(%rbp)
jmp    124a <bm_save_palette+0xfa>
mov    -0x10(%rbp),%rdi
lea    0xe7b(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11a5 <bm_save_palette+0x55>
movl   $0x0,-0x4(%rbp)
jmp    124a <bm_save_palette+0xfa>
mov    -0x28(%rbp),%rsi
lea    0xe52(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x28(%rbp),%rsi
lea    0xe4c(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%eax
lea    0xe45(%rip),%rsi
movzbl %al,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    123a <bm_save_palette+0xea>
mov    -0x18(%rbp),%rax
mov    -0x20(%rbp),%ecx
mov    (%rax,%rcx,4),%edi
lea    -0x29(%rbp),%rsi
lea    -0x2a(%rbp),%rdx
lea    -0x2b(%rbp),%rcx
call   1060 <bm_get_rgb@plt>
mov    -0x28(%rbp),%rdi
mov    -0x29(%rbp),%al
movzbl -0x2a(%rbp),%ecx
movzbl -0x2b(%rbp),%r8d
lea    0xdf0(%rip),%rsi
movzbl %al,%edx
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    11e4 <bm_save_palette+0x94>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bm_save_palette>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %ebp,%ebp
test   %rdi,%rdi
je     1239 <bm_save_palette+0xe9>
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xe8d(%rip),%rsi
call   1060 <fopen@plt>
test   %rax,%rax
je     1239 <bm_save_palette+0xe9>
mov    %rax,%rbx
lea    0xe77(%rip),%rdi
mov    $0x9,%esi
mov    $0x1,%edx
mov    %rax,%rcx
call   1070 <fwrite@plt>
lea    0xe68(%rip),%rdi
mov    $0x5,%esi
mov    $0x1,%edx
mov    %rbx,%rcx
call   1070 <fwrite@plt>
movzbl %r15b,%edx
lea    0xe57(%rip),%rsi
mov    %rbx,%rdi
xor    %eax,%eax
call   1040 <fprintf@plt>
test   %r15d,%r15d
je     122c <bm_save_palette+0xdc>
mov    %r15d,%r15d
lea    0x5(%rsp),%r13
lea    0xe33(%rip),%rbp
xor    %r12d,%r12d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%r14,%r12,4),%edi
lea    0x7(%rsp),%rsi
lea    0x6(%rsp),%rdx
mov    %r13,%rcx
call   1050 <bm_get_rgb@plt>
movzbl 0x7(%rsp),%edx
movzbl 0x6(%rsp),%ecx
movzbl 0x5(%rsp),%r8d
mov    %rbx,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
add    $0x1,%r12
cmp    %r12,%r15
jne    11f0 <bm_save_palette+0xa0>
mov    %rbx,%rdi
call   1030 <fclose@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [10 x i8] c"JASC-PAL\0A\00"
@.str.2 = [6 x i8] c"0100\0A\00"
@.str.3 = [4 x i8] c"%u\0A\00"
@.str.4 = [10 x i8] c"%u %u %u\0A\00"
define i32 @bm_save_palette(i8* %0, i32* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i8
%11 = alloca i8
%12 = alloca i8
store i8* %0, i8** %5
store i32* %1, i32** %6
store i32 %2, i32* %7
%13 = load i8*, i8** %5
%14 = icmp ne i8* %13, null
br i1 %14, label %16, label %15
15: ; preds = %3
store i32 0, i32* %4
br label %55
16: ; preds = %3
%17 = load i8*, i8** %5
%18 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %18, i32** %9
%19 = load i32*, i32** %9
%20 = icmp ne i32* %19, null
br i1 %20, label %22, label %21
21: ; preds = %16
store i32 0, i32* %4
br label %55
22: ; preds = %16
%23 = load i32*, i32** %9
%24 = call i32 @fputs(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32* %23)
%25 = load i32*, i32** %9
%26 = call i32 @fputs(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32* %25)
%27 = load i32*, i32** %9
%28 = load i32, i32* %7
%29 = trunc i32 %28 to i8
%30 = call i32 (i32*, i8*, i8, ...) @fprintf(i32* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %29)
store i32 0, i32* %8
br label %31
31: ; preds = %49, %22
%32 = load i32, i32* %8
%33 = load i32, i32* %7
%34 = icmp ult i32 %32, %33
br i1 %34, label %35, label %52
35: ; preds = %31
%36 = load i32*, i32** %6
%37 = load i32, i32* %8
%38 = zext i32 %37 to i64
%39 = getelementptr inbounds i32, i32* %36, i64 %38
%40 = load i32, i32* %39
%41 = call i32 @bm_get_rgb(i32 %40, i8* %10, i8* %11, i8* %12)
%42 = load i32*, i32** %9
%43 = load i8, i8* %10
%44 = load i8, i8* %11
%45 = zext i8 %44 to i32
%46 = load i8, i8* %12
%47 = zext i8 %46 to i32
%48 = call i32 (i32*, i8*, i8, ...) @fprintf(i32* %42, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %43, i32 %45, i32 %47)
br label %49
49: ; preds = %35
%50 = load i32, i32* %8
%51 = add i32 %50, 1
store i32 %51, i32* %8
br label %31
52: ; preds = %31
%53 = load i32*, i32** %9
%54 = call i32 @fclose(i32* %53)
store i32 1, i32* %4
br label %55
55: ; preds = %52, %21, %15
%56 = load i32, i32* %4
ret i32 %56
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [10 x i8] c"JASC-PAL\0A\00"
@.str.2 = [6 x i8] c"0100\0A\00"
@.str.3 = [4 x i8] c"%u\0A\00"
@.str.4 = [10 x i8] c"%u %u %u\0A\00"
define i32 @bm_save_palette(i8* %0, i32* nocapture %1, i32 %2) {
%4 = alloca i8
%5 = alloca i8
%6 = alloca i8
%7 = icmp eq i8* %0, null
br i1 %7, label %34, label %8
8: ; preds = %3
%9 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %34, label %11
11: ; preds = %8
%12 = tail call i64 @fwrite(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i64 9, i64 1, i32* %9)
%13 = tail call i64 @fwrite(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i64 5, i64 1, i32* %9)
%14 = trunc i32 %2 to i8
%15 = tail call i32 (i32*, i8*, i8, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %14)
%16 = icmp eq i32 %2, 0
br i1 %16, label %32, label %17
17: ; preds = %11
%18 = zext i32 %2 to i64
br label %19
19: ; preds = %17, %19
%20 = phi i64 [ 0, %17 ], [ %30, %19 ]
%21 = getelementptr inbounds i32, i32* %1, i64 %20
%22 = load i32, i32* %21
%23 = call i32 @bm_get_rgb(i32 %22, i8* %4, i8* %5, i8* %6)
%24 = load i8, i8* %4
%25 = load i8, i8* %5
%26 = zext i8 %25 to i32
%27 = load i8, i8* %6
%28 = zext i8 %27 to i32
%29 = call i32 (i32*, i8*, i8, ...) @fprintf(i32* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8 zeroext %24, i32 %26, i32 %28)
%30 = add nuw nsw i64 %20, 1
%31 = icmp eq i64 %30, %18
br i1 %31, label %32, label %19
32: ; preds = %19, %11
%33 = call i32 @fclose(i32* %9)
br label %34
34: ; preds = %8, %3, %32
%35 = phi i32 [ 1, %32 ], [ 0, %3 ], [ 0, %8 ]
ret i32 %35
}