#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 /*<<< orphan*/ AES_KEY;

/* Variables and functions */
scalar_t__ AES_set_decrypt_key(unsigned char const *, int,
                               int /*<<< orphan*/ *);
scalar_t__ AES_set_encrypt_key(unsigned char const *, int,
                               int /*<<< orphan*/ *);
int AES_unwrap_key(int /*<<< orphan*/ *, unsigned char const *, unsigned char *,
                   unsigned char *, int);
int AES_wrap_key(int /*<<< orphan*/ *, unsigned char const *, unsigned char *,
                 unsigned char const *, int);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(int);
scalar_t__ memcmp(unsigned char const *, unsigned char *, int);
int AES_wrap_unwrap_test(const unsigned char *kek, int keybits,
                         const unsigned char *iv, const unsigned char *eout,
                         const unsigned char *key, int keylen) {
  unsigned char *otmp = NULL, *ptmp = NULL;
  int r, ret = 0;
  AES_KEY wctx;
  otmp = malloc(keylen + 8);
  ptmp = malloc(keylen);
  if (otmp == NULL || ptmp == NULL)
    goto err;
  if (AES_set_encrypt_key(kek, keybits, &wctx))
    goto err;
  r = AES_wrap_key(&wctx, iv, otmp, key, keylen);
  if (r <= 0)
    goto err;
  if (eout && memcmp(eout, otmp, keylen))
    goto err;
  if (AES_set_decrypt_key(kek, keybits, &wctx))
    goto err;
  r = AES_unwrap_key(&wctx, iv, ptmp, otmp, r);
  if (memcmp(key, ptmp, keylen))
    goto err;
  ret = 1;
err:
  free(otmp);
  free(ptmp);
  return ret;
}


