#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(int);
int /*<<< orphan*/ macan_aes_decrypt(struct macan_key const *, unsigned int,
                                     int *, int *);
int /*<<< orphan*/ memchk(int *, int *, int);
int /*<<< orphan*/ memcpy(int *, int *, size_t);
int /*<<< orphan*/ memmove(int *, int *, size_t);
int /*<<< orphan*/ memset(int *, int /*<<< orphan*/, size_t);
int macan_aes_unwrap(const struct macan_key *key, size_t length, uint8_t *dst,
                     uint8_t *src, uint8_t *tmp) {
  uint8_t iv[8] = {0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6, 0xa6};
  uint8_t b[16];
  size_t block_size = 16;
  uint32_t t, n;
  unsigned i;
  int j, k;
  assert((length % 8) == 0 && length > 0);
  memmove(tmp, src, length);
  n = (uint32_t)length / 8 - 1;
  for (j = 5; j >= 0; j--) {
    for (i = n; i > 0; i--) {
      t = (n * (uint32_t)j) + i;
      t = htobe32(t);
      for (k = 0; k < 4; k++) {
        *(tmp + 4 + k) = *(tmp + 4 + k) ^ *(((uint8_t *)&t) + k);
      }
      memcpy(b, tmp, 8);
      memcpy(b + 8, tmp + (8 * i), 8);
      macan_aes_decrypt(key, (unsigned)block_size, b, b);
      memcpy(tmp, b, 8);
      memcpy(tmp + (8 * i), b + 8, 8);
    }
  }
  if (!memchk(tmp, iv, 8)) {
    memset(dst, 0, length - 8);
    return 1;
  } else {
    memcpy(dst, tmp + 8, length - 8);
    return 0;
  }
}


