#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 palette_getindex(unsigned char *);
void pixels_to_colours(int *colours, unsigned char *pixels, int width,
                       int height, int x) {
  int i = height;
  int *colour = colours;
  unsigned char *rgb = &pixels[3 * x];
  while (--i >= 0) {
    *colour = palette_getindex(rgb);
    colour++;
    rgb += 3 * width;
  }
}


x86 assembly (clang, -O0):
<pixels_to_colours>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
imul   $0x3,-0x1c(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmp    $0x0,%eax
jl     1190 <pixels_to_colours+0x80>
mov    -0x30(%rbp),%rdi
call   1030 <palette_getindex@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x28(%rbp)
imul   $0x3,-0x14(%rbp),%ecx
mov    -0x30(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    114a <pixels_to_colours+0x3a>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pixels_to_colours>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %ecx,%ecx
jle    115c <pixels_to_colours+0x4c>
mov    %ecx,%r14d
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    (%r8,%r8,2),%eax
cltq
add    %rax,%rbx
lea    (%rdx,%rdx,2),%eax
movslq %eax,%r15
add    $0x1,%r14d
nopw   0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1030 <palette_getindex@plt>
mov    %eax,0x0(%rbp)
add    $0x4,%rbp
add    %r15,%rbx
add    $0xffffffff,%r14d
cmp    $0x1,%r14d
ja     1140 <pixels_to_colours+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @pixels_to_colours(i32* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32*
%13 = alloca i8*
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%14 = load i32, i32* %9
store i32 %14, i32* %11
%15 = load i32*, i32** %6
store i32* %15, i32** %12
%16 = load i8*, i8** %7
%17 = load i32, i32* %10
%18 = mul nsw i32 3, %17
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i8, i8* %16, i64 %19
store i8* %20, i8** %13
br label %21
21: ; preds = %25, %5
%22 = load i32, i32* %11
%23 = add nsw i32 %22, -1
store i32 %23, i32* %11
%24 = icmp sge i32 %23, 0
br i1 %24, label %25, label %36
25: ; preds = %21
%26 = load i8*, i8** %13
%27 = call i32 @palette_getindex(i8* %26)
%28 = load i32*, i32** %12
store i32 %27, i32* %28
%29 = load i32*, i32** %12
%30 = getelementptr inbounds i32, i32* %29, i32 1
store i32* %30, i32** %12
%31 = load i32, i32* %8
%32 = mul nsw i32 3, %31
%33 = load i8*, i8** %13
%34 = sext i32 %32 to i64
%35 = getelementptr inbounds i8, i8* %33, i64 %34
store i8* %35, i8** %13
br label %21
36: ; preds = %21
ret void
}

LLVM IR (-O2):
define void @pixels_to_colours(i32* nocapture writeonly %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = icmp sgt i32 %3, 0
br i1 %6, label %7, label %22
7: ; preds = %5
%8 = mul nsw i32 %4, 3
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %1, i64 %9
%11 = mul nsw i32 %2, 3
%12 = sext i32 %11 to i64
br label %13
13: ; preds = %7, %13
%14 = phi i8* [ %10, %7 ], [ %20, %13 ]
%15 = phi i32* [ %0, %7 ], [ %19, %13 ]
%16 = phi i32 [ %3, %7 ], [ %17, %13 ]
%17 = add nsw i32 %16, -1
%18 = tail call i32 @palette_getindex(i8* %14)
store i32 %18, i32* %15
%19 = getelementptr inbounds i32, i32* %15, i64 1
%20 = getelementptr inbounds i8, i8* %14, i64 %12
%21 = icmp ugt i32 %16, 1
br i1 %21, label %13, label %22
22: ; preds = %13, %5
ret void
}