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

/* Variables and functions */
int /*<<< orphan*/ multosHexToBin(char *, unsigned char *, unsigned char);
unsigned short multosSendAPDU(int /*<<< orphan*/, int, int, int, unsigned char,
                              int /*<<< orphan*/, unsigned char *,
                              int /*<<< orphan*/, unsigned char *, int, int);
int strlen(char *);
int multosSelectApplication(char *hexAid) {
  unsigned char aidLen, La;
  unsigned short sw;
  unsigned char aid[16];
  aidLen = strlen(hexAid) / 2;
  if (aidLen > 16)
    aidLen = 16;
  multosHexToBin(hexAid, aid, aidLen);
  sw = multosSendAPDU(0, 0xA4, 0x04, 0x0C, aidLen, 0, &La, 0, aid, sizeof(aid),
                      1000);
  if (sw == 0x9000)
    return (1);
  return (0);
}


x86 assembly (clang, -O0):
<multosSelectApplication>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    $0x2,%ecx
cltd
idiv   %ecx
mov    %al,-0x11(%rbp)
movzbl -0x11(%rbp),%eax
cmp    $0x10,%eax
jle    1161 <multosSelectApplication+0x31>
movb   $0x10,-0x11(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
movzbl -0x11(%rbp),%edx
call   1050 <multosHexToBin@plt>
mov    -0x11(%rbp),%r8b
lea    -0x30(%rbp),%rax
xor    %r9d,%r9d
mov    $0xa4,%esi
mov    $0x4,%edx
mov    $0xc,%ecx
lea    -0x12(%rbp),%r10
mov    %r9d,%edi
movzbl %r8b,%r8d
mov    %r10,(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rax,0x10(%rsp)
movl   $0x10,0x18(%rsp)
movl   $0x3e8,0x20(%rsp)
call   1030 <multosSendAPDU@plt>
mov    %ax,-0x14(%rbp)
movzwl -0x14(%rbp),%eax
cmp    $0x9000,%eax
jne    11dc <multosSelectApplication+0xac>
movl   $0x1,-0x4(%rbp)
jmp    11e3 <multosSelectApplication+0xb3>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<multosSelectApplication>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %eax,%ecx
shr    $0x1f,%ecx
add    %eax,%ecx
sar    %ecx
movzbl %cl,%eax
cmp    $0x11,%eax
mov    $0x10,%eax
cmovb  %ecx,%eax
movzbl %al,%ebp
lea    0x10(%rsp),%r14
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1050 <multosHexToBin@plt>
sub    $0x8,%rsp
lea    0x17(%rsp),%rax
xor    %edi,%edi
mov    $0xa4,%esi
mov    $0x4,%edx
mov    $0xc,%ecx
mov    %ebp,%r8d
xor    %r9d,%r9d
push   $0x3e8
push   $0x10
push   %r14
push   $0x0
push   %rax
call   1030 <multosSendAPDU@plt>
add    $0x30,%rsp
movzwl %ax,%ecx
xor    %eax,%eax
cmp    $0x9000,%ecx
sete   %al
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @multosSelectApplication(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i8
%6 = alloca i16
%7 = alloca [16 x i8]
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = sdiv i32 %9, 2
%11 = trunc i32 %10 to i8
store i8 %11, i8* %4
%12 = load i8, i8* %4
%13 = zext i8 %12 to i32
%14 = icmp sgt i32 %13, 16
br i1 %14, label %15, label %16
15: ; preds = %1
store i8 16, i8* %4
br label %16
16: ; preds = %15, %1
%17 = load i8*, i8** %3
%18 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%19 = load i8, i8* %4
%20 = call i32 @multosHexToBin(i8* %17, i8* %18, i8 zeroext %19)
%21 = load i8, i8* %4
%22 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%23 = call zeroext i16 @multosSendAPDU(i32 0, i32 164, i32 4, i32 12, i8 zeroext %21, i32 0, i8* %5, i32 0, i8* %22, i32 16, i32 1000)
store i16 %23, i16* %6
%24 = load i16, i16* %6
%25 = zext i16 %24 to i32
%26 = icmp eq i32 %25, 36864
br i1 %26, label %27, label %28
27: ; preds = %16
store i32 1, i32* %2
br label %29
28: ; preds = %16
store i32 0, i32* %2
br label %29
29: ; preds = %28, %27
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
define i32 @multosSelectApplication(i8* %0) {
%2 = alloca i8
%3 = alloca [16 x i8]
%4 = getelementptr inbounds [16 x i8], [16 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %0)
%6 = sdiv i32 %5, 2
%7 = trunc i32 %6 to i8
%8 = and i32 %6, 255
%9 = icmp ugt i32 %8, 16
%10 = select i1 %9, i8 16, i8 %7
%11 = call i32 @multosHexToBin(i8* %0, i8* %4, i8 zeroext %10)
%12 = call zeroext i16 @multosSendAPDU(i32 0, i32 164, i32 4, i32 12, i8 zeroext %10, i32 0, i8* %2, i32 0, i8* %4, i32 16, i32 1000)
%13 = icmp eq i16 %12, -28672
%14 = zext i1 %13 to i32
ret i32 %14
}