#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 SHA512_DIGEST_LENGTH;
unsigned char __get_raw(unsigned char);
int /*<<< orphan*/ generate_hash(unsigned char *, unsigned char *, char *, int);
int /*<<< orphan*/ memcmp(unsigned char *, unsigned char *, int);
char *strchr(char *, char);
int verify_hash(char *given_password, char *stored_password) {
  char *stored_salt, *stored_hash, *algorithm;
  if (*stored_password != '$')
    return 4;
  stored_password++;
  algorithm = stored_password;
  stored_salt = strchr(algorithm, '$');
  if (!stored_salt)
    return 4;
  *stored_salt = 0;
  stored_salt++;
  stored_hash = strchr(stored_salt, '$');
  if (!stored_hash)
    return 4;
  *stored_hash = 0;
  stored_hash++;
  unsigned char salt[SHA512_DIGEST_LENGTH];
  unsigned char hash[SHA512_DIGEST_LENGTH];
  unsigned char compare_hash[SHA512_DIGEST_LENGTH];
  int i = 0;
  while (*stored_salt) {
    unsigned char v1, v2;
    v1 = *stored_salt;
    v2 = *(stored_salt + 1);
    v1 = __get_raw(v1);
    v2 = __get_raw(v2);
    salt[i] = (v1 << 4) | v2;
    stored_salt += 2;
    i++;
  }
  i = 0;
  while (*stored_hash) {
    unsigned char v1, v2;
    v1 = *stored_hash;
    v2 = *(stored_hash + 1);
    v1 = __get_raw(v1);
    v2 = __get_raw(v2);
    compare_hash[i] = (v1 << 4) | v2;
    stored_hash += 2;
    i++;
  }
  generate_hash(hash, salt, given_password, SHA512_DIGEST_LENGTH);
  if (!memcmp(hash, compare_hash, SHA512_DIGEST_LENGTH))
    return 0;
  return 3;
}


