#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 /*<<< orphan*/ int32_t;
typedef int /*<<< orphan*/ XEvent;
typedef int /*<<< orphan*/ Display;

/* Variables and functions */
int /*<<< orphan*/ AlreadyGrabbed;
int /*<<< orphan*/ DefaultRootWindow(int /*<<< orphan*/ *);
int /*<<< orphan*/ FocusChangeMask;
int /*<<< orphan*/ XMaskEvent(int /*<<< orphan*/ *, int /*<<< orphan*/,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ XSelectInput(int /*<<< orphan*/ *, int /*<<< orphan*/,
                                int /*<<< orphan*/);
int /*<<< orphan*/ grab_root_window_keyboard(int /*<<< orphan*/ *);
int32_t lock_keyboard(Display *display) {
  int32_t grab_result;
  if ((grab_result = grab_root_window_keyboard(display)) == AlreadyGrabbed) {
    XSelectInput(display, DefaultRootWindow(display), FocusChangeMask);
    XEvent focus_change_event;
    XMaskEvent(display, FocusChangeMask, &focus_change_event);
    return grab_root_window_keyboard(display);
  }
  return grab_result;
}


x86 assembly (clang, -O0):
<lock_keyboard>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <grab_root_window_keyboard@plt>
mov    %eax,-0x14(%rbp)
mov    0x2e79(%rip),%rcx
cmp    (%rcx),%eax
jne    11b3 <lock_keyboard+0x73>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <DefaultRootWindow@plt>
mov    -0x20(%rbp),%rdi
mov    %eax,%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
call   1040 <XSelectInput@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
lea    -0x18(%rbp),%rdx
call   1060 <XMaskEvent@plt>
mov    -0x10(%rbp),%rdi
call   1050 <grab_root_window_keyboard@plt>
mov    %eax,-0x4(%rbp)
jmp    11b9 <lock_keyboard+0x79>
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<lock_keyboard>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <grab_root_window_keyboard@plt>
mov    0x2e85(%rip),%rcx
cmp    (%rcx),%eax
jne    118b <lock_keyboard+0x4b>
mov    %rbx,%rdi
call   1030 <DefaultRootWindow@plt>
mov    0x2e82(%rip),%r14
mov    (%r14),%edx
mov    %rbx,%rdi
mov    %eax,%esi
call   1040 <XSelectInput@plt>
mov    (%r14),%esi
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
call   1060 <XMaskEvent@plt>
mov    %rbx,%rdi
call   1050 <grab_root_window_keyboard@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@AlreadyGrabbed = global i32 0
@FocusChangeMask = global i32 0
define i32 @lock_keyboard(i32* %0) {
%2 = alloca i32
%3 = alloca i32*
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i32 @grab_root_window_keyboard(i32* %6)
store i32 %7, i32* %4
%8 = load i32, i32* @AlreadyGrabbed
%9 = icmp eq i32 %7, %8
br i1 %9, label %10, label %21
10: ; preds = %1
%11 = load i32*, i32** %3
%12 = load i32*, i32** %3
%13 = call i32 @DefaultRootWindow(i32* %12)
%14 = load i32, i32* @FocusChangeMask
%15 = call i32 @XSelectInput(i32* %11, i32 %13, i32 %14)
%16 = load i32*, i32** %3
%17 = load i32, i32* @FocusChangeMask
%18 = call i32 @XMaskEvent(i32* %16, i32 %17, i32* %5)
%19 = load i32*, i32** %3
%20 = call i32 @grab_root_window_keyboard(i32* %19)
store i32 %20, i32* %2
br label %23
21: ; preds = %1
%22 = load i32, i32* %4
store i32 %22, i32* %2
br label %23
23: ; preds = %21, %10
%24 = load i32, i32* %2
ret i32 %24
}

LLVM IR (-O2):
@AlreadyGrabbed = global i32 0
@FocusChangeMask = global i32 0
define i32 @lock_keyboard(i32* %0) {
%2 = alloca i32
%3 = tail call i32 @grab_root_window_keyboard(i32* %0)
%4 = load i32, i32* @AlreadyGrabbed
%5 = icmp eq i32 %3, %4
br i1 %5, label %6, label %14
6: ; preds = %1
%7 = tail call i32 @DefaultRootWindow(i32* %0)
%8 = load i32, i32* @FocusChangeMask
%9 = tail call i32 @XSelectInput(i32* %0, i32 %7, i32 %8)
%10 = bitcast i32* %2 to i8*
%11 = load i32, i32* @FocusChangeMask
%12 = call i32 @XMaskEvent(i32* %0, i32 %11, i32* %2)
%13 = call i32 @grab_root_window_keyboard(i32* %0)
br label %14
14: ; preds = %1, %6
%15 = phi i32 [ %13, %6 ], [ %3, %1 ]
ret i32 %15
}