#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*/ const uint8_t;

/* Variables and functions */
int /*<<< orphan*/ HMAC_SHA256(int /*<<< orphan*/ const *, int,
                               int /*<<< orphan*/ const *, int const,
                               int /*<<< orphan*/ const *);
int /*<<< orphan*/ INT(int /*<<< orphan*/ const *, int);
int /*<<< orphan*/ XOR(int /*<<< orphan*/ const *, int /*<<< orphan*/ const *,
                       int const);
int /*<<< orphan*/ const *malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ const *,
                          int /*<<< orphan*/ const *, int const);
uint8_t *PBKDF2(const uint8_t *passwd, int plen, const uint8_t *salt, int slen,
                int c, int dklen) {
  const int hlen = 32;
  int r, ctr = 0;
  uint8_t T[hlen];
  uint8_t *final = malloc(dklen);
  uint8_t Uprev[hlen], Ucurr[hlen];
  uint8_t rnd1[slen + 4];
  memcpy(rnd1, salt, slen);
  for (int i = 1; dklen > 0; i++) {
    INT(rnd1 + slen, i);
    HMAC_SHA256(passwd, plen, rnd1, slen + 4, Uprev);
    memcpy(T, Uprev, hlen);
    for (int j = 1; j < c; j++) {
      HMAC_SHA256(passwd, plen, Uprev, hlen, Ucurr);
      XOR(T, Ucurr, hlen);
      memcpy(Uprev, Ucurr, hlen);
    }
    r = (dklen < hlen) ? dklen : hlen;
    memcpy(final + ctr, T, r);
    ctr += r;
    dklen -= r;
  }
  return final;
}


