#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 */
typedef int /*<<< orphan*/ hash;
typedef int /*<<< orphan*/ RSA;

/* Variables and functions */
int /*<<< orphan*/ NID_sha512;
int /*<<< orphan*/ RSA_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSA_verify(int /*<<< orphan*/, unsigned char *, int,
                              unsigned char *, unsigned int,
                              int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA512(unsigned char const *, size_t, unsigned char *);
int SHA512_DIGEST_LENGTH;
int /*<<< orphan*/ *createRSA(unsigned char *, int);
int verifyRSA(const unsigned char *msg, size_t msglen, unsigned char *sig,
              size_t siglen, unsigned char *pubkey) {
  unsigned char hash[SHA512_DIGEST_LENGTH];
  RSA *rsa = createRSA(pubkey, true);
  if (rsa == NULL) {
    goto verifyRSA_failure;
  }
  if (!SHA512(msg, msglen, hash)) {
    goto verifyRSA_failure;
  }
  if (!RSA_verify(NID_sha512, hash, sizeof(hash), sig, (unsigned int)siglen,
                  rsa)) {
    goto verifyRSA_failure;
  }
  if (rsa) {
    RSA_free(rsa);
  }
  return true;
verifyRSA_failure:
  if (rsa) {
    RSA_free(rsa);
  }
  return false;
}


x86 assembly (clang, -O0):
<verifyRSA>:
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    %r8,-0x30(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x60(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x58(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rdi
mov    $0x1,%esi
call   1050 <createRSA@plt>
mov    %rax,-0x48(%rbp)
cmpq   $0x0,-0x48(%rbp)
jne    11ae <verifyRSA+0x6e>
jmp    1229 <verifyRSA+0xe9>
mov    -0x58(%rbp),%rdx
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1060 <SHA512@plt>
cmp    $0x0,%eax
jne    11cd <verifyRSA+0x8d>
jmp    1229 <verifyRSA+0xe9>
mov    -0x58(%rbp),%rsi
mov    -0x60(%rbp),%rax
mov    0x2dfc(%rip),%rcx
mov    (%rcx),%edi
mov    %eax,%edx
mov    -0x20(%rbp),%rcx
mov    -0x28(%rbp),%rax
mov    %eax,%r8d
mov    -0x48(%rbp),%r9
call   1040 <RSA_verify@plt>
cmp    $0x0,%eax
jne    1202 <verifyRSA+0xc2>
jmp    1229 <verifyRSA+0xe9>
cmpq   $0x0,-0x48(%rbp)
je     1216 <verifyRSA+0xd6>
mov    -0x48(%rbp),%rdi
call   1030 <RSA_free@plt>
movl   $0x1,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
jmp    124b <verifyRSA+0x10b>
cmpq   $0x0,-0x48(%rbp)
je     123d <verifyRSA+0xfd>
mov    -0x48(%rbp),%rdi
call   1030 <RSA_free@plt>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x4c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<verifyRSA>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,-0x38(%rbp)
mov    %rdx,-0x30(%rbp)
mov    %rsi,%r14
mov    %rdi,%r15
mov    0x2e82(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%rbx
lea    0xf(%r12),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r8,%rdi
mov    $0x1,%esi
call   1050 <createRSA@plt>
test   %rax,%rax
je     11d6 <verifyRSA+0x96>
mov    %rax,%r13
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1060 <SHA512@plt>
test   %eax,%eax
je     11ca <verifyRSA+0x8a>
mov    0x2e2f(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    %r12d,%edx
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%r8
mov    %r13,%r9
call   1040 <RSA_verify@plt>
mov    $0x1,%ebx
test   %eax,%eax
jne    11cc <verifyRSA+0x8c>
xor    %ebx,%ebx
mov    %r13,%rdi
call   1030 <RSA_free@plt>
jmp    11d8 <verifyRSA+0x98>
xor    %ebx,%ebx
mov    %ebx,%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
@NID_sha512 = global i32 0
define i32 @verifyRSA(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i8*
%12 = alloca i8*
%13 = alloca i64
%14 = alloca i32*
%15 = alloca i32
store i8* %0, i8** %7
store i64 %1, i64* %8
store i8* %2, i8** %9
store i64 %3, i64* %10
store i8* %4, i8** %11
%16 = load i32, i32* @SHA512_DIGEST_LENGTH
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %12
%19 = alloca i8, i64 %17
store i64 %17, i64* %13
%20 = load i8*, i8** %11
%21 = call i32* @createRSA(i8* %20, i32 1)
store i32* %21, i32** %14
%22 = load i32*, i32** %14
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %5
br label %48
25: ; preds = %5
%26 = load i8*, i8** %7
%27 = load i64, i64* %8
%28 = call i32 @SHA512(i8* %26, i64 %27, i8* %19)
%29 = icmp ne i32 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %25
br label %48
31: ; preds = %25
%32 = load i32, i32* @NID_sha512
%33 = trunc i64 %17 to i32
%34 = load i8*, i8** %9
%35 = load i64, i64* %10
%36 = trunc i64 %35 to i32
%37 = load i32*, i32** %14
%38 = call i32 @RSA_verify(i32 %32, i8* %19, i32 %33, i8* %34, i32 %36, i32* %37)
%39 = icmp ne i32 %38, 0
br i1 %39, label %41, label %40
40: ; preds = %31
br label %48
41: ; preds = %31
%42 = load i32*, i32** %14
%43 = icmp ne i32* %42, null
br i1 %43, label %44, label %47
44: ; preds = %41
%45 = load i32*, i32** %14
%46 = call i32 @RSA_free(i32* %45)
br label %47
47: ; preds = %44, %41
store i32 1, i32* %6
store i32 1, i32* %15
br label %55
48: ; preds = %40, %30, %24
%49 = load i32*, i32** %14
%50 = icmp ne i32* %49, null
br i1 %50, label %51, label %54
51: ; preds = %48
%52 = load i32*, i32** %14
%53 = call i32 @RSA_free(i32* %52)
br label %54
54: ; preds = %51, %48
store i32 0, i32* %6
store i32 1, i32* %15
br label %55
55: ; preds = %54, %47
%56 = load i8*, i8** %12
%57 = load i32, i32* %6
ret i32 %57
}

LLVM IR (-O2):
@SHA512_DIGEST_LENGTH = global i32 0
@NID_sha512 = global i32 0
define i32 @verifyRSA(i8* %0, i64 %1, i8* %2, i64 %3, i8* %4) {
%6 = load i32, i32* @SHA512_DIGEST_LENGTH
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = tail call i32* @createRSA(i8* %4, i32 1)
%10 = icmp eq i32* %9, null
br i1 %10, label %23, label %11
11: ; preds = %5
%12 = call i32 @SHA512(i8* %0, i64 %1, i8* %8)
%13 = icmp eq i32 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = load i32, i32* @NID_sha512
%16 = trunc i64 %3 to i32
%17 = call i32 @RSA_verify(i32 %15, i8* %8, i32 %6, i8* %2, i32 %16, i32* %9)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %11, %14
br label %20
20: ; preds = %14, %19
%21 = phi i32 [ 0, %19 ], [ 1, %14 ]
%22 = call i32 @RSA_free(i32* %9)
br label %23
23: ; preds = %20, %5
%24 = phi i32 [ 0, %5 ], [ %21, %20 ]
ret i32 %24
}