#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 */
unsigned short multosSendAPDU(int, unsigned char, unsigned char, unsigned char,
                              unsigned char, int /*<<< orphan*/,
                              unsigned char *, int /*<<< orphan*/,
                              unsigned char *, int);
int loadComponent(unsigned char INS, unsigned char *component,
                  unsigned short complen) {
  unsigned short offset = 0;
  unsigned char chunksize;
  unsigned short SW;
  unsigned char P1, P2, La;
  while (complen > 0) {
    chunksize = complen;
    if (complen > 249)
      chunksize = 249;
    P1 = offset / 256;
    P2 = offset % 256;
    SW = multosSendAPDU(0xBE, INS, P1, P2, chunksize, 0, &La, 0,
                        component + offset, 10000);
    if (SW != 0x9000)
      return (0);
    complen = complen - chunksize;
    offset = offset + chunksize;
  }
  return (1);
}


x86 assembly (clang, -O0):
<loadComponent>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dx,%ax
mov    %dil,%cl
mov    %cl,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %ax,-0x12(%rbp)
movw   $0x0,-0x14(%rbp)
movzwl -0x12(%rbp),%eax
cmp    $0x0,%eax
jle    120a <loadComponent+0xfa>
mov    -0x12(%rbp),%ax
mov    %al,-0x15(%rbp)
movzwl -0x12(%rbp),%eax
cmp    $0xf9,%eax
jle    1156 <loadComponent+0x46>
movb   $0xf9,-0x15(%rbp)
movzwl -0x14(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %al,-0x19(%rbp)
movzwl -0x14(%rbp),%eax
mov    $0x100,%ecx
cltd
idiv   %ecx
mov    %dl,%al
mov    %al,-0x1a(%rbp)
mov    -0x5(%rbp),%sil
mov    -0x19(%rbp),%dl
mov    -0x1a(%rbp),%cl
mov    -0x15(%rbp),%r8b
mov    -0x10(%rbp),%rax
movzwl -0x14(%rbp),%edi
movslq %edi,%rdi
add    %rdi,%rax
mov    $0xbe,%edi
xor    %r9d,%r9d
lea    -0x1b(%rbp),%r10
movzbl %sil,%esi
movzbl %dl,%edx
movzbl %cl,%ecx
movzbl %r8b,%r8d
mov    %r10,(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rax,0x10(%rsp)
movl   $0x2710,0x18(%rsp)
call   1030 <multosSendAPDU@plt>
mov    %ax,-0x18(%rbp)
movzwl -0x18(%rbp),%eax
cmp    $0x9000,%eax
je     11e9 <loadComponent+0xd9>
movl   $0x0,-0x4(%rbp)
jmp    1211 <loadComponent+0x101>
movzwl -0x12(%rbp),%eax
movzbl -0x15(%rbp),%ecx
sub    %ecx,%eax
mov    %ax,-0x12(%rbp)
movzwl -0x14(%rbp),%eax
movzbl -0x15(%rbp),%ecx
add    %ecx,%eax
mov    %ax,-0x14(%rbp)
jmp    112f <loadComponent+0x1f>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<loadComponent>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    $0x1,%eax
test   %edx,%edx
je     119a <loadComponent+0x8a>
mov    %edx,%ebp
mov    %rsi,%r15
movzbl %dil,%r13d
xor    %eax,%eax
nop
movzwl %bp,%r14d
cmp    $0xf9,%r14d
mov    $0xf9,%ecx
cmovae %ecx,%r14d
movzwl %ax,%r12d
lea    (%r15,%r12,1),%rbx
movzbl %r14b,%r8d
movzbl %ah,%edx
movzbl %al,%ecx
mov    $0xbe,%edi
mov    %r13d,%esi
xor    %r9d,%r9d
push   $0x2710
push   %rbx
push   $0x0
lea    0x1f(%rsp),%rax
push   %rax
call   1030 <multosSendAPDU@plt>
add    $0x20,%rsp
movzwl %ax,%eax
cmp    $0x9000,%eax
jne    1198 <loadComponent+0x88>
movzwl %r14w,%ecx
add    %ecx,%r12d
mov    %r12d,%eax
sub    %cx,%bp
jne    1130 <loadComponent+0x20>
mov    $0x1,%eax
jmp    119a <loadComponent+0x8a>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @loadComponent(i8 zeroext %0, i8* %1, i16 zeroext %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i16
%8 = alloca i16
%9 = alloca i8
%10 = alloca i16
%11 = alloca i8
%12 = alloca i8
%13 = alloca i8
store i8 %0, i8* %5
store i8* %1, i8** %6
store i16 %2, i16* %7
store i16 0, i16* %8
br label %14
14: ; preds = %48, %3
%15 = load i16, i16* %7
%16 = zext i16 %15 to i32
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %61
18: ; preds = %14
%19 = load i16, i16* %7
%20 = trunc i16 %19 to i8
store i8 %20, i8* %9
%21 = load i16, i16* %7
%22 = zext i16 %21 to i32
%23 = icmp sgt i32 %22, 249
br i1 %23, label %24, label %25
24: ; preds = %18
store i8 -7, i8* %9
br label %25
25: ; preds = %24, %18
%26 = load i16, i16* %8
%27 = zext i16 %26 to i32
%28 = sdiv i32 %27, 256
%29 = trunc i32 %28 to i8
store i8 %29, i8* %11
%30 = load i16, i16* %8
%31 = zext i16 %30 to i32
%32 = srem i32 %31, 256
%33 = trunc i32 %32 to i8
store i8 %33, i8* %12
%34 = load i8, i8* %5
%35 = load i8, i8* %11
%36 = load i8, i8* %12
%37 = load i8, i8* %9
%38 = load i8*, i8** %6
%39 = load i16, i16* %8
%40 = zext i16 %39 to i32
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %38, i64 %41
%43 = call zeroext i16 @multosSendAPDU(i32 190, i8 zeroext %34, i8 zeroext %35, i8 zeroext %36, i8 zeroext %37, i32 0, i8* %13, i32 0, i8* %42, i32 10000)
store i16 %43, i16* %10
%44 = load i16, i16* %10
%45 = zext i16 %44 to i32
%46 = icmp ne i32 %45, 36864
br i1 %46, label %47, label %48
47: ; preds = %25
store i32 0, i32* %4
br label %62
48: ; preds = %25
%49 = load i16, i16* %7
%50 = zext i16 %49 to i32
%51 = load i8, i8* %9
%52 = zext i8 %51 to i32
%53 = sub nsw i32 %50, %52
%54 = trunc i32 %53 to i16
store i16 %54, i16* %7
%55 = load i16, i16* %8
%56 = zext i16 %55 to i32
%57 = load i8, i8* %9
%58 = zext i8 %57 to i32
%59 = add nsw i32 %56, %58
%60 = trunc i32 %59 to i16
store i16 %60, i16* %8
br label %14
61: ; preds = %14
store i32 1, i32* %4
br label %62
62: ; preds = %61, %47
%63 = load i32, i32* %4
ret i32 %63
}

LLVM IR (-O2):
define i32 @loadComponent(i8 zeroext %0, i8* %1, i16 zeroext %2) {
%4 = alloca i8
%5 = icmp eq i16 %2, 0
br i1 %5, label %25, label %6
6: ; preds = %3, %20
%7 = phi i16 [ %22, %20 ], [ %2, %3 ]
%8 = phi i32 [ %23, %20 ], [ 0, %3 ]
%9 = icmp ult i16 %7, 249
%10 = select i1 %9, i16 %7, i16 249
%11 = trunc i16 %10 to i8
%12 = and i32 %8, 65535
%13 = lshr i32 %8, 8
%14 = trunc i32 %13 to i8
%15 = trunc i32 %8 to i8
%16 = zext i32 %12 to i64
%17 = getelementptr inbounds i8, i8* %1, i64 %16
%18 = call zeroext i16 @multosSendAPDU(i32 190, i8 zeroext %0, i8 zeroext %14, i8 zeroext %15, i8 zeroext %11, i32 0, i8* %4, i32 0, i8* %17, i32 10000)
%19 = icmp eq i16 %18, -28672
br i1 %19, label %20, label %25
20: ; preds = %6
%21 = zext i16 %10 to i32
%22 = sub i16 %7, %10
%23 = add nuw nsw i32 %12, %21
%24 = icmp eq i16 %22, 0
br i1 %24, label %25, label %6
25: ; preds = %6, %20, %3
%26 = phi i32 [ 1, %3 ], [ 1, %20 ], [ 0, %6 ]
ret i32 %26
}