#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 */
scalar_t__ CRYPTO_memcmp(unsigned char *, unsigned char *, unsigned int);
size_t EVP_DecodeBlock(unsigned char *, unsigned char *, size_t);
int /*<<< orphan*/ OPENSSL_clear_free(unsigned char *, int);
scalar_t__ OPENSSL_malloc(int);
int /*<<< orphan*/ free(unsigned char *);
int ipapwd_hash_password(char *, char *, unsigned char *, unsigned char **,
                         unsigned int *);
char *strchr(char *, char);
size_t strlen(char *);
int ipapwd_cmp_password(char *password, char *historyString) {
  char *hash_type;
  char *b64part;
  size_t b64_len;
  unsigned char *salt;
  unsigned char *hash = NULL;
  unsigned int hash_len;
  int ret;
  hash_type = historyString;
  b64part = strchr(historyString, '}');
  if (!b64part) {
    return -1;
  }
  b64part++;
  b64_len = strlen(b64part);
  unsigned char *item_data =
      (unsigned char *)OPENSSL_malloc(sizeof(char) * b64_len);
  if (!item_data) {
    return -1;
  }
  size_t item_len =
      EVP_DecodeBlock(item_data, (unsigned char *)b64part, b64_len);
  if (!item_data) {
    return -1;
  }
  if (item_len <= 8) {
    ret = -1;
    goto done;
  }
  salt = item_data + (item_len - 8);
  ret = ipapwd_hash_password(password, hash_type, salt, &hash, &hash_len);
  if (ret != 0) {
    goto done;
  }
  if (hash_len != item_len) {
    ret = 1;
    goto done;
  }
  if (CRYPTO_memcmp(item_data, hash, hash_len)) {
    ret = 1;
    goto done;
  }
  ret = 0;
done:
  if (item_data)
    OPENSSL_clear_free(item_data, sizeof(char) * b64_len);
  free(hash);
  return ret;
}


