#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*/ memcpy(unsigned char *, unsigned char *, size_t);
int /*<<< orphan*/ pbkdf2_sha1_f(char *, char *, size_t, int, int,
                                 unsigned char *);
void pbkdf2_sha1(char *passphrase, char *ssid, size_t ssid_len, int iterations,
                 unsigned char *buf, size_t buflen) {
  int count = 0;
  unsigned char *pos = buf;
  size_t left = buflen, plen;
  unsigned char digest[20];
  while (left > 0) {
    count++;
    pbkdf2_sha1_f(passphrase, ssid, ssid_len, iterations, count, digest);
    plen = left > 20 ? 20 : left;
    memcpy(pos, digest, plen);
    pos += plen;
    left -= plen;
  }
}


x86 assembly (clang, -O0):
<pbkdf2_sha1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jbe    11e3 <pbkdf2_sha1+0xc3>
mov    -0x34(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x34(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    -0x34(%rbp),%r8d
lea    -0x70(%rbp),%r9
call   1040 <pbkdf2_sha1_f@plt>
cmpq   $0x14,-0x48(%rbp)
jbe    11a2 <pbkdf2_sha1+0x82>
mov    $0x14,%eax
mov    %rax,-0x78(%rbp)
jmp    11aa <pbkdf2_sha1+0x8a>
mov    -0x48(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x78(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x40(%rbp),%rdi
lea    -0x70(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x50(%rbp),%rax
add    -0x40(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x48(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x48(%rbp)
jmp    1159 <pbkdf2_sha1+0x39>
add    $0x80,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pbkdf2_sha1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
test   %r9,%r9
je     1195 <pbkdf2_sha1+0x75>
mov    %r9,%rbx
mov    %r8,%r14
mov    %ecx,%ebp
mov    %rdx,%r12
mov    $0x1,%r15d
mov    %rsi,0x8(%rsp)
mov    %rdi,(%rsp)
nopl   (%rax)
mov    (%rsp),%rdi
mov    0x8(%rsp),%rsi
mov    %r12,%rdx
mov    %ebp,%ecx
mov    %r15d,%r8d
lea    0x10(%rsp),%r9
call   1040 <pbkdf2_sha1_f@plt>
cmp    $0x14,%rbx
mov    $0x14,%r13d
cmovb  %rbx,%r13
mov    %r14,%rdi
lea    0x10(%rsp),%rsi
mov    %r13,%rdx
call   1030 <memcpy@plt>
add    %r13,%r14
add    $0x1,%r15d
sub    %r13,%rbx
jne    1150 <pbkdf2_sha1+0x30>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @pbkdf2_sha1(i8* %0, i8* %1, i64 %2, i32 %3, i8* %4, i64 %5) {
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i64
%13 = alloca i32
%14 = alloca i8*
%15 = alloca i64
%16 = alloca i64
%17 = alloca [20 x i8]
store i8* %0, i8** %7
store i8* %1, i8** %8
store i64 %2, i64* %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i64 %5, i64* %12
store i32 0, i32* %13
%18 = load i8*, i8** %11
store i8* %18, i8** %14
%19 = load i64, i64* %12
store i64 %19, i64* %15
br label %20
20: ; preds = %38, %6
%21 = load i64, i64* %15
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %50
23: ; preds = %20
%24 = load i32, i32* %13
%25 = add nsw i32 %24, 1
store i32 %25, i32* %13
%26 = load i8*, i8** %7
%27 = load i8*, i8** %8
%28 = load i64, i64* %9
%29 = load i32, i32* %10
%30 = load i32, i32* %13
%31 = getelementptr inbounds [20 x i8], [20 x i8]* %17, i64 0, i64 0
%32 = call i32 @pbkdf2_sha1_f(i8* %26, i8* %27, i64 %28, i32 %29, i32 %30, i8* %31)
%33 = load i64, i64* %15
%34 = icmp ugt i64 %33, 20
br i1 %34, label %35, label %36
35: ; preds = %23
br label %38
36: ; preds = %23
%37 = load i64, i64* %15
br label %38
38: ; preds = %36, %35
%39 = phi i64 [ 20, %35 ], [ %37, %36 ]
store i64 %39, i64* %16
%40 = load i8*, i8** %14
%41 = getelementptr inbounds [20 x i8], [20 x i8]* %17, i64 0, i64 0
%42 = load i64, i64* %16
%43 = call i32 @memcpy(i8* %40, i8* %41, i64 %42)
%44 = load i64, i64* %16
%45 = load i8*, i8** %14
%46 = getelementptr inbounds i8, i8* %45, i64 %44
store i8* %46, i8** %14
%47 = load i64, i64* %16
%48 = load i64, i64* %15
%49 = sub i64 %48, %47
store i64 %49, i64* %15
br label %20
50: ; preds = %20
ret void
}

LLVM IR (-O2):
define void @pbkdf2_sha1(i8* %0, i8* %1, i64 %2, i32 %3, i8* %4, i64 %5) {
%7 = alloca [20 x i8]
%8 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%9 = icmp eq i64 %5, 0
br i1 %9, label %22, label %10
10: ; preds = %6, %10
%11 = phi i32 [ %14, %10 ], [ 0, %6 ]
%12 = phi i64 [ %20, %10 ], [ %5, %6 ]
%13 = phi i8* [ %19, %10 ], [ %4, %6 ]
%14 = add nuw nsw i32 %11, 1
%15 = call i32 @pbkdf2_sha1_f(i8* %0, i8* %1, i64 %2, i32 %3, i32 %14, i8* %8)
%16 = icmp ult i64 %12, 20
%17 = select i1 %16, i64 %12, i64 20
%18 = call i32 @memcpy(i8* %13, i8* %8, i64 %17)
%19 = getelementptr inbounds i8, i8* %13, i64 %17
%20 = sub i64 %12, %17
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %10
22: ; preds = %10, %6
ret void
}