x86 assembly (clang, -O0):
<PBKDF2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1e0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9d,-0x24(%rbp)
movl   $0x20,-0x28(%rbp)
movl   $0x0,-0x30(%rbp)
mov    %rsp,%rax
mov    %rax,-0x38(%rbp)
mov    -0x24(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x4,%eax
lea    0xf(,%rax,4),%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x1d8(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x48(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1050 <memcpy@plt>
movl   $0x1,-0x4c(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1308 <PBKDF2+0x1b8>
mov    -0x1d8(%rbp),%rdi
movslq -0x1c(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x4c(%rbp),%esi
call   1040 <INT@plt>
mov    -0x1d8(%rbp),%rdx
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    -0x1c(%rbp),%ecx
add    $0x4,%ecx
lea    -0x150(%rbp),%r8
call   1030 <HMAC_SHA256@plt>
lea    -0xd0(%rbp),%rdi
lea    -0x150(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
movl   $0x1,-0x50(%rbp)
mov    -0x50(%rbp),%eax
cmp    -0x20(%rbp),%eax
jge    1297 <PBKDF2+0x147>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
lea    -0x150(%rbp),%rdx
mov    $0x20,%ecx
lea    -0x1d0(%rbp),%r8
call   1030 <HMAC_SHA256@plt>
lea    -0xd0(%rbp),%rdi
lea    -0x1d0(%rbp),%rsi
mov    $0x20,%edx
call   1060 <XOR@plt>
lea    -0x150(%rbp),%rdi
lea    -0x1d0(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    -0x50(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x50(%rbp)
jmp    122e <PBKDF2+0xde>
cmpl   $0x20,-0x24(%rbp)
jge    12af <PBKDF2+0x15f>
mov    -0x24(%rbp),%eax
mov    %eax,-0x1dc(%rbp)
jmp    12bf <PBKDF2+0x16f>
mov    $0x20,%eax
mov    %eax,-0x1dc(%rbp)
jmp    12bf <PBKDF2+0x16f>
mov    -0x1dc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x40(%rbp),%rdi
movslq -0x30(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
mov    -0x2c(%rbp),%edx
lea    -0xd0(%rbp),%rsi
call   1050 <memcpy@plt>
mov    -0x2c(%rbp),%eax
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x24(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x24(%rbp)
mov    -0x4c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x4c(%rbp)
jmp    11cb <PBKDF2+0x7b>
mov    -0x40(%rbp),%rax
mov    -0x38(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<PBKDF2>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1c8,%rsp
mov    %r9d,%ebx
mov    %r8,-0x48(%rbp)
mov    %ecx,%r14d
mov    %rdx,%r15
mov    %esi,-0x30(%rbp)
mov    %rdi,%r12
mov    %r9d,%edi
call   1070 <malloc@plt>
mov    %rax,-0x50(%rbp)
lea    0x4(%r14),%eax
mov    %rsp,%rdi
mov    %rax,-0x70(%rbp)
lea    0xf(,%rax,4),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdi
mov    %rdi,%rsp
mov    %rdi,-0x40(%rbp)
mov    %r15,%rsi
mov    %r14d,%edx
call   1050 <memcpy@plt>
test   %ebx,%ebx
jle    12d4 <PBKDF2+0x184>
movslq %r14d,%rax
mov    -0x40(%rbp),%rcx
lea    (%rcx,%rax,4),%rax
mov    %rax,-0x68(%rbp)
mov    -0x48(%rbp),%rax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
xor    %eax,%eax
mov    %rax,-0x58(%rbp)
mov    $0x1,%esi
lea    -0x1f0(%rbp),%r13
lea    -0x170(%rbp),%r14
mov    %r12,-0x60(%rbp)
jmp    1238 <PBKDF2+0xe8>
nop
mov    -0x38(%rbp),%ebx
cmp    $0x20,%ebx
mov    $0x20,%r15d
cmovl  %ebx,%r15d
mov    -0x58(%rbp),%r12
movslq %r12d,%r12
mov    -0x50(%rbp),%rax
lea    (%rax,%r12,4),%rdi
lea    -0xf0(%rbp),%rsi
mov    %r15d,%edx
call   1050 <memcpy@plt>
add    %r15d,%r12d
mov    %r12,-0x58(%rbp)
mov    -0x60(%rbp),%r12
mov    -0x34(%rbp),%esi
add    $0x1,%esi
sub    %r15d,%ebx
jle    12d4 <PBKDF2+0x184>
mov    %ebx,-0x38(%rbp)
mov    -0x68(%rbp),%rdi
mov    %esi,-0x34(%rbp)
call   1040 <INT@plt>
mov    %r12,%rdi
mov    -0x30(%rbp),%r15d
mov    %r15d,%esi
mov    -0x40(%rbp),%rdx
mov    -0x70(%rbp),%rcx
mov    %r13,%r8
call   1030 <HMAC_SHA256@plt>
lea    -0xf0(%rbp),%rdi
mov    %r13,%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
mov    -0x2c(%rbp),%ebx
cmpl   $0x2,-0x48(%rbp)
jl     11f0 <PBKDF2+0xa0>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r12,%rdi
mov    %r15d,%esi
mov    %r13,%rdx
mov    $0x20,%ecx
mov    %r14,%r8
call   1030 <HMAC_SHA256@plt>
lea    -0xf0(%rbp),%rdi
mov    %r14,%rsi
mov    $0x20,%edx
call   1060 <XOR@plt>
mov    %r13,%rdi
mov    %r14,%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
add    $0xffffffff,%ebx
jne    1290 <PBKDF2+0x140>
jmp    11f0 <PBKDF2+0xa0>
mov    -0x50(%rbp),%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32* @PBKDF2(i32* %0, i32 %1, i32* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i32*
%18 = alloca i64
%19 = alloca i32
%20 = alloca i32
store i32* %0, i32** %7
store i32 %1, i32* %8
store i32* %2, i32** %9
store i32 %3, i32* %10
store i32 %4, i32* %11
store i32 %5, i32* %12
store i32 32, i32* %13
store i32 0, i32* %15
%21 = call i8* @llvm.stacksave()
store i8* %21, i8** %16
%22 = alloca i32, i64 32
%23 = load i32, i32* %12
%24 = call i32* @malloc(i32 %23)
store i32* %24, i32** %17
%25 = alloca i32, i64 32
%26 = alloca i32, i64 32
%27 = load i32, i32* %10
%28 = add nsw i32 %27, 4
%29 = zext i32 %28 to i64
%30 = alloca i32, i64 %29
store i64 %29, i64* %18
%31 = load i32*, i32** %9
%32 = load i32, i32* %10
%33 = call i32 @memcpy(i32* %30, i32* %31, i32 %32)
store i32 1, i32* %19
br label %34
34: ; preds = %82, %6
%35 = load i32, i32* %12
%36 = icmp sgt i32 %35, 0
br i1 %36, label %37, label %85
37: ; preds = %34
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i32, i32* %30, i64 %39
%41 = load i32, i32* %19
%42 = call i32 @INT(i32* %40, i32 %41)
%43 = load i32*, i32** %7
%44 = load i32, i32* %8
%45 = load i32, i32* %10
%46 = add nsw i32 %45, 4
%47 = call i32 @HMAC_SHA256(i32* %43, i32 %44, i32* %30, i32 %46, i32* %25)
%48 = call i32 @memcpy(i32* %22, i32* %25, i32 32)
store i32 1, i32* %20
br label %49
49: ; preds = %59, %37
%50 = load i32, i32* %20
%51 = load i32, i32* %11
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %62
53: ; preds = %49
%54 = load i32*, i32** %7
%55 = load i32, i32* %8
%56 = call i32 @HMAC_SHA256(i32* %54, i32 %55, i32* %25, i32 32, i32* %26)
%57 = call i32 @XOR(i32* %22, i32* %26, i32 32)
%58 = call i32 @memcpy(i32* %25, i32* %26, i32 32)
br label %59
59: ; preds = %53
%60 = load i32, i32* %20
%61 = add nsw i32 %60, 1
store i32 %61, i32* %20
br label %49
62: ; preds = %49
%63 = load i32, i32* %12
%64 = icmp slt i32 %63, 32
br i1 %64, label %65, label %67
65: ; preds = %62
%66 = load i32, i32* %12
br label %68
67: ; preds = %62
br label %68
68: ; preds = %67, %65
%69 = phi i32 [ %66, %65 ], [ 32, %67 ]
store i32 %69, i32* %14
%70 = load i32*, i32** %17
%71 = load i32, i32* %15
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i32, i32* %70, i64 %72
%74 = load i32, i32* %14
%75 = call i32 @memcpy(i32* %73, i32* %22, i32 %74)
%76 = load i32, i32* %14
%77 = load i32, i32* %15
%78 = add nsw i32 %77, %76
store i32 %78, i32* %15
%79 = load i32, i32* %14
%80 = load i32, i32* %12
%81 = sub nsw i32 %80, %79
store i32 %81, i32* %12
br label %82
82: ; preds = %68
%83 = load i32, i32* %19
%84 = add nsw i32 %83, 1
store i32 %84, i32* %19
br label %34
85: ; preds = %34
%86 = load i32*, i32** %17
%87 = load i8*, i8** %16
ret i32* %86
}

LLVM IR (-O2):
define i32* @PBKDF2(i32* %0, i32 %1, i32* %2, i32 %3, i32 %4, i32 %5) {
%7 = alloca [32 x i32]
%8 = getelementptr inbounds [32 x i32], [32 x i32]* %7, i64 0, i64 0
%9 = tail call i32* @malloc(i32 %5)
%10 = alloca [32 x i32]
%11 = alloca [32 x i32]
%12 = getelementptr inbounds [32 x i32], [32 x i32]* %11, i64 0, i64 0
%13 = getelementptr inbounds [32 x i32], [32 x i32]* %10, i64 0, i64 0
%14 = add nsw i32 %3, 4
%15 = zext i32 %14 to i64
%16 = alloca i32, i64 %15
%17 = call i32 @memcpy(i32* %16, i32* %2, i32 %3)
%18 = icmp sgt i32 %5, 0
br i1 %18, label %19, label %23
19: ; preds = %6
%20 = sext i32 %3 to i64
%21 = getelementptr inbounds i32, i32* %16, i64 %20
%22 = icmp sgt i32 %4, 1
br label %24
23: ; preds = %31, %6
ret i32* %9
24: ; preds = %19, %31
%25 = phi i32 [ 1, %19 ], [ %39, %31 ]
%26 = phi i32 [ %5, %19 ], [ %38, %31 ]
%27 = phi i32 [ 0, %19 ], [ %37, %31 ]
%28 = call i32 @INT(i32* %21, i32 %25)
%29 = call i32 @HMAC_SHA256(i32* %0, i32 %1, i32* %16, i32 %14, i32* %13)
%30 = call i32 @memcpy(i32* %8, i32* %13, i32 32)
br i1 %22, label %41, label %31
31: ; preds = %41, %24
%32 = icmp slt i32 %26, 32
%33 = select i1 %32, i32 %26, i32 32
%34 = sext i32 %27 to i64
%35 = getelementptr inbounds i32, i32* %9, i64 %34
%36 = call i32 @memcpy(i32* %35, i32* %8, i32 %33)
%37 = add nsw i32 %33, %27
%38 = sub nsw i32 %26, %33
%39 = add nuw nsw i32 %25, 1
%40 = icmp sgt i32 %38, 0
br i1 %40, label %24, label %23
41: ; preds = %24, %41
%42 = phi i32 [ %46, %41 ], [ 1, %24 ]
%43 = call i32 @HMAC_SHA256(i32* %0, i32 %1, i32* %13, i32 32, i32* %12)
%44 = call i32 @XOR(i32* %8, i32* %12, i32 32)
%45 = call i32 @memcpy(i32* %13, i32* %12, i32 32)
%46 = add nuw nsw i32 %42, 1
%47 = icmp eq i32 %46, %4
br i1 %47, label %31, label %41
}