#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 */
typedef struct TYPE_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  int led_mask;
};
typedef TYPE_1__ XKeyboardState;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ XCloseDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ XGetKeyboardControl(int /*<<< orphan*/ *, TYPE_1__ *);
int /*<<< orphan*/ *XOpenDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
const char *keyboard_indicators(void) {
  Display *dpy = XOpenDisplay(NULL);
  XKeyboardState state;
  if (dpy == NULL) {
    fprintf(stderr, "Cannot open display\n");
    return NULL;
  }
  XGetKeyboardControl(dpy, &state);
  XCloseDisplay(dpy);
  switch (state.led_mask) {
  case 1:
    return "c";
  case 2:
    return "n";
  case 3:
    return "cn";
  default:
    return "";
  }
}


x86 assembly (clang, -O0):
<keyboard_indicators>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
xor    %eax,%eax
mov    %eax,%edi
call   1040 <XOpenDisplay@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1182 <keyboard_indicators+0x42>
mov    0x2e79(%rip),%rax
mov    (%rax),%edi
lea    0xe90(%rip),%rsi
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    1209 <keyboard_indicators+0xc9>
mov    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1030 <XGetKeyboardControl@plt>
mov    -0x10(%rbp),%rdi
call   1060 <XCloseDisplay@plt>
mov    -0x18(%rbp),%eax
mov    %eax,-0x1c(%rbp)
sub    $0x1,%eax
je     11ce <keyboard_indicators+0x8e>
jmp    11ac <keyboard_indicators+0x6c>
mov    -0x1c(%rbp),%eax
sub    $0x2,%eax
je     11de <keyboard_indicators+0x9e>
jmp    11bd <keyboard_indicators+0x7d>
mov    -0x1c(%rbp),%eax
sub    $0x3,%eax
je     11ee <keyboard_indicators+0xae>
jmp    11fe <keyboard_indicators+0xbe>
lea    0xe40(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <keyboard_indicators+0xc9>
lea    0xe33(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <keyboard_indicators+0xc9>
lea    0xe22(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1209 <keyboard_indicators+0xc9>
lea    0xe0f(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keyboard_indicators>:
push   %rbx
sub    $0x10,%rsp
xor    %edi,%edi
call   1040 <XOpenDisplay@plt>
test   %rax,%rax
je     1187 <keyboard_indicators+0x47>
mov    %rax,%rbx
lea    0x8(%rsp),%rsi
mov    %rax,%rdi
call   1030 <XGetKeyboardControl@plt>
mov    %rbx,%rdi
call   1060 <XCloseDisplay@plt>
mov    0x8(%rsp),%eax
add    $0xffffffff,%eax
cmp    $0x2,%eax
ja     11a0 <keyboard_indicators+0x60>
cltq
lea    0xe9e(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    11a7 <keyboard_indicators+0x67>
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
lea    0xe69(%rip),%rsi
call   1050 <fprintf@plt>
xor    %eax,%eax
jmp    11a7 <keyboard_indicators+0x67>
lea    0xe6d(%rip),%rax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"Cannot open display\0A\00"
@.str.1 = [2 x i8] c"c\00"
@.str.2 = [2 x i8] c"n\00"
@.str.3 = [3 x i8] c"cn\00"
@.str.4 = [1 x i8] zeroinitializer
define i8* @keyboard_indicators() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca %struct.TYPE_3__
%4 = call i32* @XOpenDisplay(i32* null)
store i32* %4, i32** %2
%5 = load i32*, i32** %2
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = load i32, i32* @stderr
%9 = call i32 @fprintf(i32 %8, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %1
br label %21
10: ; preds = %0
%11 = load i32*, i32** %2
%12 = call i32 @XGetKeyboardControl(i32* %11, %struct.TYPE_3__* %3)
%13 = load i32*, i32** %2
%14 = call i32 @XCloseDisplay(i32* %13)
%15 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %3, i32 0, i32 0
%16 = load i32, i32* %15
switch i32 %16, label %20 [
i32 1, label %17
i32 2, label %18
i32 3, label %19
]
17: ; preds = %10
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %1
br label %21
18: ; preds = %10
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i8** %1
br label %21
19: ; preds = %10
store i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8** %1
br label %21
20: ; preds = %10
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), i8** %1
br label %21
21: ; preds = %20, %19, %18, %17, %7
%22 = load i8*, i8** %1
ret i8* %22
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32 }
@stderr = global i32 0
@.str = [21 x i8] c"Cannot open display\0A\00"
@.str.1 = [2 x i8] c"c\00"
@.str.2 = [2 x i8] c"n\00"
@.str.3 = [3 x i8] c"cn\00"
@.str.4 = [1 x i8] zeroinitializer
@reltable.keyboard_indicators = [3 x i32] [i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.1 to i64), i64 ptrtoint ([3 x i32]* @reltable.keyboard_indicators to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([2 x i8]* @.str.2 to i64), i64 ptrtoint ([3 x i32]* @reltable.keyboard_indicators to i64)) to i32), i32 trunc (i64 sub (i64 ptrtoint ([3 x i8]* @.str.3 to i64), i64 ptrtoint ([3 x i32]* @reltable.keyboard_indicators to i64)) to i32)]
define i8* @keyboard_indicators() {
%1 = alloca %struct.TYPE_3__
%2 = tail call i32* @XOpenDisplay(i32* null)
%3 = bitcast %struct.TYPE_3__* %1 to i8*
%4 = icmp eq i32* %2, null
br i1 %4, label %5, label %8
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str, i64 0, i64 0))
br label %19
8: ; preds = %0
%9 = call i32 @XGetKeyboardControl(i32* %2, %struct.TYPE_3__* %1)
%10 = call i32 @XCloseDisplay(i32* %2)
%11 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %1, i64 0, i32 0
%12 = load i32, i32* %11
%13 = add i32 %12, -1
%14 = icmp ult i32 %13, 3
br i1 %14, label %15, label %19
15: ; preds = %8
%16 = sext i32 %13 to i64
%17 = shl i64 %16, 2
%18 = call i8* @llvm.load.relative.i64(i8* bitcast ([3 x i32]* @reltable.keyboard_indicators to i8*), i64 %17)
br label %19
19: ; preds = %8, %15, %5
%20 = phi i8* [ null, %5 ], [ %18, %15 ], [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str.4, i64 0, i64 0), %8 ]
ret i8* %20
}