#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 /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TIOCLINUX;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, char *);
int open(char *, int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  char shift_state;
  int fd = open("/dev/console", O_RDWR);
  if (!fd)
    return 127;
  shift_state = 6;
  ioctl(fd, TIOCLINUX, &shift_state);
  close(fd);
  return shift_state;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%esi
lea    0xeaa(%rip),%rdi
call   1050 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    1174 <main+0x44>
movl   $0x7f,-0x4(%rbp)
jmp    119c <main+0x6c>
movb   $0x6,-0x11(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2e56(%rip),%rax
mov    (%rax),%esi
lea    -0x11(%rbp),%rdx
call   1030 <ioctl@plt>
mov    -0x18(%rbp),%edi
call   1040 <close@plt>
movsbl -0x11(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x10,%rsp
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
lea    0xebb(%rip),%rdi
call   1050 <open@plt>
test   %eax,%eax
je     1178 <main+0x48>
mov    %eax,%ebx
movb   $0x6,0xf(%rsp)
mov    0x2e7c(%rip),%rax
mov    (%rax),%esi
lea    0xf(%rsp),%rdx
mov    %ebx,%edi
call   1030 <ioctl@plt>
mov    %ebx,%edi
call   1040 <close@plt>
movsbl 0xf(%rsp),%eax
jmp    117d <main+0x4d>
mov    $0x7f,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@TIOCLINUX = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i8
%7 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%8 = load i32, i32* @O_RDWR
%9 = call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %8)
store i32 %9, i32* %7
%10 = load i32, i32* %7
%11 = icmp ne i32 %10, 0
br i1 %11, label %13, label %12
12: ; preds = %2
store i32 127, i32* %3
br label %21
13: ; preds = %2
store i8 6, i8* %6
%14 = load i32, i32* %7
%15 = load i32, i32* @TIOCLINUX
%16 = call i32 @ioctl(i32 %14, i32 %15, i8* %6)
%17 = load i32, i32* %7
%18 = call i32 @close(i32 %17)
%19 = load i8, i8* %6
%20 = sext i8 %19 to i32
store i32 %20, i32* %3
br label %21
21: ; preds = %13, %12
%22 = load i32, i32* %3
ret i32 %22
}

LLVM IR (-O2):
@.str = [13 x i8] c"/dev/console\00"
@O_RDWR = global i32 0
@TIOCLINUX = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i8
%4 = load i32, i32* @O_RDWR
%5 = tail call i32 @open(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %2
store i8 6, i8* %3
%8 = load i32, i32* @TIOCLINUX
%9 = call i32 @ioctl(i32 %5, i32 %8, i8* %3)
%10 = call i32 @close(i32 %5)
%11 = load i8, i8* %3
%12 = sext i8 %11 to i32
br label %13
13: ; preds = %2, %7
%14 = phi i32 [ %12, %7 ], [ 127, %2 ]
ret i32 %14
}