#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*/ memcpy(unsigned char *, ...);
int syncCommon(unsigned char *, int, unsigned char *, int *, int);
int uiExchangeApduDirect(unsigned int uiSlot, unsigned char *psIn, int iInLen,
                         unsigned char *psOut, int *piOutLen) {
  unsigned char sRequestDataBuffer[300], sResponseDataBuffer[300];
  int iRequestDataLength, iResponseDataLength;
  memcpy(sRequestDataBuffer, "\x62\x02", 2);
  sRequestDataBuffer[2] = uiSlot;
  sRequestDataBuffer[3] = 0;
  memcpy(sRequestDataBuffer + 4, psIn, iInLen);
  iRequestDataLength = iInLen + 4;
  int ret = syncCommon(sRequestDataBuffer, iRequestDataLength,
                       sResponseDataBuffer, &iResponseDataLength, 8);
  if (ret || iResponseDataLength == 0) {
    return (1);
  }
  if (sResponseDataBuffer[0]) {
    return (ret);
  }
  memcpy(psOut, sResponseDataBuffer + 1, iResponseDataLength - 1);
  *piOutLen = iResponseDataLength - 1;
  return (0);
}


x86 assembly (clang, -O0):
<uiExchangeApduDirect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2a0,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
lea    -0x160(%rbp),%rdi
lea    0xeb5(%rip),%rsi
xor    %eax,%eax
mov    $0x2,%edx
call   1030 <memcpy@plt>
mov    -0x8(%rbp),%eax
mov    %al,-0x15e(%rbp)
movb   $0x0,-0x15d(%rbp)
lea    -0x160(%rbp),%rdi
add    $0x4,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x294(%rbp)
lea    -0x160(%rbp),%rdi
mov    -0x294(%rbp),%esi
lea    -0x290(%rbp),%rdx
lea    -0x298(%rbp),%rcx
mov    $0x8,%r8d
call   1040 <syncCommon@plt>
mov    %eax,-0x29c(%rbp)
cmpl   $0x0,-0x29c(%rbp)
jne    11d2 <uiExchangeApduDirect+0xb2>
cmpl   $0x0,-0x298(%rbp)
jne    11de <uiExchangeApduDirect+0xbe>
movl   $0x1,-0x4(%rbp)
jmp    122e <uiExchangeApduDirect+0x10e>
cmpb   $0x0,-0x290(%rbp)
je     11f9 <uiExchangeApduDirect+0xd9>
mov    -0x29c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    122e <uiExchangeApduDirect+0x10e>
mov    -0x20(%rbp),%rdi
lea    -0x290(%rbp),%rsi
add    $0x1,%rsi
mov    -0x298(%rbp),%edx
sub    $0x1,%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
mov    -0x298(%rbp),%ecx
sub    $0x1,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x2a0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<uiExchangeApduDirect>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x278,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %edx,%ebp
mov    %rsi,%r12
mov    %edi,%ebx
lea    0xebb(%rip),%rsi
lea    0x10(%rsp),%r13
mov    %r13,%rdi
mov    $0x2,%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
mov    %bl,0x12(%rsp)
movb   $0x0,0x13(%rsp)
lea    0x14(%rsp),%rdi
mov    %r12,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1030 <memcpy@plt>
add    $0x4,%ebp
lea    0x140(%rsp),%rdx
lea    0xc(%rsp),%rcx
mov    %r13,%rdi
mov    %ebp,%esi
mov    $0x8,%r8d
call   1040 <syncCommon@plt>
mov    $0x1,%ebp
test   %eax,%eax
jne    11d1 <uiExchangeApduDirect+0xb1>
mov    0xc(%rsp),%edx
test   %edx,%edx
je     11d1 <uiExchangeApduDirect+0xb1>
xor    %ebp,%ebp
cmpb   $0x0,0x140(%rsp)
jne    11d1 <uiExchangeApduDirect+0xb1>
lea    0x141(%rsp),%rsi
add    $0xffffffff,%edx
xor    %ebp,%ebp
mov    %r15,%rdi
xor    %eax,%eax
call   1030 <memcpy@plt>
mov    0xc(%rsp),%eax
add    $0xffffffff,%eax
mov    %eax,(%r14)
mov    %ebp,%eax
add    $0x278,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"b\02\00"
define i32 @uiExchangeApduDirect(i32 %0, i8* %1, i32 %2, i8* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca [300 x i8]
%13 = alloca [300 x i8]
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32* %4, i32** %11
%17 = getelementptr inbounds [300 x i8], [300 x i8]* %12, i64 0, i64 0
%18 = call i32 (i8*, ...) @memcpy(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%19 = load i32, i32* %7
%20 = trunc i32 %19 to i8
%21 = getelementptr inbounds [300 x i8], [300 x i8]* %12, i64 0, i64 2
store i8 %20, i8* %21
%22 = getelementptr inbounds [300 x i8], [300 x i8]* %12, i64 0, i64 3
store i8 0, i8* %22
%23 = getelementptr inbounds [300 x i8], [300 x i8]* %12, i64 0, i64 0
%24 = getelementptr inbounds i8, i8* %23, i64 4
%25 = load i8*, i8** %8
%26 = load i32, i32* %9
%27 = call i32 (i8*, ...) @memcpy(i8* %24, i8* %25, i32 %26)
%28 = load i32, i32* %9
%29 = add nsw i32 %28, 4
store i32 %29, i32* %14
%30 = getelementptr inbounds [300 x i8], [300 x i8]* %12, i64 0, i64 0
%31 = load i32, i32* %14
%32 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%33 = call i32 @syncCommon(i8* %30, i32 %31, i8* %32, i32* %15, i32 8)
store i32 %33, i32* %16
%34 = load i32, i32* %16
%35 = icmp ne i32 %34, 0
br i1 %35, label %39, label %36
36: ; preds = %5
%37 = load i32, i32* %15
%38 = icmp eq i32 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %36, %5
store i32 1, i32* %6
br label %56
40: ; preds = %36
%41 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%42 = load i8, i8* %41
%43 = icmp ne i8 %42, 0
br i1 %43, label %44, label %46
44: ; preds = %40
%45 = load i32, i32* %16
store i32 %45, i32* %6
br label %56
46: ; preds = %40
%47 = load i8*, i8** %10
%48 = getelementptr inbounds [300 x i8], [300 x i8]* %13, i64 0, i64 0
%49 = getelementptr inbounds i8, i8* %48, i64 1
%50 = load i32, i32* %15
%51 = sub nsw i32 %50, 1
%52 = call i32 (i8*, ...) @memcpy(i8* %47, i8* %49, i32 %51)
%53 = load i32, i32* %15
%54 = sub nsw i32 %53, 1
%55 = load i32*, i32** %11
store i32 %54, i32* %55
store i32 0, i32* %6
br label %56
56: ; preds = %46, %44, %39
%57 = load i32, i32* %6
ret i32 %57
}

LLVM IR (-O2):
@.str = [3 x i8] c"b\02\00"
define i32 @uiExchangeApduDirect(i32 %0, i8* %1, i32 %2, i8* %3, i32* nocapture writeonly %4) {
%6 = alloca [300 x i8]
%7 = alloca [300 x i8]
%8 = alloca i32
%9 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 0
%10 = getelementptr inbounds [300 x i8], [300 x i8]* %7, i64 0, i64 0
%11 = bitcast i32* %8 to i8*
%12 = call i32 (i8*, ...) @memcpy(i8* %9, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 2)
%13 = trunc i32 %0 to i8
%14 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 2
store i8 %13, i8* %14
%15 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 3
store i8 0, i8* %15
%16 = getelementptr inbounds [300 x i8], [300 x i8]* %6, i64 0, i64 4
%17 = call i32 (i8*, ...) @memcpy(i8* %16, i8* %1, i32 %2)
%18 = add nsw i32 %2, 4
%19 = call i32 @syncCommon(i8* %9, i32 %18, i8* %10, i32* %8, i32 8)
%20 = icmp ne i32 %19, 0
%21 = load i32, i32* %8
%22 = icmp eq i32 %21, 0
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %33, label %24
24: ; preds = %5
%25 = load i8, i8* %10
%26 = icmp eq i8 %25, 0
br i1 %26, label %27, label %33
27: ; preds = %24
%28 = getelementptr inbounds [300 x i8], [300 x i8]* %7, i64 0, i64 1
%29 = add nsw i32 %21, -1
%30 = call i32 (i8*, ...) @memcpy(i8* %3, i8* %28, i32 %29)
%31 = load i32, i32* %8
%32 = add nsw i32 %31, -1
store i32 %32, i32* %4
br label %33
33: ; preds = %24, %5, %27
%34 = phi i32 [ 0, %27 ], [ 1, %5 ], [ 0, %24 ]
ret i32 %34
}