x86 assembly (clang, -O0):
<verify_hash>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x24,%eax
je     116f <verify_hash+0x2f>
movl   $0x4,-0x4(%rbp)
jmp    13ae <verify_hash+0x26e>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x24,%esi
call   1030 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ac <verify_hash+0x6c>
movl   $0x4,-0x4(%rbp)
jmp    13ae <verify_hash+0x26e>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    $0x24,%esi
call   1030 <strchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11e8 <verify_hash+0xa8>
movl   $0x4,-0x4(%rbp)
jmp    13ae <verify_hash+0x26e>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x38(%rbp)
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x78(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x40(%rbp)
mov    (%rax),%ecx
mov    %rcx,%rsi
add    $0xf,%rsi
and    $0xfffffffffffffff0,%rsi
mov    %rsp,%rdx
sub    %rsi,%rdx
mov    %rdx,-0x70(%rbp)
mov    %rdx,%rsp
mov    %rcx,-0x48(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x68(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     12d8 <verify_hash+0x198>
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x55(%rbp)
mov    -0x20(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x56(%rbp)
movzbl -0x55(%rbp),%edi
call   1060 <__get_raw@plt>
mov    %al,-0x55(%rbp)
movzbl -0x56(%rbp),%edi
call   1060 <__get_raw@plt>
mov    %al,%cl
mov    -0x78(%rbp),%rax
mov    %cl,-0x56(%rbp)
movzbl -0x55(%rbp),%ecx
shl    $0x4,%ecx
movzbl -0x56(%rbp),%edx
or     %edx,%ecx
mov    %cl,%dl
movslq -0x54(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    126a <verify_hash+0x12a>
movl   $0x0,-0x54(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     134d <verify_hash+0x20d>
mov    -0x28(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x57(%rbp)
mov    -0x28(%rbp),%rax
mov    0x1(%rax),%al
mov    %al,-0x58(%rbp)
movzbl -0x57(%rbp),%edi
call   1060 <__get_raw@plt>
mov    %al,-0x57(%rbp)
movzbl -0x58(%rbp),%edi
call   1060 <__get_raw@plt>
mov    %al,%cl
mov    -0x68(%rbp),%rax
mov    %cl,-0x58(%rbp)
movzbl -0x57(%rbp),%ecx
shl    $0x4,%ecx
movzbl -0x58(%rbp),%edx
or     %edx,%ecx
mov    %cl,%dl
movslq -0x54(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x2,%rax
mov    %rax,-0x28(%rbp)
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    12df <verify_hash+0x19f>
mov    -0x70(%rbp),%rdi
mov    -0x78(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2c88(%rip),%rax
mov    (%rax),%ecx
call   1050 <generate_hash@plt>
mov    -0x70(%rbp),%rdi
mov    -0x68(%rbp),%rsi
mov    0x2c72(%rip),%rax
mov    (%rax),%edx
call   1040 <memcmp@plt>
cmp    $0x0,%eax
jne    1399 <verify_hash+0x259>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
jmp    13a7 <verify_hash+0x267>
movl   $0x3,-0x4(%rbp)
movl   $0x1,-0x5c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verify_hash>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0x4,%r15d
cmpb   $0x24,(%rsi)
jne    12bb <verify_hash+0x17b>
mov    %rdi,%rbx
add    $0x1,%rsi
mov    %rsi,%rdi
mov    $0x24,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     12bb <verify_hash+0x17b>
mov    %rax,%r14
movb   $0x0,(%rax)
mov    %rax,%r13
add    $0x1,%r13
mov    %r13,%rdi
mov    $0x24,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     12bb <verify_hash+0x17b>
mov    %rax,%r12
mov    %rbx,-0x50(%rbp)
mov    %rsp,-0x48(%rbp)
movb   $0x0,(%rax)
mov    0x2e33(%rip),%rcx
mov    (%rcx),%eax
mov    %rsp,%rdx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rdx
mov    %rdx,-0x30(%rbp)
mov    %rdx,%rsp
mov    (%rcx),%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,-0x40(%rbp)
mov    %rcx,%rsp
mov    %rsp,%rcx
sub    %rax,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    0x0(%r13),%al
test   %al,%al
je     1235 <verify_hash+0xf5>
xor    %r13d,%r13d
nopl   0x0(%rax,%rax,1)
movzbl 0x2(%r14,%r13,2),%ebx
movzbl %al,%edi
call   1060 <__get_raw@plt>
mov    %eax,%r15d
mov    %ebx,%edi
call   1060 <__get_raw@plt>
shl    $0x4,%r15b
or     %al,%r15b
mov    -0x30(%rbp),%rax
mov    %r15b,(%rax,%r13,1)
movzbl 0x3(%r14,%r13,2),%eax
add    $0x1,%r13
test   %al,%al
jne    1200 <verify_hash+0xc0>
mov    0x1(%r12),%al
test   %al,%al
mov    -0x38(%rbp),%r13
je     1280 <verify_hash+0x140>
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
movzbl 0x2(%r12,%r15,2),%r14d
movzbl %al,%edi
call   1060 <__get_raw@plt>
mov    %eax,%ebx
mov    %r14d,%edi
call   1060 <__get_raw@plt>
shl    $0x4,%bl
or     %al,%bl
mov    %bl,0x0(%r13,%r15,1)
movzbl 0x3(%r12,%r15,2),%eax
add    $0x1,%r15
test   %al,%al
jne    1250 <verify_hash+0x110>
mov    0x2d61(%rip),%r14
mov    (%r14),%ecx
mov    -0x40(%rbp),%rbx
mov    %rbx,%rdi
mov    -0x30(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1050 <generate_hash@plt>
mov    (%r14),%edx
mov    %rbx,%rdi
mov    %r13,%rsi
call   1040 <memcmp@plt>
xor    %ecx,%ecx
test   %eax,%eax
setne  %cl
lea    (%rcx,%rcx,2),%r15d
mov    -0x48(%rbp),%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@SHA512_DIGEST_LENGTH = global i32 0
define i32 @verify_hash(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i64
%12 = alloca i64
%13 = alloca i32
%14 = alloca i8
%15 = alloca i8
%16 = alloca i8
%17 = alloca i8
%18 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
%19 = load i8*, i8** %5
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp ne i32 %21, 36
br i1 %22, label %23, label %24
23: ; preds = %2
store i32 4, i32* %3
br label %124
24: ; preds = %2
%25 = load i8*, i8** %5
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %5
%27 = load i8*, i8** %5
store i8* %27, i8** %8
%28 = load i8*, i8** %8
%29 = call i8* @strchr(i8* %28, i8 signext 36)
store i8* %29, i8** %6
%30 = load i8*, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %33, label %32
32: ; preds = %24
store i32 4, i32* %3
br label %124
33: ; preds = %24
%34 = load i8*, i8** %6
store i8 0, i8* %34
%35 = load i8*, i8** %6
%36 = getelementptr inbounds i8, i8* %35, i32 1
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = call i8* @strchr(i8* %37, i8 signext 36)
store i8* %38, i8** %7
%39 = load i8*, i8** %7
%40 = icmp ne i8* %39, null
br i1 %40, label %42, label %41
41: ; preds = %33
store i32 4, i32* %3
br label %124
42: ; preds = %33
%43 = load i8*, i8** %7
store i8 0, i8* %43
%44 = load i8*, i8** %7
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %7
%46 = load i32, i32* @SHA512_DIGEST_LENGTH
%47 = zext i32 %46 to i64
%48 = call i8* @llvm.stacksave()
store i8* %48, i8** %9
%49 = alloca i8, i64 %47
store i64 %47, i64* %10
%50 = load i32, i32* @SHA512_DIGEST_LENGTH
%51 = zext i32 %50 to i64
%52 = alloca i8, i64 %51
store i64 %51, i64* %11
%53 = load i32, i32* @SHA512_DIGEST_LENGTH
%54 = zext i32 %53 to i64
%55 = alloca i8, i64 %54
store i64 %54, i64* %12
store i32 0, i32* %13
br label %56
56: ; preds = %60, %42
%57 = load i8*, i8** %6
%58 = load i8, i8* %57
%59 = icmp ne i8 %58, 0
br i1 %59, label %60, label %84
60: ; preds = %56
%61 = load i8*, i8** %6
%62 = load i8, i8* %61
store i8 %62, i8* %14
%63 = load i8*, i8** %6
%64 = getelementptr inbounds i8, i8* %63, i64 1
%65 = load i8, i8* %64
store i8 %65, i8* %15
%66 = load i8, i8* %14
%67 = call zeroext i8 @__get_raw(i8 zeroext %66)
store i8 %67, i8* %14
%68 = load i8, i8* %15
%69 = call zeroext i8 @__get_raw(i8 zeroext %68)
store i8 %69, i8* %15
%70 = load i8, i8* %14
%71 = zext i8 %70 to i32
%72 = shl i32 %71, 4
%73 = load i8, i8* %15
%74 = zext i8 %73 to i32
%75 = or i32 %72, %74
%76 = trunc i32 %75 to i8
%77 = load i32, i32* %13
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds i8, i8* %49, i64 %78
store i8 %76, i8* %79
%80 = load i8*, i8** %6
%81 = getelementptr inbounds i8, i8* %80, i64 2
store i8* %81, i8** %6
%82 = load i32, i32* %13
%83 = add nsw i32 %82, 1
store i32 %83, i32* %13
br label %56
84: ; preds = %56
store i32 0, i32* %13
br label %85
85: ; preds = %89, %84
%86 = load i8*, i8** %7
%87 = load i8, i8* %86
%88 = icmp ne i8 %87, 0
br i1 %88, label %89, label %113
89: ; preds = %85
%90 = load i8*, i8** %7
%91 = load i8, i8* %90
store i8 %91, i8* %16
%92 = load i8*, i8** %7
%93 = getelementptr inbounds i8, i8* %92, i64 1
%94 = load i8, i8* %93
store i8 %94, i8* %17
%95 = load i8, i8* %16
%96 = call zeroext i8 @__get_raw(i8 zeroext %95)
store i8 %96, i8* %16
%97 = load i8, i8* %17
%98 = call zeroext i8 @__get_raw(i8 zeroext %97)
store i8 %98, i8* %17
%99 = load i8, i8* %16
%100 = zext i8 %99 to i32
%101 = shl i32 %100, 4
%102 = load i8, i8* %17
%103 = zext i8 %102 to i32
%104 = or i32 %101, %103
%105 = trunc i32 %104 to i8
%106 = load i32, i32* %13
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds i8, i8* %55, i64 %107
store i8 %105, i8* %108
%109 = load i8*, i8** %7
%110 = getelementptr inbounds i8, i8* %109, i64 2
store i8* %110, i8** %7
%111 = load i32, i32* %13
%112 = add nsw i32 %111, 1
store i32 %112, i32* %13
br label %85
113: ; preds = %85
%114 = load i8*, i8** %4
%115 = load i32, i32* @SHA512_DIGEST_LENGTH
%116 = call i32 @generate_hash(i8* %52, i8* %49, i8* %114, i32 %115)
%117 = load i32, i32* @SHA512_DIGEST_LENGTH
%118 = call i32 @memcmp(i8* %52, i8* %55, i32 %117)
%119 = icmp ne i32 %118, 0
br i1 %119, label %121, label %120
120: ; preds = %113
store i32 0, i32* %3
store i32 1, i32* %18
br label %122
121: ; preds = %113
store i32 3, i32* %3
store i32 1, i32* %18
br label %122
122: ; preds = %121, %120
%123 = load i8*, i8** %9
br label %124
124: ; preds = %122, %41, %32, %23
%125 = load i32, i32* %3
ret i32 %125
}

LLVM IR (-O2):
@SHA512_DIGEST_LENGTH = global i32 0
define i32 @verify_hash(i8* %0, i8* %1) {
%3 = load i8, i8* %1
%4 = icmp eq i8 %3, 36
br i1 %4, label %5, label %65
5: ; preds = %2
%6 = getelementptr inbounds i8, i8* %1, i64 1
%7 = tail call i8* @strchr(i8* %6, i8 signext 36)
%8 = icmp eq i8* %7, null
br i1 %8, label %65, label %9
9: ; preds = %5
store i8 0, i8* %7
%10 = getelementptr inbounds i8, i8* %7, i64 1
%11 = tail call i8* @strchr(i8* %10, i8 signext 36)
%12 = icmp eq i8* %11, null
br i1 %12, label %65, label %13
13: ; preds = %9
store i8 0, i8* %11
%14 = getelementptr inbounds i8, i8* %11, i64 1
%15 = load i32, i32* @SHA512_DIGEST_LENGTH
%16 = zext i32 %15 to i64
%17 = tail call i8* @llvm.stacksave()
%18 = alloca i8, i64 %16
%19 = load i32, i32* @SHA512_DIGEST_LENGTH
%20 = zext i32 %19 to i64
%21 = alloca i8, i64 %20
%22 = alloca i8, i64 %20
%23 = load i8, i8* %10
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %28
25: ; preds = %28, %13
%26 = load i8, i8* %14
%27 = icmp eq i8 %26, 0
br i1 %27, label %58, label %43
28: ; preds = %13, %28
%29 = phi i64 [ %40, %28 ], [ 0, %13 ]
%30 = phi i8 [ %41, %28 ], [ %23, %13 ]
%31 = phi i8* [ %39, %28 ], [ %10, %13 ]
%32 = getelementptr inbounds i8, i8* %31, i64 1
%33 = load i8, i8* %32
%34 = tail call zeroext i8 @__get_raw(i8 zeroext %30)
%35 = tail call zeroext i8 @__get_raw(i8 zeroext %33)
%36 = shl i8 %34, 4
%37 = or i8 %36, %35
%38 = getelementptr inbounds i8, i8* %18, i64 %29
store i8 %37, i8* %38
%39 = getelementptr inbounds i8, i8* %31, i64 2
%40 = add nuw i64 %29, 1
%41 = load i8, i8* %39
%42 = icmp eq i8 %41, 0
br i1 %42, label %25, label %28
43: ; preds = %25, %43
%44 = phi i64 [ %55, %43 ], [ 0, %25 ]
%45 = phi i8 [ %56, %43 ], [ %26, %25 ]
%46 = phi i8* [ %54, %43 ], [ %14, %25 ]
%47 = getelementptr inbounds i8, i8* %46, i64 1
%48 = load i8, i8* %47
%49 = tail call zeroext i8 @__get_raw(i8 zeroext %45)
%50 = tail call zeroext i8 @__get_raw(i8 zeroext %48)
%51 = shl i8 %49, 4
%52 = or i8 %51, %50
%53 = getelementptr inbounds i8, i8* %22, i64 %44
store i8 %52, i8* %53
%54 = getelementptr inbounds i8, i8* %46, i64 2
%55 = add nuw i64 %44, 1
%56 = load i8, i8* %54
%57 = icmp eq i8 %56, 0
br i1 %57, label %58, label %43
58: ; preds = %43, %25
%59 = load i32, i32* @SHA512_DIGEST_LENGTH
%60 = call i32 @generate_hash(i8* %21, i8* %18, i8* %0, i32 %59)
%61 = load i32, i32* @SHA512_DIGEST_LENGTH
%62 = call i32 @memcmp(i8* %21, i8* %22, i32 %61)
%63 = icmp eq i32 %62, 0
%64 = select i1 %63, i32 0, i32 3
br label %65
65: ; preds = %9, %5, %2, %58
%66 = phi i32 [ %64, %58 ], [ 4, %2 ], [ 4, %5 ], [ 4, %9 ]
ret i32 %66
}