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

/* Variables and functions */
int /*<<< orphan*/ io_cli();
int io_load_eflags();
int /*<<< orphan*/ io_out8(int, unsigned char const);
int /*<<< orphan*/ io_store_eflags(int);
void set_palette(int start, int end, const unsigned char *rgb) {
  int i, eflags;
  eflags = io_load_eflags();
  io_cli();
  io_out8(0x03c8, start);
  for (i = 0; i <= end; ++i) {
    io_out8(0x03c9, rgb[0] / 4);
    io_out8(0x03c9, rgb[1] / 4);
    io_out8(0x03c9, rgb[2] / 4);
    rgb += 3;
  }
  io_store_eflags(eflags);
}


x86 assembly (clang, -O0):
<set_palette>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    $0x0,%al
call   1040 <io_load_eflags@plt>
mov    %eax,-0x18(%rbp)
mov    $0x0,%al
call   1030 <io_cli@plt>
mov    -0x4(%rbp),%eax
mov    $0x3c8,%edi
movzbl %al,%esi
call   1050 <io_out8@plt>
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     11f6 <set_palette+0xb6>
mov    -0x10(%rbp),%rax
movzbl (%rax),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    $0x3c9,%edi
movzbl %al,%esi
call   1050 <io_out8@plt>
mov    -0x10(%rbp),%rax
movzbl 0x1(%rax),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    $0x3c9,%edi
movzbl %al,%esi
call   1050 <io_out8@plt>
mov    -0x10(%rbp),%rax
movzbl 0x2(%rax),%eax
mov    $0x4,%ecx
cltd
idiv   %ecx
mov    $0x3c9,%edi
movzbl %al,%esi
call   1050 <io_out8@plt>
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117a <set_palette+0x3a>
mov    -0x18(%rbp),%edi
call   1060 <io_store_eflags@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_palette>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%ebp
mov    %edi,%r15d
xor    %eax,%eax
call   1040 <io_load_eflags@plt>
mov    %eax,%r14d
xor    %eax,%eax
call   1030 <io_cli@plt>
movzbl %r15b,%esi
mov    $0x3c8,%edi
call   1050 <io_out8@plt>
test   %ebp,%ebp
js     11c4 <set_palette+0x84>
add    $0x1,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
movzbl (%rbx),%eax
shr    $0x2,%al
movzbl %al,%esi
mov    $0x3c9,%edi
call   1050 <io_out8@plt>
movzbl 0x1(%rbx),%eax
shr    $0x2,%al
movzbl %al,%esi
mov    $0x3c9,%edi
call   1050 <io_out8@plt>
movzbl 0x2(%rbx),%eax
shr    $0x2,%al
movzbl %al,%esi
mov    $0x3c9,%edi
call   1050 <io_out8@plt>
add    $0x3,%rbx
add    $0xffffffff,%ebp
jne    1180 <set_palette+0x40>
mov    %r14d,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1060 <io_store_eflags@plt>

LLVM IR (-O0):
define void @set_palette(i32 %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8* %2, i8** %6
%9 = call i32 (...) @io_load_eflags()
store i32 %9, i32* %8
%10 = call i32 (...) @io_cli()
%11 = load i32, i32* %4
%12 = trunc i32 %11 to i8
%13 = call i32 @io_out8(i32 968, i8 zeroext %12)
store i32 0, i32* %7
br label %14
14: ; preds = %42, %3
%15 = load i32, i32* %7
%16 = load i32, i32* %5
%17 = icmp sle i32 %15, %16
br i1 %17, label %18, label %45
18: ; preds = %14
%19 = load i8*, i8** %6
%20 = getelementptr inbounds i8, i8* %19, i64 0
%21 = load i8, i8* %20
%22 = zext i8 %21 to i32
%23 = sdiv i32 %22, 4
%24 = trunc i32 %23 to i8
%25 = call i32 @io_out8(i32 969, i8 zeroext %24)
%26 = load i8*, i8** %6
%27 = getelementptr inbounds i8, i8* %26, i64 1
%28 = load i8, i8* %27
%29 = zext i8 %28 to i32
%30 = sdiv i32 %29, 4
%31 = trunc i32 %30 to i8
%32 = call i32 @io_out8(i32 969, i8 zeroext %31)
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i64 2
%35 = load i8, i8* %34
%36 = zext i8 %35 to i32
%37 = sdiv i32 %36, 4
%38 = trunc i32 %37 to i8
%39 = call i32 @io_out8(i32 969, i8 zeroext %38)
%40 = load i8*, i8** %6
%41 = getelementptr inbounds i8, i8* %40, i64 3
store i8* %41, i8** %6
br label %42
42: ; preds = %18
%43 = load i32, i32* %7
%44 = add nsw i32 %43, 1
store i32 %44, i32* %7
br label %14
45: ; preds = %14
%46 = load i32, i32* %8
%47 = call i32 @io_store_eflags(i32 %46)
ret void
}

LLVM IR (-O2):
define void @set_palette(i32 %0, i32 %1, i8* nocapture %2) {
%4 = tail call i32 (...) @io_load_eflags()
%5 = tail call i32 (...) @io_cli()
%6 = trunc i32 %0 to i8
%7 = tail call i32 @io_out8(i32 968, i8 zeroext %6)
%8 = icmp slt i32 %1, 0
br i1 %8, label %26, label %9
9: ; preds = %3, %9
%10 = phi i8* [ %23, %9 ], [ %2, %3 ]
%11 = phi i32 [ %24, %9 ], [ 0, %3 ]
%12 = load i8, i8* %10
%13 = lshr i8 %12, 2
%14 = tail call i32 @io_out8(i32 969, i8 zeroext %13)
%15 = getelementptr inbounds i8, i8* %10, i64 1
%16 = load i8, i8* %15
%17 = lshr i8 %16, 2
%18 = tail call i32 @io_out8(i32 969, i8 zeroext %17)
%19 = getelementptr inbounds i8, i8* %10, i64 2
%20 = load i8, i8* %19
%21 = lshr i8 %20, 2
%22 = tail call i32 @io_out8(i32 969, i8 zeroext %21)
%23 = getelementptr inbounds i8, i8* %10, i64 3
%24 = add nuw i32 %11, 1
%25 = icmp eq i32 %11, %1
br i1 %25, label %26, label %9
26: ; preds = %9, %3
%27 = tail call i32 @io_store_eflags(i32 %4)
ret void
}