#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 */
short alpar_recv(int, unsigned char *, unsigned char *, unsigned char **,
                 unsigned char *);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, short);
int apdu_recv(int fd, unsigned char *sw1, unsigned char *sw2,
              unsigned char *data) {
  unsigned char lrc, *packet, command, ack;
  short int len;
  command = 0xff;
  packet = NULL;
  while (command != 0x00) {
    len = alpar_recv(fd, &ack, &command, &packet, &lrc);
    if (len < 0)
      return -1;
  }
  if (ack == 0xE0)
    return -1;
  *sw1 = packet[len - 2];
  *sw2 = packet[len - 1];
  if (len - 2 > 0)
    memcpy(data, packet, len - 2);
  if (packet)
    free(packet);
  return (len - 2);
}


x86 assembly (clang, -O0):
<apdu_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
movb   $0xff,-0x31(%rbp)
movq   $0x0,-0x30(%rbp)
movzbl -0x31(%rbp),%eax
cmp    $0x0,%eax
je     119a <apdu_recv+0x6a>
mov    -0x8(%rbp),%edi
lea    -0x32(%rbp),%rsi
lea    -0x31(%rbp),%rdx
lea    -0x30(%rbp),%rcx
lea    -0x21(%rbp),%r8
call   1050 <alpar_recv@plt>
mov    %ax,-0x34(%rbp)
movswl -0x34(%rbp),%eax
cmp    $0x0,%eax
jge    1195 <apdu_recv+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <apdu_recv+0xf8>
jmp    1153 <apdu_recv+0x23>
movzbl -0x32(%rbp),%eax
cmp    $0xe0,%eax
jne    11b5 <apdu_recv+0x85>
movl   $0xffffffff,-0x4(%rbp)
jmp    1228 <apdu_recv+0xf8>
mov    -0x30(%rbp),%rax
movswl -0x34(%rbp),%ecx
sub    $0x2,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
mov    -0x30(%rbp),%rax
movswl -0x34(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
movswl -0x34(%rbp),%eax
sub    $0x2,%eax
cmp    $0x0,%eax
jle    120a <apdu_recv+0xda>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
movswl -0x34(%rbp),%eax
sub    $0x2,%eax
movswl %ax,%edx
call   1040 <memcpy@plt>
cmpq   $0x0,-0x30(%rbp)
je     121e <apdu_recv+0xee>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
movswl -0x34(%rbp),%eax
sub    $0x2,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apdu_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rcx,0x10(%rsp)
mov    %rdx,0x20(%rsp)
mov    %rsi,0x18(%rsp)
mov    %edi,%ebp
movb   $0xff,0x5(%rsp)
movq   $0x0,0x8(%rsp)
lea    0x6(%rsp),%r13
lea    0x5(%rsp),%r14
lea    0x8(%rsp),%r15
lea    0x7(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %r13,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    %r12,%r8
call   1050 <alpar_recv@plt>
mov    $0xffffffff,%ebx
test   %ax,%ax
js     11f2 <apdu_recv+0xc2>
cmpb   $0x0,0x5(%rsp)
jne    1180 <apdu_recv+0x50>
cmpb   $0xe0,0x6(%rsp)
je     11f2 <apdu_recv+0xc2>
mov    0x8(%rsp),%rsi
movzwl %ax,%ecx
lea    -0x2(%rcx),%rbx
mov    -0x2(%rsi,%rcx,1),%dl
mov    0x18(%rsp),%rdi
mov    %dl,(%rdi)
mov    -0x1(%rcx,%rsi,1),%cl
mov    0x20(%rsp),%rdx
mov    %cl,(%rdx)
cmp    $0x3,%ax
jb     11ea <apdu_recv+0xba>
movswl %bx,%edx
mov    0x10(%rsp),%rdi
call   1040 <memcpy@plt>
mov    0x8(%rsp),%rsi
test   %rsi,%rsi
je     11f2 <apdu_recv+0xc2>
mov    %rsi,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @apdu_recv(i32 %0, i8* %1, i8* %2, i8* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8
%11 = alloca i8*
%12 = alloca i8
%13 = alloca i8
%14 = alloca i16
store i32 %0, i32* %6
store i8* %1, i8** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i8 -1, i8* %12
store i8* null, i8** %11
br label %15
15: ; preds = %26, %4
%16 = load i8, i8* %12
%17 = zext i8 %16 to i32
%18 = icmp ne i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %15
%20 = load i32, i32* %6
%21 = call signext i16 @alpar_recv(i32 %20, i8* %13, i8* %12, i8** %11, i8* %10)
store i16 %21, i16* %14
%22 = load i16, i16* %14
%23 = sext i16 %22 to i32
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %19
store i32 -1, i32* %5
br label %71
26: ; preds = %19
br label %15
27: ; preds = %15
%28 = load i8, i8* %13
%29 = zext i8 %28 to i32
%30 = icmp eq i32 %29, 224
br i1 %30, label %31, label %32
31: ; preds = %27
store i32 -1, i32* %5
br label %71
32: ; preds = %27
%33 = load i8*, i8** %11
%34 = load i16, i16* %14
%35 = sext i16 %34 to i32
%36 = sub nsw i32 %35, 2
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %33, i64 %37
%39 = load i8, i8* %38
%40 = load i8*, i8** %7
store i8 %39, i8* %40
%41 = load i8*, i8** %11
%42 = load i16, i16* %14
%43 = sext i16 %42 to i32
%44 = sub nsw i32 %43, 1
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %41, i64 %45
%47 = load i8, i8* %46
%48 = load i8*, i8** %8
store i8 %47, i8* %48
%49 = load i16, i16* %14
%50 = sext i16 %49 to i32
%51 = sub nsw i32 %50, 2
%52 = icmp sgt i32 %51, 0
br i1 %52, label %53, label %61
53: ; preds = %32
%54 = load i8*, i8** %9
%55 = load i8*, i8** %11
%56 = load i16, i16* %14
%57 = sext i16 %56 to i32
%58 = sub nsw i32 %57, 2
%59 = trunc i32 %58 to i16
%60 = call i32 @memcpy(i8* %54, i8* %55, i16 signext %59)
br label %61
61: ; preds = %53, %32
%62 = load i8*, i8** %11
%63 = icmp ne i8* %62, null
br i1 %63, label %64, label %67
64: ; preds = %61
%65 = load i8*, i8** %11
%66 = call i32 @free(i8* %65)
br label %67
67: ; preds = %64, %61
%68 = load i16, i16* %14
%69 = sext i16 %68 to i32
%70 = sub nsw i32 %69, 2
store i32 %70, i32* %5
br label %71
71: ; preds = %67, %31, %25
%72 = load i32, i32* %5
ret i32 %72
}

LLVM IR (-O2):
define i32 @apdu_recv(i32 %0, i8* nocapture writeonly %1, i8* nocapture writeonly %2, i8* %3) {
%5 = alloca i8
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i8
%9 = bitcast i8** %6 to i8*
store i8 -1, i8* %7
store i8* null, i8** %6
br label %13
10: ; preds = %13
%11 = load i8, i8* %7
%12 = icmp eq i8 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %4, %10
%14 = call signext i16 @alpar_recv(i32 %0, i8* %8, i8* %7, i8** %6, i8* %5)
%15 = icmp slt i16 %14, 0
br i1 %15, label %39, label %10
16: ; preds = %10
%17 = load i8, i8* %8
%18 = icmp eq i8 %17, -32
br i1 %18, label %39, label %19
19: ; preds = %16
%20 = load i8*, i8** %6
%21 = zext i16 %14 to i32
%22 = add nsw i32 %21, -2
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = load i8, i8* %24
store i8 %25, i8* %1
%26 = add nsw i32 %21, -1
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %20, i64 %27
%29 = load i8, i8* %28
store i8 %29, i8* %2
%30 = icmp ugt i16 %14, 2
br i1 %30, label %31, label %36
31: ; preds = %19
%32 = trunc i32 %22 to i16
%33 = call i32 @memcpy(i8* %3, i8* %20, i16 signext %32)
%34 = load i8*, i8** %6
%35 = icmp eq i8* %34, null
br i1 %35, label %39, label %36
36: ; preds = %19, %31
%37 = phi i8* [ %34, %31 ], [ %20, %19 ]
%38 = call i32 @free(i8* %37)
br label %39
39: ; preds = %13, %31, %36, %16
%40 = phi i32 [ -1, %16 ], [ %22, %36 ], [ %22, %31 ], [ -1, %13 ]
ret i32 %40
}