#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(char *, char const *, short);
int /*<<< orphan*/ memset(char *, int, int);
const short aes_pkcs7padding(const char *in, const size_t length, char *blk) {
  short quot = length & 0x0f;
  memset(blk, 16 - quot, 16);
  if (quot > 0)
    memcpy(blk, in + length - quot, quot);
  return quot;
}


x86 assembly (clang, -O0):
<aes_pkcs7padding>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
and    $0xf,%rax
mov    %ax,-0x1a(%rbp)
mov    -0x18(%rbp),%rdi
movswl -0x1a(%rbp),%eax
mov    $0x10,%esi
sub    %eax,%esi
mov    $0x10,%edx
call   1030 <memset@plt>
movswl -0x1a(%rbp),%eax
cmp    $0x0,%eax
jle    118d <aes_pkcs7padding+0x6d>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    -0x10(%rbp),%rsi
movswl -0x1a(%rbp),%eax
movslq %eax,%rcx
xor    %eax,%eax
sub    %rcx,%rax
add    %rax,%rsi
mov    -0x1a(%rbp),%ax
movswl %ax,%edx
call   1040 <memcpy@plt>
movswl -0x1a(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<aes_pkcs7padding>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r12
mov    %rdi,%r15
mov    %r12d,%ebx
and    $0xf,%ebx
mov    $0x10,%esi
sub    %ebx,%esi
mov    %rdx,%rdi
mov    $0x10,%edx
call   1030 <memset@plt>
test   %ebx,%ebx
je     1163 <aes_pkcs7padding+0x43>
add    %r12,%r15
movzwl %bx,%edx
sub    %rdx,%r15
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <memcpy@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define signext i16 @aes_pkcs7padding(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i16
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%8 = load i64, i64* %5
%9 = and i64 %8, 15
%10 = trunc i64 %9 to i16
store i16 %10, i16* %7
%11 = load i8*, i8** %6
%12 = load i16, i16* %7
%13 = sext i16 %12 to i32
%14 = sub nsw i32 16, %13
%15 = call i32 @memset(i8* %11, i32 %14, i32 16)
%16 = load i16, i16* %7
%17 = sext i16 %16 to i32
%18 = icmp sgt i32 %17, 0
br i1 %18, label %19, label %31
19: ; preds = %3
%20 = load i8*, i8** %6
%21 = load i8*, i8** %4
%22 = load i64, i64* %5
%23 = getelementptr inbounds i8, i8* %21, i64 %22
%24 = load i16, i16* %7
%25 = sext i16 %24 to i32
%26 = sext i32 %25 to i64
%27 = sub i64 0, %26
%28 = getelementptr inbounds i8, i8* %23, i64 %27
%29 = load i16, i16* %7
%30 = call i32 @memcpy(i8* %20, i8* %28, i16 signext %29)
br label %31
31: ; preds = %19, %3
%32 = load i16, i16* %7
ret i16 %32
}

LLVM IR (-O2):
define signext i16 @aes_pkcs7padding(i8* %0, i64 %1, i8* %2) {
%4 = trunc i64 %1 to i16
%5 = and i16 %4, 15
%6 = sub nuw nsw i16 16, %5
%7 = zext i16 %6 to i32
%8 = tail call i32 @memset(i8* %2, i32 %7, i32 16)
%9 = icmp eq i16 %5, 0
br i1 %9, label %16, label %10
10: ; preds = %3
%11 = getelementptr inbounds i8, i8* %0, i64 %1
%12 = zext i16 %5 to i64
%13 = sub nsw i64 0, %12
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = tail call i32 @memcpy(i8* %2, i8* %14, i16 signext %5)
br label %16
16: ; preds = %10, %3
ret i16 %5
}