#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 */

/* Variables and functions */
int O_NDELAY;
int O_RDWR;
int /*<<< orphan*/ _IO(int, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int strtoull(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  if (!argv[1]) {
    printf("failed! \n");
    return -1;
  }
  int idx = strtoull(argv[1], NULL, 0);
  printf("index %d\n", idx);
  int fd = open("/dev/dpram0", O_RDWR | O_NDELAY);
  if (fd < 0) {
    printf("Failed to open dpram tty\n");
    return -1;
  }
  switch (idx) {
  case 0:
    ioctl(fd, _IO(('o'), 0xd0));
    break;
  case 1:
    ioctl(fd, _IO((0xf0), 0xc1));
    break;
  case 2:
    ioctl(fd, _IO((0xf0), 0xc0));
    break;
  case 3:
    ioctl(fd, _IO((0xf0), 0xc5));
    break;
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
jne    119f <main+0x3f>
lea    0xe84(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <main+0x16a>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1070 <strtoull@plt>
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%esi
lea    0xe5b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    0x2e1b(%rip),%rax
mov    (%rax),%esi
mov    0x2e02(%rip),%rax
or     (%rax),%esi
lea    0xe45(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    120b <main+0xab>
lea    0xe38(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12ca <main+0x16a>
mov    -0x14(%rbp),%eax
mov    %rax,-0x20(%rbp)
sub    $0x3,%rax
ja     12bb <main+0x15b>
mov    -0x20(%rbp),%rax
lea    0xdd9(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    $0x6f,%edi
mov    $0xd0,%esi
call   1040 <_IO@plt>
mov    -0x24(%rbp),%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
jmp    12bb <main+0x15b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    $0xf0,%edi
mov    $0xc1,%esi
call   1040 <_IO@plt>
mov    -0x28(%rbp),%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
jmp    12bb <main+0x15b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    $0xf0,%edi
mov    $0xc0,%esi
call   1040 <_IO@plt>
mov    -0x2c(%rbp),%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
jmp    12bb <main+0x15b>
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    $0xf0,%edi
mov    $0xc5,%esi
call   1040 <_IO@plt>
mov    -0x30(%rbp),%edi
mov    %eax,%esi
call   1050 <ioctl@plt>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
push   %rax
mov    0x8(%rsi),%rdi
test   %rdi,%rdi
je     11de <main+0x6e>
xor    %esi,%esi
xor    %edx,%edx
call   1080 <strtoull@plt>
mov    %eax,%ebp
lea    0xe82(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    0x2e4a(%rip),%rax
mov    0x2e33(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
lea    0xe6a(%rip),%rdi
call   1090 <open@plt>
test   %eax,%eax
js     11e7 <main+0x77>
mov    %eax,%ebx
cmp    $0x3,%ebp
ja     122a <main+0xba>
mov    %ebp,%eax
lea    0xe37(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
mov    $0x6f,%edi
mov    $0xd0,%esi
jmp    121c <main+0xac>
lea    0xe41(%rip),%rdi
jmp    11ee <main+0x7e>
lea    0xe41(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
jmp    1233 <main+0xc3>
mov    $0xf0,%edi
mov    $0xc1,%esi
jmp    121c <main+0xac>
mov    $0xf0,%edi
mov    $0xc0,%esi
jmp    121c <main+0xac>
mov    $0xf0,%edi
mov    $0xc5,%esi
call   1050 <_IO@plt>
mov    %ebx,%edi
mov    %eax,%esi
call   1060 <ioctl@plt>
mov    %ebx,%edi
call   1070 <close@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"failed! \0A\00"
@.str.1 = [10 x i8] c"index %d\0A\00"
@.str.2 = [12 x i8] c"/dev/dpram0\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@.str.3 = [26 x i8] c"Failed to open dpram tty\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i8**, i8*** %5
%9 = getelementptr inbounds i8*, i8** %8, i64 1
%10 = load i8*, i8** %9
%11 = icmp ne i8* %10, null
br i1 %11, label %14, label %12
12: ; preds = %2
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
14: ; preds = %2
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 1
%17 = load i8*, i8** %16
%18 = call i32 @strtoull(i8* %17, i32* null, i32 0)
store i32 %18, i32* %6
%19 = load i32, i32* %6
%20 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %19)
%21 = load i32, i32* @O_RDWR
%22 = load i32, i32* @O_NDELAY
%23 = or i32 %21, %22
%24 = call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %14
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %50
29: ; preds = %14
%30 = load i32, i32* %6
switch i32 %30, label %47 [
i32 0, label %31
i32 1, label %35
i32 2, label %39
i32 3, label %43
]
31: ; preds = %29
%32 = load i32, i32* %7
%33 = call i32 @_IO(i32 111, i32 208)
%34 = call i32 @ioctl(i32 %32, i32 %33)
br label %47
35: ; preds = %29
%36 = load i32, i32* %7
%37 = call i32 @_IO(i32 240, i32 193)
%38 = call i32 @ioctl(i32 %36, i32 %37)
br label %47
39: ; preds = %29
%40 = load i32, i32* %7
%41 = call i32 @_IO(i32 240, i32 192)
%42 = call i32 @ioctl(i32 %40, i32 %41)
br label %47
43: ; preds = %29
%44 = load i32, i32* %7
%45 = call i32 @_IO(i32 240, i32 197)
%46 = call i32 @ioctl(i32 %44, i32 %45)
br label %47
47: ; preds = %29, %43, %39, %35, %31
%48 = load i32, i32* %7
%49 = call i32 @close(i32 %48)
store i32 0, i32* %3
br label %50
50: ; preds = %47, %27, %12
%51 = load i32, i32* %3
ret i32 %51
}

LLVM IR (-O2):
@.str.1 = [10 x i8] c"index %d\0A\00"
@.str.2 = [12 x i8] c"/dev/dpram0\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@str = [9 x i8] c"failed! \00"
@str.4 = [25 x i8] c"Failed to open dpram tty\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = getelementptr inbounds i8*, i8** %1, i64 1
%4 = load i8*, i8** %3
%5 = icmp eq i8* %4, null
br i1 %5, label %6, label %8
6: ; preds = %2
%7 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @str, i64 0, i64 0))
br label %32
8: ; preds = %2
%9 = tail call i32 @strtoull(i8* nocapture %4, i32* null, i32 0)
%10 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @O_RDWR
%12 = load i32, i32* @O_NDELAY
%13 = or i32 %12, %11
%14 = tail call i32 @open(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %8
%17 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str.4, i64 0, i64 0))
br label %32
18: ; preds = %8
switch i32 %9, label %30 [
i32 0, label %19
i32 1, label %21
i32 2, label %23
i32 3, label %25
]
19: ; preds = %18
%20 = tail call i32 @_IO(i32 111, i32 208)
br label %27
21: ; preds = %18
%22 = tail call i32 @_IO(i32 240, i32 193)
br label %27
23: ; preds = %18
%24 = tail call i32 @_IO(i32 240, i32 192)
br label %27
25: ; preds = %18
%26 = tail call i32 @_IO(i32 240, i32 197)
br label %27
27: ; preds = %19, %21, %23, %25
%28 = phi i32 [ %26, %25 ], [ %24, %23 ], [ %22, %21 ], [ %20, %19 ]
%29 = tail call i32 @ioctl(i32 %14, i32 %28)
br label %30
30: ; preds = %27, %18
%31 = tail call i32 @close(i32 %14)
br label %32
32: ; preds = %16, %30, %6
%33 = phi i32 [ -1, %6 ], [ -1, %16 ], [ 0, %30 ]
ret i32 %33
}