#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, int /*<<< orphan*/,
                              int /*<<< orphan*/, unsigned char,
                              int /*<<< orphan*/, unsigned char *,
                              int /*<<< orphan*/, unsigned char *, int);
int sendCertificate(unsigned char loadNotDelete, unsigned char *certificate,
                    int certlen) {
  unsigned char INS = 24;
  unsigned short offset = 0;
  unsigned short SW;
  unsigned char chunksize, La;
  if (loadNotDelete)
    INS = 22;
  while (certlen > 0) {
    chunksize = certlen;
    if (certlen > 249)
      chunksize = 249;
    SW = multosSendAPDU(0xBE, INS, 0, 0, chunksize, 0, &La, 0,
                        certificate + offset, 10000);
    if (SW != 0x9000)
      return (0);
    certlen = certlen - chunksize;
    offset = offset + chunksize;
  }
  return (1);
}


x86 assembly (clang, -O0):
<sendCertificate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movb   $0x18,-0x15(%rbp)
movw   $0x0,-0x18(%rbp)
cmpb   $0x0,-0x5(%rbp)
je     113d <sendCertificate+0x2d>
movb   $0x16,-0x15(%rbp)
jmp    1142 <sendCertificate+0x32>
cmpl   $0x0,-0x14(%rbp)
jle    11ed <sendCertificate+0xdd>
mov    -0x14(%rbp),%eax
mov    %al,-0x1b(%rbp)
cmpl   $0xf9,-0x14(%rbp)
jle    1163 <sendCertificate+0x53>
movb   $0xf9,-0x1b(%rbp)
mov    -0x15(%rbp),%cl
mov    -0x1b(%rbp),%r8b
mov    -0x10(%rbp),%rax
movzwl -0x18(%rbp),%edx
movslq %edx,%rdx
add    %rdx,%rax
mov    $0xbe,%edi
xor    %r9d,%r9d
lea    -0x1c(%rbp),%r10
movzbl %cl,%esi
mov    %r9d,%edx
mov    %r9d,%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,-0x1a(%rbp)
movzwl -0x1a(%rbp),%eax
cmp    $0x9000,%eax
je     11ce <sendCertificate+0xbe>
movl   $0x0,-0x4(%rbp)
jmp    11f4 <sendCertificate+0xe4>
mov    -0x14(%rbp),%eax
movzbl -0x1b(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
movzwl -0x18(%rbp),%eax
movzbl -0x1b(%rbp),%ecx
add    %ecx,%eax
mov    %ax,-0x18(%rbp)
jmp    1142 <sendCertificate+0x32>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sendCertificate>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
test   %edi,%edi
sete   %cl
mov    $0x1,%eax
test   %edx,%edx
jle    1198 <sendCertificate+0x88>
mov    %edx,%ebp
mov    %rsi,%r15
add    %cl,%cl
add    $0x16,%cl
lea    0x7(%rsp),%r12
movzbl %cl,%r13d
xor    %ebx,%ebx
xchg   %ax,%ax
cmp    $0xf9,%ebp
mov    $0xf9,%r14d
cmovb  %ebp,%r14d
movzwl %bx,%ebx
lea    (%r15,%rbx,1),%rax
movzbl %r14b,%r8d
mov    $0xbe,%edi
mov    %r13d,%esi
xor    %edx,%edx
xor    %ecx,%ecx
xor    %r9d,%r9d
push   $0x2710
push   %rax
push   $0x0
push   %r12
call   1030 <multosSendAPDU@plt>
add    $0x20,%rsp
movzwl %ax,%eax
cmp    $0x9000,%eax
jne    1196 <sendCertificate+0x86>
add    %r14d,%ebx
sub    %r14d,%ebp
jg     1140 <sendCertificate+0x30>
mov    $0x1,%eax
jmp    1198 <sendCertificate+0x88>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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