#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*/ hmac_sha1(char *, size_t, unsigned char *, int,
                             unsigned char *);
int /*<<< orphan*/ hmac_sha1_vector(char *, size_t, int, unsigned char **,
                                    unsigned int *, unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
size_t strlen(char *);
void pbkdf2_sha1_f(char *passphrase, char *ssid, size_t ssid_len,
                   int iterations, int count, unsigned char *digest) {
  unsigned char tmp[20], tmp2[20];
  int i, j;
  unsigned char count_buf[4];
  unsigned char *addr[] = {ssid, count_buf};
  unsigned int len[] = {ssid_len, 4};
  size_t passphrase_len = strlen(passphrase);
  count_buf[0] = (count >> 24) & 0xff;
  count_buf[1] = (count >> 16) & 0xff;
  count_buf[2] = (count >> 8) & 0xff;
  count_buf[3] = count & 0xff;
  hmac_sha1_vector(passphrase, passphrase_len, 2, addr, len, tmp);
  memcpy(digest, tmp, 20);
  for (i = 1; i < iterations; i++) {
    hmac_sha1(passphrase, passphrase_len, tmp, 20, tmp2);
    memcpy(tmp, tmp2, 20);
    for (j = 0; j < 20; j++)
      digest[j] ^= tmp2[j];
  }
}


x86 assembly (clang, -O0):
<pbkdf2_sha1_f>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    %r9,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x6c(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x18(%rbp),%rax
mov    %eax,-0x88(%rbp)
movl   $0x4,-0x84(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x90(%rbp)
mov    -0x20(%rbp),%eax
sar    $0x18,%eax
and    $0xff,%eax
mov    %al,-0x6c(%rbp)
mov    -0x20(%rbp),%eax
sar    $0x10,%eax
and    $0xff,%eax
mov    %al,-0x6b(%rbp)
mov    -0x20(%rbp),%eax
sar    $0x8,%eax
and    $0xff,%eax
mov    %al,-0x6a(%rbp)
mov    -0x20(%rbp),%eax
and    $0xff,%eax
mov    %al,-0x69(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x90(%rbp),%rsi
lea    -0x80(%rbp),%rcx
lea    -0x88(%rbp),%r8
lea    -0x40(%rbp),%r9
mov    $0x2,%edx
call   1040 <hmac_sha1_vector@plt>
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    $0x14,%edx
call   1050 <memcpy@plt>
movl   $0x1,-0x64(%rbp)
mov    -0x64(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jge    128f <pbkdf2_sha1_f+0x14f>
mov    -0x8(%rbp),%rdi
mov    -0x90(%rbp),%rsi
lea    -0x40(%rbp),%rdx
lea    -0x60(%rbp),%r8
mov    $0x14,%ecx
call   1060 <hmac_sha1@plt>
lea    -0x40(%rbp),%rdi
lea    -0x60(%rbp),%rsi
mov    $0x14,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x68(%rbp)
cmpl   $0x14,-0x68(%rbp)
jge    127c <pbkdf2_sha1_f+0x13c>
movslq -0x68(%rbp),%rax
movzbl -0x60(%rbp,%rax,1),%esi
mov    -0x28(%rbp),%rax
movslq -0x68(%rbp),%rcx
movzbl (%rax,%rcx,1),%edx
xor    %esi,%edx
mov    %dl,(%rax,%rcx,1)
mov    -0x68(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x68(%rbp)
jmp    124a <pbkdf2_sha1_f+0x10a>
jmp    1281 <pbkdf2_sha1_f+0x141>
mov    -0x64(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x64(%rbp)
jmp    1208 <pbkdf2_sha1_f+0xc8>
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pbkdf2_sha1_f>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x58,%rsp
mov    %r9,%rbx
mov    %r8d,%r12d
mov    %ecx,%ebp
mov    %rdi,%r14
mov    %rsi,0x30(%rsp)
lea    0x4(%rsp),%rax
mov    %rax,0x38(%rsp)
mov    %edx,0x8(%rsp)
movl   $0x4,0xc(%rsp)
call   1030 <strlen@plt>
mov    %rax,%r15
bswap  %r12d
mov    %r12d,0x4(%rsp)
lea    0x30(%rsp),%rcx
lea    0x8(%rsp),%r8
lea    0x40(%rsp),%r12
mov    %r14,%rdi
mov    %rax,%rsi
mov    $0x2,%edx
mov    %r12,%r9
call   1040 <hmac_sha1_vector@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0x14,%edx
call   1050 <memcpy@plt>
cmp    $0x2,%ebp
jl     1226 <pbkdf2_sha1_f+0xe6>
add    $0xffffffff,%ebp
lea    0x40(%rsp),%r12
lea    0x10(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %r15,%rsi
mov    %r12,%rdx
mov    $0x14,%ecx
mov    %r13,%r8
call   1060 <hmac_sha1@plt>
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0x14,%edx
call   1050 <memcpy@plt>
movups (%rbx),%xmm0
xorps  0x10(%rsp),%xmm0
movups %xmm0,(%rbx)
movzbl 0x20(%rsp),%eax
xor    %al,0x10(%rbx)
movzbl 0x21(%rsp),%eax
xor    %al,0x11(%rbx)
movzbl 0x22(%rsp),%eax
xor    %al,0x12(%rbx)
movzbl 0x23(%rsp),%eax
xor    %al,0x13(%rbx)
add    $0xffffffff,%ebp
jne    11d0 <pbkdf2_sha1_f+0x90>
add    $0x58,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @pbkdf2_sha1_f(i8* %0, i8* %1, i64 %2, i32 %3, i32 %4, i8* %5) {
%7 = alloca [20 x i8]
%8 = alloca [20 x i8]
%9 = alloca [4 x i8]
%10 = alloca [2 x i8*]
%11 = alloca [2 x i32]
%12 = getelementptr inbounds [20 x i8], [20 x i8]* %7, i64 0, i64 0
%13 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 0
%14 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 0
%15 = bitcast [2 x i8*]* %10 to i8*
%16 = getelementptr inbounds [2 x i8*], [2 x i8*]* %10, i64 0, i64 0
store i8* %1, i8** %16
%17 = getelementptr inbounds [2 x i8*], [2 x i8*]* %10, i64 0, i64 1
store i8* %14, i8** %17
%18 = bitcast [2 x i32]* %11 to i8*
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 0
%20 = trunc i64 %2 to i32
store i32 %20, i32* %19
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %11, i64 0, i64 1
store i32 4, i32* %21
%22 = call i64 @strlen(i8* dereferenceable(1) %0)
%23 = lshr i32 %4, 24
%24 = trunc i32 %23 to i8
store i8 %24, i8* %14
%25 = lshr i32 %4, 16
%26 = trunc i32 %25 to i8
%27 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 1
store i8 %26, i8* %27
%28 = lshr i32 %4, 8
%29 = trunc i32 %28 to i8
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 2
store i8 %29, i8* %30
%31 = trunc i32 %4 to i8
%32 = getelementptr inbounds [4 x i8], [4 x i8]* %9, i64 0, i64 3
store i8 %31, i8* %32
%33 = call i32 @hmac_sha1_vector(i8* %0, i64 %22, i32 2, i8** %16, i32* %19, i8* %12)
%34 = call i32 @memcpy(i8* %5, i8* %12, i32 20)
%35 = icmp sgt i32 %3, 1
br i1 %35, label %36, label %69
36: ; preds = %6
%37 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 16
%38 = getelementptr inbounds i8, i8* %5, i64 16
%39 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 17
%40 = getelementptr inbounds i8, i8* %5, i64 17
%41 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 18
%42 = getelementptr inbounds i8, i8* %5, i64 18
%43 = getelementptr inbounds [20 x i8], [20 x i8]* %8, i64 0, i64 19
%44 = getelementptr inbounds i8, i8* %5, i64 19
%45 = bitcast [20 x i8]* %8 to <16 x i8>*
%46 = bitcast i8* %5 to <16 x i8>*
%47 = bitcast i8* %5 to <16 x i8>*
br label %48
48: ; preds = %36, %48
%49 = phi i32 [ %67, %48 ], [ 1, %36 ]
%50 = call i32 @hmac_sha1(i8* %0, i64 %22, i8* %12, i32 20, i8* %13)
%51 = call i32 @memcpy(i8* %12, i8* %13, i32 20)
%52 = load <16 x i8>, <16 x i8>* %45
%53 = load <16 x i8>, <16 x i8>* %46
%54 = xor <16 x i8> %53, %52
store <16 x i8> %54, <16 x i8>* %47
%55 = load i8, i8* %37
%56 = load i8, i8* %38
%57 = xor i8 %56, %55
store i8 %57, i8* %38
%58 = load i8, i8* %39
%59 = load i8, i8* %40
%60 = xor i8 %59, %58
store i8 %60, i8* %40
%61 = load i8, i8* %41
%62 = load i8, i8* %42
%63 = xor i8 %62, %61
store i8 %63, i8* %42
%64 = load i8, i8* %43
%65 = load i8, i8* %44
%66 = xor i8 %65, %64
store i8 %66, i8* %44
%67 = add nuw nsw i32 %49, 1
%68 = icmp eq i32 %67, %3
br i1 %68, label %69, label %48
69: ; preds = %48, %6
ret void
}