#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 uint_fast8_t;
typedef int uint32_t;

/* Variables and functions */
int be32(int);
int /*<<< orphan*/ free(unsigned char *);
int /*<<< orphan*/ hmac_sha256(void const *, int, unsigned char *, int,
                               unsigned char *);
unsigned char *malloc(int);
int /*<<< orphan*/ memcpy(unsigned char *, ...);
void kdf(const void *key, const size_t key_len, unsigned char *res) {
  uint32_t kdk_len = key_len * 8;
  uint_fast8_t j = 0;
  char *salt = "\x57\x69\x2d\x46\x69\x20\x45\x61\x73\x79\x20\x61\x6e\x64\x20"
               "\x53\x65\x63\x75\x72\x65\x20\x4b\x65\x79\x20\x44\x65\x72\x69"
               "\x76\x61\x74\x69\x6f\x6e";
  unsigned char *buffer = malloc(36 + 4 * 2);
  for (uint32_t i = 1; i < 4; i++) {
    uint32_t be = be32(i);
    memcpy(buffer, &be, 4);
    memcpy(buffer + 4, salt, 36);
    be = be32(kdk_len);
    memcpy(buffer + 4 + 36, &be, 4);
    hmac_sha256(key, 32, buffer, 36 + 4 * 2, res + j);
    j += 32;
  }
  free(buffer);
}


