#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*/ assert(int /*<<< orphan*/);
scalar_t__ crypto_sha256(unsigned char *, unsigned int const, unsigned char *);
int /*<<< orphan*/ free(unsigned char *);
unsigned char *malloc(unsigned int const);
scalar_t__ memcmp(unsigned char *, unsigned char const *, int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *,
                          unsigned int const);
int crypto_verify_salted_sha256(const unsigned char *salted_hash,
                                const unsigned char *data,
                                const unsigned int length) {
  int ret = 1;
  unsigned char salted_hash_new[40];
  unsigned char *buf;
  assert(salted_hash != NULL);
  assert(data != NULL);
  if (!salted_hash || !data || length == 0)
    return 1;
  buf = malloc(length + 8);
  if (!buf)
    return 1;
  memcpy(buf, salted_hash, 8);
  memcpy(salted_hash_new, buf, 8);
  memcpy(buf + 8, data, length);
  if (crypto_sha256(buf, length + 8, salted_hash_new + 8) != 0) {
    ret = 3;
    goto cleanup;
  }
  if (memcmp(salted_hash_new, salted_hash, 40) == 0)
    ret = 0;
  else
    ret = 1;
cleanup:
  free(buf);
  return ret;
}


x86 assembly (clang, -O0):
<crypto_verify_salted_sha256>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x1,-0x20(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x18(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
cmpq   $0x0,-0x10(%rbp)
je     11be <crypto_verify_salted_sha256+0x5e>
cmpq   $0x0,-0x18(%rbp)
je     11be <crypto_verify_salted_sha256+0x5e>
cmpl   $0x0,-0x1c(%rbp)
jne    11ca <crypto_verify_salted_sha256+0x6a>
movl   $0x1,-0x4(%rbp)
jmp    1298 <crypto_verify_salted_sha256+0x138>
mov    -0x1c(%rbp),%edi
add    $0x8,%edi
call   1070 <malloc@plt>
mov    %rax,-0x58(%rbp)
cmpq   $0x0,-0x58(%rbp)
jne    11f0 <crypto_verify_salted_sha256+0x90>
movl   $0x1,-0x4(%rbp)
jmp    1298 <crypto_verify_salted_sha256+0x138>
mov    -0x58(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
lea    -0x50(%rbp),%rdi
mov    -0x58(%rbp),%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    -0x58(%rbp),%rdi
add    $0x8,%rdi
mov    -0x18(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1060 <memcpy@plt>
mov    -0x58(%rbp),%rdi
mov    -0x1c(%rbp),%esi
add    $0x8,%esi
lea    -0x50(%rbp),%rdx
add    $0x8,%rdx
call   1080 <crypto_sha256@plt>
cmp    $0x0,%rax
je     1255 <crypto_verify_salted_sha256+0xf5>
movl   $0x3,-0x20(%rbp)
jmp    1289 <crypto_verify_salted_sha256+0x129>
lea    -0x50(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x28,%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
jne    127d <crypto_verify_salted_sha256+0x11d>
movl   $0x0,-0x20(%rbp)
jmp    1284 <crypto_verify_salted_sha256+0x124>
movl   $0x1,-0x20(%rbp)
jmp    1289 <crypto_verify_salted_sha256+0x129>
mov    -0x58(%rbp),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<crypto_verify_salted_sha256>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %rsi,%rbx
mov    %rdi,%r12
xor    %edi,%edi
test   %r12,%r12
setne  %dil
call   1050 <assert@plt>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1050 <assert@plt>
mov    $0x1,%ebp
test   %r12,%r12
je     1234 <crypto_verify_salted_sha256+0xd4>
test   %rbx,%rbx
je     1234 <crypto_verify_salted_sha256+0xd4>
test   %r14d,%r14d
je     1234 <crypto_verify_salted_sha256+0xd4>
lea    0x8(%r14),%r13d
mov    %r13d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     1234 <crypto_verify_salted_sha256+0xd4>
mov    %rax,%r15
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    %rsp,%rdi
mov    %r15,%rsi
mov    $0x8,%edx
call   1060 <memcpy@plt>
mov    %r15,%rdi
add    $0x8,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1060 <memcpy@plt>
lea    0x8(%rsp),%rdx
mov    %r15,%rdi
mov    %r13d,%esi
call   1080 <crypto_sha256@plt>
mov    $0x3,%ebp
test   %rax,%rax
jne    122c <crypto_verify_salted_sha256+0xcc>
mov    %rsp,%rdi
mov    %r12,%rsi
mov    $0x28,%edx
call   1040 <memcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
setne  %bpl
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebp,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @crypto_verify_salted_sha256(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca [40 x i8]
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 1, i32* %8
%11 = load i8*, i8** %5
%12 = icmp ne i8* %11, null
%13 = zext i1 %12 to i32
%14 = call i32 @assert(i32 %13)
%15 = load i8*, i8** %6
%16 = icmp ne i8* %15, null
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i8*, i8** %5
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %27
21: ; preds = %3
%22 = load i8*, i8** %6
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* %7
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %28
27: ; preds = %24, %21, %3
store i32 1, i32* %4
br label %67
28: ; preds = %24
%29 = load i32, i32* %7
%30 = add i32 %29, 8
%31 = call i8* @malloc(i32 %30)
store i8* %31, i8** %10
%32 = load i8*, i8** %10
%33 = icmp ne i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %28
store i32 1, i32* %4
br label %67
35: ; preds = %28
%36 = load i8*, i8** %10
%37 = load i8*, i8** %5
%38 = call i32 @memcpy(i8* %36, i8* %37, i32 8)
%39 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%40 = load i8*, i8** %10
%41 = call i32 @memcpy(i8* %39, i8* %40, i32 8)
%42 = load i8*, i8** %10
%43 = getelementptr inbounds i8, i8* %42, i64 8
%44 = load i8*, i8** %6
%45 = load i32, i32* %7
%46 = call i32 @memcpy(i8* %43, i8* %44, i32 %45)
%47 = load i8*, i8** %10
%48 = load i32, i32* %7
%49 = add i32 %48, 8
%50 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%51 = getelementptr inbounds i8, i8* %50, i64 8
%52 = call i64 @crypto_sha256(i8* %47, i32 %49, i8* %51)
%53 = icmp ne i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %35
store i32 3, i32* %8
br label %63
55: ; preds = %35
%56 = getelementptr inbounds [40 x i8], [40 x i8]* %9, i64 0, i64 0
%57 = load i8*, i8** %5
%58 = call i64 @memcmp(i8* %56, i8* %57, i32 40)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %55
store i32 0, i32* %8
br label %62
61: ; preds = %55
store i32 1, i32* %8
br label %62
62: ; preds = %61, %60
br label %63
63: ; preds = %62, %54
%64 = load i8*, i8** %10
%65 = call i32 @free(i8* %64)
%66 = load i32, i32* %8
store i32 %66, i32* %4
br label %67
67: ; preds = %63, %34, %27
%68 = load i32, i32* %4
ret i32 %68
}

LLVM IR (-O2):
define i32 @crypto_verify_salted_sha256(i8* %0, i8* %1, i32 %2) {
%4 = alloca [40 x i8]
%5 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 0
%6 = icmp ne i8* %0, null
%7 = zext i1 %6 to i32
%8 = tail call i32 @assert(i32 %7)
%9 = icmp ne i8* %1, null
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = and i1 %6, %9
%13 = icmp ne i32 %2, 0
%14 = and i1 %12, %13
br i1 %14, label %15, label %34
15: ; preds = %3
%16 = add i32 %2, 8
%17 = tail call i8* @malloc(i32 %16)
%18 = icmp eq i8* %17, null
br i1 %18, label %34, label %19
19: ; preds = %15
%20 = tail call i32 @memcpy(i8* %17, i8* %0, i32 8)
%21 = call i32 @memcpy(i8* %5, i8* %17, i32 8)
%22 = getelementptr inbounds i8, i8* %17, i64 8
%23 = call i32 @memcpy(i8* %22, i8* %1, i32 %2)
%24 = getelementptr inbounds [40 x i8], [40 x i8]* %4, i64 0, i64 8
%25 = call i64 @crypto_sha256(i8* %17, i32 %16, i8* %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %27, label %31
27: ; preds = %19
%28 = call i64 @memcmp(i8* %5, i8* %0, i32 40)
%29 = icmp ne i64 %28, 0
%30 = zext i1 %29 to i32
br label %31
31: ; preds = %27, %19
%32 = phi i32 [ 3, %19 ], [ %30, %27 ]
%33 = call i32 @free(i8* %17)
br label %34
34: ; preds = %15, %3, %31
%35 = phi i32 [ %32, %31 ], [ 1, %3 ], [ 1, %15 ]
ret i32 %35
}