x86 assembly (clang, -O0):
<ipapwd_cmp_password>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rdi
mov    $0x7d,%esi
call   1060 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c9 <ipapwd_cmp_password+0x49>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <ipapwd_cmp_password+0x180>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%edi
call   1090 <OPENSSL_malloc@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    120c <ipapwd_cmp_password+0x8c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <ipapwd_cmp_password+0x180>
mov    -0x50(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1040 <EVP_DecodeBlock@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1238 <ipapwd_cmp_password+0xb8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1300 <ipapwd_cmp_password+0x180>
cmpq   $0x8,-0x58(%rbp)
ja     124f <ipapwd_cmp_password+0xcf>
movl   $0xffffffff,-0x48(%rbp)
jmp    12d3 <ipapwd_cmp_password+0x153>
mov    -0x50(%rbp),%rax
mov    -0x58(%rbp),%rcx
sub    $0x8,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%rdx
lea    -0x40(%rbp),%rcx
lea    -0x44(%rbp),%r8
call   1070 <ipapwd_hash_password@plt>
mov    %eax,-0x48(%rbp)
cmpl   $0x0,-0x48(%rbp)
je     128d <ipapwd_cmp_password+0x10d>
jmp    12d3 <ipapwd_cmp_password+0x153>
mov    -0x44(%rbp),%eax
cmp    -0x58(%rbp),%rax
je     12a6 <ipapwd_cmp_password+0x126>
movl   $0x1,-0x48(%rbp)
jmp    12d3 <ipapwd_cmp_password+0x153>
mov    -0x50(%rbp),%rdi
mov    -0x40(%rbp),%rsi
mov    -0x44(%rbp),%edx
call   10a0 <CRYPTO_memcmp@plt>
cmp    $0x0,%rax
je     12cc <ipapwd_cmp_password+0x14c>
movl   $0x1,-0x48(%rbp)
jmp    12d3 <ipapwd_cmp_password+0x153>
movl   $0x0,-0x48(%rbp)
cmpq   $0x0,-0x50(%rbp)
je     12f1 <ipapwd_cmp_password+0x171>
mov    -0x50(%rbp),%rdi
mov    -0x30(%rbp),%rax
shl    $0x0,%rax
mov    %eax,%esi
call   1080 <OPENSSL_clear_free@plt>
mov    -0x40(%rbp),%rdi
call   1030 <free@plt>
mov    -0x48(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ipapwd_cmp_password>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%r12
mov    %rdi,%r13
movq   $0x0,(%rsp)
mov    %rsi,%rdi
mov    $0x7d,%esi
call   1060 <strchr@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1254 <ipapwd_cmp_password+0xd4>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %rax,%r14
mov    %r14d,%edi
call   1090 <OPENSSL_malloc@plt>
test   %rax,%rax
je     1254 <ipapwd_cmp_password+0xd4>
mov    %rax,%r15
mov    %r13,0x10(%rsp)
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1040 <EVP_DecodeBlock@plt>
mov    $0xffffffff,%ebp
cmp    $0x9,%rax
jb     1240 <ipapwd_cmp_password+0xc0>
mov    %rax,%r13
lea    (%rax,%r15,1),%rdx
add    $0xfffffffffffffff8,%rdx
mov    %rsp,%rcx
lea    0xc(%rsp),%r8
mov    0x10(%rsp),%rdi
mov    %r12,%rsi
call   1070 <ipapwd_hash_password@plt>
mov    %eax,%ebp
test   %eax,%eax
jne    1240 <ipapwd_cmp_password+0xc0>
mov    0xc(%rsp),%edx
mov    $0x1,%ebp
cmp    %rdx,%r13
jne    1240 <ipapwd_cmp_password+0xc0>
mov    (%rsp),%rsi
mov    %r15,%rdi
call   10a0 <CRYPTO_memcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %r15,%rdi
mov    %r14d,%esi
call   1080 <OPENSSL_clear_free@plt>
mov    (%rsp),%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @ipapwd_cmp_password(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
%14 = alloca i64
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %10
%15 = load i8*, i8** %5
store i8* %15, i8** %6
%16 = load i8*, i8** %5
%17 = call i8* @strchr(i8* %16, i8 signext 125)
store i8* %17, i8** %7
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %2
store i32 -1, i32* %3
br label %85
21: ; preds = %2
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %7
%24 = load i8*, i8** %7
%25 = call i64 @strlen(i8* %24)
store i64 %25, i64* %8
%26 = load i64, i64* %8
%27 = mul i64 1, %26
%28 = trunc i64 %27 to i32
%29 = call i64 @OPENSSL_malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %13
%31 = load i8*, i8** %13
%32 = icmp ne i8* %31, null
br i1 %32, label %34, label %33
33: ; preds = %21
store i32 -1, i32* %3
br label %85
34: ; preds = %21
%35 = load i8*, i8** %13
%36 = load i8*, i8** %7
%37 = load i64, i64* %8
%38 = call i64 @EVP_DecodeBlock(i8* %35, i8* %36, i64 %37)
store i64 %38, i64* %14
%39 = load i8*, i8** %13
%40 = icmp ne i8* %39, null
br i1 %40, label %42, label %41
41: ; preds = %34
store i32 -1, i32* %3
br label %85
42: ; preds = %34
%43 = load i64, i64* %14
%44 = icmp ule i64 %43, 8
br i1 %44, label %45, label %46
45: ; preds = %42
store i32 -1, i32* %12
br label %72
46: ; preds = %42
%47 = load i8*, i8** %13
%48 = load i64, i64* %14
%49 = sub i64 %48, 8
%50 = getelementptr inbounds i8, i8* %47, i64 %49
store i8* %50, i8** %9
%51 = load i8*, i8** %4
%52 = load i8*, i8** %6
%53 = load i8*, i8** %9
%54 = call i32 @ipapwd_hash_password(i8* %51, i8* %52, i8* %53, i8** %10, i32* %11)
store i32 %54, i32* %12
%55 = load i32, i32* %12
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %46
br label %72
58: ; preds = %46
%59 = load i32, i32* %11
%60 = zext i32 %59 to i64
%61 = load i64, i64* %14
%62 = icmp ne i64 %60, %61
br i1 %62, label %63, label %64
63: ; preds = %58
store i32 1, i32* %12
br label %72
64: ; preds = %58
%65 = load i8*, i8** %13
%66 = load i8*, i8** %10
%67 = load i32, i32* %11
%68 = call i64 @CRYPTO_memcmp(i8* %65, i8* %66, i32 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %64
store i32 1, i32* %12
br label %72
71: ; preds = %64
store i32 0, i32* %12
br label %72
72: ; preds = %71, %70, %63, %57, %45
%73 = load i8*, i8** %13
%74 = icmp ne i8* %73, null
br i1 %74, label %75, label %81
75: ; preds = %72
%76 = load i8*, i8** %13
%77 = load i64, i64* %8
%78 = mul i64 1, %77
%79 = trunc i64 %78 to i32
%80 = call i32 @OPENSSL_clear_free(i8* %76, i32 %79)
br label %81
81: ; preds = %75, %72
%82 = load i8*, i8** %10
%83 = call i32 @free(i8* %82)
%84 = load i32, i32* %12
store i32 %84, i32* %3
br label %85
85: ; preds = %81, %41, %33, %20
%86 = load i32, i32* %3
ret i32 %86
}

LLVM IR (-O2):
define i32 @ipapwd_cmp_password(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = bitcast i8** %3 to i8*
store i8* null, i8** %3
%6 = bitcast i32* %4 to i8*
%7 = tail call i8* @strchr(i8* dereferenceable(1) %1, i8 signext 125)
%8 = icmp eq i8* %7, null
br i1 %8, label %38, label %9
9: ; preds = %2
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = tail call i64 @strlen(i8* %10)
%12 = trunc i64 %11 to i32
%13 = tail call i64 @OPENSSL_malloc(i32 %12)
%14 = inttoptr i64 %13 to i8*
%15 = icmp eq i64 %13, 0
br i1 %15, label %38, label %16
16: ; preds = %9
%17 = tail call i64 @EVP_DecodeBlock(i8* %14, i8* %10, i64 %11)
%18 = icmp ult i64 %17, 9
br i1 %18, label %33, label %19
19: ; preds = %16
%20 = add i64 %17, -8
%21 = getelementptr inbounds i8, i8* %14, i64 %20
%22 = call i32 @ipapwd_hash_password(i8* %0, i8* %1, i8* %21, i8** %3, i32* %4)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %33
24: ; preds = %19
%25 = load i32, i32* %4
%26 = zext i32 %25 to i64
%27 = icmp eq i64 %17, %26
br i1 %27, label %28, label %33
28: ; preds = %24
%29 = load i8*, i8** %3
%30 = call i64 @CRYPTO_memcmp(i8* %14, i8* %29, i32 %25)
%31 = icmp ne i64 %30, 0
%32 = zext i1 %31 to i32
br label %33
33: ; preds = %19, %16, %24, %28
%34 = phi i32 [ %22, %19 ], [ -1, %16 ], [ 1, %24 ], [ %32, %28 ]
%35 = call i32 @OPENSSL_clear_free(i8* %14, i32 %12)
%36 = load i8*, i8** %3
%37 = call i32 @free(i8* %36)
br label %38
38: ; preds = %33, %9, %2
%39 = phi i32 [ -1, %2 ], [ %34, %33 ], [ -1, %9 ]
ret i32 %39
}