#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*/ GIO_CMAP;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ exit(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned char ***);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, unsigned char, unsigned char, unsigned char);
int main(int argc, char **argv) {
  int fd;
  unsigned char palette[16][3];
  int idx;
  if ((fd = open("/dev/tty0", O_RDWR)) < 0) {
    perror("open");
    exit(1);
  }
  if (ioctl(fd, GIO_CMAP, &palette) < 0) {
    perror("ioctl");
    exit(1);
  }
  for (idx = 0; idx < 16; idx++) {
    printf("#%02x%02x%02x\n", palette[idx][0], palette[idx][1],
           palette[idx][2]);
  }
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e7b(%rip),%rax
mov    (%rax),%esi
lea    0xe8a(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    119d <main+0x4d>
lea    0xe7c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x14(%rbp),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
lea    -0x50(%rbp),%rdx
call   1040 <ioctl@plt>
cmp    $0x0,%rax
jge    11d2 <main+0x82>
lea    0xe4c(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
movl   $0x0,-0x54(%rbp)
cmpl   $0x10,-0x54(%rbp)
jge    1239 <main+0xe9>
movslq -0x54(%rbp),%rcx
lea    -0x50(%rbp),%rax
imul   $0x3,%rcx,%rcx
add    %rcx,%rax
mov    (%rax),%dl
movslq -0x54(%rbp),%rcx
lea    -0x50(%rbp),%rax
imul   $0x3,%rcx,%rcx
add    %rcx,%rax
mov    0x1(%rax),%cl
movslq -0x54(%rbp),%rsi
lea    -0x50(%rbp),%rax
imul   $0x3,%rsi,%rsi
add    %rsi,%rax
lea    0xdf9(%rip),%rdi
movzbl %dl,%esi
movzbl %cl,%edx
movzbl 0x2(%rax),%ecx
call   1030 <printf@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    11d9 <main+0x89>
xor    %edi,%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x38,%rsp
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    0xe99(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
jns    1179 <main+0x29>
lea    0xe93(%rip),%rdi
jmp    1198 <main+0x48>
mov    0x2e58(%rip),%rcx
mov    (%rcx),%esi
mov    %rsp,%rdx
mov    %eax,%edi
call   1040 <ioctl@plt>
test   %rax,%rax
jns    11a7 <main+0x57>
lea    0xe77(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    $0x2,%ebx
lea    0xe62(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movzbl -0x2(%rsp,%rbx,1),%esi
movzbl -0x1(%rsp,%rbx,1),%edx
movzbl (%rsp,%rbx,1),%ecx
mov    %r14,%rdi
call   1030 <printf@plt>
add    $0x3,%rbx
cmp    $0x32,%rbx
jne    11c0 <main+0x70>
xor    %edi,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@.str = [10 x i8] c"/dev/tty0\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@GIO_CMAP = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [15 x i8] c"#%02x%02x%02x\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [16 x [3 x i8]]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* @O_RDWR
%10 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %9)
store i32 %10, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32, i32* @GIO_CMAP
%18 = bitcast [16 x [3 x i8]]* %7 to i8***
%19 = call i64 @ioctl(i32 %16, i32 %17, i8*** %18)
%20 = icmp slt i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %15
%22 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%23 = call i32 @exit(i32 1)
unreachable
24: ; preds = %15
store i32 0, i32* %8
br label %25
25: ; preds = %45, %24
%26 = load i32, i32* %8
%27 = icmp slt i32 %26, 16
br i1 %27, label %28, label %48
28: ; preds = %25
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [16 x [3 x i8]], [16 x [3 x i8]]* %7, i64 0, i64 %30
%32 = getelementptr inbounds [3 x i8], [3 x i8]* %31, i64 0, i64 0
%33 = load i8, i8* %32
%34 = load i32, i32* %8
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [16 x [3 x i8]], [16 x [3 x i8]]* %7, i64 0, i64 %35
%37 = getelementptr inbounds [3 x i8], [3 x i8]* %36, i64 0, i64 1
%38 = load i8, i8* %37
%39 = load i32, i32* %8
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds [16 x [3 x i8]], [16 x [3 x i8]]* %7, i64 0, i64 %40
%42 = getelementptr inbounds [3 x i8], [3 x i8]* %41, i64 0, i64 2
%43 = load i8, i8* %42
%44 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %33, i8 zeroext %38, i8 zeroext %43)
br label %45
45: ; preds = %28
%46 = load i32, i32* %8
%47 = add nsw i32 %46, 1
store i32 %47, i32* %8
br label %25
48: ; preds = %25
%49 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [10 x i8] c"/dev/tty0\00"
@O_RDWR = global i32 0
@.str.1 = [5 x i8] c"open\00"
@GIO_CMAP = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@.str.3 = [15 x i8] c"#%02x%02x%02x\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [16 x [3 x i8]]
%4 = getelementptr inbounds [16 x [3 x i8]], [16 x [3 x i8]]* %3, i64 0, i64 0, i64 0
%5 = load i32, i32* @O_RDWR
%6 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %5)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = load i32, i32* @GIO_CMAP
%13 = bitcast [16 x [3 x i8]]* %3 to i8***
%14 = call i64 @ioctl(i32 %6, i32 %12, i8*** %13)
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11, %19
%20 = phi i64 [ %28, %19 ], [ 0, %11 ]
%21 = getelementptr inbounds [16 x [3 x i8]], [16 x [3 x i8]]* %3, i64 0, i64 %20, i64 0
%22 = load i8, i8* %21
%23 = getelementptr inbounds [16 x [3 x i8]], [16 x [3 x i8]]* %3, i64 0, i64 %20, i64 1
%24 = load i8, i8* %23
%25 = getelementptr inbounds [16 x [3 x i8]], [16 x [3 x i8]]* %3, i64 0, i64 %20, i64 2
%26 = load i8, i8* %25
%27 = call i32 @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i8 zeroext %22, i8 zeroext %24, i8 zeroext %26)
%28 = add nuw nsw i64 %20, 1
%29 = icmp eq i64 %28, 16
br i1 %29, label %30, label %19
30: ; preds = %19
%31 = call i32 @exit(i32 0)
unreachable
}