#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*/ bytecpy(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ bytexor(unsigned char *, unsigned char *,
                           unsigned char const *, int);
int /*<<< orphan*/ clefiaconset(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ clefiadoubleswap(unsigned char *);
int /*<<< orphan*/ clefiagfn4(unsigned char *, unsigned char const *,
                              unsigned char *, int);
int clefiakeyset(unsigned char *rk, const unsigned char *skey) {
  const unsigned char iv[2] = {0x42u, 0x8au};
  unsigned char lk[16];
  unsigned char con128[4 * 60];
  int i;
  clefiaconset(con128, iv, 30);
  clefiagfn4(lk, skey, con128, 12);
  bytecpy(rk, skey, 8);
  rk += 8;
  for (i = 0; i < 9; i++) {
    bytexor(rk, lk, con128 + i * 16 + (4 * 24), 16);
    if (i % 2) {
      bytexor(rk, rk, skey, 16);
    }
    clefiadoubleswap(lk);
    rk += 16;
  }
  bytecpy(rk, skey + 8, 8);
  return 18;
}


x86 assembly (clang, -O0):
<clefiakeyset>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xe96(%rip),%ax
mov    %ax,-0x12(%rbp)
lea    -0x120(%rbp),%rdi
lea    -0x12(%rbp),%rsi
mov    $0x1e,%edx
call   1070 <clefiaconset@plt>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
lea    -0x120(%rbp),%rdx
mov    $0xc,%ecx
call   1040 <clefiagfn4@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1050 <bytecpy@plt>
mov    -0x8(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
movl   $0x0,-0x124(%rbp)
cmpl   $0x9,-0x124(%rbp)
jge    1252 <clefiakeyset+0x102>
mov    -0x8(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x120(%rbp),%rdx
mov    -0x124(%rbp),%eax
shl    $0x4,%eax
cltq
add    %rax,%rdx
add    $0x60,%rdx
mov    $0x10,%ecx
call   1030 <bytexor@plt>
mov    -0x124(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
je     1229 <clefiakeyset+0xd9>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    $0x10,%ecx
call   1030 <bytexor@plt>
lea    -0x30(%rbp),%rdi
call   1060 <clefiadoubleswap@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rax
mov    %rax,-0x8(%rbp)
mov    -0x124(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x124(%rbp)
jmp    11c4 <clefiakeyset+0x74>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x8,%rsi
mov    $0x8,%edx
call   1050 <bytecpy@plt>
mov    $0x12,%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<clefiakeyset>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    %rsi,%r14
mov    %rdi,%r12
movw   $0x8a42,0xe(%rsp)
lea    0x20(%rsp),%rbx
lea    0xe(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x1e,%edx
call   1070 <clefiaconset@plt>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
mov    $0xc,%ecx
call   1040 <clefiagfn4@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1050 <bytecpy@plt>
add    $0x8,%r12
lea    0x80(%rsp),%r13
xor    %ebx,%ebx
jmp    11da <clefiakeyset+0x8a>
nopl   0x0(%rax)
mov    %r15,%rdi
call   1060 <clefiadoubleswap@plt>
add    $0x10,%r12
add    $0x1,%rbx
add    $0x10,%r13
cmp    $0x9,%rbx
je     1207 <clefiakeyset+0xb7>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r13,%rdx
mov    $0x10,%ecx
call   1030 <bytexor@plt>
test   $0x1,%bl
je     11c0 <clefiakeyset+0x70>
mov    %r12,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
mov    $0x10,%ecx
call   1030 <bytexor@plt>
jmp    11c0 <clefiakeyset+0x70>
add    $0x8,%r14
mov    %r12,%rdi
mov    %r14,%rsi
mov    $0x8,%edx
call   1050 <bytecpy@plt>
mov    $0x12,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@__const.clefiakeyset.iv = [2 x i8] c"B\8A"
define i32 @clefiakeyset(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [2 x i8]
%6 = alloca [16 x i8]
%7 = alloca [240 x i8]
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%9 = bitcast [2 x i8]* %5 to i8*
%10 = getelementptr inbounds [240 x i8], [240 x i8]* %7, i64 0, i64 0
%11 = getelementptr inbounds [2 x i8], [2 x i8]* %5, i64 0, i64 0
%12 = call i32 @clefiaconset(i8* %10, i8* %11, i32 30)
%13 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%14 = load i8*, i8** %4
%15 = getelementptr inbounds [240 x i8], [240 x i8]* %7, i64 0, i64 0
%16 = call i32 @clefiagfn4(i8* %13, i8* %14, i8* %15, i32 12)
%17 = load i8*, i8** %3
%18 = load i8*, i8** %4
%19 = call i32 @bytecpy(i8* %17, i8* %18, i32 8)
%20 = load i8*, i8** %3
%21 = getelementptr inbounds i8, i8* %20, i64 8
store i8* %21, i8** %3
store i32 0, i32* %8
br label %22
22: ; preds = %48, %2
%23 = load i32, i32* %8
%24 = icmp slt i32 %23, 9
br i1 %24, label %25, label %51
25: ; preds = %22
%26 = load i8*, i8** %3
%27 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%28 = getelementptr inbounds [240 x i8], [240 x i8]* %7, i64 0, i64 0
%29 = load i32, i32* %8
%30 = mul nsw i32 %29, 16
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
%33 = getelementptr inbounds i8, i8* %32, i64 96
%34 = call i32 @bytexor(i8* %26, i8* %27, i8* %33, i32 16)
%35 = load i32, i32* %8
%36 = srem i32 %35, 2
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %43
38: ; preds = %25
%39 = load i8*, i8** %3
%40 = load i8*, i8** %3
%41 = load i8*, i8** %4
%42 = call i32 @bytexor(i8* %39, i8* %40, i8* %41, i32 16)
br label %43
43: ; preds = %38, %25
%44 = getelementptr inbounds [16 x i8], [16 x i8]* %6, i64 0, i64 0
%45 = call i32 @clefiadoubleswap(i8* %44)
%46 = load i8*, i8** %3
%47 = getelementptr inbounds i8, i8* %46, i64 16
store i8* %47, i8** %3
br label %48
48: ; preds = %43
%49 = load i32, i32* %8
%50 = add nsw i32 %49, 1
store i32 %50, i32* %8
br label %22
51: ; preds = %22
%52 = load i8*, i8** %3
%53 = load i8*, i8** %4
%54 = getelementptr inbounds i8, i8* %53, i64 8
%55 = call i32 @bytecpy(i8* %52, i8* %54, i32 8)
ret i32 18
}

LLVM IR (-O2):
define i32 @clefiakeyset(i8* %0, i8* %1) {
%3 = alloca i16
%4 = alloca [16 x i8]
%5 = alloca [240 x i8]
%6 = bitcast i16* %3 to i8*
store i16 -30142, i16* %3
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [240 x i8], [240 x i8]* %5, i64 0, i64 0
%9 = call i32 @clefiaconset(i8* %8, i8* %6, i32 30)
%10 = call i32 @clefiagfn4(i8* %7, i8* %1, i8* %8, i32 12)
%11 = call i32 @bytecpy(i8* %0, i8* %1, i32 8)
%12 = getelementptr inbounds i8, i8* %0, i64 8
br label %13
13: ; preds = %2, %24
%14 = phi i64 [ 0, %2 ], [ %27, %24 ]
%15 = phi i8* [ %12, %2 ], [ %26, %24 ]
%16 = shl nuw nsw i64 %14, 4
%17 = getelementptr inbounds [240 x i8], [240 x i8]* %5, i64 0, i64 %16
%18 = getelementptr inbounds i8, i8* %17, i64 96
%19 = call i32 @bytexor(i8* %15, i8* %7, i8* %18, i32 16)
%20 = and i64 %14, 1
%21 = icmp eq i64 %20, 0
br i1 %21, label %24, label %22
22: ; preds = %13
%23 = call i32 @bytexor(i8* %15, i8* %15, i8* %1, i32 16)
br label %24
24: ; preds = %22, %13
%25 = call i32 @clefiadoubleswap(i8* %7)
%26 = getelementptr inbounds i8, i8* %15, i64 16
%27 = add nuw nsw i64 %14, 1
%28 = icmp eq i64 %27, 9
br i1 %28, label %29, label %13
29: ; preds = %24
%30 = getelementptr inbounds i8, i8* %1, i64 8
%31 = call i32 @bytecpy(i8* %26, i8* %30, i32 8)
ret i32 18
}