#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 */
typedef int /*<<< orphan*/ XkbDescPtr;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ *XOpenDisplay(int /*<<< orphan*/);
int /*<<< orphan*/ XkbAllocKeyboard();
int /*<<< orphan*/ enable_gr_events(int /*<<< orphan*/ *);
int /*<<< orphan*/ eprint(char *);
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int get_active_gr(int /*<<< orphan*/ *);
int /*<<< orphan*/ get_gr_names(int /*<<< orphan*/ *, int /*<<< orphan*/, int,
                                char **);
int get_gr_num(int /*<<< orphan*/ *, int /*<<< orphan*/);
void *malloc(int);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ wait_gr_event(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  Display *dpy;
  XkbDescPtr kb;
  int ngroups = 0;
  char **groups;
  int active = 0;
  int old = -1;
  int i;
  if (!(dpy = XOpenDisplay(0)))
    eprint("skb: cannot open display\n");
  if (!(kb = XkbAllocKeyboard()))
    eprint("skb: XkbAllocKeyboard()\n");
  ngroups = get_gr_num(dpy, kb);
  groups = malloc(sizeof(char *) * ngroups);
  for (i = 0; i < ngroups; i++)
    groups[i] = malloc(256);
  get_gr_names(dpy, kb, ngroups, groups);
  enable_gr_events(dpy);
  for (;;) {
    active = get_active_gr(dpy);
    if (active != old) {
      puts(groups[active]);
      fflush(stdout);
      old = active;
    }
    if (argc > 1)
      break;
    wait_gr_event(dpy);
  };
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
movl   $0xffffffff,-0x30(%rbp)
xor    %edi,%edi
call   1070 <XOpenDisplay@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    11fc <main+0x4c>
lea    0xe09(%rip),%rdi
call   10d0 <eprint@plt>
mov    $0x0,%al
call   1050 <XkbAllocKeyboard@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jne    121b <main+0x6b>
lea    0xe04(%rip),%rdi
call   10d0 <eprint@plt>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   10a0 <get_gr_num@plt>
mov    %eax,-0x20(%rbp)
movslq -0x20(%rbp),%rax
shl    $0x3,%rax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x34(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1277 <main+0xc7>
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,%rdx
mov    -0x28(%rbp),%rax
movslq -0x34(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1244 <main+0x94>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
mov    -0x28(%rbp),%rcx
call   1060 <get_gr_names@plt>
mov    -0x18(%rbp),%rdi
call   10c0 <enable_gr_events@plt>
mov    -0x18(%rbp),%rdi
call   1040 <get_active_gr@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
je     12d0 <main+0x120>
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
call   1030 <puts@plt>
mov    0x2d1d(%rip),%rax
mov    (%rax),%edi
call   1090 <fflush@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x30(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    12df <main+0x12f>
jmp    12ed <main+0x13d>
mov    -0x18(%rbp),%rdi
call   10b0 <wait_gr_event@plt>
jmp    1293 <main+0xe3>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edi,%r14d
xor    %edi,%edi
call   1070 <XOpenDisplay@plt>
mov    %rax,%r12
test   %rax,%rax
jne    11d9 <main+0x29>
lea    0xe2c(%rip),%rdi
call   10d0 <eprint@plt>
xor    %eax,%eax
call   1050 <XkbAllocKeyboard@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    11f2 <main+0x42>
lea    0xe2d(%rip),%rdi
call   10d0 <eprint@plt>
mov    %r12,%rdi
mov    %ebx,0x4(%rsp)
mov    %ebx,%esi
call   10a0 <get_gr_num@plt>
mov    %eax,%ebx
lea    0x0(,%rbx,8),%edi
call   1080 <malloc@plt>
mov    %rax,%r13
test   %ebx,%ebx
jle    1238 <main+0x88>
mov    %ebx,%ebp
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    $0x100,%edi
call   1080 <malloc@plt>
mov    %rax,0x0(%r13,%r15,8)
add    $0x1,%r15
cmp    %r15,%rbp
jne    1220 <main+0x70>
mov    %r12,%rdi
mov    0x4(%rsp),%esi
mov    %ebx,%edx
mov    %r13,%rcx
call   1060 <get_gr_names@plt>
mov    %r12,%rdi
call   10c0 <enable_gr_events@plt>
mov    $0xffffffff,%ebx
mov    0x2d83(%rip),%r15
nopl   (%rax)
mov    %r12,%rdi
call   1040 <get_active_gr@plt>
cmp    %ebx,%eax
je     1284 <main+0xd4>
mov    %eax,%ebp
cltq
mov    0x0(%r13,%rax,8),%rdi
call   1030 <puts@plt>
mov    (%r15),%edi
call   1090 <fflush@plt>
mov    %ebp,%ebx
cmp    $0x1,%r14d
jg     1294 <main+0xe4>
mov    %r12,%rdi
call   10b0 <wait_gr_event@plt>
jmp    1260 <main+0xb0>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [26 x i8] c"skb: cannot open display\0A\00"
@.str.1 = [25 x i8] c"skb: XkbAllocKeyboard()\0A\00"
@stdout = global i32 0
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 i8**
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %8
store i32 0, i32* %10
store i32 -1, i32* %11
%13 = call i32* @XOpenDisplay(i32 0)
store i32* %13, i32** %6
%14 = icmp ne i32* %13, null
br i1 %14, label %17, label %15
15: ; preds = %2
%16 = call i32 @eprint(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %17
17: ; preds = %15, %2
%18 = call i32 (...) @XkbAllocKeyboard()
store i32 %18, i32* %7
%19 = icmp ne i32 %18, 0
br i1 %19, label %22, label %20
20: ; preds = %17
%21 = call i32 @eprint(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %22
22: ; preds = %20, %17
%23 = load i32*, i32** %6
%24 = load i32, i32* %7
%25 = call i32 @get_gr_num(i32* %23, i32 %24)
store i32 %25, i32* %8
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = mul i64 8, %27
%29 = trunc i64 %28 to i32
%30 = call i8* @malloc(i32 %29)
%31 = bitcast i8* %30 to i8**
store i8** %31, i8*** %9
store i32 0, i32* %12
br label %32
32: ; preds = %42, %22
%33 = load i32, i32* %12
%34 = load i32, i32* %8
%35 = icmp slt i32 %33, %34
br i1 %35, label %36, label %45
36: ; preds = %32
%37 = call i8* @malloc(i32 256)
%38 = load i8**, i8*** %9
%39 = load i32, i32* %12
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %38, i64 %40
store i8* %37, i8** %41
br label %42
42: ; preds = %36
%43 = load i32, i32* %12
%44 = add nsw i32 %43, 1
store i32 %44, i32* %12
br label %32
45: ; preds = %32
%46 = load i32*, i32** %6
%47 = load i32, i32* %7
%48 = load i32, i32* %8
%49 = load i8**, i8*** %9
%50 = call i32 @get_gr_names(i32* %46, i32 %47, i32 %48, i8** %49)
%51 = load i32*, i32** %6
%52 = call i32 @enable_gr_events(i32* %51)
br label %53
53: ; preds = %73, %45
%54 = load i32*, i32** %6
%55 = call i32 @get_active_gr(i32* %54)
store i32 %55, i32* %10
%56 = load i32, i32* %10
%57 = load i32, i32* %11
%58 = icmp ne i32 %56, %57
br i1 %58, label %59, label %69
59: ; preds = %53
%60 = load i8**, i8*** %9
%61 = load i32, i32* %10
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8*, i8** %60, i64 %62
%64 = load i8*, i8** %63
%65 = call i32 @puts(i8* %64)
%66 = load i32, i32* @stdout
%67 = call i32 @fflush(i32 %66)
%68 = load i32, i32* %10
store i32 %68, i32* %11
br label %69
69: ; preds = %59, %53
%70 = load i32, i32* %4
%71 = icmp sgt i32 %70, 1
br i1 %71, label %72, label %73
72: ; preds = %69
br label %76
73: ; preds = %69
%74 = load i32*, i32** %6
%75 = call i32 @wait_gr_event(i32* %74)
br label %53
76: ; preds = %72
ret i32 0
}

LLVM IR (-O2):
@.str = [26 x i8] c"skb: cannot open display\0A\00"
@.str.1 = [25 x i8] c"skb: XkbAllocKeyboard()\0A\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = tail call i32* @XOpenDisplay(i32 0)
%4 = icmp eq i32* %3, null
br i1 %4, label %5, label %7
5: ; preds = %2
%6 = tail call i32 @eprint(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %7
7: ; preds = %5, %2
%8 = tail call i32 (...) @XkbAllocKeyboard()
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %7
%11 = tail call i32 @eprint(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0))
br label %12
12: ; preds = %10, %7
%13 = tail call i32 @get_gr_num(i32* %3, i32 %8)
%14 = shl i32 %13, 3
%15 = tail call i8* @malloc(i32 %14)
%16 = bitcast i8* %15 to i8**
%17 = icmp sgt i32 %13, 0
br i1 %17, label %18, label %26
18: ; preds = %12
%19 = zext i32 %13 to i64
br label %20
20: ; preds = %18, %20
%21 = phi i64 [ 0, %18 ], [ %24, %20 ]
%22 = tail call dereferenceable_or_null(256) i8* @malloc(i32 256)
%23 = getelementptr inbounds i8*, i8** %16, i64 %21
store i8* %22, i8** %23
%24 = add nuw nsw i64 %21, 1
%25 = icmp eq i64 %24, %19
br i1 %25, label %26, label %20
26: ; preds = %20, %12
%27 = tail call i32 @get_gr_names(i32* %3, i32 %8, i32 %13, i8** %16)
%28 = tail call i32 @enable_gr_events(i32* %3)
%29 = icmp sgt i32 %0, 1
br label %30
30: ; preds = %43, %26
%31 = phi i32 [ -1, %26 ], [ %42, %43 ]
%32 = tail call i32 @get_active_gr(i32* %3)
%33 = icmp eq i32 %32, %31
br i1 %33, label %41, label %34
34: ; preds = %30
%35 = sext i32 %32 to i64
%36 = getelementptr inbounds i8*, i8** %16, i64 %35
%37 = load i8*, i8** %36
%38 = tail call i32 @puts(i8* dereferenceable(1) %37)
%39 = load i32, i32* @stdout
%40 = tail call i32 @fflush(i32 %39)
br label %41
41: ; preds = %34, %30
%42 = phi i32 [ %32, %34 ], [ %31, %30 ]
br i1 %29, label %45, label %43
43: ; preds = %41
%44 = tail call i32 @wait_gr_event(i32* %3)
br label %30
45: ; preds = %41
ret i32 0
}