x86 assembly (clang, -O0):
<kdf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
shl    $0x3,%rax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
lea    0xe83(%rip),%rax
mov    %rax,-0x28(%rbp)
mov    $0x2c,%edi
call   1050 <malloc@plt>
mov    %rax,-0x30(%rbp)
movl   $0x1,-0x34(%rbp)
cmpl   $0x4,-0x34(%rbp)
jge    1237 <kdf+0xe7>
mov    -0x34(%rbp),%edi
call   1070 <be32@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
xor    %eax,%eax
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x30(%rbp),%rdi
add    $0x4,%rdi
mov    -0x28(%rbp),%rsi
xor    %eax,%eax
mov    $0x24,%edx
call   1040 <memcpy@plt>
mov    -0x1c(%rbp),%edi
call   1070 <be32@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
add    $0x4,%rdi
add    $0x24,%rdi
xor    %eax,%eax
lea    -0x38(%rbp),%rsi
mov    $0x4,%edx
call   1040 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x30(%rbp),%rdx
mov    -0x18(%rbp),%r8
movslq -0x20(%rbp),%rax
add    %rax,%r8
mov    $0x20,%esi
mov    $0x2c,%ecx
call   1060 <hmac_sha256@plt>
mov    -0x20(%rbp),%eax
add    $0x20,%eax
mov    %eax,-0x20(%rbp)
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
jmp    1196 <kdf+0x46>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kdf>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r15
mov    %rdi,0x10(%rsp)
lea    0x0(,%rsi,8),%r12d
mov    $0x2c,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
lea    0x4(%rax),%rbp
mov    %rbp,0x8(%rsp)
lea    0x28(%rax),%r14
mov    $0x1,%edi
call   1070 <be32@plt>
mov    %eax,(%rsp)
mov    %rsp,%r13
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
lea    0xe4f(%rip),%rsi
mov    %rbp,%rdi
mov    $0x24,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    %r12d,%edi
mov    %r12d,0x4(%rsp)
call   1070 <be32@plt>
mov    %eax,(%rsp)
mov    %r14,%rdi
mov    %r14,%rbp
mov    %r13,%rsi
mov    %r13,%r14
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    0x10(%rsp),%r13
mov    %r13,%rdi
mov    $0x20,%esi
mov    %rbx,%rdx
mov    $0x2c,%ecx
mov    %r15,%r8
call   1060 <hmac_sha256@plt>
mov    $0x2,%edi
call   1070 <be32@plt>
mov    %eax,(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    0x8(%rsp),%r14
mov    %r14,%rdi
lea    0xdcd(%rip),%rsi
mov    $0x24,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    %r12d,%edi
call   1070 <be32@plt>
mov    %eax,(%rsp)
mov    %rbp,%rdi
mov    %rsp,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
lea    0x20(%r15),%r8
mov    %r13,%rdi
mov    $0x20,%esi
mov    %rbx,%rdx
mov    $0x2c,%ecx
call   1060 <hmac_sha256@plt>
mov    $0x3,%edi
call   1070 <be32@plt>
mov    %eax,(%rsp)
mov    %rbx,%rdi
mov    %rsp,%r12
mov    %r12,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    %r14,%rdi
lea    0xd5f(%rip),%rsi
mov    $0x24,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
mov    0x4(%rsp),%edi
call   1070 <be32@plt>
mov    %eax,(%rsp)
mov    %rbp,%rdi
mov    %r12,%rsi
mov    $0x4,%edx
xor    %eax,%eax
call   1040 <memcpy@plt>
add    $0x40,%r15
mov    %r13,%rdi
mov    $0x20,%esi
mov    %rbx,%rdx
mov    $0x2c,%ecx
mov    %r15,%r8
call   1060 <hmac_sha256@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [37 x i8] c"Wi-Fi Easy and Secure Key Derivation\00"
define void @kdf(i8* %0, i64 %1, i8* %2) {
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
store i8* %2, i8** %6
%13 = load i64, i64* %5
%14 = mul i64 %13, 8
%15 = trunc i64 %14 to i32
store i32 %15, i32* %7
store i32 0, i32* %8
store i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i8** %9
%16 = call i8* @malloc(i32 44)
store i8* %16, i8** %10
store i32 1, i32* %11
br label %17
17: ; preds = %44, %3
%18 = load i32, i32* %11
%19 = icmp slt i32 %18, 4
br i1 %19, label %20, label %47
20: ; preds = %17
%21 = load i32, i32* %11
%22 = call i32 @be32(i32 %21)
store i32 %22, i32* %12
%23 = load i8*, i8** %10
%24 = call i32 (i8*, ...) @memcpy(i8* %23, i32* %12, i32 4)
%25 = load i8*, i8** %10
%26 = getelementptr inbounds i8, i8* %25, i64 4
%27 = load i8*, i8** %9
%28 = call i32 (i8*, ...) @memcpy(i8* %26, i8* %27, i32 36)
%29 = load i32, i32* %7
%30 = call i32 @be32(i32 %29)
store i32 %30, i32* %12
%31 = load i8*, i8** %10
%32 = getelementptr inbounds i8, i8* %31, i64 4
%33 = getelementptr inbounds i8, i8* %32, i64 36
%34 = call i32 (i8*, ...) @memcpy(i8* %33, i32* %12, i32 4)
%35 = load i8*, i8** %4
%36 = load i8*, i8** %10
%37 = load i8*, i8** %6
%38 = load i32, i32* %8
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = call i32 @hmac_sha256(i8* %35, i32 32, i8* %36, i32 44, i8* %40)
%42 = load i32, i32* %8
%43 = add nsw i32 %42, 32
store i32 %43, i32* %8
br label %44
44: ; preds = %20
%45 = load i32, i32* %11
%46 = add nsw i32 %45, 1
store i32 %46, i32* %11
br label %17
47: ; preds = %17
%48 = load i8*, i8** %10
%49 = call i32 @free(i8* %48)
ret void
}

LLVM IR (-O2):
@.str = [37 x i8] c"Wi-Fi Easy and Secure Key Derivation\00"
define void @kdf(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = trunc i64 %1 to i32
%6 = shl i32 %5, 3
%7 = tail call dereferenceable_or_null(44) i8* @malloc(i32 44)
%8 = bitcast i32* %4 to i8*
%9 = getelementptr inbounds i8, i8* %7, i64 4
%10 = getelementptr inbounds i8, i8* %7, i64 40
%11 = call i32 @be32(i32 1)
store i32 %11, i32* %4
%12 = call i32 (i8*, ...) @memcpy(i8* %7, i32* %4, i32 4)
%13 = call i32 (i8*, ...) @memcpy(i8* %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 36)
%14 = call i32 @be32(i32 %6)
store i32 %14, i32* %4
%15 = call i32 (i8*, ...) @memcpy(i8* %10, i32* %4, i32 4)
%16 = call i32 @hmac_sha256(i8* %0, i32 32, i8* %7, i32 44, i8* %2)
%17 = call i32 @be32(i32 2)
store i32 %17, i32* %4
%18 = call i32 (i8*, ...) @memcpy(i8* %7, i32* %4, i32 4)
%19 = call i32 (i8*, ...) @memcpy(i8* %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 36)
%20 = call i32 @be32(i32 %6)
store i32 %20, i32* %4
%21 = call i32 (i8*, ...) @memcpy(i8* %10, i32* %4, i32 4)
%22 = getelementptr inbounds i8, i8* %2, i64 32
%23 = call i32 @hmac_sha256(i8* %0, i32 32, i8* %7, i32 44, i8* %22)
%24 = call i32 @be32(i32 3)
store i32 %24, i32* %4
%25 = call i32 (i8*, ...) @memcpy(i8* %7, i32* %4, i32 4)
%26 = call i32 (i8*, ...) @memcpy(i8* %9, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0), i32 36)
%27 = call i32 @be32(i32 %6)
store i32 %27, i32* %4
%28 = call i32 (i8*, ...) @memcpy(i8* %10, i32* %4, i32 4)
%29 = getelementptr inbounds i8, i8* %2, i64 64
%30 = call i32 @hmac_sha256(i8* %0, i32 32, i8* %7, i32 44, i8* %29)
%31 = call i32 @free(i8* %7)
ret void
}