#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*/ X509;
typedef int /*<<< orphan*/ EVP_PKEY;
typedef int /*<<< orphan*/ EVP_MD_CTX;
typedef int /*<<< orphan*/ BIO;

/* Variables and functions */
int /*<<< orphan*/ BIO_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *BIO_new_mem_buf(unsigned char *, int);
int /*<<< orphan*/ EVP_MD_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_MD_CTX_new();
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
int EVP_VerifyFinal(int /*<<< orphan*/ *, unsigned char *, int,
                    int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_VerifyInit(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ EVP_VerifyUpdate(int /*<<< orphan*/ *, char const *, int);
int /*<<< orphan*/ EVP_sha1();
int /*<<< orphan*/ *PEM_read_bio_PUBKEY(int /*<<< orphan*/ *,
                                        int /*<<< orphan*/ *,
                                        int /*<<< orphan*/,
                                        int /*<<< orphan*/ *);
int /*<<< orphan*/ *PEM_read_bio_X509(int /*<<< orphan*/ *,
                                      int /*<<< orphan*/ *, int /*<<< orphan*/,
                                      int /*<<< orphan*/ *);
int /*<<< orphan*/ X509_free(int /*<<< orphan*/ *);
scalar_t__ X509_get_pubkey(int /*<<< orphan*/ *);
int oauth_decode_base64(unsigned char *, char const *);
int strlen(char const *);
int /*<<< orphan*/ xfree(unsigned char *);
scalar_t__ xmalloc(int);
int oauth_verify_rsa_sha1(const char *m, const char *c, const char *s) {
  EVP_MD_CTX *md_ctx;
  EVP_PKEY *pkey;
  BIO *in;
  X509 *cert = NULL;
  unsigned char *b64d;
  int slen, err;
  in = BIO_new_mem_buf((unsigned char *)c, strlen(c));
  cert = PEM_read_bio_X509(in, NULL, 0, NULL);
  if (cert) {
    pkey = (EVP_PKEY *)X509_get_pubkey(cert);
    X509_free(cert);
  } else {
    pkey = PEM_read_bio_PUBKEY(in, NULL, 0, NULL);
  }
  BIO_free(in);
  if (pkey == NULL) {
    return -2;
  }
  b64d = (unsigned char *)xmalloc(sizeof(char) * strlen(s));
  slen = oauth_decode_base64(b64d, s);
  md_ctx = EVP_MD_CTX_new();
  EVP_VerifyInit(md_ctx, EVP_sha1());
  EVP_VerifyUpdate(md_ctx, m, strlen(m));
  err = EVP_VerifyFinal(md_ctx, b64d, slen, pkey);
  EVP_MD_CTX_free(md_ctx);
  EVP_PKEY_free(pkey);
  xfree(b64d);
  return (err);
}


x86 assembly (clang, -O0):
<oauth_verify_rsa_sha1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
call   10e0 <BIO_new_mem_buf@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
xor    %edx,%edx
mov    %rcx,%rsi
call   1130 <PEM_read_bio_X509@plt>
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     1288 <oauth_verify_rsa_sha1+0x78>
mov    -0x40(%rbp),%rdi
call   10d0 <X509_get_pubkey@plt>
mov    %rax,-0x30(%rbp)
mov    -0x40(%rbp),%rdi
call   1110 <X509_free@plt>
jmp    129e <oauth_verify_rsa_sha1+0x8e>
mov    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%ecx
xor    %edx,%edx
mov    %rcx,%rsi
call   1090 <PEM_read_bio_PUBKEY@plt>
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
call   10f0 <BIO_free@plt>
cmpq   $0x0,-0x30(%rbp)
jne    12be <oauth_verify_rsa_sha1+0xae>
movl   $0xfffffffe,-0x4(%rbp)
jmp    136d <oauth_verify_rsa_sha1+0x15d>
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   10c0 <xmalloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <oauth_decode_base64@plt>
mov    %eax,-0x4c(%rbp)
mov    $0x0,%al
call   1100 <EVP_MD_CTX_new@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    $0x0,%al
call   1070 <EVP_sha1@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%esi
call   1040 <EVP_VerifyInit@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1120 <EVP_VerifyUpdate@plt>
mov    -0x28(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x4c(%rbp),%edx
mov    -0x30(%rbp),%rcx
call   1050 <EVP_VerifyFinal@plt>
mov    %eax,-0x50(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <EVP_MD_CTX_free@plt>
mov    -0x30(%rbp),%rdi
call   10a0 <EVP_PKEY_free@plt>
mov    -0x48(%rbp),%rdi
call   10b0 <xfree@plt>
mov    -0x50(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<oauth_verify_rsa_sha1>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   10e0 <BIO_new_mem_buf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1130 <PEM_read_bio_X509@plt>
test   %rax,%rax
je     1264 <oauth_verify_rsa_sha1+0x54>
mov    %rax,%rbp
mov    %rax,%rdi
call   10d0 <X509_get_pubkey@plt>
mov    %rax,%r12
mov    %rbp,%rdi
call   1110 <X509_free@plt>
jmp    1275 <oauth_verify_rsa_sha1+0x65>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1090 <PEM_read_bio_PUBKEY@plt>
mov    %rax,%r12
mov    %rbx,%rdi
call   10f0 <BIO_free@plt>
test   %r12,%r12
je     1301 <oauth_verify_rsa_sha1+0xf1>
mov    %r15,%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
call   10c0 <xmalloc@plt>
mov    %rax,%r13
mov    %rax,%rdi
mov    %r15,%rsi
call   1080 <oauth_decode_base64@plt>
mov    %eax,%r15d
xor    %eax,%eax
call   1100 <EVP_MD_CTX_new@plt>
mov    %rax,%rbp
xor    %eax,%eax
call   1070 <EVP_sha1@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1040 <EVP_VerifyInit@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1120 <EVP_VerifyUpdate@plt>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
mov    %r12,%rcx
call   1050 <EVP_VerifyFinal@plt>
mov    %eax,%ebx
mov    %rbp,%rdi
call   1030 <EVP_MD_CTX_free@plt>
mov    %r12,%rdi
call   10a0 <EVP_PKEY_free@plt>
mov    %r13,%rdi
call   10b0 <xfree@plt>
mov    %ebx,%eax
jmp    1306 <oauth_verify_rsa_sha1+0xf6>
mov    $0xfffffffe,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @oauth_verify_rsa_sha1(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i8*
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32* null, i32** %11
%15 = load i8*, i8** %6
%16 = load i8*, i8** %6
%17 = call i32 @strlen(i8* %16)
%18 = call i32* @BIO_new_mem_buf(i8* %15, i32 %17)
store i32* %18, i32** %10
%19 = load i32*, i32** %10
%20 = call i32* @PEM_read_bio_X509(i32* %19, i32* null, i32 0, i32* null)
store i32* %20, i32** %11
%21 = load i32*, i32** %11
%22 = icmp ne i32* %21, null
br i1 %22, label %23, label %29
23: ; preds = %3
%24 = load i32*, i32** %11
%25 = call i64 @X509_get_pubkey(i32* %24)
%26 = inttoptr i64 %25 to i32*
store i32* %26, i32** %9
%27 = load i32*, i32** %11
%28 = call i32 @X509_free(i32* %27)
br label %32
29: ; preds = %3
%30 = load i32*, i32** %10
%31 = call i32* @PEM_read_bio_PUBKEY(i32* %30, i32* null, i32 0, i32* null)
store i32* %31, i32** %9
br label %32
32: ; preds = %29, %23
%33 = load i32*, i32** %10
%34 = call i32 @BIO_free(i32* %33)
%35 = load i32*, i32** %9
%36 = icmp eq i32* %35, null
br i1 %36, label %37, label %38
37: ; preds = %32
store i32 -2, i32* %4
br label %70
38: ; preds = %32
%39 = load i8*, i8** %7
%40 = call i32 @strlen(i8* %39)
%41 = sext i32 %40 to i64
%42 = mul i64 1, %41
%43 = trunc i64 %42 to i32
%44 = call i64 @xmalloc(i32 %43)
%45 = inttoptr i64 %44 to i8*
store i8* %45, i8** %12
%46 = load i8*, i8** %12
%47 = load i8*, i8** %7
%48 = call i32 @oauth_decode_base64(i8* %46, i8* %47)
store i32 %48, i32* %13
%49 = call i32* (...) @EVP_MD_CTX_new()
store i32* %49, i32** %8
%50 = load i32*, i32** %8
%51 = call i32 (...) @EVP_sha1()
%52 = call i32 @EVP_VerifyInit(i32* %50, i32 %51)
%53 = load i32*, i32** %8
%54 = load i8*, i8** %5
%55 = load i8*, i8** %5
%56 = call i32 @strlen(i8* %55)
%57 = call i32 @EVP_VerifyUpdate(i32* %53, i8* %54, i32 %56)
%58 = load i32*, i32** %8
%59 = load i8*, i8** %12
%60 = load i32, i32* %13
%61 = load i32*, i32** %9
%62 = call i32 @EVP_VerifyFinal(i32* %58, i8* %59, i32 %60, i32* %61)
store i32 %62, i32* %14
%63 = load i32*, i32** %8
%64 = call i32 @EVP_MD_CTX_free(i32* %63)
%65 = load i32*, i32** %9
%66 = call i32 @EVP_PKEY_free(i32* %65)
%67 = load i8*, i8** %12
%68 = call i32 @xfree(i8* %67)
%69 = load i32, i32* %14
store i32 %69, i32* %4
br label %70
70: ; preds = %38, %37
%71 = load i32, i32* %4
ret i32 %71
}

LLVM IR (-O2):
define i32 @oauth_verify_rsa_sha1(i8* %0, i8* %1, i8* %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = tail call i32* @BIO_new_mem_buf(i8* %1, i32 %4)
%6 = tail call i32* @PEM_read_bio_X509(i32* %5, i32* null, i32 0, i32* null)
%7 = icmp eq i32* %6, null
br i1 %7, label %12, label %8
8: ; preds = %3
%9 = tail call i64 @X509_get_pubkey(i32* %6)
%10 = inttoptr i64 %9 to i32*
%11 = tail call i32 @X509_free(i32* %6)
br label %14
12: ; preds = %3
%13 = tail call i32* @PEM_read_bio_PUBKEY(i32* %5, i32* null, i32 0, i32* null)
br label %14
14: ; preds = %12, %8
%15 = phi i32* [ %10, %8 ], [ %13, %12 ]
%16 = tail call i32 @BIO_free(i32* %5)
%17 = icmp eq i32* %15, null
br i1 %17, label %32, label %18
18: ; preds = %14
%19 = tail call i32 @strlen(i8* %2)
%20 = tail call i64 @xmalloc(i32 %19)
%21 = inttoptr i64 %20 to i8*
%22 = tail call i32 @oauth_decode_base64(i8* %21, i8* %2)
%23 = tail call i32* (...) @EVP_MD_CTX_new()
%24 = tail call i32 (...) @EVP_sha1()
%25 = tail call i32 @EVP_VerifyInit(i32* %23, i32 %24)
%26 = tail call i32 @strlen(i8* %0)
%27 = tail call i32 @EVP_VerifyUpdate(i32* %23, i8* %0, i32 %26)
%28 = tail call i32 @EVP_VerifyFinal(i32* %23, i8* %21, i32 %22, i32* %15)
%29 = tail call i32 @EVP_MD_CTX_free(i32* %23)
%30 = tail call i32 @EVP_PKEY_free(i32* %15)
%31 = tail call i32 @xfree(i8* %21)
br label %32
32: ; preds = %14, %18
%33 = phi i32 [ %28, %18 ], [ -2, %14 ]
ret i32 %33
}