#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 */
struct kbentry {
  int kb_index;
  int kb_table;
  short kb_value;
};
typedef int /*<<< orphan*/ magic;
typedef int /*<<< orphan*/ keymaps;
typedef int /*<<< orphan*/ keymap;

/* Variables and functions */
int /*<<< orphan*/ KDGKBENT;
scalar_t__ KTYP(short);
scalar_t__ KT_SPEC;
int MAX_NR_KEYMAPS;
int NR_KEYS;
int O_CREAT;
int /*<<< orphan*/ O_RDWR;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *getenv(char *);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct kbentry *);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, int);
int open(char *, int, ...);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int write(int, ...);
int main(int argc, char **argv) {
  int console;
  int kmap, key;
  struct kbentry entry;
  int keymaps[MAX_NR_KEYMAPS];
  int count = 0;
  int out;
  short keymap[NR_KEYS];
  int magic = 0x8B39C07F;
  int verbose = 0;
  if (argc != 2) {
    printf("bad usage\n");
    exit(1);
  }
  if (getenv("DEBUG") != NULL)
    verbose = 1;
  memset(keymaps, 0, sizeof(keymaps));
  console = open("/dev/tty0", O_RDWR);
  if (console < 0) {
    perror("open VGA+KBD");
    exit(1);
  }
  for (kmap = 0; kmap < MAX_NR_KEYMAPS; kmap++) {
    for (key = 0; key < NR_KEYS; key++) {
      entry.kb_index = key;
      entry.kb_table = kmap;
      if (ioctl(console, KDGKBENT, &entry)) {
        perror("ioctl failed");
        exit(1);
      } else if (KTYP(entry.kb_value) != KT_SPEC) {
        keymaps[kmap] = 1;
        count++;
        break;
      }
    }
  }
  if (verbose)
    fprintf(stderr, "found %d valid keymaps\n", count);
  if (verbose)
    fprintf(stderr, "creating keymap file %s\n", argv[1]);
  if ((out = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 1) {
    perror("open keymap");
    exit(1);
  }
  if (write(out, &magic, sizeof(magic)) != sizeof(magic)) {
    perror("write magic");
    exit(1);
  }
  if (write(out, keymaps, sizeof(keymaps)) != sizeof(keymaps)) {
    perror("write header");
    exit(1);
  }
  for (kmap = 0; kmap < MAX_NR_KEYMAPS; kmap++) {
    if (!keymaps[kmap])
      continue;
    for (key = 0; key < NR_KEYS; key++) {
      entry.kb_index = key;
      entry.kb_table = kmap;
      if (ioctl(console, KDGKBENT, &entry)) {
        perror("ioctl failed");
        exit(1);
      } else {
        keymap[key] = entry.kb_value;
      }
    }
    if (write(out, keymap, sizeof(keymap)) != sizeof(keymap)) {
      perror("write keymap");
      exit(1);
    }
  }
  close(out);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x70(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x30(%rbp)
lea    0xf(,%rax,4),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    0x2dc3(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
lea    0xf(%rax,%rax,1),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x48(%rbp)
movl   $0x8b39c07f,-0x4c(%rbp)
movl   $0x0,-0x50(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     1253 <main+0xa3>
lea    0xdbc(%rip),%rdi
call   1060 <printf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xdb1(%rip),%rdi
call   1030 <getenv@plt>
cmp    $0x0,%rax
je     1270 <main+0xc0>
movl   $0x1,-0x50(%rbp)
mov    -0x68(%rbp),%rdi
mov    -0x70(%rbp),%rax
shl    $0x2,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1070 <memset@plt>
mov    0x2d4c(%rip),%rax
mov    (%rax),%esi
lea    0xd7c(%rip),%rdi
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    12bf <main+0x10f>
lea    0xd6b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2ce0(%rip),%rcx
cmp    (%rcx),%eax
jge    1393 <main+0x1e3>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2cdf(%rip),%rcx
cmp    (%rcx),%eax
jge    1380 <main+0x1d0>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2cc9(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1080 <ioctl@plt>
cmp    $0x0,%rax
je     1332 <main+0x182>
lea    0xd05(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movswl -0x20(%rbp),%edi
call   1050 <KTYP@plt>
mov    0x2c66(%rip),%rcx
cmp    (%rcx),%rax
je     1368 <main+0x1b8>
mov    -0x68(%rbp),%rax
movslq -0x18(%rbp),%rcx
movl   $0x1,(%rax,%rcx,4)
mov    -0x3c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x3c(%rbp)
jmp    1380 <main+0x1d0>
jmp    136d <main+0x1bd>
jmp    1372 <main+0x1c2>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    12df <main+0x12f>
jmp    1385 <main+0x1d5>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    12c6 <main+0x116>
cmpl   $0x0,-0x50(%rbp)
je     13b7 <main+0x207>
mov    0x2c14(%rip),%rax
mov    (%rax),%edi
mov    -0x3c(%rbp),%edx
lea    0xc85(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
cmpl   $0x0,-0x50(%rbp)
je     13e0 <main+0x230>
mov    0x2bf0(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
lea    0xc74(%rip),%rsi
mov    $0x0,%al
call   10a0 <fprintf@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2ba9(%rip),%rax
mov    (%rax),%esi
mov    0x2c00(%rip),%rax
or     (%rax),%esi
mov    0x2bef(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
mov    $0x0,%al
call   10b0 <open@plt>
mov    %eax,-0x40(%rbp)
cmp    $0x1,%eax
jge    1431 <main+0x281>
lea    0xc44(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x40(%rbp),%edi
lea    -0x4c(%rbp),%rsi
mov    $0x4,%edx
mov    $0x0,%al
call   1040 <write@plt>
cltq
cmp    $0x4,%rax
je     1466 <main+0x2b6>
lea    0xc1b(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x70(%rbp),%rdx
mov    -0x68(%rbp),%rsi
mov    -0x40(%rbp),%edi
shl    $0x2,%rdx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x70(%rbp),%rcx
cltq
shl    $0x2,%rcx
cmp    %rcx,%rax
je     14a5 <main+0x2f5>
lea    0xbe8(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    0x2afa(%rip),%rcx
cmp    (%rcx),%eax
jge    15a4 <main+0x3f4>
mov    -0x68(%rbp),%rax
movslq -0x18(%rbp),%rcx
cmpl   $0x0,(%rax,%rcx,4)
jne    14d5 <main+0x325>
jmp    1596 <main+0x3e6>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
mov    0x2ae2(%rip),%rcx
cmp    (%rcx),%eax
jge    1552 <main+0x3a2>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2acc(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1080 <ioctl@plt>
cmp    $0x0,%rax
je     152f <main+0x37f>
lea    0xb08(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x58(%rbp),%rax
mov    -0x20(%rbp),%dx
movslq -0x1c(%rbp),%rcx
mov    %dx,(%rax,%rcx,2)
jmp    1544 <main+0x394>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    14dc <main+0x32c>
mov    -0x60(%rbp),%rdx
mov    -0x58(%rbp),%rsi
mov    -0x40(%rbp),%edi
shl    $0x1,%rdx
mov    $0x0,%al
call   1040 <write@plt>
mov    -0x60(%rbp),%rcx
cltq
shl    $0x1,%rcx
cmp    %rcx,%rax
je     1591 <main+0x3e1>
lea    0xb09(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    1596 <main+0x3e6>
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    14ac <main+0x2fc>
mov    -0x40(%rbp),%edi
call   1090 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %rsp,%rcx
mov    0x2de5(%rip),%rax
mov    (%rax),%r14d
mov    %rsp,%rbx
lea    0xf(,%r14,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2dde(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rdx
mov    %rax,-0x40(%rbp)
add    %rax,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdx
mov    %rdx,-0x58(%rbp)
mov    %rdx,%rsp
movl   $0x8b39c07f,-0x4c(%rbp)
cmp    $0x2,%edi
jne    14e4 <main+0x334>
mov    %rsi,-0x78(%rbp)
mov    %rcx,-0x60(%rbp)
lea    0xdd9(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x70(%rbp)
shl    $0x2,%r14
movl   $0x0,-0x2c(%rbp)
mov    %rbx,-0x48(%rbp)
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,-0x68(%rbp)
mov    %r14d,%edx
call   1070 <memset@plt>
mov    0x2d81(%rip),%rax
mov    (%rax),%esi
lea    0xda6(%rip),%rdi
xor    %eax,%eax
call   10b0 <open@plt>
test   %eax,%eax
js     14fa <main+0x34a>
mov    %eax,%r12d
mov    0x2d37(%rip),%rax
cmpl   $0x0,(%rax)
jle    1325 <main+0x175>
xor    %ebx,%ebx
lea    -0x38(%rbp),%r14
mov    0x2d19(%rip),%r13
movl   $0x0,-0x2c(%rbp)
jmp    12c2 <main+0x112>
nopl   0x0(%rax,%rax,1)
mov    -0x48(%rbp),%rax
movl   $0x1,(%rax,%rbx,4)
addl   $0x1,-0x2c(%rbp)
add    $0x1,%rbx
mov    0x2cf6(%rip),%rax
movslq (%rax),%rax
cmp    %rax,%rbx
jge    1325 <main+0x175>
mov    0x2cff(%rip),%rax
cmpl   $0x0,(%rax)
jle    12af <main+0xff>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r15d,-0x38(%rbp)
mov    %ebx,-0x34(%rbp)
mov    0x2ce2(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r14,%rdx
call   1080 <ioctl@plt>
test   %rax,%rax
jne    14c5 <main+0x315>
movswl -0x30(%rbp),%edi
call   1060 <KTYP@plt>
cmp    0x0(%r13),%rax
jne    12a0 <main+0xf0>
add    $0x1,%r15d
mov    0x2caa(%rip),%rax
cmp    (%rax),%r15d
jl     12e0 <main+0x130>
jmp    12af <main+0xff>
cmpq   $0x0,-0x70(%rbp)
mov    -0x78(%rbp),%rbx
je     1360 <main+0x1b0>
mov    0x2c81(%rip),%r14
mov    (%r14),%edi
lea    0xce9(%rip),%rsi
mov    -0x2c(%rbp),%edx
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    (%r14),%edi
mov    0x8(%rbx),%rdx
lea    0xce9(%rip),%rsi
xor    %eax,%eax
call   10a0 <fprintf@plt>
mov    0x2c31(%rip),%rax
mov    0x2c8a(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x8(%rbx),%rdi
mov    0x2c73(%rip),%rax
or     (%rax),%esi
mov    $0x1b6,%edx
xor    %eax,%eax
call   10b0 <open@plt>
test   %eax,%eax
mov    -0x48(%rbp),%rbx
jle    1503 <main+0x353>
mov    %eax,%r14d
lea    -0x4c(%rbp),%rsi
mov    $0x4,%edx
mov    %eax,%edi
xor    %eax,%eax
call   1050 <write@plt>
cmp    $0x4,%eax
jne    150c <main+0x35c>
mov    %r14d,-0x2c(%rbp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    -0x68(%rbp),%r14
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <write@plt>
cltq
cmp    %rax,%r14
jne    1515 <main+0x365>
mov    0x2bd1(%rip),%rax
mov    (%rax),%eax
test   %eax,%eax
jle    14a8 <main+0x2f8>
mov    -0x40(%rbp),%rcx
add    %rcx,%rcx
mov    %rcx,-0x40(%rbp)
mov    0x2bd5(%rip),%r13
lea    -0x38(%rbp),%r14
xor    %r15d,%r15d
jmp    1420 <main+0x270>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r15
movslq %eax,%rcx
cmp    %rcx,%r15
jge    14a8 <main+0x2f8>
cmpl   $0x0,(%rbx,%r15,4)
je     1410 <main+0x260>
mov    0x2b9a(%rip),%rax
cmpl   $0x0,(%rax)
jle    147a <main+0x2ca>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %ebx,-0x38(%rbp)
mov    %r15d,-0x34(%rbp)
mov    0x0(%r13),%esi
mov    %r12d,%edi
mov    %r14,%rdx
call   1080 <ioctl@plt>
test   %rax,%rax
jne    14c5 <main+0x315>
movzwl -0x30(%rbp),%eax
mov    -0x58(%rbp),%rcx
mov    %ax,(%rcx,%rbx,2)
add    $0x1,%rbx
mov    0x2b56(%rip),%rax
movslq (%rax),%rax
cmp    %rax,%rbx
jl     1440 <main+0x290>
mov    -0x2c(%rbp),%edi
mov    -0x58(%rbp),%rsi
mov    -0x40(%rbp),%rbx
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <write@plt>
cltq
cmp    %rax,%rbx
jne    14db <main+0x32b>
mov    0x2b13(%rip),%rax
mov    (%rax),%eax
mov    -0x48(%rbp),%rbx
jmp    1410 <main+0x260>
mov    -0x2c(%rbp),%edi
call   1090 <close@plt>
mov    -0x60(%rbp),%rsp
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xb51(%rip),%rdi
call   10c0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xb9e(%rip),%rdi
jmp    14cc <main+0x31c>
lea    0xba2(%rip),%rdi
call   1040 <puts@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
lea    0xb0f(%rip),%rdi
jmp    14cc <main+0x31c>
lea    0xb51(%rip),%rdi
jmp    14cc <main+0x31c>
lea    0xb54(%rip),%rdi
jmp    14cc <main+0x31c>
lea    0xb57(%rip),%rdi
jmp    14cc <main+0x31c>

LLVM IR (-O0):
%struct.kbentry = type { i32, i32, i16 }
@MAX_NR_KEYMAPS = global i32 0
@NR_KEYS = global i32 0
@.str = [11 x i8] c"bad usage\0A\00"
@.str.1 = [6 x i8] c"DEBUG\00"
@.str.2 = [10 x i8] c"/dev/tty0\00"
@O_RDWR = global i32 0
@.str.3 = [13 x i8] c"open VGA+KBD\00"
@KDGKBENT = global i32 0
@.str.4 = [13 x i8] c"ioctl failed\00"
@KT_SPEC = global i64 0
@stderr = global i32 0
@.str.5 = [24 x i8] c"found %d valid keymaps\0A\00"
@.str.6 = [25 x i8] c"creating keymap file %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.7 = [12 x i8] c"open keymap\00"
@.str.8 = [12 x i8] c"write magic\00"
@.str.9 = [13 x i8] c"write header\00"
@.str.10 = [13 x i8] c"write keymap\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 %struct.kbentry
%10 = alloca i8*
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%17 = load i32, i32* @MAX_NR_KEYMAPS
%18 = zext i32 %17 to i64
%19 = call i8* @llvm.stacksave()
store i8* %19, i8** %10
%20 = alloca i32, i64 %18
store i64 %18, i64* %11
store i32 0, i32* %12
%21 = load i32, i32* @NR_KEYS
%22 = zext i32 %21 to i64
%23 = alloca i16, i64 %22
store i64 %22, i64* %14
store i32 -1959149441, i32* %15
store i32 0, i32* %16
%24 = load i32, i32* %4
%25 = icmp ne i32 %24, 2
br i1 %25, label %26, label %29
26: ; preds = %2
%27 = call i32 @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %2
%30 = call i32* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %33
32: ; preds = %29
store i32 1, i32* %16
br label %33
33: ; preds = %32, %29
%34 = mul nuw i64 4, %18
%35 = trunc i64 %34 to i32
%36 = call i32 @memset(i32* %20, i32 0, i32 %35)
%37 = load i32, i32* @O_RDWR
%38 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %37)
store i32 %38, i32* %6
%39 = load i32, i32* %6
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %44
41: ; preds = %33
%42 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%43 = call i32 @exit(i32 1)
unreachable
44: ; preds = %33
store i32 0, i32* %7
br label %45
45: ; preds = %84, %44
%46 = load i32, i32* %7
%47 = load i32, i32* @MAX_NR_KEYMAPS
%48 = icmp slt i32 %46, %47
br i1 %48, label %49, label %87
49: ; preds = %45
store i32 0, i32* %8
br label %50
50: ; preds = %80, %49
%51 = load i32, i32* %8
%52 = load i32, i32* @NR_KEYS
%53 = icmp slt i32 %51, %52
br i1 %53, label %54, label %83
54: ; preds = %50
%55 = load i32, i32* %8
%56 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %9, i32 0, i32 0
store i32 %55, i32* %56
%57 = load i32, i32* %7
%58 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %9, i32 0, i32 1
store i32 %57, i32* %58
%59 = load i32, i32* %6
%60 = load i32, i32* @KDGKBENT
%61 = call i64 @ioctl(i32 %59, i32 %60, %struct.kbentry* %9)
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %54
%64 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%65 = call i32 @exit(i32 1)
unreachable
66: ; preds = %54
%67 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %9, i32 0, i32 2
%68 = load i16, i16* %67
%69 = call i64 @KTYP(i16 signext %68)
%70 = load i64, i64* @KT_SPEC
%71 = icmp ne i64 %69, %70
br i1 %71, label %72, label %78
72: ; preds = %66
%73 = load i32, i32* %7
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i32, i32* %20, i64 %74
store i32 1, i32* %75
%76 = load i32, i32* %12
%77 = add nsw i32 %76, 1
store i32 %77, i32* %12
br label %83
78: ; preds = %66
br label %79
79: ; preds = %78
br label %80
80: ; preds = %79
%81 = load i32, i32* %8
%82 = add nsw i32 %81, 1
store i32 %82, i32* %8
br label %50
83: ; preds = %72, %50
br label %84
84: ; preds = %83
%85 = load i32, i32* %7
%86 = add nsw i32 %85, 1
store i32 %86, i32* %7
br label %45
87: ; preds = %45
%88 = load i32, i32* %16
%89 = icmp ne i32 %88, 0
br i1 %89, label %90, label %94
90: ; preds = %87
%91 = load i32, i32* @stderr
%92 = load i32, i32* %12
%93 = call i32 (i32, i8*, ...) @fprintf(i32 %91, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %92)
br label %94
94: ; preds = %90, %87
%95 = load i32, i32* %16
%96 = icmp ne i32 %95, 0
br i1 %96, label %97, label %103
97: ; preds = %94
%98 = load i32, i32* @stderr
%99 = load i8**, i8*** %5
%100 = getelementptr inbounds i8*, i8** %99, i64 1
%101 = load i8*, i8** %100
%102 = call i32 (i32, i8*, ...) @fprintf(i32 %98, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %101)
br label %103
103: ; preds = %97, %94
%104 = load i8**, i8*** %5
%105 = getelementptr inbounds i8*, i8** %104, i64 1
%106 = load i8*, i8** %105
%107 = load i32, i32* @O_WRONLY
%108 = load i32, i32* @O_CREAT
%109 = or i32 %107, %108
%110 = load i32, i32* @O_TRUNC
%111 = or i32 %109, %110
%112 = call i32 (i8*, i32, ...) @open(i8* %106, i32 %111, i32 438)
store i32 %112, i32* %13
%113 = icmp slt i32 %112, 1
br i1 %113, label %114, label %117
114: ; preds = %103
%115 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%116 = call i32 @exit(i32 1)
unreachable
117: ; preds = %103
%118 = load i32, i32* %13
%119 = call i32 (i32, ...) @write(i32 %118, i32* %15, i64 4)
%120 = sext i32 %119 to i64
%121 = icmp ne i64 %120, 4
br i1 %121, label %122, label %125
122: ; preds = %117
%123 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
%124 = call i32 @exit(i32 1)
unreachable
125: ; preds = %117
%126 = load i32, i32* %13
%127 = mul nuw i64 4, %18
%128 = call i32 (i32, ...) @write(i32 %126, i32* %20, i64 %127)
%129 = sext i32 %128 to i64
%130 = mul nuw i64 4, %18
%131 = icmp ne i64 %129, %130
br i1 %131, label %132, label %135
132: ; preds = %125
%133 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
%134 = call i32 @exit(i32 1)
unreachable
135: ; preds = %125
store i32 0, i32* %7
br label %136
136: ; preds = %185, %135
%137 = load i32, i32* %7
%138 = load i32, i32* @MAX_NR_KEYMAPS
%139 = icmp slt i32 %137, %138
br i1 %139, label %140, label %188
140: ; preds = %136
%141 = load i32, i32* %7
%142 = sext i32 %141 to i64
%143 = getelementptr inbounds i32, i32* %20, i64 %142
%144 = load i32, i32* %143
%145 = icmp ne i32 %144, 0
br i1 %145, label %147, label %146
146: ; preds = %140
br label %185
147: ; preds = %140
store i32 0, i32* %8
br label %148
148: ; preds = %171, %147
%149 = load i32, i32* %8
%150 = load i32, i32* @NR_KEYS
%151 = icmp slt i32 %149, %150
br i1 %151, label %152, label %174
152: ; preds = %148
%153 = load i32, i32* %8
%154 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %9, i32 0, i32 0
store i32 %153, i32* %154
%155 = load i32, i32* %7
%156 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %9, i32 0, i32 1
store i32 %155, i32* %156
%157 = load i32, i32* %6
%158 = load i32, i32* @KDGKBENT
%159 = call i64 @ioctl(i32 %157, i32 %158, %struct.kbentry* %9)
%160 = icmp ne i64 %159, 0
br i1 %160, label %161, label %164
161: ; preds = %152
%162 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%163 = call i32 @exit(i32 1)
unreachable
164: ; preds = %152
%165 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %9, i32 0, i32 2
%166 = load i16, i16* %165
%167 = load i32, i32* %8
%168 = sext i32 %167 to i64
%169 = getelementptr inbounds i16, i16* %23, i64 %168
store i16 %166, i16* %169
br label %170
170: ; preds = %164
br label %171
171: ; preds = %170
%172 = load i32, i32* %8
%173 = add nsw i32 %172, 1
store i32 %173, i32* %8
br label %148
174: ; preds = %148
%175 = load i32, i32* %13
%176 = mul nuw i64 2, %22
%177 = call i32 (i32, ...) @write(i32 %175, i16* %23, i64 %176)
%178 = sext i32 %177 to i64
%179 = mul nuw i64 2, %22
%180 = icmp ne i64 %178, %179
br i1 %180, label %181, label %184
181: ; preds = %174
%182 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0))
%183 = call i32 @exit(i32 1)
unreachable
184: ; preds = %174
br label %185
185: ; preds = %184, %146
%186 = load i32, i32* %7
%187 = add nsw i32 %186, 1
store i32 %187, i32* %7
br label %136
188: ; preds = %136
%189 = load i32, i32* %13
%190 = call i32 @close(i32 %189)
store i32 0, i32* %3
%191 = load i8*, i8** %10
%192 = load i32, i32* %3
ret i32 %192
}

LLVM IR (-O2):
%struct.kbentry = type { i32, i32, i16 }
@MAX_NR_KEYMAPS = global i32 0
@NR_KEYS = global i32 0
@.str.1 = [6 x i8] c"DEBUG\00"
@.str.2 = [10 x i8] c"/dev/tty0\00"
@O_RDWR = global i32 0
@.str.3 = [13 x i8] c"open VGA+KBD\00"
@KDGKBENT = global i32 0
@.str.4 = [13 x i8] c"ioctl failed\00"
@KT_SPEC = global i64 0
@stderr = global i32 0
@.str.5 = [24 x i8] c"found %d valid keymaps\0A\00"
@.str.6 = [25 x i8] c"creating keymap file %s\0A\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.7 = [12 x i8] c"open keymap\00"
@.str.8 = [12 x i8] c"write magic\00"
@.str.9 = [13 x i8] c"write header\00"
@.str.10 = [13 x i8] c"write keymap\00"
@str = [10 x i8] c"bad usage\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.kbentry
%4 = alloca i32
%5 = bitcast %struct.kbentry* %3 to i8*
%6 = load i32, i32* @MAX_NR_KEYMAPS
%7 = zext i32 %6 to i64
%8 = tail call i8* @llvm.stacksave()
%9 = alloca i32, i64 %7
%10 = load i32, i32* @NR_KEYS
%11 = zext i32 %10 to i64
%12 = alloca i16, i64 %11
%13 = bitcast i32* %4 to i8*
store i32 -1959149441, i32* %4
%14 = icmp eq i32 %0, 2
br i1 %14, label %18, label %15
15: ; preds = %2
%16 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %2
%19 = tail call i32* @getenv(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%20 = icmp eq i32* %19, null
%21 = shl nuw nsw i64 %7, 2
%22 = trunc i64 %21 to i32
%23 = call i32 @memset(i32* %9, i32 0, i32 %22)
%24 = load i32, i32* @O_RDWR
%25 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32 %24)
%26 = icmp slt i32 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %18
%28 = load i32, i32* @MAX_NR_KEYMAPS
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %70
30: ; preds = %27
%31 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %3, i64 0, i32 0
%32 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %3, i64 0, i32 1
%33 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %3, i64 0, i32 2
br label %37
34: ; preds = %18
%35 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
%36 = call i32 @exit(i32 1)
unreachable
37: ; preds = %30, %64
%38 = phi i64 [ 0, %30 ], [ %66, %64 ]
%39 = phi i32 [ 0, %30 ], [ %65, %64 ]
%40 = load i32, i32* @NR_KEYS
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %64
42: ; preds = %37
%43 = trunc i64 %38 to i32
br label %47
44: ; preds = %55
%45 = load i32, i32* @NR_KEYS
%46 = icmp slt i32 %60, %45
br i1 %46, label %47, label %64
47: ; preds = %42, %44
%48 = phi i32 [ %60, %44 ], [ 0, %42 ]
store i32 %48, i32* %31
store i32 %43, i32* %32
%49 = load i32, i32* @KDGKBENT
%50 = call i64 @ioctl(i32 %25, i32 %49, %struct.kbentry* %3)
%51 = icmp eq i64 %50, 0
br i1 %51, label %55, label %52
52: ; preds = %47
%53 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%54 = call i32 @exit(i32 1)
unreachable
55: ; preds = %47
%56 = load i16, i16* %33
%57 = call i64 @KTYP(i16 signext %56)
%58 = load i64, i64* @KT_SPEC
%59 = icmp eq i64 %57, %58
%60 = add nuw nsw i32 %48, 1
br i1 %59, label %44, label %61
61: ; preds = %55
%62 = getelementptr inbounds i32, i32* %9, i64 %38
store i32 1, i32* %62
%63 = add nsw i32 %39, 1
br label %64
64: ; preds = %44, %37, %61
%65 = phi i32 [ %63, %61 ], [ %39, %37 ], [ %39, %44 ]
%66 = add nuw nsw i64 %38, 1
%67 = load i32, i32* @MAX_NR_KEYMAPS
%68 = sext i32 %67 to i64
%69 = icmp slt i64 %66, %68
br i1 %69, label %37, label %70
70: ; preds = %64, %27
%71 = phi i32 [ 0, %27 ], [ %65, %64 ]
br i1 %20, label %79, label %72
72: ; preds = %70
%73 = load i32, i32* @stderr
%74 = call i32 (i32, i8*, ...) @fprintf(i32 %73, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.5, i64 0, i64 0), i32 %71)
%75 = load i32, i32* @stderr
%76 = getelementptr inbounds i8*, i8** %1, i64 1
%77 = load i8*, i8** %76
%78 = call i32 (i32, i8*, ...) @fprintf(i32 %75, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.6, i64 0, i64 0), i8* %77)
br label %79
79: ; preds = %70, %72
%80 = getelementptr inbounds i8*, i8** %1, i64 1
%81 = load i8*, i8** %80
%82 = load i32, i32* @O_WRONLY
%83 = load i32, i32* @O_CREAT
%84 = or i32 %83, %82
%85 = load i32, i32* @O_TRUNC
%86 = or i32 %84, %85
%87 = call i32 (i8*, i32, ...) @open(i8* %81, i32 %86, i32 438)
%88 = icmp slt i32 %87, 1
br i1 %88, label %89, label %92
89: ; preds = %79
%90 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0))
%91 = call i32 @exit(i32 1)
unreachable
92: ; preds = %79
%93 = call i32 (i32, ...) @write(i32 %87, i32* %4, i64 4)
%94 = icmp eq i32 %93, 4
br i1 %94, label %98, label %95
95: ; preds = %92
%96 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.8, i64 0, i64 0))
%97 = call i32 @exit(i32 1)
unreachable
98: ; preds = %92
%99 = call i32 (i32, ...) @write(i32 %87, i32* %9, i64 %21)
%100 = sext i32 %99 to i64
%101 = icmp eq i64 %21, %100
br i1 %101, label %102, label %110
102: ; preds = %98
%103 = load i32, i32* @MAX_NR_KEYMAPS
%104 = icmp sgt i32 %103, 0
br i1 %104, label %105, label %154
105: ; preds = %102
%106 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %3, i64 0, i32 0
%107 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %3, i64 0, i32 1
%108 = getelementptr inbounds %struct.kbentry, %struct.kbentry* %3, i64 0, i32 2
%109 = shl nuw nsw i64 %11, 1
br label %113
110: ; preds = %98
%111 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.9, i64 0, i64 0))
%112 = call i32 @exit(i32 1)
unreachable
113: ; preds = %105, %149
%114 = phi i32 [ %103, %105 ], [ %150, %149 ]
%115 = phi i64 [ 0, %105 ], [ %151, %149 ]
%116 = getelementptr inbounds i32, i32* %9, i64 %115
%117 = load i32, i32* %116
%118 = icmp eq i32 %117, 0
br i1 %118, label %149, label %119
119: ; preds = %113
%120 = load i32, i32* @NR_KEYS
%121 = icmp sgt i32 %120, 0
br i1 %121, label %122, label %140
122: ; preds = %119
%123 = trunc i64 %115 to i32
br label %124
124: ; preds = %122, %133
%125 = phi i64 [ 0, %122 ], [ %136, %133 ]
%126 = trunc i64 %125 to i32
store i32 %126, i32* %106
store i32 %123, i32* %107
%127 = load i32, i32* @KDGKBENT
%128 = call i64 @ioctl(i32 %25, i32 %127, %struct.kbentry* %3)
%129 = icmp eq i64 %128, 0
br i1 %129, label %133, label %130
130: ; preds = %124
%131 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0))
%132 = call i32 @exit(i32 1)
unreachable
133: ; preds = %124
%134 = load i16, i16* %108
%135 = getelementptr inbounds i16, i16* %12, i64 %125
store i16 %134, i16* %135
%136 = add nuw nsw i64 %125, 1
%137 = load i32, i32* @NR_KEYS
%138 = sext i32 %137 to i64
%139 = icmp slt i64 %136, %138
br i1 %139, label %124, label %140
140: ; preds = %133, %119
%141 = call i32 (i32, ...) @write(i32 %87, i16* %12, i64 %109)
%142 = sext i32 %141 to i64
%143 = icmp eq i64 %109, %142
br i1 %143, label %144, label %146
144: ; preds = %140
%145 = load i32, i32* @MAX_NR_KEYMAPS
br label %149
146: ; preds = %140
%147 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.10, i64 0, i64 0))
%148 = call i32 @exit(i32 1)
unreachable
149: ; preds = %144, %113
%150 = phi i32 [ %145, %144 ], [ %114, %113 ]
%151 = add nuw nsw i64 %115, 1
%152 = sext i32 %150 to i64
%153 = icmp slt i64 %151, %152
br i1 %153, label %113, label %154
154: ; preds = %149, %102
%155 = call i32 @close(i32 %87)
ret i32 0
}