#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_10__ TYPE_3__;
typedef struct TYPE_9__ TYPE_2__;
typedef struct TYPE_8__ TYPE_1__;

/* Type definitions */
struct TYPE_9__ {
  int /*<<< orphan*/ group;
};
typedef TYPE_2__ XkbStateRec;
struct TYPE_10__ {
  TYPE_1__ *names;
};
typedef TYPE_3__ XkbDescRec;
struct TYPE_8__ {
  int /*<<< orphan*/ symbols;
};
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
scalar_t__ XCloseDisplay(int /*<<< orphan*/ *);
int /*<<< orphan*/ XFree(char *);
char *XGetAtomName(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ *XOpenDisplay(int /*<<< orphan*/ *);
TYPE_3__ *XkbAllocKeyboard();
int /*<<< orphan*/ XkbFreeKeyboard(TYPE_3__ *, int /*<<< orphan*/, int);
scalar_t__ XkbGetNames(int /*<<< orphan*/ *, int /*<<< orphan*/, TYPE_3__ *);
scalar_t__ XkbGetState(int /*<<< orphan*/ *, int /*<<< orphan*/, TYPE_2__ *);
int /*<<< orphan*/ XkbSymbolsNameMask;
int /*<<< orphan*/ XkbUseCoreKbd;
scalar_t__ bprintf(char *, int /*<<< orphan*/);
int /*<<< orphan*/ get_layout(char *, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *);
const char *keymap(void) {
  Display *dpy;
  XkbDescRec *desc;
  XkbStateRec state;
  char *symbols, *layout;
  layout = NULL;
  if (!(dpy = XOpenDisplay(NULL))) {
    warn("XOpenDisplay: Failed to open display");
    return NULL;
  }
  if (!(desc = XkbAllocKeyboard())) {
    warn("XkbAllocKeyboard: Failed to allocate keyboard");
    goto end;
  }
  if (XkbGetNames(dpy, XkbSymbolsNameMask, desc)) {
    warn("XkbGetNames: Failed to retrieve key symbols");
    goto end;
  }
  if (XkbGetState(dpy, XkbUseCoreKbd, &state)) {
    warn("XkbGetState: Failed to retrieve keyboard state");
    goto end;
  }
  if (!(symbols = XGetAtomName(dpy, desc->names->symbols))) {
    warn("XGetAtomName: Failed to get atom name");
    goto end;
  }
  layout = (char *)bprintf("%s", get_layout(symbols, state.group));
  XFree(symbols);
end:
  XkbFreeKeyboard(desc, XkbSymbolsNameMask, 1);
  if (XCloseDisplay(dpy)) {
    warn("XCloseDisplay: Failed to close display");
  }
  return layout;
}


x86 assembly (clang, -O0):
<keymap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movq   $0x0,-0x30(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1090 <XOpenDisplay@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11f0 <keymap+0x40>
lea    0xe22(%rip),%rdi
call   1040 <warn@plt>
movq   $0x0,-0x8(%rbp)
jmp    130e <keymap+0x15e>
mov    $0x0,%al
call   1050 <XkbAllocKeyboard@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    1216 <keymap+0x66>
lea    0xe19(%rip),%rdi
call   1040 <warn@plt>
jmp    12d0 <keymap+0x120>
mov    -0x10(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rdx
call   10c0 <XkbGetNames@plt>
cmp    $0x0,%rax
je     1247 <keymap+0x97>
lea    0xe16(%rip),%rdi
call   1040 <warn@plt>
jmp    12d0 <keymap+0x120>
mov    -0x10(%rbp),%rdi
mov    0x2d8e(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1060 <XkbGetState@plt>
cmp    $0x0,%rax
je     1278 <keymap+0xc8>
lea    0xe11(%rip),%rdi
call   1040 <warn@plt>
jmp    12d0 <keymap+0x120>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    (%rax),%esi
call   10a0 <XGetAtomName@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    12a9 <keymap+0xf9>
lea    0xe0f(%rip),%rdi
call   1040 <warn@plt>
jmp    12d0 <keymap+0x120>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%esi
call   1030 <get_layout@plt>
mov    %eax,%esi
lea    0xe16(%rip),%rdi
call   1080 <bprintf@plt>
mov    %rax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
call   1070 <XFree@plt>
mov    -0x18(%rbp),%rdi
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
mov    $0x1,%edx
call   10b0 <XkbFreeKeyboard@plt>
mov    -0x10(%rbp),%rdi
call   10d0 <XCloseDisplay@plt>
cmp    $0x0,%rax
je     1306 <keymap+0x156>
lea    0xdd6(%rip),%rdi
call   1040 <warn@plt>
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<keymap>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
xor    %edi,%edi
call   1090 <XOpenDisplay@plt>
test   %rax,%rax
je     11f8 <keymap+0x48>
mov    %rax,%r15
xor    %eax,%eax
call   1050 <XkbAllocKeyboard@plt>
mov    %rax,%r14
test   %rax,%rax
je     1208 <keymap+0x58>
mov    0x2e0b(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
mov    %r14,%rdx
call   10c0 <XkbGetNames@plt>
test   %rax,%rax
je     1211 <keymap+0x61>
lea    0xe5d(%rip),%rdi
jmp    1231 <keymap+0x81>
lea    0xe01(%rip),%rdi
call   1040 <warn@plt>
xor    %ebx,%ebx
jmp    1267 <keymap+0xb7>
lea    0xe16(%rip),%rdi
jmp    1231 <keymap+0x81>
mov    0x2dc8(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r15,%rdi
call   1060 <XkbGetState@plt>
test   %rax,%rax
je     1276 <keymap+0xc6>
lea    0xe4e(%rip),%rdi
call   1040 <warn@plt>
xor    %ebx,%ebx
mov    0x2da9(%rip),%rax
mov    (%rax),%esi
mov    %r14,%rdi
mov    $0x1,%edx
call   10b0 <XkbFreeKeyboard@plt>
mov    %r15,%rdi
call   10d0 <XCloseDisplay@plt>
test   %rax,%rax
je     1267 <keymap+0xb7>
lea    0xe75(%rip),%rdi
call   1040 <warn@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
mov    (%r14),%rax
mov    (%rax),%esi
mov    %r15,%rdi
call   10a0 <XGetAtomName@plt>
test   %rax,%rax
je     12b1 <keymap+0x101>
mov    %rax,%r12
mov    (%rsp),%esi
mov    %rax,%rdi
call   1030 <get_layout@plt>
lea    0xe37(%rip),%rdi
mov    %eax,%esi
call   1080 <bprintf@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1070 <XFree@plt>
jmp    1238 <keymap+0x88>
lea    0xdf6(%rip),%rdi
jmp    1231 <keymap+0x81>

LLVM IR (-O0):
%struct.TYPE_10__ = type { %struct.TYPE_8__* }
%struct.TYPE_8__ = type { i32 }
%struct.TYPE_9__ = type { i32 }
@.str = [37 x i8] c"XOpenDisplay: Failed to open display\00"
@.str.1 = [46 x i8] c"XkbAllocKeyboard: Failed to allocate keyboard\00"
@XkbSymbolsNameMask = global i32 0
@.str.2 = [44 x i8] c"XkbGetNames: Failed to retrieve key symbols\00"
@XkbUseCoreKbd = global i32 0
@.str.3 = [47 x i8] c"XkbGetState: Failed to retrieve keyboard state\00"
@.str.4 = [38 x i8] c"XGetAtomName: Failed to get atom name\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [39 x i8] c"XCloseDisplay: Failed to close display\00"
define i8* @keymap() {
%1 = alloca i8*
%2 = alloca i32*
%3 = alloca %struct.TYPE_10__*
%4 = alloca %struct.TYPE_9__
%5 = alloca i8*
%6 = alloca i8*
store i8* null, i8** %6
%7 = call i32* @XOpenDisplay(i32* null)
store i32* %7, i32** %2
%8 = icmp ne i32* %7, null
br i1 %8, label %11, label %9
9: ; preds = %0
%10 = call i32 @warn(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %1
br label %62
11: ; preds = %0
%12 = call %struct.TYPE_10__* (...) @XkbAllocKeyboard()
store %struct.TYPE_10__* %12, %struct.TYPE_10__** %3
%13 = icmp ne %struct.TYPE_10__* %12, null
br i1 %13, label %16, label %14
14: ; preds = %11
%15 = call i32 @warn(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %51
16: ; preds = %11
%17 = load i32*, i32** %2
%18 = load i32, i32* @XkbSymbolsNameMask
%19 = load %struct.TYPE_10__*, %struct.TYPE_10__** %3
%20 = call i64 @XkbGetNames(i32* %17, i32 %18, %struct.TYPE_10__* %19)
%21 = icmp ne i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %16
%23 = call i32 @warn(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %51
24: ; preds = %16
%25 = load i32*, i32** %2
%26 = load i32, i32* @XkbUseCoreKbd
%27 = call i64 @XkbGetState(i32* %25, i32 %26, %struct.TYPE_9__* %4)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @warn(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %51
31: ; preds = %24
%32 = load i32*, i32** %2
%33 = load %struct.TYPE_10__*, %struct.TYPE_10__** %3
%34 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %33, i32 0, i32 0
%35 = load %struct.TYPE_8__*, %struct.TYPE_8__** %34
%36 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %35, i32 0, i32 0
%37 = load i32, i32* %36
%38 = call i8* @XGetAtomName(i32* %32, i32 %37)
store i8* %38, i8** %5
%39 = icmp ne i8* %38, null
br i1 %39, label %42, label %40
40: ; preds = %31
%41 = call i32 @warn(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %51
42: ; preds = %31
%43 = load i8*, i8** %5
%44 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %4, i32 0, i32 0
%45 = load i32, i32* %44
%46 = call i32 @get_layout(i8* %43, i32 %45)
%47 = call i64 @bprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %46)
%48 = inttoptr i64 %47 to i8*
store i8* %48, i8** %6
%49 = load i8*, i8** %5
%50 = call i32 @XFree(i8* %49)
br label %51
51: ; preds = %42, %40, %29, %22, %14
%52 = load %struct.TYPE_10__*, %struct.TYPE_10__** %3
%53 = load i32, i32* @XkbSymbolsNameMask
%54 = call i32 @XkbFreeKeyboard(%struct.TYPE_10__* %52, i32 %53, i32 1)
%55 = load i32*, i32** %2
%56 = call i64 @XCloseDisplay(i32* %55)
%57 = icmp ne i64 %56, 0
br i1 %57, label %58, label %60
58: ; preds = %51
%59 = call i32 @warn(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
br label %60
60: ; preds = %58, %51
%61 = load i8*, i8** %6
store i8* %61, i8** %1
br label %62
62: ; preds = %60, %9
%63 = load i8*, i8** %1
ret i8* %63
}

LLVM IR (-O2):
%struct.TYPE_9__ = type { i32 }
%struct.TYPE_10__ = type { %struct.TYPE_8__* }
%struct.TYPE_8__ = type { i32 }
@.str = [37 x i8] c"XOpenDisplay: Failed to open display\00"
@.str.1 = [46 x i8] c"XkbAllocKeyboard: Failed to allocate keyboard\00"
@XkbSymbolsNameMask = global i32 0
@.str.2 = [44 x i8] c"XkbGetNames: Failed to retrieve key symbols\00"
@XkbUseCoreKbd = global i32 0
@.str.3 = [47 x i8] c"XkbGetState: Failed to retrieve keyboard state\00"
@.str.4 = [38 x i8] c"XGetAtomName: Failed to get atom name\00"
@.str.5 = [3 x i8] c"%s\00"
@.str.6 = [39 x i8] c"XCloseDisplay: Failed to close display\00"
define i8* @keymap() {
%1 = alloca %struct.TYPE_9__
%2 = bitcast %struct.TYPE_9__* %1 to i8*
%3 = tail call i32* @XOpenDisplay(i32* null)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %0
%6 = tail call i32 @warn(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %48
7: ; preds = %0
%8 = tail call %struct.TYPE_10__* (...) @XkbAllocKeyboard()
%9 = icmp eq %struct.TYPE_10__* %8, null
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @warn(i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str.1, i64 0, i64 0))
br label %40
12: ; preds = %7
%13 = load i32, i32* @XkbSymbolsNameMask
%14 = tail call i64 @XkbGetNames(i32* %3, i32 %13, %struct.TYPE_10__* %8)
%15 = icmp eq i64 %14, 0
br i1 %15, label %18, label %16
16: ; preds = %12
%17 = tail call i32 @warn(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.2, i64 0, i64 0))
br label %40
18: ; preds = %12
%19 = load i32, i32* @XkbUseCoreKbd
%20 = call i64 @XkbGetState(i32* %3, i32 %19, %struct.TYPE_9__* %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %18
%23 = call i32 @warn(i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str.3, i64 0, i64 0))
br label %40
24: ; preds = %18
%25 = getelementptr inbounds %struct.TYPE_10__, %struct.TYPE_10__* %8, i64 0, i32 0
%26 = load %struct.TYPE_8__*, %struct.TYPE_8__** %25
%27 = getelementptr inbounds %struct.TYPE_8__, %struct.TYPE_8__* %26, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i8* @XGetAtomName(i32* %3, i32 %28)
%30 = icmp eq i8* %29, null
br i1 %30, label %31, label %33
31: ; preds = %24
%32 = call i32 @warn(i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.4, i64 0, i64 0))
br label %40
33: ; preds = %24
%34 = getelementptr inbounds %struct.TYPE_9__, %struct.TYPE_9__* %1, i64 0, i32 0
%35 = load i32, i32* %34
%36 = call i32 @get_layout(i8* %29, i32 %35)
%37 = call i64 @bprintf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0), i32 %36)
%38 = inttoptr i64 %37 to i8*
%39 = call i32 @XFree(i8* %29)
br label %40
40: ; preds = %33, %31, %22, %16, %10
%41 = phi i8* [ null, %16 ], [ null, %22 ], [ %38, %33 ], [ null, %31 ], [ null, %10 ]
%42 = load i32, i32* @XkbSymbolsNameMask
%43 = call i32 @XkbFreeKeyboard(%struct.TYPE_10__* %8, i32 %42, i32 1)
%44 = call i64 @XCloseDisplay(i32* %3)
%45 = icmp eq i64 %44, 0
br i1 %45, label %48, label %46
46: ; preds = %40
%47 = call i32 @warn(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.6, i64 0, i64 0))
br label %48
48: ; preds = %40, %46, %5
%49 = phi i8* [ null, %5 ], [ %41, %46 ], [ %41, %40 ]
ret i8* %49
}