#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 */
typedef int uint8_t;
typedef int uint32_t;
struct macan_key {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int htobe32(unsigned int);
int /*<<< orphan*/ macan_aes_encrypt(struct macan_key const *, unsigned int,
                                     int *, int *);
int /*<<< orphan*/ memcpy(int *, int const *, int);
void macan_aes_wrap(const struct macan_key *key, size_t length, uint8_t *dst,
                    const uint8_t *src) {
  uint8_t a[8] = {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
  uint8_t b[16];
  size_t block_size = 16;
  size_t t, n;
  uint32_t *af;
  unsigned i, j;
  assert((length % 8) == 0);
  memcpy(dst + 8, src, length);
  n = length / 8;
  for (j = 0; j < 6; j++) {
    for (i = 1; i < n + 1; i++) {
      memcpy(b, a, 8);
      memcpy(b + 8, dst + (8 * i), 8);
      macan_aes_encrypt(key, (unsigned)block_size, b, b);
      memcpy(a, b, 8);
      t = (n * j) + i;
      af = (uint32_t *)(a + 4);
      *af = *af ^ htobe32((unsigned)t);
      memcpy(dst + (8 * i), b + 8, 8);
    }
  }
  memcpy(dst, a, 8);
}


x86 assembly (clang, -O0):
<macan_aes_wrap>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    0xe9e(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe9b(%rip),%rax
mov    %rax,-0x38(%rbp)
mov    0xe98(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    0xe95(%rip),%rax
mov    %rax,-0x28(%rbp)
movq   $0x10,-0x88(%rbp)
mov    -0x10(%rbp),%rax
and    $0x7,%rax
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1030 <assert@plt>
mov    -0x18(%rbp),%rdi
add    $0x20,%rdi
mov    -0x20(%rbp),%rsi
mov    -0x10(%rbp),%rax
mov    %eax,%edx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
shr    $0x3,%rax
mov    %rax,-0x98(%rbp)
movl   $0x0,-0xa8(%rbp)
cmpl   $0x6,-0xa8(%rbp)
jae    1325 <macan_aes_wrap+0x1e5>
movl   $0x1,-0xa4(%rbp)
mov    -0xa4(%rbp),%eax
mov    -0x98(%rbp),%rcx
add    $0x1,%rcx
cmp    %rcx,%rax
jae    130c <macan_aes_wrap+0x1cc>
lea    -0x80(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
lea    -0x80(%rbp),%rdi
add    $0x20,%rdi
mov    -0x18(%rbp),%rsi
mov    -0xa4(%rbp),%eax
shl    $0x3,%eax
mov    %eax,%eax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x88(%rbp),%rax
mov    %eax,%esi
lea    -0x80(%rbp),%rdx
lea    -0x80(%rbp),%rcx
call   1060 <macan_aes_encrypt@plt>
lea    -0x40(%rbp),%rdi
lea    -0x80(%rbp),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    -0x98(%rbp),%rax
mov    -0xa8(%rbp),%ecx
imul   %rcx,%rax
mov    -0xa4(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x90(%rbp)
lea    -0x40(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0xa0(%rbp)
mov    -0xa0(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0xac(%rbp)
mov    -0x90(%rbp),%rax
mov    %eax,%edi
call   1050 <htobe32@plt>
mov    -0xac(%rbp),%ecx
xor    %eax,%ecx
mov    -0xa0(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rdi
mov    -0xa4(%rbp),%eax
shl    $0x3,%eax
mov    %eax,%eax
shl    $0x2,%rax
add    %rax,%rdi
lea    -0x80(%rbp),%rsi
add    $0x20,%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    -0xa4(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa4(%rbp)
jmp    11f2 <macan_aes_wrap+0xb2>
jmp    1311 <macan_aes_wrap+0x1d1>
mov    -0xa8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xa8(%rbp)
jmp    11db <macan_aes_wrap+0x9b>
mov    -0x18(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<macan_aes_wrap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %rcx,%r14
mov    %rdx,%rbx
mov    %rsi,%rbp
mov    %rdi,0x30(%rsp)
movaps 0xeaa(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movaps 0xe8e(%rip),%xmm0
movaps %xmm0,0x40(%rsp)
xor    %edi,%edi
test   $0x7,%bpl
sete   %dil
call   1030 <assert@plt>
mov    %rbx,%r15
lea    0x20(%rbx),%rdi
mov    %r14,%rsi
mov    %ebp,%edx
call   1040 <memcpy@plt>
mov    %rbp,0x28(%rsp)
mov    %rbp,%rax
shr    $0x3,%rax
lea    0x80(%rsp),%r12
xor    %ecx,%ecx
mov    $0x1,%edx
lea    0x60(%rsp),%rbx
lea    0x40(%rsp),%r14
mov    %rax,0x20(%rsp)
mov    %r15,0x18(%rsp)
jmp    11e7 <macan_aes_wrap+0xa7>
nopl   0x0(%rax,%rax,1)
mov    0xc(%rsp),%ecx
add    $0x1,%ecx
mov    0x10(%rsp),%rdx
add    %eax,%edx
cmp    $0x6,%ecx
je     12b5 <macan_aes_wrap+0x175>
mov    %rdx,0x10(%rsp)
mov    %ecx,0xc(%rsp)
cmpq   $0x8,0x28(%rsp)
jb     11d0 <macan_aes_wrap+0x90>
mov    $0x2,%r13d
mov    $0x8,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    %ebp,%eax
mov    0x18(%rsp),%rcx
lea    (%rcx,%rax,4),%rsi
mov    %rsi,0x38(%rsp)
mov    %r12,%rdi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    0x30(%rsp),%rdi
mov    $0x10,%esi
mov    %rbx,%rdx
mov    %rbx,%rcx
call   1060 <macan_aes_encrypt@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    0x50(%rsp),%r15d
mov    0x10(%rsp),%rax
lea    (%rax,%r13,1),%edi
add    $0xfffffffe,%edi
call   1050 <htobe32@plt>
xor    %r15d,%eax
mov    %eax,0x50(%rsp)
mov    0x38(%rsp),%rdi
mov    0x18(%rsp),%r15
mov    %r12,%rsi
mov    $0x8,%edx
call   1040 <memcpy@plt>
mov    %r13d,%eax
add    $0x1,%r13d
add    $0x8,%ebp
cmp    %rax,0x20(%rsp)
mov    0x20(%rsp),%rax
jae    1210 <macan_aes_wrap+0xd0>
jmp    11d0 <macan_aes_wrap+0x90>
lea    0x40(%rsp),%rsi
mov    %r15,%rdi
mov    $0x8,%edx
call   1040 <memcpy@plt>
add    $0xa8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.macan_key = type { i32 }
@__const.macan_aes_wrap.a = [8 x i32] [i32 166, i32 166, i32 166, i32 166, i32 166, i32 166, i32 166, i32 166]
define void @macan_aes_wrap(%struct.macan_key* %0, i64 %1, i32* %2, i32* %3) {
%5 = alloca [8 x i32]
%6 = alloca [16 x i32]
%7 = bitcast [8 x i32]* %5 to i8*
%8 = bitcast [16 x i32]* %6 to i8*
%9 = and i64 %1, 7
%10 = icmp eq i64 %9, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = getelementptr inbounds i32, i32* %2, i64 8
%14 = trunc i64 %1 to i32
%15 = tail call i32 @memcpy(i32* %13, i32* %3, i32 %14)
%16 = lshr i64 %1, 3
%17 = icmp ult i64 %1, 8
%18 = getelementptr inbounds [16 x i32], [16 x i32]* %6, i64 0, i64 0
%19 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 0
%20 = getelementptr inbounds [16 x i32], [16 x i32]* %6, i64 0, i64 8
%21 = getelementptr inbounds [8 x i32], [8 x i32]* %5, i64 0, i64 4
%22 = trunc i64 %16 to i32
br label %23
23: ; preds = %4, %44
%24 = phi i32 [ 0, %4 ], [ %45, %44 ]
br i1 %17, label %44, label %25
25: ; preds = %23
%26 = mul i32 %24, %22
br label %27
27: ; preds = %25, %27
%28 = phi i32 [ 1, %25 ], [ %41, %27 ]
%29 = call i32 @memcpy(i32* %18, i32* %19, i32 8)
%30 = shl i32 %28, 3
%31 = zext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %2, i64 %31
%33 = call i32 @memcpy(i32* %20, i32* %32, i32 8)
%34 = call i32 @macan_aes_encrypt(%struct.macan_key* %0, i32 16, i32* %18, i32* %18)
%35 = call i32 @memcpy(i32* %19, i32* %18, i32 8)
%36 = load i32, i32* %21
%37 = add i32 %28, %26
%38 = call i32 @htobe32(i32 %37)
%39 = xor i32 %38, %36
store i32 %39, i32* %21
%40 = call i32 @memcpy(i32* %32, i32* %20, i32 8)
%41 = add i32 %28, 1
%42 = zext i32 %41 to i64
%43 = icmp ult i64 %16, %42
br i1 %43, label %44, label %27
44: ; preds = %27, %23
%45 = add nuw nsw i32 %24, 1
%46 = icmp eq i32 %45, 6
br i1 %46, label %47, label %23
47: ; preds = %44
%48 = call i32 @memcpy(i32* %2, i32* %19, i32 8)
ret void
}