x86 assembly (clang, -O0):
<AES_wrap_unwrap_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9d,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
movq   $0x0,-0x40(%rbp)
movl   $0x0,-0x48(%rbp)
mov    -0x2c(%rbp),%edi
add    $0x8,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x38(%rbp)
je     11d7 <AES_wrap_unwrap_test+0x67>
cmpq   $0x0,-0x40(%rbp)
jne    11dc <AES_wrap_unwrap_test+0x6c>
jmp    12b1 <AES_wrap_unwrap_test+0x141>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
lea    -0x4c(%rbp),%rdx
call   1040 <AES_set_encrypt_key@plt>
cmp    $0x0,%rax
je     11fb <AES_wrap_unwrap_test+0x8b>
jmp    12b1 <AES_wrap_unwrap_test+0x141>
mov    -0x18(%rbp),%rsi
mov    -0x38(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
lea    -0x4c(%rbp),%rdi
call   1090 <AES_wrap_key@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0x0,-0x44(%rbp)
jg     1226 <AES_wrap_unwrap_test+0xb6>
jmp    12b1 <AES_wrap_unwrap_test+0x141>
cmpq   $0x0,-0x20(%rbp)
je     1250 <AES_wrap_unwrap_test+0xe0>
mov    -0x20(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     1250 <AES_wrap_unwrap_test+0xe0>
jmp    12b1 <AES_wrap_unwrap_test+0x141>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
lea    -0x4c(%rbp),%rdx
call   1070 <AES_set_decrypt_key@plt>
cmp    $0x0,%rax
je     126f <AES_wrap_unwrap_test+0xff>
jmp    12b1 <AES_wrap_unwrap_test+0x141>
mov    -0x18(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x38(%rbp),%rcx
mov    -0x44(%rbp),%r8d
lea    -0x4c(%rbp),%rdi
call   1080 <AES_unwrap_key@plt>
mov    %eax,-0x44(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     12aa <AES_wrap_unwrap_test+0x13a>
jmp    12b1 <AES_wrap_unwrap_test+0x141>
movl   $0x1,-0x48(%rbp)
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<AES_wrap_unwrap_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9d,%ebx
mov    %r8,0x18(%rsp)
mov    %rcx,0x20(%rsp)
mov    %rdx,0x10(%rsp)
mov    %esi,%r12d
mov    %rdi,%r13
lea    0x8(%rbx),%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %ebx,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
xor    %ebp,%ebp
test   %r15,%r15
je     11cc <AES_wrap_unwrap_test+0x5c>
test   %r14,%r14
je     11cc <AES_wrap_unwrap_test+0x5c>
lea    0x8(%rsp),%rdx
mov    %r13,%rdi
mov    %r12d,%esi
call   1040 <AES_set_encrypt_key@plt>
test   %rax,%rax
je     11ed <AES_wrap_unwrap_test+0x7d>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0x8(%rsp),%rdi
mov    0x10(%rsp),%rsi
mov    %r15,%rdx
mov    0x18(%rsp),%rcx
mov    %ebx,%r8d
call   1090 <AES_wrap_key@plt>
test   %eax,%eax
jle    11cc <AES_wrap_unwrap_test+0x5c>
mov    %eax,0xc(%rsp)
mov    0x20(%rsp),%rdi
test   %rdi,%rdi
je     1228 <AES_wrap_unwrap_test+0xb8>
mov    %r15,%rsi
mov    %ebx,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
jne    11cc <AES_wrap_unwrap_test+0x5c>
lea    0x8(%rsp),%rdx
mov    %r13,%rdi
mov    %r12d,%esi
call   1070 <AES_set_decrypt_key@plt>
test   %rax,%rax
jne    11cc <AES_wrap_unwrap_test+0x5c>
lea    0x8(%rsp),%rdi
mov    0x10(%rsp),%rsi
mov    %r14,%rdx
mov    %r15,%rcx
mov    0xc(%rsp),%r8d
call   1080 <AES_unwrap_key@plt>
mov    0x18(%rsp),%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1050 <memcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
jmp    11cc <AES_wrap_unwrap_test+0x5c>

LLVM IR (-O0):
define i32 @AES_wrap_unwrap_test(i8* %0, i32 %1, i8* %2, i8* %3, i8* %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i8* %4, i8** %11
store i32 %5, i32* %12
store i8* null, i8** %13
store i8* null, i8** %14
store i32 0, i32* %16
%18 = load i32, i32* %12
%19 = add nsw i32 %18, 8
%20 = call i8* @malloc(i32 %19)
store i8* %20, i8** %13
%21 = load i32, i32* %12
%22 = call i8* @malloc(i32 %21)
store i8* %22, i8** %14
%23 = load i8*, i8** %13
%24 = icmp eq i8* %23, null
br i1 %24, label %28, label %25
25: ; preds = %6
%26 = load i8*, i8** %14
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %29
28: ; preds = %25, %6
br label %73
29: ; preds = %25
%30 = load i8*, i8** %7
%31 = load i32, i32* %8
%32 = call i64 @AES_set_encrypt_key(i8* %30, i32 %31, i32* %17)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %29
br label %73
35: ; preds = %29
%36 = load i8*, i8** %9
%37 = load i8*, i8** %13
%38 = load i8*, i8** %11
%39 = load i32, i32* %12
%40 = call i32 @AES_wrap_key(i32* %17, i8* %36, i8* %37, i8* %38, i32 %39)
store i32 %40, i32* %15
%41 = load i32, i32* %15
%42 = icmp sle i32 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %35
br label %73
44: ; preds = %35
%45 = load i8*, i8** %10
%46 = icmp ne i8* %45, null
br i1 %46, label %47, label %54
47: ; preds = %44
%48 = load i8*, i8** %10
%49 = load i8*, i8** %13
%50 = load i32, i32* %12
%51 = call i64 @memcmp(i8* %48, i8* %49, i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %47
br label %73
54: ; preds = %47, %44
%55 = load i8*, i8** %7
%56 = load i32, i32* %8
%57 = call i64 @AES_set_decrypt_key(i8* %55, i32 %56, i32* %17)
%58 = icmp ne i64 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %54
br label %73
60: ; preds = %54
%61 = load i8*, i8** %9
%62 = load i8*, i8** %14
%63 = load i8*, i8** %13
%64 = load i32, i32* %15
%65 = call i32 @AES_unwrap_key(i32* %17, i8* %61, i8* %62, i8* %63, i32 %64)
store i32 %65, i32* %15
%66 = load i8*, i8** %11
%67 = load i8*, i8** %14
%68 = load i32, i32* %12
%69 = call i64 @memcmp(i8* %66, i8* %67, i32 %68)
%70 = icmp ne i64 %69, 0
br i1 %70, label %71, label %72
71: ; preds = %60
br label %73
72: ; preds = %60
store i32 1, i32* %16
br label %73
73: ; preds = %72, %71, %59, %53, %43, %34, %28
%74 = load i8*, i8** %13
%75 = call i32 @free(i8* %74)
%76 = load i8*, i8** %14
%77 = call i32 @free(i8* %76)
%78 = load i32, i32* %16
ret i32 %78
}

LLVM IR (-O2):
define i32 @AES_wrap_unwrap_test(i8* %0, i32 %1, i8* %2, i8* %3, i8* %4, i32 %5) {
%7 = alloca i32
%8 = bitcast i32* %7 to i8*
%9 = add nsw i32 %5, 8
%10 = tail call i8* @malloc(i32 %9)
%11 = tail call i8* @malloc(i32 %5)
%12 = icmp eq i8* %10, null
%13 = icmp eq i8* %11, null
%14 = or i1 %12, %13
br i1 %14, label %34, label %15
15: ; preds = %6
%16 = call i64 @AES_set_encrypt_key(i8* %0, i32 %1, i32* %7)
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %34
18: ; preds = %15
%19 = call i32 @AES_wrap_key(i32* %7, i8* %2, i8* %10, i8* %4, i32 %5)
%20 = icmp slt i32 %19, 1
br i1 %20, label %34, label %21
21: ; preds = %18
%22 = icmp eq i8* %3, null
br i1 %22, label %26, label %23
23: ; preds = %21
%24 = call i64 @memcmp(i8* %3, i8* %10, i32 %5)
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %34
26: ; preds = %23, %21
%27 = call i64 @AES_set_decrypt_key(i8* %0, i32 %1, i32* %7)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %26
%30 = call i32 @AES_unwrap_key(i32* %7, i8* %2, i8* %11, i8* %10, i32 %19)
%31 = call i64 @memcmp(i8* %4, i8* %11, i32 %5)
%32 = icmp eq i64 %31, 0
%33 = zext i1 %32 to i32
br label %34
34: ; preds = %29, %26, %23, %18, %15, %6
%35 = phi i32 [ 0, %6 ], [ 0, %15 ], [ 0, %18 ], [ 0, %23 ], [ 0, %26 ], [ %33, %29 ]
%36 = call i32 @free(i8* %10)
%37 = call i32 @free(i8* %11)
ret i32 %35
}