#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 */
typedef struct TYPE_4__ TYPE_1__;

/* Type definitions */
struct TYPE_4__ {
  int /*<<< orphan*/ data;
  scalar_t__ length;
};
typedef int /*<<< orphan*/ RSA;
typedef TYPE_1__ ASN1_OCTET_STRING;

/* Variables and functions */
int /*<<< orphan*/ ASN1_OCTET_STRING_free(TYPE_1__ *);
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ RSA_PKCS1_PADDING;
int /*<<< orphan*/ RSA_R_BAD_SIGNATURE;
int /*<<< orphan*/ RSA_R_WRONG_SIGNATURE_LENGTH;
int RSA_public_decrypt(int, unsigned char *, unsigned char *,
                       int /*<<< orphan*/ *, int /*<<< orphan*/);
scalar_t__ RSA_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSAerror(int /*<<< orphan*/);
TYPE_1__ *d2i_ASN1_OCTET_STRING(int /*<<< orphan*/ *, unsigned char const **,
                                long);
int /*<<< orphan*/ freezero(unsigned char *, unsigned int);
unsigned char *malloc(unsigned int);
scalar_t__ memcmp(unsigned char const *, int /*<<< orphan*/, unsigned int);
int RSA_verify_ASN1_OCTET_STRING(int dtype, const unsigned char *m,
                                 unsigned int m_len, unsigned char *sigbuf,
                                 unsigned int siglen, RSA *rsa) {
  int i, ret = 0;
  unsigned char *s;
  const unsigned char *p;
  ASN1_OCTET_STRING *sig = NULL;
  if (siglen != (unsigned int)RSA_size(rsa)) {
    RSAerror(RSA_R_WRONG_SIGNATURE_LENGTH);
    return 0;
  }
  s = malloc(siglen);
  if (s == NULL) {
    RSAerror(ERR_R_MALLOC_FAILURE);
    goto err;
  }
  i = RSA_public_decrypt((int)siglen, sigbuf, s, rsa, RSA_PKCS1_PADDING);
  if (i <= 0)
    goto err;
  p = s;
  sig = d2i_ASN1_OCTET_STRING(NULL, &p, (long)i);
  if (sig == NULL)
    goto err;
  if ((unsigned int)sig->length != m_len || memcmp(m, sig->data, m_len) != 0) {
    RSAerror(RSA_R_BAD_SIGNATURE);
  } else
    ret = 1;
err:
  ASN1_OCTET_STRING_free(sig);
  freezero(s, (unsigned int)siglen);
  return ret;
}


