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

/* Variables and functions */
int /*<<< orphan*/ BTN_MISC;
int JSIOCGBTNMAP;
int _IOR(char, int, int /*<<< orphan*/);
int /*<<< orphan*/ *__u16;
int determine_ioctl(int, int *, int *, int /*<<< orphan*/ *);
int ioctl(int, int, int /*<<< orphan*/ *);
int getbtnmap(int fd, uint16_t *btnmap) {
  static int jsiocgbtnmap = 0;
  int ioctls[] = {JSIOCGBTNMAP, _IOR('j', 0x34, __u16[0x2FF - BTN_MISC + 1]),
                  _IOR('j', 0x34, __u16[0x1FF - BTN_MISC + 1]), 0};
  if (jsiocgbtnmap != 0) {
    return ioctl(fd, jsiocgbtnmap, btnmap);
  } else {
    return determine_ioctl(fd, ioctls, &jsiocgbtnmap, btnmap);
  }
}


x86 assembly (clang, -O0):
<getbtnmap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e8e(%rip),%rax
mov    (%rax),%rax
mov    $0x2ff,%ecx
mov    0x2e6f(%rip),%rdx
sub    (%rdx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    $0x6a,%edi
mov    $0x34,%esi
call   1040 <_IOR@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e5b(%rip),%rax
mov    (%rax),%rax
mov    $0x1ff,%ecx
mov    0x2e3c(%rip),%rdx
sub    (%rdx),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    $0x6a,%edi
mov    $0x34,%esi
call   1040 <_IOR@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,0x2e81(%rip)
je     11df <getbtnmap+0xaf>
mov    -0x8(%rbp),%edi
mov    0x2e72(%rip),%esi
mov    -0x10(%rbp),%rdx
call   1030 <ioctl@plt>
mov    %eax,-0x4(%rbp)
jmp    11f9 <getbtnmap+0xc9>
mov    -0x8(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rcx
lea    0x2e4f(%rip),%rdx
call   1050 <determine_ioctl@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getbtnmap>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r14
mov    %edi,%r15d
mov    0x2ea1(%rip),%rax
mov    (%rax),%eax
mov    %eax,(%rsp)
mov    0x2e8d(%rip),%rbx
mov    (%rbx),%rax
mov    0x2e73(%rip),%rbp
mov    $0x300,%ecx
sub    0x0(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    $0x6a,%edi
mov    $0x34,%esi
call   1040 <_IOR@plt>
mov    %eax,0x4(%rsp)
mov    (%rbx),%rax
mov    $0x200,%ecx
sub    0x0(%rbp),%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,4),%edx
mov    $0x6a,%edi
mov    $0x34,%esi
call   1040 <_IOR@plt>
mov    %eax,0x8(%rsp)
movl   $0x0,0xc(%rsp)
mov    0x2e90(%rip),%esi
test   %esi,%esi
je     11c9 <getbtnmap+0x99>
mov    %r15d,%edi
mov    %r14,%rdx
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <ioctl@plt>
lea    0x2e70(%rip),%rdx
mov    %rsp,%rsi
mov    %r15d,%edi
mov    %r14,%rcx
call   1050 <determine_ioctl@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@getbtnmap.jsiocgbtnmap = internal global i32 0
@JSIOCGBTNMAP = global i32 0
@__u16 = global i32* null
@BTN_MISC = global i32 0
define i32 @getbtnmap(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [4 x i32]
store i32 %0, i32* %4
store i32* %1, i32** %5
%7 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
%8 = load i32, i32* @JSIOCGBTNMAP
store i32 %8, i32* %7
%9 = getelementptr inbounds i32, i32* %7, i64 1
%10 = load i32*, i32** @__u16
%11 = load i32, i32* @BTN_MISC
%12 = sub nsw i32 767, %11
%13 = add nsw i32 %12, 1
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i32, i32* %10, i64 %14
%16 = load i32, i32* %15
%17 = call i32 @_IOR(i8 signext 106, i32 52, i32 %16)
store i32 %17, i32* %9
%18 = getelementptr inbounds i32, i32* %9, i64 1
%19 = load i32*, i32** @__u16
%20 = load i32, i32* @BTN_MISC
%21 = sub nsw i32 511, %20
%22 = add nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i32, i32* %19, i64 %23
%25 = load i32, i32* %24
%26 = call i32 @_IOR(i8 signext 106, i32 52, i32 %25)
store i32 %26, i32* %18
%27 = getelementptr inbounds i32, i32* %18, i64 1
store i32 0, i32* %27
%28 = load i32, i32* @getbtnmap.jsiocgbtnmap
%29 = icmp ne i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %2
%31 = load i32, i32* %4
%32 = load i32, i32* @getbtnmap.jsiocgbtnmap
%33 = load i32*, i32** %5
%34 = call i32 @ioctl(i32 %31, i32 %32, i32* %33)
store i32 %34, i32* %3
br label %40
35: ; preds = %2
%36 = load i32, i32* %4
%37 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
%38 = load i32*, i32** %5
%39 = call i32 @determine_ioctl(i32 %36, i32* %37, i32* @getbtnmap.jsiocgbtnmap, i32* %38)
store i32 %39, i32* %3
br label %40
40: ; preds = %35, %30
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@getbtnmap.jsiocgbtnmap = internal global i32 0
@JSIOCGBTNMAP = global i32 0
@__u16 = global i32* null
@BTN_MISC = global i32 0
define i32 @getbtnmap(i32 %0, i32* %1) {
%3 = alloca [4 x i32]
%4 = bitcast [4 x i32]* %3 to i8*
%5 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 0
%6 = load i32, i32* @JSIOCGBTNMAP
store i32 %6, i32* %5
%7 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 1
%8 = load i32*, i32** @__u16
%9 = load i32, i32* @BTN_MISC
%10 = sub i32 768, %9
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %8, i64 %11
%13 = load i32, i32* %12
%14 = tail call i32 @_IOR(i8 signext 106, i32 52, i32 %13)
store i32 %14, i32* %7
%15 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 2
%16 = load i32*, i32** @__u16
%17 = load i32, i32* @BTN_MISC
%18 = sub i32 512, %17
%19 = sext i32 %18 to i64
%20 = getelementptr inbounds i32, i32* %16, i64 %19
%21 = load i32, i32* %20
%22 = tail call i32 @_IOR(i8 signext 106, i32 52, i32 %21)
store i32 %22, i32* %15
%23 = getelementptr inbounds [4 x i32], [4 x i32]* %3, i64 0, i64 3
store i32 0, i32* %23
%24 = load i32, i32* @getbtnmap.jsiocgbtnmap
%25 = icmp eq i32 %24, 0
br i1 %25, label %28, label %26
26: ; preds = %2
%27 = tail call i32 @ioctl(i32 %0, i32 %24, i32* %1)
br label %30
28: ; preds = %2
%29 = call i32 @determine_ioctl(i32 %0, i32* %5, i32* @getbtnmap.jsiocgbtnmap, i32* %1)
br label %30
30: ; preds = %28, %26
%31 = phi i32 [ %27, %26 ], [ %29, %28 ]
ret i32 %31
}