#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*/ PIO_CMAP;
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getfd(int /*<<< orphan*/ *);
scalar_t__ ioctl(int, int /*<<< orphan*/, unsigned char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int fd, indx, red, green, blue;
  unsigned char cmap[48];
  fd = getfd(NULL);
  if (ioctl(fd, GIO_CMAP, cmap))
    perror("GIO_CMAP");
  if (argc != 5) {
    fprintf(stderr, "call: setpalette index red green blue\n");
    exit(1);
  }
  indx = atoi(argv[1]);
  red = atoi(argv[2]);
  green = atoi(argv[3]);
  blue = atoi(argv[4]);
  if (indx < 0 || red < 0 || green < 0 || blue < 0 || indx > 15 || red > 255 ||
      green > 255 || blue > 255) {
    fprintf(stderr, "indx must be in 0..15, colors in 0..255\n");
    exit(1);
  }
  cmap[3 * indx] = red;
  cmap[3 * indx + 1] = green;
  cmap[3 * indx + 2] = blue;
  if (ioctl(fd, PIO_CMAP, cmap))
    perror("PIO_CMAP");
  return 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)
xor    %eax,%eax
mov    %eax,%edi
call   1080 <getfd@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2e4c(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     11ad <main+0x4d>
lea    0xe58(%rip),%rdi
call   1050 <perror@plt>
cmpl   $0x5,-0x8(%rbp)
je     11d6 <main+0x76>
mov    0x2e22(%rip),%rax
mov    (%rax),%edi
lea    0xe42(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    0x20(%rax),%rdi
call   1060 <atoi@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x18(%rbp)
jl     126f <main+0x10f>
cmpl   $0x0,-0x1c(%rbp)
jl     126f <main+0x10f>
cmpl   $0x0,-0x20(%rbp)
jl     126f <main+0x10f>
cmpl   $0x0,-0x24(%rbp)
jl     126f <main+0x10f>
cmpl   $0xf,-0x18(%rbp)
jg     126f <main+0x10f>
cmpl   $0xff,-0x1c(%rbp)
jg     126f <main+0x10f>
cmpl   $0xff,-0x20(%rbp)
jg     126f <main+0x10f>
cmpl   $0xff,-0x24(%rbp)
jle    128e <main+0x12e>
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
lea    0xdb1(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    %al,%cl
imul   $0x3,-0x18(%rbp),%eax
cltq
mov    %cl,-0x60(%rbp,%rax,1)
mov    -0x20(%rbp),%eax
mov    %al,%cl
imul   $0x3,-0x18(%rbp),%eax
add    $0x1,%eax
cltq
mov    %cl,-0x60(%rbp,%rax,1)
mov    -0x24(%rbp),%eax
mov    %al,%cl
imul   $0x3,-0x18(%rbp),%eax
add    $0x2,%eax
cltq
mov    %cl,-0x60(%rbp,%rax,1)
mov    -0x14(%rbp),%edi
mov    0x2d05(%rip),%rax
mov    (%rax),%esi
lea    -0x60(%rbp),%rdx
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     12ec <main+0x18c>
lea    0xd72(%rip),%rdi
call   1050 <perror@plt>
xor    %eax,%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
xor    %edi,%edi
call   1080 <getfd@plt>
mov    %eax,%r14d
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    124c <main+0xec>
cmp    $0x5,%ebx
jne    1261 <main+0x101>
mov    0x8(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,%r15d
mov    0x10(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,%r12d
mov    0x18(%rbp),%rdi
call   1060 <atoi@plt>
mov    %eax,%ebx
mov    0x20(%rbp),%rdi
call   1060 <atoi@plt>
test   %r15d,%r15d
js     1281 <main+0x121>
test   %r12d,%r12d
js     1281 <main+0x121>
test   %ebx,%ebx
js     1281 <main+0x121>
test   %eax,%eax
js     1281 <main+0x121>
cmp    $0xf,%r15d
jg     1281 <main+0x121>
cmp    $0xff,%r12d
jg     1281 <main+0x121>
cmp    $0xff,%ebx
jg     1281 <main+0x121>
cmp    $0x100,%eax
jge    1281 <main+0x121>
mov    %r15d,%ecx
lea    (%rcx,%rcx,2),%rcx
mov    %r12b,(%rsp,%rcx,1)
mov    %bl,0x1(%rsp,%rcx,1)
mov    %al,0x2(%rsp,%rcx,1)
mov    0x2da5(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1030 <ioctl@plt>
test   %rax,%rax
jne    1273 <main+0x113>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdad(%rip),%rdi
call   1050 <perror@plt>
cmp    $0x5,%ebx
je     11a1 <main+0x41>
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xd98(%rip),%rsi
jmp    1291 <main+0x131>
lea    0xddf(%rip),%rdi
call   1050 <perror@plt>
jmp    123d <main+0xdd>
mov    0x2d58(%rip),%rax
mov    (%rax),%edi
lea    0xd9f(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@GIO_CMAP = global i32 0
@.str = [9 x i8] c"GIO_CMAP\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"call: setpalette index red green blue\0A\00"
@.str.2 = [41 x i8] c"indx must be in 0..15, colors in 0..255\0A\00"
@PIO_CMAP = global i32 0
@.str.3 = [9 x i8] c"PIO_CMAP\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [48 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = call i32 @getfd(i32* null)
store i32 %12, i32* %6
%13 = load i32, i32* %6
%14 = load i32, i32* @GIO_CMAP
%15 = getelementptr inbounds [48 x i8], [48 x i8]* %11, i64 0, i64 0
%16 = call i64 @ioctl(i32 %13, i32 %14, i8* %15)
%17 = icmp ne i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %2
%19 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %20
20: ; preds = %18, %2
%21 = load i32, i32* %4
%22 = icmp ne i32 %21, 5
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %20
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 1
%30 = load i8*, i8** %29
%31 = call i32 @atoi(i8* %30)
store i32 %31, i32* %7
%32 = load i8**, i8*** %5
%33 = getelementptr inbounds i8*, i8** %32, i64 2
%34 = load i8*, i8** %33
%35 = call i32 @atoi(i8* %34)
store i32 %35, i32* %8
%36 = load i8**, i8*** %5
%37 = getelementptr inbounds i8*, i8** %36, i64 3
%38 = load i8*, i8** %37
%39 = call i32 @atoi(i8* %38)
store i32 %39, i32* %9
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 4
%42 = load i8*, i8** %41
%43 = call i32 @atoi(i8* %42)
store i32 %43, i32* %10
%44 = load i32, i32* %7
%45 = icmp slt i32 %44, 0
br i1 %45, label %67, label %46
46: ; preds = %27
%47 = load i32, i32* %8
%48 = icmp slt i32 %47, 0
br i1 %48, label %67, label %49
49: ; preds = %46
%50 = load i32, i32* %9
%51 = icmp slt i32 %50, 0
br i1 %51, label %67, label %52
52: ; preds = %49
%53 = load i32, i32* %10
%54 = icmp slt i32 %53, 0
br i1 %54, label %67, label %55
55: ; preds = %52
%56 = load i32, i32* %7
%57 = icmp sgt i32 %56, 15
br i1 %57, label %67, label %58
58: ; preds = %55
%59 = load i32, i32* %8
%60 = icmp sgt i32 %59, 255
br i1 %60, label %67, label %61
61: ; preds = %58
%62 = load i32, i32* %9
%63 = icmp sgt i32 %62, 255
br i1 %63, label %67, label %64
64: ; preds = %61
%65 = load i32, i32* %10
%66 = icmp sgt i32 %65, 255
br i1 %66, label %67, label %71
67: ; preds = %64, %61, %58, %55, %52, %49, %46, %27
%68 = load i32, i32* @stderr
%69 = call i32 @fprintf(i32 %68, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%70 = call i32 @exit(i32 1)
unreachable
71: ; preds = %64
%72 = load i32, i32* %8
%73 = trunc i32 %72 to i8
%74 = load i32, i32* %7
%75 = mul nsw i32 3, %74
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds [48 x i8], [48 x i8]* %11, i64 0, i64 %76
store i8 %73, i8* %77
%78 = load i32, i32* %9
%79 = trunc i32 %78 to i8
%80 = load i32, i32* %7
%81 = mul nsw i32 3, %80
%82 = add nsw i32 %81, 1
%83 = sext i32 %82 to i64
%84 = getelementptr inbounds [48 x i8], [48 x i8]* %11, i64 0, i64 %83
store i8 %79, i8* %84
%85 = load i32, i32* %10
%86 = trunc i32 %85 to i8
%87 = load i32, i32* %7
%88 = mul nsw i32 3, %87
%89 = add nsw i32 %88, 2
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds [48 x i8], [48 x i8]* %11, i64 0, i64 %90
store i8 %86, i8* %91
%92 = load i32, i32* %6
%93 = load i32, i32* @PIO_CMAP
%94 = getelementptr inbounds [48 x i8], [48 x i8]* %11, i64 0, i64 0
%95 = call i64 @ioctl(i32 %92, i32 %93, i8* %94)
%96 = icmp ne i64 %95, 0
br i1 %96, label %97, label %99
97: ; preds = %71
%98 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %99
99: ; preds = %97, %71
ret i32 0
}

LLVM IR (-O2):
@GIO_CMAP = global i32 0
@.str = [9 x i8] c"GIO_CMAP\00"
@stderr = global i32 0
@.str.1 = [39 x i8] c"call: setpalette index red green blue\0A\00"
@.str.2 = [41 x i8] c"indx must be in 0..15, colors in 0..255\0A\00"
@PIO_CMAP = global i32 0
@.str.3 = [9 x i8] c"PIO_CMAP\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [48 x i8]
%4 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @getfd(i32* null)
%6 = load i32, i32* @GIO_CMAP
%7 = call i64 @ioctl(i32 %5, i32 %6, i8* %4)
%8 = icmp eq i64 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %2
%12 = icmp eq i32 %0, 5
br i1 %12, label %17, label %13
13: ; preds = %11
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %11
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = load i8*, i8** %18
%20 = call i32 @atoi(i8* %19)
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
%24 = getelementptr inbounds i8*, i8** %1, i64 3
%25 = load i8*, i8** %24
%26 = call i32 @atoi(i8* %25)
%27 = getelementptr inbounds i8*, i8** %1, i64 4
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = icmp slt i32 %20, 0
%31 = icmp slt i32 %23, 0
%32 = select i1 %30, i1 true, i1 %31
%33 = icmp slt i32 %26, 0
%34 = select i1 %32, i1 true, i1 %33
%35 = icmp slt i32 %29, 0
%36 = select i1 %34, i1 true, i1 %35
%37 = icmp sgt i32 %20, 15
%38 = select i1 %36, i1 true, i1 %37
%39 = icmp sgt i32 %23, 255
%40 = select i1 %38, i1 true, i1 %39
%41 = icmp sgt i32 %26, 255
%42 = select i1 %40, i1 true, i1 %41
%43 = icmp sgt i32 %29, 255
%44 = select i1 %42, i1 true, i1 %43
br i1 %44, label %45, label %49
45: ; preds = %17
%46 = load i32, i32* @stderr
%47 = call i32 @fprintf(i32 %46, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.2, i64 0, i64 0))
%48 = call i32 @exit(i32 1)
unreachable
49: ; preds = %17
%50 = trunc i32 %23 to i8
%51 = mul nuw nsw i32 %20, 3
%52 = zext i32 %51 to i64
%53 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 %52
store i8 %50, i8* %53
%54 = trunc i32 %26 to i8
%55 = add nuw nsw i32 %51, 1
%56 = zext i32 %55 to i64
%57 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 %56
store i8 %54, i8* %57
%58 = trunc i32 %29 to i8
%59 = add nuw nsw i32 %51, 2
%60 = zext i32 %59 to i64
%61 = getelementptr inbounds [48 x i8], [48 x i8]* %3, i64 0, i64 %60
store i8 %58, i8* %61
%62 = load i32, i32* @PIO_CMAP
%63 = call i64 @ioctl(i32 %5, i32 %62, i8* %4)
%64 = icmp eq i64 %63, 0
br i1 %64, label %67, label %65
65: ; preds = %49
%66 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %67
67: ; preds = %65, %49
ret i32 0
}