#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*/ 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 /*<<< orphan*/);
int /*<<< orphan*/ EVP_MD_CTX_free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *EVP_MD_CTX_new();
int /*<<< orphan*/ EVP_PKEY_free(int /*<<< orphan*/ *);
unsigned int EVP_PKEY_size(int /*<<< orphan*/ *);
scalar_t__ EVP_SignFinal(int /*<<< orphan*/ *, unsigned char *, unsigned int *,
                         int /*<<< orphan*/ *);
int /*<<< orphan*/ EVP_SignInit(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ EVP_SignUpdate(int /*<<< orphan*/ *, char const *,
                                  int /*<<< orphan*/);
int /*<<< orphan*/ EVP_sha1();
int /*<<< orphan*/ OPENSSL_free(unsigned char *);
int /*<<< orphan*/ *PEM_read_bio_PrivateKey(int /*<<< orphan*/ *,
                                            int /*<<< orphan*/ *,
                                            int /*<<< orphan*/,
                                            unsigned char *);
char *oauth_encode_base64(unsigned int, unsigned char *);
int /*<<< orphan*/ strlen(char const *);
scalar_t__ xmalloc(unsigned int);
char *xstrdup(char *);
char *oauth_sign_rsa_sha1(const char *m, const char *k) {
  unsigned char *sig = NULL;
  unsigned char *passphrase = NULL;
  unsigned int len = 0;
  EVP_MD_CTX *md_ctx;
  EVP_PKEY *pkey;
  BIO *in;
  in = BIO_new_mem_buf((unsigned char *)k, strlen(k));
  pkey = PEM_read_bio_PrivateKey(in, NULL, 0, passphrase);
  BIO_free(in);
  if (pkey == NULL) {
    return xstrdup("liboauth/OpenSSL: can not read private key");
  }
  len = EVP_PKEY_size(pkey);
  sig = (unsigned char *)xmalloc((len + 1) * sizeof(char));
  md_ctx = EVP_MD_CTX_new();
  EVP_SignInit(md_ctx, EVP_sha1());
  EVP_SignUpdate(md_ctx, m, strlen(m));
  if (EVP_SignFinal(md_ctx, sig, &len, pkey)) {
    char *tmp;
    sig[len] = '\0';
    tmp = oauth_encode_base64(len, sig);
    OPENSSL_free(sig);
    EVP_PKEY_free(pkey);
    EVP_MD_CTX_free(md_ctx);
    return tmp;
  }
  EVP_MD_CTX_free(md_ctx);
  return xstrdup("liboauth/OpenSSL: rsa-sha1 signing failed");
}


x86 assembly (clang, -O0):
<oauth_sign_rsa_sha1>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
call   10a0 <BIO_new_mem_buf@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1110 <PEM_read_bio_PrivateKey@plt>
mov    %rax,-0x40(%rbp)
mov    -0x48(%rbp),%rdi
call   10b0 <BIO_free@plt>
cmpq   $0x0,-0x40(%rbp)
jne    1287 <oauth_sign_rsa_sha1+0x87>
lea    0xd87(%rip),%rdi
call   1090 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
jmp    1372 <oauth_sign_rsa_sha1+0x172>
mov    -0x40(%rbp),%rdi
call   10c0 <EVP_PKEY_size@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,%eax
shl    $0x0,%rax
mov    %eax,%edi
call   1080 <xmalloc@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   10d0 <EVP_MD_CTX_new@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x70(%rbp)
mov    $0x0,%al
call   1060 <EVP_sha1@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%esi
call   10e0 <EVP_SignInit@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x68(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x68(%rbp),%rdi
mov    -0x60(%rbp),%rsi
mov    %eax,%edx
call   1100 <EVP_SignUpdate@plt>
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x40(%rbp),%rcx
lea    -0x2c(%rbp),%rdx
call   1030 <EVP_SignFinal@plt>
cmp    $0x0,%rax
je     1359 <oauth_sign_rsa_sha1+0x159>
mov    -0x20(%rbp),%rax
mov    -0x2c(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1120 <oauth_encode_base64@plt>
mov    %rax,-0x50(%rbp)
mov    -0x20(%rbp),%rdi
call   10f0 <OPENSSL_free@plt>
mov    -0x40(%rbp),%rdi
call   1070 <EVP_PKEY_free@plt>
mov    -0x38(%rbp),%rdi
call   1040 <EVP_MD_CTX_free@plt>
mov    -0x50(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1372 <oauth_sign_rsa_sha1+0x172>
mov    -0x38(%rbp),%rdi
call   1040 <EVP_MD_CTX_free@plt>
lea    0xcc2(%rip),%rdi
call   1090 <xstrdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<oauth_sign_rsa_sha1>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   10a0 <BIO_new_mem_buf@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1110 <PEM_read_bio_PrivateKey@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   10b0 <BIO_free@plt>
test   %r15,%r15
je     12d6 <oauth_sign_rsa_sha1+0xd6>
mov    %r15,%rdi
call   10c0 <EVP_PKEY_size@plt>
mov    %eax,0x4(%rsp)
lea    0x1(%rax),%edi
call   1080 <xmalloc@plt>
mov    %rax,%r12
xor    %eax,%eax
call   10d0 <EVP_MD_CTX_new@plt>
mov    %rax,%rbx
xor    %eax,%eax
call   1060 <EVP_sha1@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   10e0 <EVP_SignInit@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %eax,%edx
call   1100 <EVP_SignUpdate@plt>
lea    0x4(%rsp),%rdx
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r15,%rcx
call   1030 <EVP_SignFinal@plt>
test   %rax,%rax
je     12ed <oauth_sign_rsa_sha1+0xed>
mov    0x4(%rsp),%eax
movb   $0x0,(%r12,%rax,1)
mov    0x4(%rsp),%edi
mov    %r12,%rsi
call   1120 <oauth_encode_base64@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   10f0 <OPENSSL_free@plt>
mov    %r15,%rdi
call   1070 <EVP_PKEY_free@plt>
mov    %rbx,%rdi
call   1040 <EVP_MD_CTX_free@plt>
jmp    1304 <oauth_sign_rsa_sha1+0x104>
lea    0xd23(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
jmp    1090 <xstrdup@plt>
mov    %rbx,%rdi
call   1040 <EVP_MD_CTX_free@plt>
lea    0xd2f(%rip),%rdi
call   1090 <xstrdup@plt>
mov    %rax,%r14
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [43 x i8] c"liboauth/OpenSSL: can not read private key\00"
@.str.1 = [42 x i8] c"liboauth/OpenSSL: rsa-sha1 signing failed\00"
define i8* @oauth_sign_rsa_sha1(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32*
%12 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8* null, i8** %6
store i8* null, i8** %7
store i32 0, i32* %8
%13 = load i8*, i8** %5
%14 = load i8*, i8** %5
%15 = call i32 @strlen(i8* %14)
%16 = call i32* @BIO_new_mem_buf(i8* %13, i32 %15)
store i32* %16, i32** %11
%17 = load i32*, i32** %11
%18 = load i8*, i8** %7
%19 = call i32* @PEM_read_bio_PrivateKey(i32* %17, i32* null, i32 0, i8* %18)
store i32* %19, i32** %10
%20 = load i32*, i32** %11
%21 = call i32 @BIO_free(i32* %20)
%22 = load i32*, i32** %10
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %26
24: ; preds = %2
%25 = call i8* @xstrdup(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
store i8* %25, i8** %3
br label %69
26: ; preds = %2
%27 = load i32*, i32** %10
%28 = call i32 @EVP_PKEY_size(i32* %27)
store i32 %28, i32* %8
%29 = load i32, i32* %8
%30 = add i32 %29, 1
%31 = zext i32 %30 to i64
%32 = mul i64 %31, 1
%33 = trunc i64 %32 to i32
%34 = call i64 @xmalloc(i32 %33)
%35 = inttoptr i64 %34 to i8*
store i8* %35, i8** %6
%36 = call i32* (...) @EVP_MD_CTX_new()
store i32* %36, i32** %9
%37 = load i32*, i32** %9
%38 = call i32 (...) @EVP_sha1()
%39 = call i32 @EVP_SignInit(i32* %37, i32 %38)
%40 = load i32*, i32** %9
%41 = load i8*, i8** %4
%42 = load i8*, i8** %4
%43 = call i32 @strlen(i8* %42)
%44 = call i32 @EVP_SignUpdate(i32* %40, i8* %41, i32 %43)
%45 = load i32*, i32** %9
%46 = load i8*, i8** %6
%47 = load i32*, i32** %10
%48 = call i64 @EVP_SignFinal(i32* %45, i8* %46, i32* %8, i32* %47)
%49 = icmp ne i64 %48, 0
br i1 %49, label %50, label %65
50: ; preds = %26
%51 = load i8*, i8** %6
%52 = load i32, i32* %8
%53 = zext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 0, i8* %54
%55 = load i32, i32* %8
%56 = load i8*, i8** %6
%57 = call i8* @oauth_encode_base64(i32 %55, i8* %56)
store i8* %57, i8** %12
%58 = load i8*, i8** %6
%59 = call i32 @OPENSSL_free(i8* %58)
%60 = load i32*, i32** %10
%61 = call i32 @EVP_PKEY_free(i32* %60)
%62 = load i32*, i32** %9
%63 = call i32 @EVP_MD_CTX_free(i32* %62)
%64 = load i8*, i8** %12
store i8* %64, i8** %3
br label %69
65: ; preds = %26
%66 = load i32*, i32** %9
%67 = call i32 @EVP_MD_CTX_free(i32* %66)
%68 = call i8* @xstrdup(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
store i8* %68, i8** %3
br label %69
69: ; preds = %65, %50, %24
%70 = load i8*, i8** %3
ret i8* %70
}

LLVM IR (-O2):
@.str = [43 x i8] c"liboauth/OpenSSL: can not read private key\00"
@.str.1 = [42 x i8] c"liboauth/OpenSSL: rsa-sha1 signing failed\00"
define i8* @oauth_sign_rsa_sha1(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32 @strlen(i8* %1)
%6 = tail call i32* @BIO_new_mem_buf(i8* %1, i32 %5)
%7 = tail call i32* @PEM_read_bio_PrivateKey(i32* %6, i32* null, i32 0, i8* null)
%8 = tail call i32 @BIO_free(i32* %6)
%9 = icmp eq i32* %7, null
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = tail call i8* @xstrdup(i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str, i64 0, i64 0))
br label %36
12: ; preds = %2
%13 = tail call i32 @EVP_PKEY_size(i32* %7)
store i32 %13, i32* %3
%14 = add i32 %13, 1
%15 = tail call i64 @xmalloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i32* (...) @EVP_MD_CTX_new()
%18 = tail call i32 (...) @EVP_sha1()
%19 = tail call i32 @EVP_SignInit(i32* %17, i32 %18)
%20 = tail call i32 @strlen(i8* %0)
%21 = tail call i32 @EVP_SignUpdate(i32* %17, i8* %0, i32 %20)
%22 = call i64 @EVP_SignFinal(i32* %17, i8* %16, i32* %3, i32* %7)
%23 = icmp eq i64 %22, 0
br i1 %23, label %33, label %24
24: ; preds = %12
%25 = load i32, i32* %3
%26 = zext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %16, i64 %26
store i8 0, i8* %27
%28 = load i32, i32* %3
%29 = call i8* @oauth_encode_base64(i32 %28, i8* %16)
%30 = call i32 @OPENSSL_free(i8* %16)
%31 = call i32 @EVP_PKEY_free(i32* %7)
%32 = call i32 @EVP_MD_CTX_free(i32* %17)
br label %36
33: ; preds = %12
%34 = call i32 @EVP_MD_CTX_free(i32* %17)
%35 = call i8* @xstrdup(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %33, %24, %10
%37 = phi i8* [ %11, %10 ], [ %29, %24 ], [ %35, %33 ]
ret i8* %37
}