#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*/ EINVAL;
int /*<<< orphan*/ ENOMEM;
int alpar_send(int, int, int, unsigned char *);
int /*<<< orphan*/ errno;
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, char const *, int);
int apdu_send(int fd, unsigned char cla, unsigned char ins, unsigned char p1,
              unsigned char p2, unsigned char p3, int datalen,
              const char *data) {
  int packetlen;
  unsigned char *packet;
  if (datalen < 0) {
    errno = EINVAL;
    return -1;
  }
  packetlen = 5 + datalen;
  packet = (unsigned char *)malloc(packetlen);
  if (!packet) {
    errno = ENOMEM;
    return -1;
  }
  packet[0] = cla;
  packet[1] = ins;
  packet[2] = p1;
  packet[3] = p2;
  packet[4] = p3;
  if (datalen) {
    memcpy(packet + 5, data, datalen);
  }
  return alpar_send(fd, 0x00, packetlen, packet);
}


x86 assembly (clang, -O0):
<apdu_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %r8d,-0x30(%rbp)
mov    %ecx,%eax
mov    -0x30(%rbp),%ecx
mov    %eax,-0x2c(%rbp)
mov    %edx,%eax
mov    -0x2c(%rbp),%edx
mov    %eax,-0x28(%rbp)
mov    %esi,%eax
mov    -0x28(%rbp),%esi
mov    %eax,-0x24(%rbp)
mov    %edi,%r8d
mov    -0x24(%rbp),%edi
mov    %r9b,%al
mov    0x18(%rbp),%r9
mov    0x10(%rbp),%r9d
mov    %r8d,-0x8(%rbp)
mov    %dil,-0x9(%rbp)
mov    %sil,-0xa(%rbp)
mov    %dl,-0xb(%rbp)
mov    %cl,-0xc(%rbp)
mov    %al,-0xd(%rbp)
cmpl   $0x0,0x10(%rbp)
jge    11a2 <apdu_send+0x72>
mov    0x2e45(%rip),%rax
mov    (%rax),%ecx
mov    0x2e44(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <apdu_send+0x113>
mov    0x10(%rbp),%eax
add    $0x5,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11e0 <apdu_send+0xb0>
mov    0x2e17(%rip),%rax
mov    (%rax),%ecx
mov    0x2e06(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1243 <apdu_send+0x113>
mov    -0x9(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0xa(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x1(%rax)
mov    -0xb(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x2(%rax)
mov    -0xc(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x3(%rax)
mov    -0xd(%rbp),%cl
mov    -0x20(%rbp),%rax
mov    %cl,0x4(%rax)
cmpl   $0x0,0x10(%rbp)
je     122f <apdu_send+0xff>
mov    -0x20(%rbp),%rdi
add    $0x5,%rdi
mov    0x18(%rbp),%rsi
mov    0x10(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%edi
mov    -0x14(%rbp),%edx
mov    -0x20(%rbp),%rcx
xor    %esi,%esi
call   1030 <alpar_send@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<apdu_send>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    0x50(%rsp),%ebx
test   %ebx,%ebx
js     11ce <apdu_send+0x9e>
mov    %ecx,%r14d
mov    %edx,%r15d
mov    %esi,%r13d
mov    %r8d,0x4(%rsp)
mov    %r9d,0x8(%rsp)
mov    %edi,0xc(%rsp)
mov    0x58(%rsp),%rax
mov    %rax,0x10(%rsp)
lea    0x5(%rbx),%r12d
mov    %r12d,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11d7 <apdu_send+0xa7>
mov    %rax,%rbp
mov    %r13b,(%rax)
mov    %r15b,0x1(%rax)
mov    %r14b,0x2(%rax)
mov    0x4(%rsp),%eax
mov    %al,0x3(%rbp)
mov    0x8(%rsp),%eax
mov    %al,0x4(%rbp)
test   %ebx,%ebx
je     11af <apdu_send+0x7f>
mov    %rbp,%rdi
add    $0x5,%rdi
mov    0x10(%rsp),%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    0xc(%rsp),%edi
xor    %esi,%esi
mov    %r12d,%edx
mov    %rbp,%rcx
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <alpar_send@plt>
mov    0x2dfb(%rip),%rax
jmp    11de <apdu_send+0xae>
mov    0x2e02(%rip),%rax
mov    (%rax),%eax
mov    0x2df1(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @apdu_send(i32 %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i8 zeroext %4, i8 zeroext %5, i32 %6, i8* %7) {
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8
%12 = alloca i8
%13 = alloca i8
%14 = alloca i8
%15 = alloca i8
%16 = alloca i32
%17 = alloca i8*
%18 = alloca i32
%19 = alloca i8*
store i32 %0, i32* %10
store i8 %1, i8* %11
store i8 %2, i8* %12
store i8 %3, i8* %13
store i8 %4, i8* %14
store i8 %5, i8* %15
store i32 %6, i32* %16
store i8* %7, i8** %17
%20 = load i32, i32* %16
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %8
%23 = load i32, i32* @EINVAL
store i32 %23, i32* @errno
store i32 -1, i32* %9
br label %63
24: ; preds = %8
%25 = load i32, i32* %16
%26 = add nsw i32 5, %25
store i32 %26, i32* %18
%27 = load i32, i32* %18
%28 = call i64 @malloc(i32 %27)
%29 = inttoptr i64 %28 to i8*
store i8* %29, i8** %19
%30 = load i8*, i8** %19
%31 = icmp ne i8* %30, null
br i1 %31, label %34, label %32
32: ; preds = %24
%33 = load i32, i32* @ENOMEM
store i32 %33, i32* @errno
store i32 -1, i32* %9
br label %63
34: ; preds = %24
%35 = load i8, i8* %11
%36 = load i8*, i8** %19
%37 = getelementptr inbounds i8, i8* %36, i64 0
store i8 %35, i8* %37
%38 = load i8, i8* %12
%39 = load i8*, i8** %19
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8 %38, i8* %40
%41 = load i8, i8* %13
%42 = load i8*, i8** %19
%43 = getelementptr inbounds i8, i8* %42, i64 2
store i8 %41, i8* %43
%44 = load i8, i8* %14
%45 = load i8*, i8** %19
%46 = getelementptr inbounds i8, i8* %45, i64 3
store i8 %44, i8* %46
%47 = load i8, i8* %15
%48 = load i8*, i8** %19
%49 = getelementptr inbounds i8, i8* %48, i64 4
store i8 %47, i8* %49
%50 = load i32, i32* %16
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %58
52: ; preds = %34
%53 = load i8*, i8** %19
%54 = getelementptr inbounds i8, i8* %53, i64 5
%55 = load i8*, i8** %17
%56 = load i32, i32* %16
%57 = call i32 @memcpy(i8* %54, i8* %55, i32 %56)
br label %58
58: ; preds = %52, %34
%59 = load i32, i32* %10
%60 = load i32, i32* %18
%61 = load i8*, i8** %19
%62 = call i32 @alpar_send(i32 %59, i32 0, i32 %60, i8* %61)
store i32 %62, i32* %9
br label %63
63: ; preds = %58, %32, %22
%64 = load i32, i32* %9
ret i32 %64
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i32 @apdu_send(i32 %0, i8 zeroext %1, i8 zeroext %2, i8 zeroext %3, i8 zeroext %4, i8 zeroext %5, i32 %6, i8* %7) {
%9 = icmp slt i32 %6, 0
br i1 %9, label %10, label %12
10: ; preds = %8
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %30
12: ; preds = %8
%13 = add nuw nsw i32 %6, 5
%14 = tail call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = icmp eq i64 %14, 0
br i1 %16, label %17, label %19
17: ; preds = %12
%18 = load i32, i32* @ENOMEM
store i32 %18, i32* @errno
br label %30
19: ; preds = %12
store i8 %1, i8* %15
%20 = getelementptr inbounds i8, i8* %15, i64 1
store i8 %2, i8* %20
%21 = getelementptr inbounds i8, i8* %15, i64 2
store i8 %3, i8* %21
%22 = getelementptr inbounds i8, i8* %15, i64 3
store i8 %4, i8* %22
%23 = getelementptr inbounds i8, i8* %15, i64 4
store i8 %5, i8* %23
%24 = icmp eq i32 %6, 0
br i1 %24, label %28, label %25
25: ; preds = %19
%26 = getelementptr inbounds i8, i8* %15, i64 5
%27 = tail call i32 @memcpy(i8* %26, i8* %7, i32 %6)
br label %28
28: ; preds = %25, %19
%29 = tail call i32 @alpar_send(i32 %0, i32 0, i32 %13, i8* %15)
br label %30
30: ; preds = %28, %17, %10
%31 = phi i32 [ -1, %10 ], [ %29, %28 ], [ -1, %17 ]
ret i32 %31
}