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

/* Variables and functions */
int /*<<< orphan*/ NID_sha1;
int RSA_sign(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/,
             int /*<<< orphan*/ *, int *, int /*<<< orphan*/ *);
int RSA_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ SHA_DIGEST_LENGTH;
int /*<<< orphan*/ hash_sha1(int /*<<< orphan*/ *, int, int /*<<< orphan*/);
scalar_t__ xmalloc(int);
u_char *rsa_sign(u_char *msg, u_int m_len, RSA *priv, u_int *siglen) {
  u_char *signature;
  int ret, len;
  ret = RSA_size(priv);
  if (!ret)
    return 0;
  signature = (u_char *)xmalloc(ret);
  ret = RSA_sign(NID_sha1, hash_sha1(msg, m_len, 0), SHA_DIGEST_LENGTH,
                 signature, (u_int *)&len, priv);
  if (siglen)
    *siglen = len;
  return !ret ? 0 : signature;
}


x86 assembly (clang, -O0):
<rsa_sign>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <RSA_size@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jne    117a <rsa_sign+0x3a>
movq   $0x0,-0x8(%rbp)
jmp    11fb <rsa_sign+0xbb>
mov    -0x34(%rbp),%edi
call   1040 <xmalloc@plt>
mov    %rax,-0x30(%rbp)
mov    0x2e4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   1050 <hash_sha1@plt>
mov    -0x3c(%rbp),%edi
mov    %eax,%esi
mov    0x2e3c(%rip),%rax
mov    (%rax),%edx
mov    -0x30(%rbp),%rcx
mov    -0x20(%rbp),%r9
lea    -0x38(%rbp),%r8
call   1060 <RSA_sign@plt>
mov    %eax,-0x34(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11d6 <rsa_sign+0x96>
mov    -0x38(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
cmpl   $0x0,-0x34(%rbp)
jne    11eb <rsa_sign+0xab>
xor    %eax,%eax
mov    %rax,-0x48(%rbp)
jmp    11f3 <rsa_sign+0xb3>
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rsa_sign>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r15
mov    %esi,%r12d
mov    %rdi,%rbp
mov    %rdx,%rdi
call   1030 <RSA_size@plt>
test   %eax,%eax
je     11b8 <rsa_sign+0x78>
mov    %eax,%edi
call   1040 <xmalloc@plt>
mov    %rax,%rbx
mov    0x2e64(%rip),%rax
mov    (%rax),%r13d
mov    %rbp,%rdi
mov    %r12d,%esi
xor    %edx,%edx
call   1050 <hash_sha1@plt>
mov    0x2e5d(%rip),%rcx
mov    (%rcx),%edx
lea    0x4(%rsp),%r8
mov    %r13d,%edi
mov    %eax,%esi
mov    %rbx,%rcx
mov    %r15,%r9
call   1060 <RSA_sign@plt>
test   %r14,%r14
je     11ae <rsa_sign+0x6e>
mov    0x4(%rsp),%ecx
mov    %ecx,(%r14)
xor    %ecx,%ecx
test   %eax,%eax
cmove  %rcx,%rbx
jmp    11ba <rsa_sign+0x7a>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@NID_sha1 = global i32 0
@SHA_DIGEST_LENGTH = global i32 0
define i32* @rsa_sign(i32* %0, i32 %1, i32* %2, i32* %3) {
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca i32
store i32* %0, i32** %6
store i32 %1, i32* %7
store i32* %2, i32** %8
store i32* %3, i32** %9
%13 = load i32*, i32** %8
%14 = call i32 @RSA_size(i32* %13)
store i32 %14, i32* %11
%15 = load i32, i32* %11
%16 = icmp ne i32 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %4
store i32* null, i32** %5
br label %43
18: ; preds = %4
%19 = load i32, i32* %11
%20 = call i64 @xmalloc(i32 %19)
%21 = inttoptr i64 %20 to i32*
store i32* %21, i32** %10
%22 = load i32, i32* @NID_sha1
%23 = load i32*, i32** %6
%24 = load i32, i32* %7
%25 = call i32 @hash_sha1(i32* %23, i32 %24, i32 0)
%26 = load i32, i32* @SHA_DIGEST_LENGTH
%27 = load i32*, i32** %10
%28 = load i32*, i32** %8
%29 = call i32 @RSA_sign(i32 %22, i32 %25, i32 %26, i32* %27, i32* %12, i32* %28)
store i32 %29, i32* %11
%30 = load i32*, i32** %9
%31 = icmp ne i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %18
%33 = load i32, i32* %12
%34 = load i32*, i32** %9
store i32 %33, i32* %34
br label %35
35: ; preds = %32, %18
%36 = load i32, i32* %11
%37 = icmp ne i32 %36, 0
br i1 %37, label %39, label %38
38: ; preds = %35
br label %41
39: ; preds = %35
%40 = load i32*, i32** %10
br label %41
41: ; preds = %39, %38
%42 = phi i32* [ null, %38 ], [ %40, %39 ]
store i32* %42, i32** %5
br label %43
43: ; preds = %41, %17
%44 = load i32*, i32** %5
ret i32* %44
}

LLVM IR (-O2):
@NID_sha1 = global i32 0
@SHA_DIGEST_LENGTH = global i32 0
define i32* @rsa_sign(i32* %0, i32 %1, i32* %2, i32* writeonly %3) {
%5 = alloca i32
%6 = bitcast i32* %5 to i8*
%7 = tail call i32 @RSA_size(i32* %2)
%8 = icmp eq i32 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %4
%10 = tail call i64 @xmalloc(i32 %7)
%11 = inttoptr i64 %10 to i32*
%12 = load i32, i32* @NID_sha1
%13 = tail call i32 @hash_sha1(i32* %0, i32 %1, i32 0)
%14 = load i32, i32* @SHA_DIGEST_LENGTH
%15 = call i32 @RSA_sign(i32 %12, i32 %13, i32 %14, i32* %11, i32* %5, i32* %2)
%16 = icmp eq i32* %3, null
br i1 %16, label %19, label %17
17: ; preds = %9
%18 = load i32, i32* %5
store i32 %18, i32* %3
br label %19
19: ; preds = %17, %9
%20 = icmp eq i32 %15, 0
%21 = select i1 %20, i32* null, i32* %11
br label %22
22: ; preds = %4, %19
%23 = phi i32* [ %21, %19 ], [ null, %4 ]
ret i32* %23
}