#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__ memcmp(unsigned char *, ...);
int strlen(char *);
int pkcs1_v1_5_insecure_validate(const char *signature,
                                 const size_t signature_len,
                                 const unsigned char *origin_hash,
                                 const size_t origin_hash_len) {
  unsigned char *sig_pointer, *hash;
  sig_pointer = (unsigned char *)signature;
  if (*sig_pointer != 0x00)
    return -1;
  sig_pointer++;
  if (*sig_pointer != 0x01) {
    return -1;
  }
  sig_pointer++;
  while (0x00 != *sig_pointer) {
    if (0xff != *sig_pointer) {
      return -1;
    }
    sig_pointer++;
    if (sig_pointer > (unsigned char *)(signature + signature_len))
      return -1;
  }
  if (0 == memcmp(sig_pointer + 1, ("sha1"), strlen(("sha1")))) {
    hash = sig_pointer + 1 + strlen(("sha1"));
  } else if (0 == memcmp(sig_pointer + 1, ("sha256"), strlen(("sha256")))) {
    hash = sig_pointer + 1 + strlen(("sha256"));
  } else
    return -1;
  return (0 == memcmp(hash, origin_hash, origin_hash_len));
}


x86 assembly (clang, -O0):
<pkcs1_v1_5_insecure_validate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     115c <pkcs1_v1_5_insecure_validate+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <pkcs1_v1_5_insecure_validate+0x1d8>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x1,%eax
je     1184 <pkcs1_v1_5_insecure_validate+0x64>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <pkcs1_v1_5_insecure_validate+0x1d8>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
movzbl (%rax),%ecx
xor    %eax,%eax
cmp    %ecx,%eax
je     11f3 <pkcs1_v1_5_insecure_validate+0xd3>
mov    -0x30(%rbp),%rax
movzbl (%rax),%ecx
mov    $0xff,%eax
cmp    %ecx,%eax
je     11c1 <pkcs1_v1_5_insecure_validate+0xa1>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <pkcs1_v1_5_insecure_validate+0x1d8>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    -0x18(%rbp),%rcx
cmp    %rcx,%rax
jbe    11ee <pkcs1_v1_5_insecure_validate+0xce>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <pkcs1_v1_5_insecure_validate+0x1d8>
jmp    1190 <pkcs1_v1_5_insecure_validate+0x70>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x40(%rbp)
lea    0xdfa(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x40(%rbp),%rdi
mov    %eax,%edx
lea    0xde8(%rip),%rsi
xor    %eax,%eax
call   1040 <memcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    125a <pkcs1_v1_5_insecure_validate+0x13a>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x48(%rbp)
lea    0xdc0(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    12d2 <pkcs1_v1_5_insecure_validate+0x1b2>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x50(%rbp)
lea    0xd98(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x50(%rbp),%rdi
mov    %eax,%edx
lea    0xd86(%rip),%rsi
xor    %eax,%eax
call   1040 <memcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    12c1 <pkcs1_v1_5_insecure_validate+0x1a1>
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
lea    0xd5e(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x38(%rbp)
jmp    12cd <pkcs1_v1_5_insecure_validate+0x1ad>
movl   $0xffffffff,-0x4(%rbp)
jmp    12f8 <pkcs1_v1_5_insecure_validate+0x1d8>
jmp    12d2 <pkcs1_v1_5_insecure_validate+0x1b2>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x28(%rbp),%rdx
xor    %eax,%eax
call   1040 <memcmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pkcs1_v1_5_insecure_validate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    $0xffffffff,%ebp
cmpb   $0x0,(%rdi)
jne    11d6 <pkcs1_v1_5_insecure_validate+0xb6>
mov    %rdi,%rbx
cmpb   $0x1,0x1(%rdi)
jne    11d6 <pkcs1_v1_5_insecure_validate+0xb6>
mov    %rcx,%r14
mov    %rdx,%r15
add    %rbx,%rsi
add    $0x3,%rbx
movzbl -0x1(%rbx),%eax
cmp    $0xff,%al
jne    1166 <pkcs1_v1_5_insecure_validate+0x46>
lea    0x1(%rbx),%rax
cmp    %rsi,%rbx
mov    %rax,%rbx
jbe    1150 <pkcs1_v1_5_insecure_validate+0x30>
jmp    11d6 <pkcs1_v1_5_insecure_validate+0xb6>
test   %al,%al
jne    11d6 <pkcs1_v1_5_insecure_validate+0xb6>
lea    0xe8f(%rip),%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <memcmp@plt>
test   %rax,%rax
je     11b0 <pkcs1_v1_5_insecure_validate+0x90>
lea    0xe71(%rip),%r12
mov    %r12,%rdi
call   1030 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
xor    %eax,%eax
call   1040 <memcmp@plt>
test   %rax,%rax
jne    11d6 <pkcs1_v1_5_insecure_validate+0xb6>
mov    %r12,%rdi
call   1030 <strlen@plt>
cltq
add    %rax,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1040 <memcmp@plt>
xor    %ebp,%ebp
test   %rax,%rax
sete   %bpl
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"sha1\00"
@.str.1 = [7 x i8] c"sha256\00"
define i32 @pkcs1_v1_5_insecure_validate(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i64 %3, i64* %9
%12 = load i8*, i8** %6
store i8* %12, i8** %10
%13 = load i8*, i8** %10
%14 = load i8, i8* %13
%15 = zext i8 %14 to i32
%16 = icmp ne i32 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %4
store i32 -1, i32* %5
br label %83
18: ; preds = %4
%19 = load i8*, i8** %10
%20 = getelementptr inbounds i8, i8* %19, i32 1
store i8* %20, i8** %10
%21 = load i8*, i8** %10
%22 = load i8, i8* %21
%23 = zext i8 %22 to i32
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %26
25: ; preds = %18
store i32 -1, i32* %5
br label %83
26: ; preds = %18
%27 = load i8*, i8** %10
%28 = getelementptr inbounds i8, i8* %27, i32 1
store i8* %28, i8** %10
br label %29
29: ; preds = %49, %26
%30 = load i8*, i8** %10
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = icmp ne i32 0, %32
br i1 %33, label %34, label %50
34: ; preds = %29
%35 = load i8*, i8** %10
%36 = load i8, i8* %35
%37 = zext i8 %36 to i32
%38 = icmp ne i32 255, %37
br i1 %38, label %39, label %40
39: ; preds = %34
store i32 -1, i32* %5
br label %83
40: ; preds = %34
%41 = load i8*, i8** %10
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %10
%43 = load i8*, i8** %10
%44 = load i8*, i8** %6
%45 = load i64, i64* %7
%46 = getelementptr inbounds i8, i8* %44, i64 %45
%47 = icmp ugt i8* %43, %46
br i1 %47, label %48, label %49
48: ; preds = %40
store i32 -1, i32* %5
br label %83
49: ; preds = %40
br label %29
50: ; preds = %29
%51 = load i8*, i8** %10
%52 = getelementptr inbounds i8, i8* %51, i64 1
%53 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%54 = call i64 (i8*, ...) @memcmp(i8* %52, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %53)
%55 = icmp eq i64 0, %54
br i1 %55, label %56, label %62
56: ; preds = %50
%57 = load i8*, i8** %10
%58 = getelementptr inbounds i8, i8* %57, i64 1
%59 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8, i8* %58, i64 %60
store i8* %61, i8** %11
br label %76
62: ; preds = %50
%63 = load i8*, i8** %10
%64 = getelementptr inbounds i8, i8* %63, i64 1
%65 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%66 = call i64 (i8*, ...) @memcmp(i8* %64, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %65)
%67 = icmp eq i64 0, %66
br i1 %67, label %68, label %74
68: ; preds = %62
%69 = load i8*, i8** %10
%70 = getelementptr inbounds i8, i8* %69, i64 1
%71 = call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds i8, i8* %70, i64 %72
store i8* %73, i8** %11
br label %75
74: ; preds = %62
store i32 -1, i32* %5
br label %83
75: ; preds = %68
br label %76
76: ; preds = %75, %56
%77 = load i8*, i8** %11
%78 = load i8*, i8** %8
%79 = load i64, i64* %9
%80 = call i64 (i8*, ...) @memcmp(i8* %77, i8* %78, i64 %79)
%81 = icmp eq i64 0, %80
%82 = zext i1 %81 to i32
store i32 %82, i32* %5
br label %83
83: ; preds = %76, %74, %48, %39, %25, %17
%84 = load i32, i32* %5
ret i32 %84
}

LLVM IR (-O2):
@.str = [5 x i8] c"sha1\00"
@.str.1 = [7 x i8] c"sha256\00"
define i32 @pkcs1_v1_5_insecure_validate(i8* %0, i64 %1, i8* %2, i64 %3) {
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %7, label %37
7: ; preds = %4
%8 = getelementptr inbounds i8, i8* %0, i64 1
%9 = load i8, i8* %8
%10 = icmp eq i8 %9, 1
br i1 %10, label %11, label %37
11: ; preds = %7
%12 = getelementptr inbounds i8, i8* %0, i64 2
%13 = getelementptr inbounds i8, i8* %0, i64 %1
br label %14
14: ; preds = %17, %11
%15 = phi i8* [ %12, %11 ], [ %18, %17 ]
%16 = load i8, i8* %15
switch i8 %16, label %37 [
i8 0, label %20
i8 -1, label %17
]
17: ; preds = %14
%18 = getelementptr inbounds i8, i8* %15, i64 1
%19 = icmp ugt i8* %18, %13
br i1 %19, label %37, label %14
20: ; preds = %14
%21 = getelementptr inbounds i8, i8* %15, i64 1
%22 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%23 = tail call i64 (i8*, ...) @memcmp(i8* %21, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 %22)
%24 = icmp eq i64 %23, 0
br i1 %24, label %29, label %25
25: ; preds = %20
%26 = tail call i32 @strlen(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0))
%27 = tail call i64 (i8*, ...) @memcmp(i8* %21, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i32 %26)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %37
29: ; preds = %25, %20
%30 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), %20 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), %25 ]
%31 = tail call i32 @strlen(i8* %30)
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %21, i64 %32
%34 = tail call i64 (i8*, ...) @memcmp(i8* %33, i8* %2, i64 %3)
%35 = icmp eq i64 %34, 0
%36 = zext i1 %35 to i32
br label %37
37: ; preds = %17, %14, %25, %7, %4, %29
%38 = phi i32 [ %36, %29 ], [ -1, %4 ], [ -1, %7 ], [ -1, %25 ], [ -1, %14 ], [ -1, %17 ]
ret i32 %38
}