x86 assembly (clang, -O0):
<RSA_verify_ASN1_OCTET_STRING>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x0,-0x38(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x24(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <RSA_size@plt>
mov    %rax,%rcx
mov    -0x54(%rbp),%eax
cmp    %ecx,%eax
je     11e4 <RSA_verify_ASN1_OCTET_STRING+0x64>
mov    0x2e17(%rip),%rax
mov    (%rax),%edi
call   10a0 <RSAerror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12d2 <RSA_verify_ASN1_OCTET_STRING+0x152>
mov    -0x24(%rbp),%edi
call   1090 <malloc@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jne    120e <RSA_verify_ASN1_OCTET_STRING+0x8e>
mov    0x2dd6(%rip),%rax
mov    (%rax),%edi
call   10a0 <RSAerror@plt>
jmp    12b7 <RSA_verify_ASN1_OCTET_STRING+0x137>
mov    -0x24(%rbp),%edi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x2d9c(%rip),%rax
mov    (%rax),%r8d
call   1080 <RSA_public_decrypt@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jg     123e <RSA_verify_ASN1_OCTET_STRING+0xbe>
jmp    12b7 <RSA_verify_ASN1_OCTET_STRING+0x137>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
movslq -0x34(%rbp),%rdx
xor    %eax,%eax
mov    %eax,%edi
lea    -0x48(%rbp),%rsi
call   1050 <d2i_ASN1_OCTET_STRING@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    126b <RSA_verify_ASN1_OCTET_STRING+0xeb>
jmp    12b7 <RSA_verify_ASN1_OCTET_STRING+0x137>
mov    -0x50(%rbp),%rax
mov    0x8(%rax),%rax
cmp    -0x14(%rbp),%eax
jne    1298 <RSA_verify_ASN1_OCTET_STRING+0x118>
mov    -0x10(%rbp),%rdi
mov    -0x50(%rbp),%rax
mov    (%rax),%esi
mov    -0x14(%rbp),%edx
call   1070 <memcmp@plt>
cmp    $0x0,%rax
je     12ab <RSA_verify_ASN1_OCTET_STRING+0x12b>
mov    0x2d41(%rip),%rax
mov    (%rax),%edi
call   10a0 <RSAerror@plt>
jmp    12b2 <RSA_verify_ASN1_OCTET_STRING+0x132>
movl   $0x1,-0x38(%rbp)
jmp    12b7 <RSA_verify_ASN1_OCTET_STRING+0x137>
mov    -0x50(%rbp),%rdi
call   1060 <ASN1_OCTET_STRING_free@plt>
mov    -0x40(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1030 <freezero@plt>
mov    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RSA_verify_ASN1_OCTET_STRING>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r9,%rbp
mov    %r8d,%r12d
mov    %rcx,%rbx
mov    %edx,%r13d
mov    %rsi,%r15
mov    %r9,%rdi
call   1040 <RSA_size@plt>
cmp    %r12d,%eax
jne    121d <RSA_verify_ASN1_OCTET_STRING+0x9d>
mov    %r12d,%edi
call   1090 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1230 <RSA_verify_ASN1_OCTET_STRING+0xb0>
mov    0x2e02(%rip),%rax
mov    (%rax),%r8d
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %rbp,%rcx
call   1080 <RSA_public_decrypt@plt>
test   %eax,%eax
jle    123e <RSA_verify_ASN1_OCTET_STRING+0xbe>
mov    %r15,%rbx
mov    %r14,(%rsp)
mov    %eax,%edx
xor    %r15d,%r15d
mov    %rsp,%rsi
xor    %edi,%edi
call   1050 <d2i_ASN1_OCTET_STRING@plt>
test   %rax,%rax
je     1241 <RSA_verify_ASN1_OCTET_STRING+0xc1>
mov    %rax,%rbp
cmp    %r13d,0x8(%rax)
jne    120d <RSA_verify_ASN1_OCTET_STRING+0x8d>
mov    0x0(%rbp),%esi
mov    %rbx,%rdi
mov    %r13d,%edx
call   1070 <memcmp@plt>
test   %rax,%rax
je     1268 <RSA_verify_ASN1_OCTET_STRING+0xe8>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   10a0 <RSAerror@plt>
jmp    1243 <RSA_verify_ASN1_OCTET_STRING+0xc3>
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
call   10a0 <RSAerror@plt>
xor    %r15d,%r15d
jmp    1256 <RSA_verify_ASN1_OCTET_STRING+0xd6>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10a0 <RSAerror@plt>
xor    %r15d,%r15d
xor    %ebp,%ebp
mov    %rbp,%rdi
call   1060 <ASN1_OCTET_STRING_free@plt>
mov    %r14,%rdi
mov    %r12d,%esi
call   1030 <freezero@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    $0x1,%r15d
jmp    1243 <RSA_verify_ASN1_OCTET_STRING+0xc3>

LLVM IR (-O0):
%struct.TYPE_4__ = type { i32, i64 }
@RSA_R_WRONG_SIGNATURE_LENGTH = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@RSA_PKCS1_PADDING = global i32 0
@RSA_R_BAD_SIGNATURE = global i32 0
define i32 @RSA_verify_ASN1_OCTET_STRING(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4, i32* %5) {
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32*
%14 = alloca i32
%15 = alloca i32
%16 = alloca i8*
%17 = alloca i8*
%18 = alloca %struct.TYPE_4__*
store i32 %0, i32* %8
store i8* %1, i8** %9
store i32 %2, i32* %10
store i8* %3, i8** %11
store i32 %4, i32* %12
store i32* %5, i32** %13
store i32 0, i32* %15
store %struct.TYPE_4__* null, %struct.TYPE_4__** %18
%19 = load i32, i32* %12
%20 = load i32*, i32** %13
%21 = call i64 @RSA_size(i32* %20)
%22 = trunc i64 %21 to i32
%23 = icmp ne i32 %19, %22
br i1 %23, label %24, label %27
24: ; preds = %6
%25 = load i32, i32* @RSA_R_WRONG_SIGNATURE_LENGTH
%26 = call i32 @RSAerror(i32 %25)
store i32 0, i32* %7
br label %80
27: ; preds = %6
%28 = load i32, i32* %12
%29 = call i8* @malloc(i32 %28)
store i8* %29, i8** %16
%30 = load i8*, i8** %16
%31 = icmp eq i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %27
%33 = load i32, i32* @ERR_R_MALLOC_FAILURE
%34 = call i32 @RSAerror(i32 %33)
br label %73
35: ; preds = %27
%36 = load i32, i32* %12
%37 = load i8*, i8** %11
%38 = load i8*, i8** %16
%39 = load i32*, i32** %13
%40 = load i32, i32* @RSA_PKCS1_PADDING
%41 = call i32 @RSA_public_decrypt(i32 %36, i8* %37, i8* %38, i32* %39, i32 %40)
store i32 %41, i32* %14
%42 = load i32, i32* %14
%43 = icmp sle i32 %42, 0
br i1 %43, label %44, label %45
44: ; preds = %35
br label %73
45: ; preds = %35
%46 = load i8*, i8** %16
store i8* %46, i8** %17
%47 = load i32, i32* %14
%48 = sext i32 %47 to i64
%49 = call %struct.TYPE_4__* @d2i_ASN1_OCTET_STRING(i32* null, i8** %17, i64 %48)
store %struct.TYPE_4__* %49, %struct.TYPE_4__** %18
%50 = load %struct.TYPE_4__*, %struct.TYPE_4__** %18
%51 = icmp eq %struct.TYPE_4__* %50, null
br i1 %51, label %52, label %53
52: ; preds = %45
br label %73
53: ; preds = %45
%54 = load %struct.TYPE_4__*, %struct.TYPE_4__** %18
%55 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %54, i32 0, i32 1
%56 = load i64, i64* %55
%57 = trunc i64 %56 to i32
%58 = load i32, i32* %10
%59 = icmp ne i32 %57, %58
br i1 %59, label %68, label %60
60: ; preds = %53
%61 = load i8*, i8** %9
%62 = load %struct.TYPE_4__*, %struct.TYPE_4__** %18
%63 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %62, i32 0, i32 0
%64 = load i32, i32* %63
%65 = load i32, i32* %10
%66 = call i64 @memcmp(i8* %61, i32 %64, i32 %65)
%67 = icmp ne i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %60, %53
%69 = load i32, i32* @RSA_R_BAD_SIGNATURE
%70 = call i32 @RSAerror(i32 %69)
br label %72
71: ; preds = %60
store i32 1, i32* %15
br label %72
72: ; preds = %71, %68
br label %73
73: ; preds = %72, %52, %44, %32
%74 = load %struct.TYPE_4__*, %struct.TYPE_4__** %18
%75 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_4__* %74)
%76 = load i8*, i8** %16
%77 = load i32, i32* %12
%78 = call i32 @freezero(i8* %76, i32 %77)
%79 = load i32, i32* %15
store i32 %79, i32* %7
br label %80
80: ; preds = %73, %24
%81 = load i32, i32* %7
ret i32 %81
}

LLVM IR (-O2):
%struct.TYPE_4__ = type { i32, i64 }
@RSA_R_WRONG_SIGNATURE_LENGTH = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@RSA_PKCS1_PADDING = global i32 0
@RSA_R_BAD_SIGNATURE = global i32 0
define i32 @RSA_verify_ASN1_OCTET_STRING(i32 %0, i8* %1, i32 %2, i8* %3, i32 %4, i32* %5) {
%7 = alloca i8*
%8 = bitcast i8** %7 to i8*
%9 = tail call i64 @RSA_size(i32* %5)
%10 = trunc i64 %9 to i32
%11 = icmp eq i32 %10, %4
br i1 %11, label %15, label %12
12: ; preds = %6
%13 = load i32, i32* @RSA_R_WRONG_SIGNATURE_LENGTH
%14 = tail call i32 @RSAerror(i32 %13)
br label %47
15: ; preds = %6
%16 = tail call i8* @malloc(i32 %4)
%17 = icmp eq i8* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @ERR_R_MALLOC_FAILURE
%20 = tail call i32 @RSAerror(i32 %19)
br label %42
21: ; preds = %15
%22 = load i32, i32* @RSA_PKCS1_PADDING
%23 = tail call i32 @RSA_public_decrypt(i32 %4, i8* %3, i8* %16, i32* %5, i32 %22)
%24 = icmp slt i32 %23, 1
br i1 %24, label %42, label %25
25: ; preds = %21
store i8* %16, i8** %7
%26 = zext i32 %23 to i64
%27 = call %struct.TYPE_4__* @d2i_ASN1_OCTET_STRING(i32* null, i8** %7, i64 %26)
%28 = icmp eq %struct.TYPE_4__* %27, null
br i1 %28, label %42, label %29
29: ; preds = %25
%30 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %27, i64 0, i32 1
%31 = load i64, i64* %30
%32 = trunc i64 %31 to i32
%33 = icmp eq i32 %32, %2
br i1 %33, label %34, label %39
34: ; preds = %29
%35 = getelementptr inbounds %struct.TYPE_4__, %struct.TYPE_4__* %27, i64 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @memcmp(i8* %1, i32 %36, i32 %2)
%38 = icmp eq i64 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %34, %29
%40 = load i32, i32* @RSA_R_BAD_SIGNATURE
%41 = call i32 @RSAerror(i32 %40)
br label %42
42: ; preds = %34, %39, %25, %21, %18
%43 = phi i32 [ 0, %18 ], [ 0, %21 ], [ 0, %25 ], [ 0, %39 ], [ 1, %34 ]
%44 = phi %struct.TYPE_4__* [ null, %18 ], [ null, %21 ], [ null, %25 ], [ %27, %39 ], [ %27, %34 ]
%45 = call i32 @ASN1_OCTET_STRING_free(%struct.TYPE_4__* %44)
%46 = call i32 @freezero(i8* %16, i32 %4)
br label %47
47: ; preds = %42, %12
%48 = phi i32 [ 0, %12 ], [ %43, %42 ]
ret i32 %48
}