#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 */
struct fb_cmap {
  unsigned short *red;
  unsigned short *green;
  unsigned short *blue;
  unsigned short *transp;
  int len;
  scalar_t__ start;
};

/* Variables and functions */
int /*<<< orphan*/ FBIOPUTCMAP;
scalar_t__ ioctl(int, int /*<<< orphan*/, struct fb_cmap *);
int /*<<< orphan*/ perror(char *);
int colortable(int fd) {
  unsigned short red[256];
  unsigned short green[256];
  unsigned short blue[256];
  unsigned short transp[256];
  struct fb_cmap colormap;
  int count;
  colormap.red = red;
  colormap.green = green;
  colormap.blue = blue;
  colormap.transp = transp;
  colormap.start = 0;
  colormap.len = 256;
  for (count = 0; count < 256; count++) {
    switch (count & 3) {
    case 0:
      transp[count] = 0x0000;
      break;
    case 1:
      transp[count] = 0xFFFF;
      break;
    default:
      transp[count] = 0x1FFF;
      break;
    }
    red[count] = (count & 0xc0) << 8;
    green[count] = (count & 0x30) << 10;
    blue[count] = (count & 0x0c) << 12;
  }
  if (ioctl(fd, FBIOPUTCMAP, &colormap) < 0) {
    perror("FBIOPUTCMAP");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<colortable>:
push   %rbp
mov    %rsp,%rbp
sub    $0x850,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x840(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x838(%rbp)
lea    -0x610(%rbp),%rax
mov    %rax,-0x830(%rbp)
lea    -0x810(%rbp),%rax
mov    %rax,-0x828(%rbp)
movq   $0x0,-0x818(%rbp)
movl   $0x100,-0x820(%rbp)
movl   $0x0,-0x844(%rbp)
cmpl   $0x100,-0x844(%rbp)
jge    1270 <colortable+0x150>
mov    -0x844(%rbp),%eax
and    $0x3,%eax
mov    %eax,-0x848(%rbp)
je     11c3 <colortable+0xa3>
jmp    11af <colortable+0x8f>
mov    -0x848(%rbp),%eax
sub    $0x1,%eax
je     11d9 <colortable+0xb9>
jmp    11ef <colortable+0xcf>
movslq -0x844(%rbp),%rax
movw   $0x0,-0x810(%rbp,%rax,2)
jmp    1200 <colortable+0xe0>
movslq -0x844(%rbp),%rax
movw   $0xffff,-0x810(%rbp,%rax,2)
ff ff ff
jmp    1200 <colortable+0xe0>
movslq -0x844(%rbp),%rax
movw   $0x1fff,-0x810(%rbp,%rax,2)
ff ff 1f
mov    -0x844(%rbp),%eax
and    $0xc0,%eax
shl    $0x8,%eax
mov    %ax,%cx
movslq -0x844(%rbp),%rax
mov    %cx,-0x210(%rbp,%rax,2)
ff
mov    -0x844(%rbp),%eax
and    $0x30,%eax
shl    $0xa,%eax
mov    %ax,%cx
movslq -0x844(%rbp),%rax
mov    %cx,-0x410(%rbp,%rax,2)
ff
mov    -0x844(%rbp),%eax
and    $0xc,%eax
shl    $0xc,%eax
mov    %ax,%cx
movslq -0x844(%rbp),%rax
mov    %cx,-0x610(%rbp,%rax,2)
ff
mov    -0x844(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x844(%rbp)
jmp    1185 <colortable+0x65>
mov    -0x8(%rbp),%edi
mov    0x2d66(%rip),%rax
mov    (%rax),%esi
lea    -0x840(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
jge    12aa <colortable+0x18a>
lea    0xd67(%rip),%rdi
call   1040 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12b1 <colortable+0x191>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x850,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<colortable>:
sub    $0x838,%rsp
lea    0x630(%rsp),%rax
mov    %rax,(%rsp)
lea    0x430(%rsp),%rax
mov    %rax,0x8(%rsp)
lea    0x230(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0x30(%rsp),%rax
mov    %rax,0x18(%rsp)
movq   $0x0,0x28(%rsp)
movl   $0x100,0x20(%rsp)
movdqa 0xe90(%rip),%xmm5
movdqa 0xe98(%rip),%xmm4
movdqa 0xea0(%rip),%xmm2
xor    %eax,%eax
movdqa 0xea5(%rip),%xmm10
movdqa 0xeac(%rip),%xmm11
movdqa 0xeb3(%rip),%xmm8
pxor   %xmm12,%xmm12
movdqa 0xeb6(%rip),%xmm7
movdqa 0xebd(%rip),%xmm13
movdqa 0xec4(%rip),%xmm9
nopl   0x0(%rax)
movdqa %xmm5,%xmm3
pand   %xmm10,%xmm3
movdqa %xmm4,%xmm1
pand   %xmm10,%xmm1
movdqa %xmm1,%xmm6
pcmpeqd %xmm11,%xmm6
movdqa %xmm3,%xmm0
pcmpeqd %xmm11,%xmm0
packssdw %xmm0,%xmm6
por    %xmm8,%xmm6
pcmpeqd %xmm12,%xmm1
pcmpeqd %xmm12,%xmm3
packssdw %xmm3,%xmm1
pandn  %xmm6,%xmm1
movdqa %xmm1,0x30(%rsp,%rax,2)
movdqa %xmm2,%xmm0
psllw  $0x8,%xmm0
pand   %xmm7,%xmm0
movdqa %xmm0,0x630(%rsp,%rax,2)
movdqa %xmm2,%xmm0
psllw  $0xa,%xmm0
pand   %xmm7,%xmm0
movdqa %xmm0,0x430(%rsp,%rax,2)
movdqa %xmm2,%xmm0
psllw  $0xc,%xmm0
pand   %xmm7,%xmm0
movdqa %xmm0,0x230(%rsp,%rax,2)
add    $0x8,%rax
paddd  %xmm13,%xmm4
paddd  %xmm13,%xmm5
paddw  %xmm9,%xmm2
cmp    $0x100,%rax
jne    11c0 <colortable+0xa0>
mov    0x2d73(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
call   1030 <ioctl@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
js     1289 <colortable+0x169>
add    $0x838,%rsp
ret
lea    0xe00(%rip),%rdi
call   1040 <perror@plt>
mov    $0xffffffff,%eax
add    $0x838,%rsp
ret

LLVM IR (-O0):
%struct.fb_cmap = type { i16*, i16*, i16*, i16*, i32, i64 }
@FBIOPUTCMAP = global i32 0
@.str = [12 x i8] c"FBIOPUTCMAP\00"
define i32 @colortable(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [256 x i16]
%5 = alloca [256 x i16]
%6 = alloca [256 x i16]
%7 = alloca [256 x i16]
%8 = alloca %struct.fb_cmap
%9 = alloca i32
store i32 %0, i32* %3
%10 = getelementptr inbounds [256 x i16], [256 x i16]* %4, i64 0, i64 0
%11 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %8, i32 0, i32 0
store i16* %10, i16** %11
%12 = getelementptr inbounds [256 x i16], [256 x i16]* %5, i64 0, i64 0
%13 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %8, i32 0, i32 1
store i16* %12, i16** %13
%14 = getelementptr inbounds [256 x i16], [256 x i16]* %6, i64 0, i64 0
%15 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %8, i32 0, i32 2
store i16* %14, i16** %15
%16 = getelementptr inbounds [256 x i16], [256 x i16]* %7, i64 0, i64 0
%17 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %8, i32 0, i32 3
store i16* %16, i16** %17
%18 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %8, i32 0, i32 5
store i64 0, i64* %18
%19 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %8, i32 0, i32 4
store i32 256, i32* %19
store i32 0, i32* %9
br label %20
20: ; preds = %60, %1
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 256
br i1 %22, label %23, label %63
23: ; preds = %20
%24 = load i32, i32* %9
%25 = and i32 %24, 3
switch i32 %25, label %34 [
i32 0, label %26
i32 1, label %30
]
26: ; preds = %23
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [256 x i16], [256 x i16]* %7, i64 0, i64 %28
store i16 0, i16* %29
br label %38
30: ; preds = %23
%31 = load i32, i32* %9
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [256 x i16], [256 x i16]* %7, i64 0, i64 %32
store i16 -1, i16* %33
br label %38
34: ; preds = %23
%35 = load i32, i32* %9
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds [256 x i16], [256 x i16]* %7, i64 0, i64 %36
store i16 8191, i16* %37
br label %38
38: ; preds = %34, %30, %26
%39 = load i32, i32* %9
%40 = and i32 %39, 192
%41 = shl i32 %40, 8
%42 = trunc i32 %41 to i16
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [256 x i16], [256 x i16]* %4, i64 0, i64 %44
store i16 %42, i16* %45
%46 = load i32, i32* %9
%47 = and i32 %46, 48
%48 = shl i32 %47, 10
%49 = trunc i32 %48 to i16
%50 = load i32, i32* %9
%51 = sext i32 %50 to i64
%52 = getelementptr inbounds [256 x i16], [256 x i16]* %5, i64 0, i64 %51
store i16 %49, i16* %52
%53 = load i32, i32* %9
%54 = and i32 %53, 12
%55 = shl i32 %54, 12
%56 = trunc i32 %55 to i16
%57 = load i32, i32* %9
%58 = sext i32 %57 to i64
%59 = getelementptr inbounds [256 x i16], [256 x i16]* %6, i64 0, i64 %58
store i16 %56, i16* %59
br label %60
60: ; preds = %38
%61 = load i32, i32* %9
%62 = add nsw i32 %61, 1
store i32 %62, i32* %9
br label %20
63: ; preds = %20
%64 = load i32, i32* %3
%65 = load i32, i32* @FBIOPUTCMAP
%66 = call i64 @ioctl(i32 %64, i32 %65, %struct.fb_cmap* %8)
%67 = icmp slt i64 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %71
70: ; preds = %63
store i32 0, i32* %2
br label %71
71: ; preds = %70, %68
%72 = load i32, i32* %2
ret i32 %72
}

LLVM IR (-O2):
%struct.fb_cmap = type { i16*, i16*, i16*, i16*, i32, i64 }
@FBIOPUTCMAP = global i32 0
@.str = [12 x i8] c"FBIOPUTCMAP\00"
define i32 @colortable(i32 %0) {
%2 = alloca [256 x i16]
%3 = alloca [256 x i16]
%4 = alloca [256 x i16]
%5 = alloca [256 x i16]
%6 = alloca %struct.fb_cmap
%7 = bitcast [256 x i16]* %2 to i8*
%8 = bitcast [256 x i16]* %3 to i8*
%9 = bitcast [256 x i16]* %4 to i8*
%10 = bitcast [256 x i16]* %5 to i8*
%11 = bitcast %struct.fb_cmap* %6 to i8*
%12 = getelementptr inbounds [256 x i16], [256 x i16]* %2, i64 0, i64 0
%13 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %6, i64 0, i32 0
store i16* %12, i16** %13
%14 = getelementptr inbounds [256 x i16], [256 x i16]* %3, i64 0, i64 0
%15 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %6, i64 0, i32 1
store i16* %14, i16** %15
%16 = getelementptr inbounds [256 x i16], [256 x i16]* %4, i64 0, i64 0
%17 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %6, i64 0, i32 2
store i16* %16, i16** %17
%18 = getelementptr inbounds [256 x i16], [256 x i16]* %5, i64 0, i64 0
%19 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %6, i64 0, i32 3
store i16* %18, i16** %19
%20 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %6, i64 0, i32 5
store i64 0, i64* %20
%21 = getelementptr inbounds %struct.fb_cmap, %struct.fb_cmap* %6, i64 0, i32 4
store i32 256, i32* %21
br label %22
22: ; preds = %22, %1
%23 = phi i64 [ 0, %1 ], [ %45, %22 ]
%24 = phi <8 x i32> [ <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>, %1 ], [ %46, %22 ]
%25 = phi <8 x i16> [ <i16 0, i16 1, i16 2, i16 3, i16 4, i16 5, i16 6, i16 7>, %1 ], [ %47, %22 ]
%26 = and <8 x i32> %24, <i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3, i32 3>
%27 = icmp eq <8 x i32> %26, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>
%28 = select <8 x i1> %27, <8 x i16> <i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1, i16 -1>, <8 x i16> <i16 8191, i16 8191, i16 8191, i16 8191, i16 8191, i16 8191, i16 8191, i16 8191>
%29 = icmp eq <8 x i32> %26, zeroinitializer
%30 = select <8 x i1> %29, <8 x i16> zeroinitializer, <8 x i16> %28
%31 = getelementptr inbounds [256 x i16], [256 x i16]* %5, i64 0, i64 %23
%32 = bitcast i16* %31 to <8 x i16>*
store <8 x i16> %30, <8 x i16>* %32
%33 = shl nuw <8 x i16> %25, <i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8>
%34 = and <8 x i16> %33, <i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384>
%35 = getelementptr inbounds [256 x i16], [256 x i16]* %2, i64 0, i64 %23
%36 = bitcast i16* %35 to <8 x i16>*
store <8 x i16> %34, <8 x i16>* %36
%37 = shl <8 x i16> %25, <i16 10, i16 10, i16 10, i16 10, i16 10, i16 10, i16 10, i16 10>
%38 = and <8 x i16> %37, <i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384>
%39 = getelementptr inbounds [256 x i16], [256 x i16]* %3, i64 0, i64 %23
%40 = bitcast i16* %39 to <8 x i16>*
store <8 x i16> %38, <8 x i16>* %40
%41 = shl <8 x i16> %25, <i16 12, i16 12, i16 12, i16 12, i16 12, i16 12, i16 12, i16 12>
%42 = and <8 x i16> %41, <i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384, i16 -16384>
%43 = getelementptr inbounds [256 x i16], [256 x i16]* %4, i64 0, i64 %23
%44 = bitcast i16* %43 to <8 x i16>*
store <8 x i16> %42, <8 x i16>* %44
%45 = add nuw i64 %23, 8
%46 = add <8 x i32> %24, <i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8, i32 8>
%47 = add <8 x i16> %25, <i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8, i16 8>
%48 = icmp eq i64 %45, 256
br i1 %48, label %49, label %22
49: ; preds = %22
%50 = load i32, i32* @FBIOPUTCMAP
%51 = call i64 @ioctl(i32 %0, i32 %50, %struct.fb_cmap* %6)
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %49
%54 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %55
55: ; preds = %49, %53
%56 = phi i32 [ -1, %53 ], [ 0, %49 ]
ret i32 %56
}