#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_4__ TYPE_1__;

/* Type definitions */
typedef int wchar_t;
typedef int /*<<< orphan*/ e;
struct TYPE_4__ {
  int serial;
  int state;
  int keycode;
  void *same_screen;
  int /*<<< orphan*/ *display;
  void *send_event;
  int /*<<< orphan*/ type;
};
typedef TYPE_1__ XKeyPressedEvent;
typedef scalar_t__ KeySym;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ KeyPress;
int /*<<< orphan*/ LC_CTYPE;
void *True;
int /*<<< orphan*/ XCloseDisplay(int /*<<< orphan*/ *);
char *XDisplayName(char *);
int XLookupString(TYPE_1__ *, char *, int, scalar_t__ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *XOpenDisplay(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int mbstowcs(int *, char *, int);
int /*<<< orphan*/ memset(TYPE_1__ *, int /*<<< orphan*/, int);
int /*<<< orphan*/ setlocale(int /*<<< orphan*/, char *);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  const char *programname;
  char text[256];
  char *displayname = NULL;
  char *outfname;
  char *sections[6] = {"noshift",    "shift",    "altgr",
                       "shiftaltgr", "capslock", "shiftcapslock"};
  int states[6] = {0, 1, 0x80, 0x81, 2, 3};
  int i;
  int idx;
  int char_count;
  int nbytes = 0;
  int unicode;
  Display *dpy;
  KeySym ks;
  FILE *outf;
  XKeyPressedEvent e;
  wchar_t wtext[256];
  setlocale(LC_CTYPE, "");
  programname = argv[0];
  if (argc != 2) {
    fprintf(stderr, "Usage: %s out_filename\n", programname);
    fprintf(stderr, "Example: %s /etc/xrdp/km-0409.ini\n", programname);
    return 1;
  }
  outfname = argv[1];
  dpy = XOpenDisplay(displayname);
  if (!dpy) {
    fprintf(stderr, "%s:  unable to open display '%s'\n", programname,
            XDisplayName(displayname));
    return 1;
  }
  outf = fopen(outfname, "w");
  if (outf == NULL) {
    fprintf(stderr, "%s:  unable to create file '%s'\n", programname, outfname);
    XCloseDisplay(dpy);
    return 1;
  }
  memset(&e, 0, sizeof(e));
  e.type = KeyPress;
  e.serial = 16;
  e.send_event = True;
  e.display = dpy;
  e.same_screen = True;
  for (idx = 0; idx < 6; idx++) {
    fprintf(outf, "[%s]\n", sections[idx]);
    e.state = states[idx];
    for (i = 8; i <= 137; i++) {
      e.keycode = i;
      nbytes = XLookupString(&e, text, 255, &ks, NULL);
      text[nbytes] = 0;
      char_count = mbstowcs(wtext, text, 255);
      unicode = 0;
      if (char_count == 1) {
        unicode = wtext[0];
      }
      fprintf(outf, "Key%d=%d:%d\n", i, (int)ks, unicode);
    }
    if (idx != 4) {
      fprintf(outf, "\n");
    }
  }
  XCloseDisplay(dpy);
  fclose(outf);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x5f0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x128(%rbp)
lea    -0x160(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1060 <memset@plt>
lea    0xe22(%rip),%rax
mov    %rax,-0x160(%rbp)
lea    0xe16(%rip),%rax
mov    %rax,-0x158(%rbp)
lea    0xe13(%rip),%rax
mov    %rax,-0x150(%rbp)
lea    0xe00(%rip),%rax
mov    %rax,-0x148(%rbp)
lea    0xe02(%rip),%rax
mov    %rax,-0x140(%rbp)
lea    0xdef(%rip),%rax
mov    %rax,-0x138(%rbp)
mov    0xe8e(%rip),%rax
mov    %rax,-0x180(%rbp)
mov    0xe88(%rip),%rax
mov    %rax,-0x178(%rbp)
mov    0xe82(%rip),%rax
mov    %rax,-0x170(%rbp)
movl   $0x0,-0x190(%rbp)
mov    0x2d82(%rip),%rax
mov    (%rax),%edi
lea    0xe31(%rip),%rsi
call   10a0 <setlocale@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x18(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     12cd <main+0x12d>
mov    0x2d38(%rip),%rax
movslq (%rax),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd83(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    0x2d1c(%rip),%rax
movslq (%rax),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd7f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1597 <main+0x3f7>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x130(%rbp)
mov    -0x128(%rbp),%rdi
call   1070 <XOpenDisplay@plt>
mov    %rax,-0x1a0(%rbp)
cmpq   $0x0,-0x1a0(%rbp)
jne    1350 <main+0x1b0>
mov    0x2cc4(%rip),%rax
movslq (%rax),%rax
mov    %rax,-0x5f0(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x5e8(%rbp)
mov    -0x128(%rbp),%rdi
call   1090 <XDisplayName@plt>
mov    -0x5f0(%rbp),%rdi
mov    -0x5e8(%rbp),%rdx
mov    %rax,%rcx
lea    0xd1f(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1597 <main+0x3f7>
mov    -0x130(%rbp),%rdi
lea    0xd20(%rip),%rsi
call   10b0 <fopen@plt>
mov    %rax,-0x1b0(%rbp)
cmpq   $0x0,-0x1b0(%rbp)
jne    13b3 <main+0x213>
mov    0x2c49(%rip),%rax
movslq (%rax),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x130(%rbp),%rcx
lea    0xcec(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x1a0(%rbp),%rdi
call   10c0 <XCloseDisplay@plt>
movl   $0x1,-0x4(%rbp)
jmp    1597 <main+0x3f7>
lea    -0x1e0(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1060 <memset@plt>
mov    0x2c0b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x1b8(%rbp)
movl   $0x10,-0x1e0(%rbp)
mov    0x2bfa(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x1c0(%rbp)
mov    -0x1a0(%rbp),%rax
mov    %rax,-0x1c8(%rbp)
mov    0x2bdb(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x1d0(%rbp)
movl   $0x0,-0x188(%rbp)
cmpl   $0x6,-0x188(%rbp)
jge    1578 <main+0x3d8>
mov    -0x1b0(%rbp),%rdi
movslq -0x188(%rbp),%rax
mov    -0x160(%rbp,%rax,8),%rdx
ff
lea    0xc5e(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
movslq -0x188(%rbp),%rax
mov    -0x180(%rbp,%rax,4),%eax
mov    %eax,-0x1dc(%rbp)
movl   $0x8,-0x184(%rbp)
cmpl   $0x89,-0x184(%rbp)
jg     153d <main+0x39d>
mov    -0x184(%rbp),%eax
mov    %eax,-0x1d8(%rbp)
lea    -0x120(%rbp),%rsi
lea    -0x1e0(%rbp),%rdi
mov    $0xff,%edx
lea    -0x1a8(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%r8d
call   1030 <XLookupString@plt>
mov    %eax,-0x190(%rbp)
movslq -0x190(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x5e0(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1040 <mbstowcs@plt>
mov    %eax,-0x18c(%rbp)
movl   $0x0,-0x194(%rbp)
cmpl   $0x1,-0x18c(%rbp)
jne    14fe <main+0x35e>
mov    -0x5e0(%rbp),%eax
mov    %eax,-0x194(%rbp)
mov    -0x1b0(%rbp),%rdi
mov    -0x184(%rbp),%edx
mov    -0x1a8(%rbp),%rax
mov    %eax,%ecx
mov    -0x194(%rbp),%r8d
lea    0xb85(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    -0x184(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x184(%rbp)
jmp    1468 <main+0x2c8>
cmpl   $0x4,-0x188(%rbp)
je     155f <main+0x3bf>
mov    -0x1b0(%rbp),%rdi
lea    0xb47(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    1564 <main+0x3c4>
mov    -0x188(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x188(%rbp)
jmp    1419 <main+0x279>
mov    -0x1a0(%rbp),%rdi
call   10c0 <XCloseDisplay@plt>
mov    -0x1b0(%rbp),%rdi
call   1050 <fclose@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x5f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x588,%rsp
mov    %rsi,%rbp
mov    %edi,%ebx
lea    0xe33(%rip),%rax
mov    %rax,0x50(%rsp)
lea    0xe29(%rip),%rax
mov    %rax,0x58(%rsp)
lea    0xe28(%rip),%rax
mov    %rax,0x60(%rsp)
lea    0xe17(%rip),%rax
mov    %rax,0x68(%rsp)
lea    0xe1b(%rip),%rax
mov    %rax,0x70(%rsp)
lea    0xe0a(%rip),%rax
mov    %rax,0x78(%rsp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
lea    0xe82(%rip),%rsi
call   10b0 <setlocale@plt>
mov    0x0(%rbp),%r14
cmp    $0x2,%ebx
jne    13cb <main+0x21b>
mov    0x8(%rbp),%rbp
xor    %edi,%edi
call   1080 <XOpenDisplay@plt>
test   %rax,%rax
je     13fc <main+0x24c>
mov    %rax,%rbx
lea    0xe30(%rip),%rsi
mov    %rbp,%rdi
call   10c0 <fopen@plt>
test   %rax,%rax
je     1426 <main+0x276>
mov    %rax,%r13
lea    0x20(%rsp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1060 <memset@plt>
mov    0x2d5e(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x48(%rsp)
movl   $0x10,0x20(%rsp)
mov    0x2d51(%rip),%rax
mov    (%rax),%rax
mov    %rax,0x40(%rsp)
mov    %rbx,0x8(%rsp)
mov    %rbx,0x38(%rsp)
mov    %rax,0x30(%rsp)
lea    0x80(%rsp),%rbx
lea    0x18(%rsp),%rbp
lea    0x180(%rsp),%r14
lea    0xde5(%rip),%r12
xor    %r15d,%r15d
lea    0xd34(%rip),%rdx
jmp    12d8 <main+0x128>
xchg   %ax,%ax
mov    0x50(%rsp,%rax,8),%rdx
mov    %rax,%r15
mov    %r13,%rdi
lea    0xdbf(%rip),%rsi
xor    %eax,%eax
call   1090 <fprintf@plt>
lea    0xdd0(%rip),%rax
mov    %r15,0x10(%rsp)
mov    (%rax,%r15,4),%eax
mov    %eax,0x24(%rsp)
mov    $0x8,%r15d
jmp    1331 <main+0x181>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x18(%rsp),%ecx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r15d,%edx
xor    %eax,%eax
call   1090 <fprintf@plt>
add    $0x1,%r15d
cmp    $0x8a,%r15d
je     1380 <main+0x1d0>
mov    %r15d,0x28(%rsp)
lea    0x20(%rsp),%rdi
mov    %rbx,%rsi
mov    $0xff,%edx
mov    %rbp,%rcx
xor    %r8d,%r8d
call   1030 <XLookupString@plt>
cltq
movb   $0x0,0x80(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0xff,%edx
call   1040 <mbstowcs@plt>
mov    $0x0,%r8d
cmp    $0x1,%eax
jne    1310 <main+0x160>
mov    0x180(%rsp),%r8d
jmp    1310 <main+0x160>
nopl   (%rax)
mov    $0x5,%eax
mov    0x10(%rsp),%r15
cmp    $0x4,%r15
je     12d0 <main+0x120>
mov    $0xa,%edi
mov    %r13,%rsi
call   1070 <fputc@plt>
add    $0x1,%r15
mov    %r15,%rax
cmp    $0x6,%r15
jne    12d0 <main+0x120>
mov    0x8(%rsp),%rdi
call   10d0 <XCloseDisplay@plt>
mov    %r13,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
jmp    1451 <main+0x2a1>
mov    0x2bf6(%rip),%rbx
movslq (%rbx),%rdi
lea    0xc45(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
movslq (%rbx),%rdi
lea    0xc49(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    144c <main+0x29c>
mov    0x2bc5(%rip),%rax
movslq (%rax),%rbx
xor    %edi,%edi
call   10a0 <XDisplayName@plt>
lea    0xc48(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
jmp    144c <main+0x29c>
mov    0x2b9b(%rip),%rax
movslq (%rax),%rdi
lea    0xc49(%rip),%rsi
mov    %r14,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1090 <fprintf@plt>
mov    %rbx,%rdi
call   10d0 <XCloseDisplay@plt>
mov    $0x1,%eax
add    $0x588,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i32, i32, i8*, i32*, i8*, i32 }
@.str = [8 x i8] c"noshift\00"
@.str.1 = [6 x i8] c"shift\00"
@.str.2 = [6 x i8] c"altgr\00"
@.str.3 = [11 x i8] c"shiftaltgr\00"
@.str.4 = [9 x i8] c"capslock\00"
@.str.5 = [14 x i8] c"shiftcapslock\00"
@__const.main.states = [6 x i32] [i32 0, i32 1, i32 128, i32 129, i32 2, i32 3]
@LC_CTYPE = global i32 0
@.str.6 = [1 x i8] zeroinitializer
@stderr = global i32 0
@.str.7 = [24 x i8] c"Usage: %s out_filename\0A\00"
@.str.8 = [35 x i8] c"Example: %s /etc/xrdp/km-0409.ini\0A\00"
@.str.9 = [34 x i8] c"%s: unable to open display '%s'\0A\00"
@.str.10 = [2 x i8] c"w\00"
@.str.11 = [33 x i8] c"%s: unable to create file '%s'\0A\00"
@KeyPress = global i32 0
@True = global i8* null
@.str.12 = [6 x i8] c"[%s]\0A\00"
@.str.13 = [13 x i8] c"Key%d=%d:%d\0A\00"
@.str.14 = [2 x i8] c"\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8*
%7 = alloca [256 x i8]
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [6 x i8*]
%11 = alloca [6 x i32]
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32*
%18 = alloca i64
%19 = alloca i32*
%20 = alloca %struct.TYPE_4__
%21 = alloca [256 x i32]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %8
%22 = bitcast [6 x i8*]* %10 to i8*
%23 = bitcast i8* %22 to [6 x i8*]*
%24 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i32 0, i32 0), i8** %24
%25 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i32 0, i32 0), i8** %25
%26 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i32 0, i32 0), i8** %26
%27 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 3
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i32 0, i32 0), i8** %27
%28 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 4
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i32 0, i32 0), i8** %28
%29 = getelementptr inbounds [6 x i8*], [6 x i8*]* %23, i32 0, i32 5
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i32 0, i32 0), i8** %29
%30 = bitcast [6 x i32]* %11 to i8*
store i32 0, i32* %15
%31 = load i32, i32* @LC_CTYPE
%32 = call i32 @setlocale(i32 %31, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = load i8*, i8** %34
store i8* %35, i8** %6
%36 = load i32, i32* %4
%37 = icmp ne i32 %36, 2
br i1 %37, label %38, label %49
38: ; preds = %2
%39 = load i32, i32* @stderr
%40 = sext i32 %39 to i64
%41 = inttoptr i64 %40 to i32*
%42 = load i8*, i8** %6
%43 = call i32 (i32*, i8*, ...) @fprintf(i32* %41, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %42)
%44 = load i32, i32* @stderr
%45 = sext i32 %44 to i64
%46 = inttoptr i64 %45 to i32*
%47 = load i8*, i8** %6
%48 = call i32 (i32*, i8*, ...) @fprintf(i32* %46, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0), i8* %47)
store i32 1, i32* %3
br label %149
49: ; preds = %2
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 1
%52 = load i8*, i8** %51
store i8* %52, i8** %9
%53 = load i8*, i8** %8
%54 = call i32* @XOpenDisplay(i8* %53)
store i32* %54, i32** %17
%55 = load i32*, i32** %17
%56 = icmp ne i32* %55, null
br i1 %56, label %65, label %57
57: ; preds = %49
%58 = load i32, i32* @stderr
%59 = sext i32 %58 to i64
%60 = inttoptr i64 %59 to i32*
%61 = load i8*, i8** %6
%62 = load i8*, i8** %8
%63 = call i8* @XDisplayName(i8* %62)
%64 = call i32 (i32*, i8*, ...) @fprintf(i32* %60, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0), i8* %61, i8* %63)
store i32 1, i32* %3
br label %149
65: ; preds = %49
%66 = load i8*, i8** %9
%67 = call i32* @fopen(i8* %66, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
store i32* %67, i32** %19
%68 = load i32*, i32** %19
%69 = icmp eq i32* %68, null
br i1 %69, label %70, label %79
70: ; preds = %65
%71 = load i32, i32* @stderr
%72 = sext i32 %71 to i64
%73 = inttoptr i64 %72 to i32*
%74 = load i8*, i8** %6
%75 = load i8*, i8** %9
%76 = call i32 (i32*, i8*, ...) @fprintf(i32* %73, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.11, i64 0, i64 0), i8* %74, i8* %75)
%77 = load i32*, i32** %17
%78 = call i32 @XCloseDisplay(i32* %77)
store i32 1, i32* %3
br label %149
79: ; preds = %65
%80 = call i32 @memset(%struct.TYPE_4__* %20, i32 0, i32 48)
%81 = load i32, i32* @KeyPress
%82 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %20, i32 0, i32 6
store i32 %81, i32* %82
%83 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %20, i32 0, i32 0
store i32 16, i32* %83
%84 = load i8*, i8** @True
%85 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %20, i32 0, i32 5
store i8* %84, i8** %85
%86 = load i32*, i32** %17
%87 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %20, i32 0, i32 4
store i32* %86, i32** %87
%88 = load i8*, i8** @True
%89 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %20, i32 0, i32 3
store i8* %88, i8** %89
store i32 0, i32* %13
br label %90
90: ; preds = %141, %79
%91 = load i32, i32* %13
%92 = icmp slt i32 %91, 6
br i1 %92, label %93, label %144
93: ; preds = %90
%94 = load i32*, i32** %19
%95 = load i32, i32* %13
%96 = sext i32 %95 to i64
%97 = getelementptr inbounds [6 x i8*], [6 x i8*]* %10, i64 0, i64 %96
%98 = load i8*, i8** %97
%99 = call i32 (i32*, i8*, ...) @fprintf(i32* %94, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i8* %98)
%100 = load i32, i32* %13
%101 = sext i32 %100 to i64
%102 = getelementptr inbounds [6 x i32], [6 x i32]* %11, i64 0, i64 %101
%103 = load i32, i32* %102
%104 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %20, i32 0, i32 1
store i32 %103, i32* %104
store i32 8, i32* %12
br label %105
105: ; preds = %131, %93
%106 = load i32, i32* %12
%107 = icmp sle i32 %106, 137
br i1 %107, label %108, label %134
108: ; preds = %105
%109 = load i32, i32* %12
%110 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %20, i32 0, i32 2
store i32 %109, i32* %110
%111 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%112 = call i32 @XLookupString(%struct.TYPE_4__* %20, i8* %111, i32 255, i64* %18, i32* null)
store i32 %112, i32* %15
%113 = load i32, i32* %15
%114 = sext i32 %113 to i64
%115 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 %114
store i8 0, i8* %115
%116 = getelementptr inbounds [256 x i32], [256 x i32]* %21, i64 0, i64 0
%117 = getelementptr inbounds [256 x i8], [256 x i8]* %7, i64 0, i64 0
%118 = call i32 @mbstowcs(i32* %116, i8* %117, i32 255)
store i32 %118, i32* %14
store i32 0, i32* %16
%119 = load i32, i32* %14
%120 = icmp eq i32 %119, 1
br i1 %120, label %121, label %124
121: ; preds = %108
%122 = getelementptr inbounds [256 x i32], [256 x i32]* %21, i64 0, i64 0
%123 = load i32, i32* %122
store i32 %123, i32* %16
br label %124
124: ; preds = %121, %108
%125 = load i32*, i32** %19
%126 = load i32, i32* %12
%127 = load i64, i64* %18
%128 = trunc i64 %127 to i32
%129 = load i32, i32* %16
%130 = call i32 (i32*, i8*, ...) @fprintf(i32* %125, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.13, i64 0, i64 0), i32 %126, i32 %128, i32 %129)
br label %131
131: ; preds = %124
%132 = load i32, i32* %12
%133 = add nsw i32 %132, 1
store i32 %133, i32* %12
br label %105
134: ; preds = %105
%135 = load i32, i32* %13
%136 = icmp ne i32 %135, 4
br i1 %136, label %137, label %140
137: ; preds = %134
%138 = load i32*, i32** %19
%139 = call i32 (i32*, i8*, ...) @fprintf(i32* %138, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.14, i64 0, i64 0))
br label %140
140: ; preds = %137, %134
br label %141
141: ; preds = %140
%142 = load i32, i32* %13
%143 = add nsw i32 %142, 1
store i32 %143, i32* %13
br label %90
144: ; preds = %90
%145 = load i32*, i32** %17
%146 = call i32 @XCloseDisplay(i32* %145)
%147 = load i32*, i32** %19
%148 = call i32 @fclose(i32* %147)
store i32 0, i32* %3
br label %149
149: ; preds = %144, %70, %57, %38
%150 = load i32, i32* %3
ret i32 %150
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i32, i32, i8*, i32*, i8*, i32 }
@.str = [8 x i8] c"noshift\00"
@.str.1 = [6 x i8] c"shift\00"
@.str.2 = [6 x i8] c"altgr\00"
@.str.3 = [11 x i8] c"shiftaltgr\00"
@.str.4 = [9 x i8] c"capslock\00"
@.str.5 = [14 x i8] c"shiftcapslock\00"
@__const.main.states = [6 x i32] [i32 0, i32 1, i32 128, i32 129, i32 2, i32 3]
@LC_CTYPE = global i32 0
@.str.6 = [1 x i8] zeroinitializer
@stderr = global i32 0
@.str.7 = [24 x i8] c"Usage: %s out_filename\0A\00"
@.str.8 = [35 x i8] c"Example: %s /etc/xrdp/km-0409.ini\0A\00"
@.str.9 = [34 x i8] c"%s: unable to open display '%s'\0A\00"
@.str.10 = [2 x i8] c"w\00"
@.str.11 = [33 x i8] c"%s: unable to create file '%s'\0A\00"
@KeyPress = global i32 0
@True = global i8* null
@.str.12 = [6 x i8] c"[%s]\0A\00"
@.str.13 = [13 x i8] c"Key%d=%d:%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = alloca [6 x i8*]
%5 = alloca i64
%6 = alloca %struct.TYPE_4__
%7 = alloca [256 x i32]
%8 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%9 = bitcast [6 x i8*]* %4 to i8*
%10 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 0
store i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8** %10
%11 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 1
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %11
%12 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 2
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8** %12
%13 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 3
store i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.3, i64 0, i64 0), i8** %13
%14 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 4
store i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i8** %14
%15 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 5
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.5, i64 0, i64 0), i8** %15
%16 = bitcast i64* %5 to i8*
%17 = bitcast %struct.TYPE_4__* %6 to i8*
%18 = bitcast [256 x i32]* %7 to i8*
%19 = load i32, i32* @LC_CTYPE
%20 = tail call i32 @setlocale(i32 %19, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.6, i64 0, i64 0))
%21 = load i8*, i8** %1
%22 = icmp eq i32 %0, 2
br i1 %22, label %32, label %23
23: ; preds = %2
%24 = load i32, i32* @stderr
%25 = sext i32 %24 to i64
%26 = inttoptr i64 %25 to i32*
%27 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %26, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.7, i64 0, i64 0), i8* %21)
%28 = load i32, i32* @stderr
%29 = sext i32 %28 to i64
%30 = inttoptr i64 %29 to i32*
%31 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %30, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.8, i64 0, i64 0), i8* %21)
br label %97
32: ; preds = %2
%33 = getelementptr inbounds i8*, i8** %1, i64 1
%34 = load i8*, i8** %33
%35 = tail call i32* @XOpenDisplay(i8* null)
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %43
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = sext i32 %38 to i64
%40 = inttoptr i64 %39 to i32*
%41 = tail call i8* @XDisplayName(i8* null)
%42 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %40, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.9, i64 0, i64 0), i8* %21, i8* %41)
br label %97
43: ; preds = %32
%44 = tail call i32* @fopen(i8* %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.10, i64 0, i64 0))
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %52
46: ; preds = %43
%47 = load i32, i32* @stderr
%48 = sext i32 %47 to i64
%49 = inttoptr i64 %48 to i32*
%50 = tail call i32 (i32*, i8*, ...) @fprintf(i32* %49, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.11, i64 0, i64 0), i8* %21, i8* %34)
%51 = tail call i32 @XCloseDisplay(i32* %35)
br label %97
52: ; preds = %43
%53 = call i32 @memset(%struct.TYPE_4__* %6, i32 0, i32 48)
%54 = load i32, i32* @KeyPress
%55 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 6
store i32 %54, i32* %55
%56 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 0
store i32 16, i32* %56
%57 = load i8*, i8** @True
%58 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 5
store i8* %57, i8** %58
%59 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 4
store i32* %35, i32** %59
%60 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 3
store i8* %57, i8** %60
%61 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 1
%62 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %6, i64 0, i32 2
%63 = getelementptr inbounds [256 x i32], [256 x i32]* %7, i64 0, i64 0
br label %64
64: ; preds = %90, %52
%65 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %52 ], [ %93, %90 ]
%66 = phi i64 [ 0, %52 ], [ %91, %90 ]
%67 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.12, i64 0, i64 0), i8* %65)
%68 = getelementptr inbounds [6 x i32], [6 x i32]* @__const.main.states, i64 0, i64 %66
%69 = load i32, i32* %68
store i32 %69, i32* %61
br label %70
70: ; preds = %64, %70
%71 = phi i32 [ 8, %64 ], [ %82, %70 ]
store i32 %71, i32* %62
%72 = call i32 @XLookupString(%struct.TYPE_4__* %6, i8* %8, i32 255, i64* %5, i32* null)
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %73
store i8 0, i8* %74
%75 = call i32 @mbstowcs(i32* %63, i8* %8, i32 255)
%76 = icmp eq i32 %75, 1
%77 = load i32, i32* %63
%78 = select i1 %76, i32 %77, i32 0
%79 = load i64, i64* %5
%80 = trunc i64 %79 to i32
%81 = call i32 (i32*, i8*, ...) @fprintf(i32* %44, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.13, i64 0, i64 0), i32 %71, i32 %80, i32 %78)
%82 = add nuw nsw i32 %71, 1
%83 = icmp eq i32 %82, 138
br i1 %83, label %84, label %70
84: ; preds = %70
%85 = icmp eq i64 %66, 4
br i1 %85, label %90, label %86
86: ; preds = %84
%87 = call i32 @fputc(i32 10, i32* %44)
%88 = add nuw nsw i64 %66, 1
%89 = icmp eq i64 %88, 6
br i1 %89, label %94, label %90
90: ; preds = %84, %86
%91 = phi i64 [ %88, %86 ], [ 5, %84 ]
%92 = getelementptr inbounds [6 x i8*], [6 x i8*]* %4, i64 0, i64 %91
%93 = load i8*, i8** %92
br label %64
94: ; preds = %86
%95 = call i32 @XCloseDisplay(i32* %35)
%96 = call i32 @fclose(i32* %44)
br label %97
97: ; preds = %94, %46, %37, %23
%98 = phi i32 [ 1, %23 ], [ 1, %46 ], [ 0, %94 ], [ 1, %37 ]
ret i32 %98
}