#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*/ crypto_hash_sha512_tweet(unsigned char *, unsigned char *,
                                            int const);
int /*<<< orphan*/ crypto_scalarmult_curve25519_tweet_base(unsigned char *,
                                                           unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int /*<<< orphan*/ memset(unsigned char *, int /*<<< orphan*/, int);
void brainkey(unsigned char pk[32], unsigned char sk[32], const char *key,
              const int klen) {
  unsigned char h[64];
  crypto_hash_sha512_tweet(h, (unsigned char *)key, klen);
  for (int i = 0; i < 10000; i++)
    crypto_hash_sha512_tweet(h, h, 64);
  memcpy(sk, h, 32);
  crypto_scalarmult_curve25519_tweet_base(pk, sk);
  memset(h, 0, 64);
}


x86 assembly (clang, -O0):
<brainkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <crypto_hash_sha512_tweet@plt>
movl   $0x0,-0x64(%rbp)
cmpl   $0x2710,-0x64(%rbp)
jge    119b <brainkey+0x5b>
lea    -0x60(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x40,%edx
call   1060 <crypto_hash_sha512_tweet@plt>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    116e <brainkey+0x2e>
mov    -0x10(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <crypto_scalarmult_curve25519_tweet_base@plt>
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<brainkey>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x48,%rsp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %rdx,%rsi
mov    %ecx,%edx
call   1060 <crypto_hash_sha512_tweet@plt>
mov    $0x2710,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %rbx,%rsi
mov    $0x40,%edx
call   1060 <crypto_hash_sha512_tweet@plt>
add    $0xffffffff,%ebp
jne    1170 <brainkey+0x30>
mov    %rsp,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <crypto_scalarmult_curve25519_tweet_base@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x40,%edx
call   1030 <memset@plt>
add    $0x48,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @brainkey(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [64 x i8]
%10 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%11 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%12 = load i8*, i8** %7
%13 = load i32, i32* %8
%14 = call i32 @crypto_hash_sha512_tweet(i8* %11, i8* %12, i32 %13)
store i32 0, i32* %10
br label %15
15: ; preds = %22, %4
%16 = load i32, i32* %10
%17 = icmp slt i32 %16, 10000
br i1 %17, label %18, label %25
18: ; preds = %15
%19 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%20 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%21 = call i32 @crypto_hash_sha512_tweet(i8* %19, i8* %20, i32 64)
br label %22
22: ; preds = %18
%23 = load i32, i32* %10
%24 = add nsw i32 %23, 1
store i32 %24, i32* %10
br label %15
25: ; preds = %15
%26 = load i8*, i8** %6
%27 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%28 = call i32 @memcpy(i8* %26, i8* %27, i32 32)
%29 = load i8*, i8** %5
%30 = load i8*, i8** %6
%31 = call i32 @crypto_scalarmult_curve25519_tweet_base(i8* %29, i8* %30)
%32 = getelementptr inbounds [64 x i8], [64 x i8]* %9, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 64)
ret void
}

LLVM IR (-O2):
define void @brainkey(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [64 x i8]
%6 = getelementptr inbounds [64 x i8], [64 x i8]* %5, i64 0, i64 0
%7 = call i32 @crypto_hash_sha512_tweet(i8* %6, i8* %2, i32 %3)
br label %12
8: ; preds = %12
%9 = call i32 @memcpy(i8* %1, i8* %6, i32 32)
%10 = call i32 @crypto_scalarmult_curve25519_tweet_base(i8* %0, i8* %1)
%11 = call i32 @memset(i8* %6, i32 0, i32 64)
ret void
12: ; preds = %4, %12
%13 = phi i32 [ 0, %4 ], [ %15, %12 ]
%14 = call i32 @crypto_hash_sha512_tweet(i8* %6, i8* %6, i32 64)
%15 = add nuw nsw i32 %13, 1
%16 = icmp eq i32 %15, 10000
br i1 %16, label %8, label %12
}