#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*/ AES_128_ENC(unsigned char const *, unsigned char *,
                               unsigned char *);
int /*<<< orphan*/ generate_subkey(unsigned char const *, unsigned char *,
                                   unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ padding(unsigned char const *, unsigned char *, int);
int /*<<< orphan*/ xor_128(unsigned char const *, unsigned char const *,
                           unsigned char *);
void AES_CMAC(const unsigned char *key, const unsigned char *input, int length,
              unsigned char *mac) {
  unsigned char X[16], Y[16], M_last[16], padded[16];
  unsigned char K1[16], K2[16];
  int n, i, flag;
  generate_subkey(key, K1, K2);
  n = (length + (16 - 1)) / 16;
  if (n == 0) {
    n = 1;
    flag = 0;
  } else {
    if ((length % 16) == 0) {
      flag = 1;
    } else {
      flag = 0;
    }
  }
  if (flag) {
    xor_128(&input[16 * (n - 1)], K1, M_last);
  } else {
    padding(&input[16 * (n - 1)], padded, length % 16);
    xor_128(padded, K2, M_last);
  }
  memset(X, 0, 16);
  for (i = 0; i < n - 1; i++) {
    xor_128(X, &input[16 * i], Y);
    AES_128_ENC(key, Y, X);
  }
  xor_128(X, M_last, Y);
  AES_128_ENC(key, Y, X);
  memcpy(mac, X, 16);
}


x86 assembly (clang, -O0):
<AES_CMAC>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x70(%rbp),%rsi
lea    -0x80(%rbp),%rdx
call   1070 <generate_subkey@plt>
mov    -0x14(%rbp),%eax
add    $0xf,%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jne    11c5 <AES_CMAC+0x65>
movl   $0x1,-0x84(%rbp)
movl   $0x0,-0x8c(%rbp)
jmp    11f7 <AES_CMAC+0x97>
mov    -0x14(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    11e8 <AES_CMAC+0x88>
movl   $0x1,-0x8c(%rbp)
jmp    11f2 <AES_CMAC+0x92>
movl   $0x0,-0x8c(%rbp)
jmp    11f7 <AES_CMAC+0x97>
cmpl   $0x0,-0x8c(%rbp)
je     122b <AES_CMAC+0xcb>
mov    -0x10(%rbp),%rdi
mov    -0x84(%rbp),%eax
sub    $0x1,%eax
shl    $0x4,%eax
cltq
add    %rax,%rdi
lea    -0x70(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1030 <xor_128@plt>
jmp    1265 <AES_CMAC+0x105>
mov    -0x10(%rbp),%rdi
mov    -0x84(%rbp),%eax
sub    $0x1,%eax
shl    $0x4,%eax
cltq
add    %rax,%rdi
lea    -0x60(%rbp),%rsi
mov    -0x14(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
call   1080 <padding@plt>
lea    -0x60(%rbp),%rdi
lea    -0x80(%rbp),%rsi
lea    -0x50(%rbp),%rdx
call   1030 <xor_128@plt>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
movl   $0x0,-0x88(%rbp)
mov    -0x88(%rbp),%eax
mov    -0x84(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    12da <AES_CMAC+0x17a>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x88(%rbp),%eax
shl    $0x4,%eax
cltq
add    %rax,%rsi
lea    -0x40(%rbp),%rdx
call   1030 <xor_128@plt>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <AES_128_ENC@plt>
mov    -0x88(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x88(%rbp)
jmp    127f <AES_CMAC+0x11f>
lea    -0x30(%rbp),%rdi
lea    -0x50(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   1030 <xor_128@plt>
mov    -0x8(%rbp),%rdi
lea    -0x40(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1040 <AES_128_ENC@plt>
mov    -0x20(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AES_CMAC>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rcx,%r14
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %rdi,%r15
lea    0x30(%rsp),%rsi
lea    0x20(%rsp),%rdx
call   1070 <generate_subkey@plt>
lea    0xf(%r12),%eax
lea    0x1e(%r12),%ecx
test   %eax,%eax
mov    %eax,%edx
cmovs  %ecx,%edx
sar    $0x4,%edx
cmp    $0x1f,%ecx
mov    $0x1,%ebp
cmovae %edx,%ebp
mov    %ebp,%edx
shl    $0x4,%edx
add    $0xfffffff0,%edx
movslq %edx,%rdi
add    %rbx,%rdi
cmp    $0x1f,%ecx
jb     11cd <AES_CMAC+0x6d>
mov    %r12d,%ecx
and    $0xf,%ecx
jne    11cd <AES_CMAC+0x6d>
lea    0x30(%rsp),%rsi
mov    %rsp,%rdx
jmp    11f5 <AES_CMAC+0x95>
test   %r12d,%r12d
cmovns %r12d,%eax
and    $0xfffffff0,%eax
sub    %eax,%r12d
lea    0x50(%rsp),%r13
mov    %r13,%rsi
mov    %r12d,%edx
call   1080 <padding@plt>
lea    0x20(%rsp),%rsi
mov    %rsp,%rdx
mov    %r13,%rdi
call   1030 <xor_128@plt>
lea    0x10(%rsp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1050 <memset@plt>
cmp    $0x2,%ebp
jl     1246 <AES_CMAC+0xe6>
add    $0xffffffff,%ebp
lea    0x10(%rsp),%r12
lea    0x40(%rsp),%r13
nopl   (%rax)
mov    %r12,%rdi
mov    %rbx,%rsi
mov    %r13,%rdx
call   1030 <xor_128@plt>
mov    %r15,%rdi
mov    %r13,%rsi
mov    %r12,%rdx
call   1040 <AES_128_ENC@plt>
add    $0x10,%rbx
add    $0xffffffffffffffff,%rbp
jne    1220 <AES_CMAC+0xc0>
lea    0x10(%rsp),%rbx
mov    %rsp,%rsi
lea    0x40(%rsp),%r12
mov    %rbx,%rdi
mov    %r12,%rdx
call   1030 <xor_128@plt>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %rbx,%rdx
call   1040 <AES_128_ENC@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x10,%edx
call   1060 <memcpy@plt>
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @AES_CMAC(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca [16 x i8]
%10 = alloca [16 x i8]
%11 = alloca [16 x i8]
%12 = alloca [16 x i8]
%13 = alloca [16 x i8]
%14 = alloca [16 x i8]
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i8* %3, i8** %8
%18 = load i8*, i8** %5
%19 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
%20 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%21 = call i32 @generate_subkey(i8* %18, i8* %19, i8* %20)
%22 = load i32, i32* %7
%23 = add nsw i32 %22, 15
%24 = sdiv i32 %23, 16
store i32 %24, i32* %15
%25 = load i32, i32* %15
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %4
store i32 1, i32* %15
store i32 0, i32* %17
br label %35
28: ; preds = %4
%29 = load i32, i32* %7
%30 = srem i32 %29, 16
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %28
store i32 1, i32* %17
br label %34
33: ; preds = %28
store i32 0, i32* %17
br label %34
34: ; preds = %33, %32
br label %35
35: ; preds = %34, %27
%36 = load i32, i32* %17
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %48
38: ; preds = %35
%39 = load i8*, i8** %6
%40 = load i32, i32* %15
%41 = sub nsw i32 %40, 1
%42 = mul nsw i32 16, %41
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %39, i64 %43
%45 = getelementptr inbounds [16 x i8], [16 x i8]* %13, i64 0, i64 0
%46 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%47 = call i32 @xor_128(i8* %44, i8* %45, i8* %46)
br label %63
48: ; preds = %35
%49 = load i8*, i8** %6
%50 = load i32, i32* %15
%51 = sub nsw i32 %50, 1
%52 = mul nsw i32 16, %51
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %49, i64 %53
%55 = getelementptr inbounds [16 x i8], [16 x i8]* %12, i64 0, i64 0
%56 = load i32, i32* %7
%57 = srem i32 %56, 16
%58 = call i32 @padding(i8* %54, i8* %55, i32 %57)
%59 = getelementptr inbounds [16 x i8], [16 x i8]* %12, i64 0, i64 0
%60 = getelementptr inbounds [16 x i8], [16 x i8]* %14, i64 0, i64 0
%61 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%62 = call i32 @xor_128(i8* %59, i8* %60, i8* %61)
br label %63
63: ; preds = %48, %38
%64 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%65 = call i32 @memset(i8* %64, i32 0, i32 16)
store i32 0, i32* %16
br label %66
66: ; preds = %84, %63
%67 = load i32, i32* %16
%68 = load i32, i32* %15
%69 = sub nsw i32 %68, 1
%70 = icmp slt i32 %67, %69
br i1 %70, label %71, label %87
71: ; preds = %66
%72 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%73 = load i8*, i8** %6
%74 = load i32, i32* %16
%75 = mul nsw i32 16, %74
%76 = sext i32 %75 to i64
%77 = getelementptr inbounds i8, i8* %73, i64 %76
%78 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%79 = call i32 @xor_128(i8* %72, i8* %77, i8* %78)
%80 = load i8*, i8** %5
%81 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%82 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%83 = call i32 @AES_128_ENC(i8* %80, i8* %81, i8* %82)
br label %84
84: ; preds = %71
%85 = load i32, i32* %16
%86 = add nsw i32 %85, 1
store i32 %86, i32* %16
br label %66
87: ; preds = %66
%88 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%89 = getelementptr inbounds [16 x i8], [16 x i8]* %11, i64 0, i64 0
%90 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%91 = call i32 @xor_128(i8* %88, i8* %89, i8* %90)
%92 = load i8*, i8** %5
%93 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%94 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%95 = call i32 @AES_128_ENC(i8* %92, i8* %93, i8* %94)
%96 = load i8*, i8** %8
%97 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%98 = call i32 @memcpy(i8* %96, i8* %97, i32 16)
ret void
}

LLVM IR (-O2):
define void @AES_CMAC(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca [16 x i8]
%6 = alloca [16 x i8]
%7 = alloca [16 x i8]
%8 = alloca [16 x i8]
%9 = alloca [16 x i8]
%10 = alloca [16 x i8]
%11 = getelementptr inbounds [16 x i8], [16 x i8]* %5, i64 0, i64 0
%12 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %7, i64 0, i64 0
%14 = getelementptr inbounds [16 x i8], [16 x i8]* %8, i64 0, i64 0
%15 = getelementptr inbounds [16 x i8], [16 x i8]* %9, i64 0, i64 0
%16 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%17 = call i32 @generate_subkey(i8* %0, i8* %15, i8* %16)
%18 = add nsw i32 %2, 15
%19 = sdiv i32 %18, 16
%20 = add i32 %2, 30
%21 = icmp ult i32 %20, 31
%22 = and i32 %2, 15
%23 = icmp ne i32 %22, 0
%24 = select i1 %21, i32 1, i32 %19
%25 = or i1 %21, %23
%26 = shl nsw i32 %24, 4
%27 = add i32 %26, -16
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %1, i64 %28
br i1 %25, label %32, label %30
30: ; preds = %4
%31 = call i32 @xor_128(i8* %29, i8* %15, i8* %13)
br label %36
32: ; preds = %4
%33 = srem i32 %2, 16
%34 = call i32 @padding(i8* %29, i8* %14, i32 %33)
%35 = call i32 @xor_128(i8* %14, i8* %16, i8* %13)
br label %36
36: ; preds = %32, %30
%37 = call i32 @memset(i8* %11, i32 0, i32 16)
%38 = icmp sgt i32 %24, 1
br i1 %38, label %39, label %50
39: ; preds = %36
%40 = add nsw i32 %24, -1
%41 = zext i32 %40 to i64
br label %42
42: ; preds = %39, %42
%43 = phi i64 [ 0, %39 ], [ %48, %42 ]
%44 = shl nsw i64 %43, 4
%45 = getelementptr inbounds i8, i8* %1, i64 %44
%46 = call i32 @xor_128(i8* %11, i8* %45, i8* %12)
%47 = call i32 @AES_128_ENC(i8* %0, i8* %12, i8* %11)
%48 = add nuw nsw i64 %43, 1
%49 = icmp eq i64 %48, %41
br i1 %49, label %50, label %42
50: ; preds = %42, %36
%51 = call i32 @xor_128(i8* %11, i8* %13, i8* %12)
%52 = call i32 @AES_128_ENC(i8* %0, i8* %12, i8* %11)
%53 = call i32 @memcpy(i8* %3, i8* %11, i32 16)
ret void
}