#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 ulong;

/* Variables and functions */
int /*<<< orphan*/ KDGETLED;
int /*<<< orphan*/ KDGKBLED;
int /*<<< orphan*/ KDSETLED;
int LED_NUM;
int LED_SCR;
scalar_t__ ioctl(int, int /*<<< orphan*/, ...);
int /*<<< orphan*/ is_on_X(int);
ulong detach_vt_leds(int tty, int wantDetach) {
  ulong ledVal;
  if (ioctl(tty, KDGETLED, &ledVal)) {
    return 0;
  }
  ledVal &= ~LED_SCR;
  ledVal &= ~LED_NUM;
  if (!wantDetach && !is_on_X(tty)) {
    ioctl(tty, KDGKBLED, &ledVal);
    ledVal |= 0x08;
  }
  ioctl(tty, KDSETLED, (char)ledVal);
  return ledVal;
}


x86 assembly (clang, -O0):
<detach_vt_leds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1030 <ioctl@plt>
cmp    $0x0,%rax
je     115b <detach_vt_leds+0x3b>
movl   $0x0,-0x4(%rbp)
jmp    11d9 <detach_vt_leds+0xb9>
mov    0x2e5e(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x10(%rbp),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0xc(%rbp)
jne    11ba <detach_vt_leds+0x9a>
mov    -0x8(%rbp),%edi
call   1040 <is_on_X@plt>
cmp    $0x0,%eax
jne    11ba <detach_vt_leds+0x9a>
mov    -0x8(%rbp),%edi
mov    0x2e54(%rip),%rax
mov    (%rax),%esi
lea    -0x10(%rbp),%rdx
mov    $0x0,%al
call   1030 <ioctl@plt>
mov    -0x10(%rbp),%eax
or     $0x8,%eax
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
mov    -0x10(%rbp),%eax
movsbl %al,%edx
mov    $0x0,%al
call   1030 <ioctl@plt>
mov    -0x10(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<detach_vt_leds>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %esi,%r14d
mov    %edi,%ebx
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
xor    %ebp,%ebp
lea    0xc(%rsp),%rdx
xor    %eax,%eax
call   1030 <ioctl@plt>
test   %rax,%rax
jne    11b6 <detach_vt_leds+0x96>
mov    0x2e70(%rip),%rax
mov    (%rax),%eax
mov    0x2e87(%rip),%rcx
or     (%rcx),%eax
not    %eax
and    0xc(%rsp),%eax
mov    %eax,0xc(%rsp)
test   %r14d,%r14d
jne    119d <detach_vt_leds+0x7d>
mov    %ebx,%edi
call   1040 <is_on_X@plt>
test   %eax,%eax
je     117b <detach_vt_leds+0x5b>
mov    0xc(%rsp),%eax
jmp    119d <detach_vt_leds+0x7d>
mov    0x2e76(%rip),%rax
mov    (%rax),%esi
lea    0xc(%rsp),%rdx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <ioctl@plt>
mov    0xc(%rsp),%eax
or     $0x8,%eax
mov    %eax,0xc(%rsp)
mov    0x2e24(%rip),%rcx
mov    (%rcx),%esi
movsbl %al,%edx
mov    %ebx,%edi
xor    %eax,%eax
call   1030 <ioctl@plt>
mov    0xc(%rsp),%ebp
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@KDGETLED = global i32 0
@LED_SCR = global i32 0
@LED_NUM = global i32 0
@KDGKBLED = global i32 0
@KDSETLED = global i32 0
define i32 @detach_vt_leds(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %4
store i32 %1, i32* %5
%7 = load i32, i32* %4
%8 = load i32, i32* @KDGETLED
%9 = call i64 (i32, i32, ...) @ioctl(i32 %7, i32 %8, i32* %6)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %12
11: ; preds = %2
store i32 0, i32* %3
br label %41
12: ; preds = %2
%13 = load i32, i32* @LED_SCR
%14 = xor i32 %13, -1
%15 = load i32, i32* %6
%16 = and i32 %15, %14
store i32 %16, i32* %6
%17 = load i32, i32* @LED_NUM
%18 = xor i32 %17, -1
%19 = load i32, i32* %6
%20 = and i32 %19, %18
store i32 %20, i32* %6
%21 = load i32, i32* %5
%22 = icmp ne i32 %21, 0
br i1 %22, label %33, label %23
23: ; preds = %12
%24 = load i32, i32* %4
%25 = call i32 @is_on_X(i32 %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %33, label %27
27: ; preds = %23
%28 = load i32, i32* %4
%29 = load i32, i32* @KDGKBLED
%30 = call i64 (i32, i32, ...) @ioctl(i32 %28, i32 %29, i32* %6)
%31 = load i32, i32* %6
%32 = or i32 %31, 8
store i32 %32, i32* %6
br label %33
33: ; preds = %27, %23, %12
%34 = load i32, i32* %4
%35 = load i32, i32* @KDSETLED
%36 = load i32, i32* %6
%37 = trunc i32 %36 to i8
%38 = sext i8 %37 to i32
%39 = call i64 (i32, i32, ...) @ioctl(i32 %34, i32 %35, i32 %38)
%40 = load i32, i32* %6
store i32 %40, i32* %3
br label %41
41: ; preds = %33, %11
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
@KDGETLED = global i32 0
@LED_SCR = global i32 0
@LED_NUM = global i32 0
@KDGKBLED = global i32 0
@KDSETLED = global i32 0
define i32 @detach_vt_leds(i32 %0, i32 %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = load i32, i32* @KDGETLED
%6 = call i64 (i32, i32, ...) @ioctl(i32 %0, i32 %5, i32* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %33
8: ; preds = %2
%9 = load i32, i32* @LED_SCR
%10 = load i32, i32* %3
%11 = load i32, i32* @LED_NUM
%12 = or i32 %9, %11
%13 = xor i32 %12, -1
%14 = and i32 %10, %13
store i32 %14, i32* %3
%15 = icmp eq i32 %1, 0
br i1 %15, label %16, label %26
16: ; preds = %8
%17 = call i32 @is_on_X(i32 %0)
%18 = icmp eq i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = load i32, i32* %3
br label %26
21: ; preds = %16
%22 = load i32, i32* @KDGKBLED
%23 = call i64 (i32, i32, ...) @ioctl(i32 %0, i32 %22, i32* %3)
%24 = load i32, i32* %3
%25 = or i32 %24, 8
store i32 %25, i32* %3
br label %26
26: ; preds = %19, %21, %8
%27 = phi i32 [ %20, %19 ], [ %25, %21 ], [ %14, %8 ]
%28 = load i32, i32* @KDSETLED
%29 = shl i32 %27, 24
%30 = ashr exact i32 %29, 24
%31 = call i64 (i32, i32, ...) @ioctl(i32 %0, i32 %28, i32 %30)
%32 = load i32, i32* %3
br label %33
33: ; preds = %2, %26
%34 = phi i32 [ %32, %26 ], [ 0, %2 ]
ret i32 %34
}