x86 assembly (clang, -O0):
<macan_aes_unwrap>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    0xe6a(%rip),%rax
mov    %rax,-0x50(%rbp)
mov    0xe67(%rip),%rax
mov    %rax,-0x48(%rbp)
mov    0xe64(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0xe61(%rip),%rax
mov    %rax,-0x38(%rbp)
movq   $0x10,-0x98(%rbp)
mov    -0x18(%rbp),%rcx
and    $0x7,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0xad(%rbp)
jne    11ee <macan_aes_unwrap+0x7e>
cmpq   $0x0,-0x18(%rbp)
seta   %al
mov    %al,-0xad(%rbp)
mov    -0xad(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    -0x30(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1080 <memmove@plt>
mov    -0x18(%rbp),%rax
mov    $0x8,%ecx
cltd
idiv   %ecx
sub    $0x1,%eax
mov    %eax,-0xa0(%rbp)
movl   $0x5,-0xa8(%rbp)
cmpl   $0x0,-0xa8(%rbp)
jl     13a2 <macan_aes_unwrap+0x232>
mov    -0xa0(%rbp),%eax
mov    %eax,-0xa4(%rbp)
cmpl   $0x0,-0xa4(%rbp)
jbe    1389 <macan_aes_unwrap+0x219>
mov    -0xa0(%rbp),%eax
imul   -0xa8(%rbp),%eax
add    -0xa4(%rbp),%eax
mov    %eax,-0x9c(%rbp)
mov    -0x9c(%rbp),%edi
call   1070 <htobe32@plt>
mov    %eax,-0x9c(%rbp)
movl   $0x0,-0xac(%rbp)
cmpl   $0x4,-0xac(%rbp)
jge    12d5 <macan_aes_unwrap+0x165>
mov    -0x30(%rbp),%rax
movslq -0xac(%rbp),%rcx
mov    0x10(%rax,%rcx,4),%edx
movslq -0xac(%rbp),%rax
xor    -0x9c(%rbp,%rax,4),%edx
mov    -0x30(%rbp),%rax
movslq -0xac(%rbp),%rcx
mov    %edx,0x10(%rax,%rcx,4)
mov    -0xac(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xac(%rbp)
jmp    1288 <macan_aes_unwrap+0x118>
lea    -0x90(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
lea    -0x90(%rbp),%rdi
add    $0x20,%rdi
mov    -0x30(%rbp),%rsi
mov    -0xa4(%rbp),%eax
shl    $0x3,%eax
mov    %eax,%eax
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    -0x10(%rbp),%rdi
mov    -0x98(%rbp),%rax
mov    %eax,%esi
lea    -0x90(%rbp),%rdx
lea    -0x90(%rbp),%rcx
call   1040 <macan_aes_decrypt@plt>
mov    -0x30(%rbp),%rdi
lea    -0x90(%rbp),%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    -0xa4(%rbp),%eax
shl    $0x3,%eax
mov    %eax,%eax
shl    $0x2,%rax
add    %rax,%rdi
lea    -0x90(%rbp),%rsi
add    $0x20,%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    -0xa4(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xa4(%rbp)
jmp    1247 <macan_aes_unwrap+0xd7>
jmp    138e <macan_aes_unwrap+0x21e>
mov    -0xa8(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xa8(%rbp)
jmp    122e <macan_aes_unwrap+0xbe>
mov    -0x30(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    $0x8,%edx
call   1090 <memchk@plt>
cmp    $0x0,%eax
jne    13dc <macan_aes_unwrap+0x26c>
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    $0x8,%rdx
xor    %esi,%esi
call   1030 <memset@plt>
movl   $0x1,-0x4(%rbp)
jmp    13fc <macan_aes_unwrap+0x28c>
mov    -0x20(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x20,%rsi
mov    -0x18(%rbp),%rdx
sub    $0x8,%rdx
call   1060 <memcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<macan_aes_unwrap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xa8,%rsp
mov    %r8,%rbx
mov    %rcx,%r14
mov    %rdx,0x10(%rsp)
mov    %rsi,%rbp
mov    %rdi,0x38(%rsp)
movaps 0xe75(%rip),%xmm0
movaps %xmm0,0x50(%rsp)
movdqa 0xe58(%rip),%xmm0
movdqa %xmm0,0x40(%rsp)
test   $0x7,%bpl
sete   %al
test   %rsi,%rsi
setne  %cl
and    %al,%cl
movzbl %cl,%edi
call   1050 <assert@plt>
mov    %rbx,0x20(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rbp,%rdx
call   1080 <memmove@plt>
lea    0x7(%rbp),%eax
test   %ebp,%ebp
mov    %rbp,0x18(%rsp)
cmovns %ebp,%eax
sar    $0x3,%eax
lea    -0x1(%rax),%ecx
mov    %rcx,0x28(%rsp)
lea    0x0(,%rcx,8),%rcx
mov    %rcx,0x30(%rsp)
lea    (%rax,%rax,2),%ecx
add    %ecx,%ecx
add    $0xfffffffa,%ecx
mov    %ecx,0x4(%rsp)
mov    $0x1,%ecx
sub    %eax,%ecx
mov    %ecx,0x8(%rsp)
mov    $0x5,%eax
lea    0x60(%rsp),%rbx
mov    0x20(%rsp),%rbp
jmp    1245 <macan_aes_unwrap+0xd5>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x8(%rsp),%eax
add    %eax,0x4(%rsp)
mov    0xc(%rsp),%eax
sub    $0x1,%eax
jb     1316 <macan_aes_unwrap+0x1a6>
mov    %eax,0xc(%rsp)
cmpl   $0x0,0x28(%rsp)
je     1230 <macan_aes_unwrap+0xc0>
mov    0x28(%rsp),%rax
mov    %eax,%r12d
mov    0x4(%rsp),%r14d
mov    0x30(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14d,%edi
call   1070 <htobe32@plt>
movdqu 0x10(%rbp),%xmm0
movd   %eax,%xmm1
pshufd $0x0,%xmm1,%xmm1
pxor   %xmm0,%xmm1
movdqu %xmm1,0x10(%rbp)
mov    $0x8,%edx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1060 <memcpy@plt>
mov    %r13d,%eax
and    $0xfffffff8,%eax
lea    0x0(,%rax,4),%r15
add    %rbp,%r15
mov    $0x8,%edx
lea    0x80(%rsp),%rdi
mov    %r15,%rsi
call   1060 <memcpy@plt>
mov    0x38(%rsp),%rdi
mov    $0x10,%esi
mov    %rbx,%rdx
mov    %rbx,%rcx
call   1040 <macan_aes_decrypt@plt>
mov    $0x8,%edx
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1060 <memcpy@plt>
mov    $0x8,%edx
mov    %r15,%rdi
lea    0x80(%rsp),%rsi
call   1060 <memcpy@plt>
add    $0xfffffffffffffff8,%r13
add    $0xffffffff,%r14d
add    $0xffffffff,%r12d
jne    1270 <macan_aes_unwrap+0x100>
jmp    1230 <macan_aes_unwrap+0xc0>
lea    0x40(%rsp),%rsi
mov    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x8,%edx
call   1090 <memchk@plt>
test   %eax,%eax
je     134f <macan_aes_unwrap+0x1df>
add    $0x20,%rbx
mov    0x18(%rsp),%rdx
add    $0xfffffffffffffff8,%rdx
mov    0x10(%rsp),%rdi
mov    %rbx,%rsi
call   1060 <memcpy@plt>
xor    %eax,%eax
jmp    1369 <macan_aes_unwrap+0x1f9>
mov    0x18(%rsp),%rdx
add    $0xfffffffffffffff8,%rdx
mov    0x10(%rsp),%rdi
xor    %esi,%esi
call   1030 <memset@plt>
mov    $0x1,%eax
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_unwrap.iv = [8 x i32] [i32 166, i32 166, i32 166, i32 166, i32 166, i32 166, i32 166, i32 166]
define i32 @macan_aes_unwrap(%struct.macan_key* %0, i64 %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca i32
%7 = alloca %struct.macan_key*
%8 = alloca i64
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca [8 x i32]
%13 = alloca [16 x i32]
%14 = alloca i64
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca i32
store %struct.macan_key* %0, %struct.macan_key** %7
store i64 %1, i64* %8
store i32* %2, i32** %9
store i32* %3, i32** %10
store i32* %4, i32** %11
%20 = bitcast [8 x i32]* %12 to i8*
store i64 16, i64* %14
%21 = load i64, i64* %8
%22 = urem i64 %21, 8
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %27
24: ; preds = %5
%25 = load i64, i64* %8
%26 = icmp ugt i64 %25, 0
br label %27
27: ; preds = %24, %5
%28 = phi i1 [ false, %5 ], [ %26, %24 ]
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i32*, i32** %11
%32 = load i32*, i32** %10
%33 = load i64, i64* %8
%34 = call i32 @memmove(i32* %31, i32* %32, i64 %33)
%35 = load i64, i64* %8
%36 = trunc i64 %35 to i32
%37 = sdiv i32 %36, 8
%38 = sub nsw i32 %37, 1
store i32 %38, i32* %16
store i32 5, i32* %18
br label %39
39: ; preds = %111, %27
%40 = load i32, i32* %18
%41 = icmp sge i32 %40, 0
br i1 %41, label %42, label %114
42: ; preds = %39
%43 = load i32, i32* %16
store i32 %43, i32* %17
br label %44
44: ; preds = %107, %42
%45 = load i32, i32* %17
%46 = icmp ugt i32 %45, 0
br i1 %46, label %47, label %110
47: ; preds = %44
%48 = load i32, i32* %16
%49 = load i32, i32* %18
%50 = mul nsw i32 %48, %49
%51 = load i32, i32* %17
%52 = add i32 %50, %51
store i32 %52, i32* %15
%53 = load i32, i32* %15
%54 = call i32 @htobe32(i32 %53)
store i32 %54, i32* %15
store i32 0, i32* %19
br label %55
55: ; preds = %75, %47
%56 = load i32, i32* %19
%57 = icmp slt i32 %56, 4
br i1 %57, label %58, label %78
58: ; preds = %55
%59 = load i32*, i32** %11
%60 = getelementptr inbounds i32, i32* %59, i64 4
%61 = load i32, i32* %19
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i32, i32* %60, i64 %62
%64 = load i32, i32* %63
%65 = load i32, i32* %19
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds i32, i32* %15, i64 %66
%68 = load i32, i32* %67
%69 = xor i32 %64, %68
%70 = load i32*, i32** %11
%71 = getelementptr inbounds i32, i32* %70, i64 4
%72 = load i32, i32* %19
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i32, i32* %71, i64 %73
store i32 %69, i32* %74
br label %75
75: ; preds = %58
%76 = load i32, i32* %19
%77 = add nsw i32 %76, 1
store i32 %77, i32* %19
br label %55
78: ; preds = %55
%79 = getelementptr inbounds [16 x i32], [16 x i32]* %13, i64 0, i64 0
%80 = load i32*, i32** %11
%81 = call i32 @memcpy(i32* %79, i32* %80, i64 8)
%82 = getelementptr inbounds [16 x i32], [16 x i32]* %13, i64 0, i64 0
%83 = getelementptr inbounds i32, i32* %82, i64 8
%84 = load i32*, i32** %11
%85 = load i32, i32* %17
%86 = mul i32 8, %85
%87 = zext i32 %86 to i64
%88 = getelementptr inbounds i32, i32* %84, i64 %87
%89 = call i32 @memcpy(i32* %83, i32* %88, i64 8)
%90 = load %struct.macan_key*, %struct.macan_key** %7
%91 = load i64, i64* %14
%92 = trunc i64 %91 to i32
%93 = getelementptr inbounds [16 x i32], [16 x i32]* %13, i64 0, i64 0
%94 = getelementptr inbounds [16 x i32], [16 x i32]* %13, i64 0, i64 0
%95 = call i32 @macan_aes_decrypt(%struct.macan_key* %90, i32 %92, i32* %93, i32* %94)
%96 = load i32*, i32** %11
%97 = getelementptr inbounds [16 x i32], [16 x i32]* %13, i64 0, i64 0
%98 = call i32 @memcpy(i32* %96, i32* %97, i64 8)
%99 = load i32*, i32** %11
%100 = load i32, i32* %17
%101 = mul i32 8, %100
%102 = zext i32 %101 to i64
%103 = getelementptr inbounds i32, i32* %99, i64 %102
%104 = getelementptr inbounds [16 x i32], [16 x i32]* %13, i64 0, i64 0
%105 = getelementptr inbounds i32, i32* %104, i64 8
%106 = call i32 @memcpy(i32* %103, i32* %105, i64 8)
br label %107
107: ; preds = %78
%108 = load i32, i32* %17
%109 = add i32 %108, -1
store i32 %109, i32* %17
br label %44
110: ; preds = %44
br label %111
111: ; preds = %110
%112 = load i32, i32* %18
%113 = add nsw i32 %112, -1
store i32 %113, i32* %18
br label %39
114: ; preds = %39
%115 = load i32*, i32** %11
%116 = getelementptr inbounds [8 x i32], [8 x i32]* %12, i64 0, i64 0
%117 = call i32 @memchk(i32* %115, i32* %116, i32 8)
%118 = icmp ne i32 %117, 0
br i1 %118, label %124, label %119
119: ; preds = %114
%120 = load i32*, i32** %9
%121 = load i64, i64* %8
%122 = sub i64 %121, 8
%123 = call i32 @memset(i32* %120, i32 0, i64 %122)
store i32 1, i32* %6
br label %131
124: ; preds = %114
%125 = load i32*, i32** %9
%126 = load i32*, i32** %11
%127 = getelementptr inbounds i32, i32* %126, i64 8
%128 = load i64, i64* %8
%129 = sub i64 %128, 8
%130 = call i32 @memcpy(i32* %125, i32* %127, i64 %129)
store i32 0, i32* %6
br label %131
131: ; preds = %124, %119
%132 = load i32, i32* %6
ret i32 %132
}

LLVM IR (-O2):
%struct.macan_key = type { i32 }
@__const.macan_aes_unwrap.iv = [8 x i32] [i32 166, i32 166, i32 166, i32 166, i32 166, i32 166, i32 166, i32 166]
define i32 @macan_aes_unwrap(%struct.macan_key* %0, i64 %1, i32* %2, i32* %3, i32* %4) {
%6 = alloca [8 x i32]
%7 = alloca [16 x i32]
%8 = bitcast [8 x i32]* %6 to i8*
%9 = bitcast [16 x i32]* %7 to i8*
%10 = and i64 %1, 7
%11 = icmp eq i64 %10, 0
%12 = icmp ne i64 %1, 0
%13 = and i1 %12, %11
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i32 @memmove(i32* %4, i32* %3, i64 %1)
%17 = trunc i64 %1 to i32
%18 = sdiv i32 %17, 8
%19 = add nsw i32 %18, -1
%20 = icmp eq i32 %19, 0
%21 = getelementptr inbounds i32, i32* %4, i64 4
%22 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 0
%23 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 8
%24 = zext i32 %19 to i64
%25 = bitcast i32* %21 to <4 x i32>*
%26 = bitcast i32* %21 to <4 x i32>*
br label %27
27: ; preds = %5, %51
%28 = phi i32 [ 5, %5 ], [ %52, %51 ]
br i1 %20, label %51, label %29
29: ; preds = %27
%30 = mul nsw i32 %28, %19
br label %31
31: ; preds = %29, %31
%32 = phi i64 [ %24, %29 ], [ %48, %31 ]
%33 = trunc i64 %32 to i32
%34 = add i32 %30, %33
%35 = call i32 @htobe32(i32 %34)
%36 = load <4 x i32>, <4 x i32>* %25
%37 = insertelement <4 x i32> poison, i32 %35, i64 0
%38 = shufflevector <4 x i32> %37, <4 x i32> poison, <4 x i32> zeroinitializer
%39 = xor <4 x i32> %36, %38
store <4 x i32> %39, <4 x i32>* %26
%40 = call i32 @memcpy(i32* %22, i32* %4, i64 8)
%41 = shl i64 %32, 3
%42 = and i64 %41, 4294967288
%43 = getelementptr inbounds i32, i32* %4, i64 %42
%44 = call i32 @memcpy(i32* %23, i32* %43, i64 8)
%45 = call i32 @macan_aes_decrypt(%struct.macan_key* %0, i32 16, i32* %22, i32* %22)
%46 = call i32 @memcpy(i32* %4, i32* %22, i64 8)
%47 = call i32 @memcpy(i32* %43, i32* %23, i64 8)
%48 = add nsw i64 %32, -1
%49 = trunc i64 %48 to i32
%50 = icmp eq i32 %49, 0
br i1 %50, label %51, label %31
51: ; preds = %31, %27
%52 = add nsw i32 %28, -1
%53 = icmp eq i32 %28, 0
br i1 %53, label %54, label %27
54: ; preds = %51
%55 = getelementptr inbounds [8 x i32], [8 x i32]* %6, i64 0, i64 0
%56 = call i32 @memchk(i32* %4, i32* %55, i32 8)
%57 = icmp eq i32 %56, 0
br i1 %57, label %58, label %61
58: ; preds = %54
%59 = add i64 %1, -8
%60 = call i32 @memset(i32* %2, i32 0, i64 %59)
br label %65
61: ; preds = %54
%62 = getelementptr inbounds i32, i32* %4, i64 8
%63 = add i64 %1, -8
%64 = call i32 @memcpy(i32* %2, i32* %62, i64 %63)
br label %65
65: ; preds = %61, %58
%66 = phi i32 [ 0, %61 ], [ 1, %58 ]
ret i32 %66
}