#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_3__ TYPE_1__;

/* Type definitions */
struct TYPE_3__ {
  unsigned int length;
  unsigned char *data;
  int /*<<< orphan*/ type;
};
typedef int /*<<< orphan*/ RSA;
typedef TYPE_1__ ASN1_OCTET_STRING;

/* Variables and functions */
int /*<<< orphan*/ ERR_R_MALLOC_FAILURE;
int /*<<< orphan*/ RSA_PKCS1_PADDING;
int RSA_PKCS1_PADDING_SIZE;
int /*<<< orphan*/ RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY;
int RSA_private_encrypt(int, unsigned char *, unsigned char *,
                        int /*<<< orphan*/ *, int /*<<< orphan*/);
int RSA_size(int /*<<< orphan*/ *);
int /*<<< orphan*/ RSAerror(int /*<<< orphan*/);
int /*<<< orphan*/ V_ASN1_OCTET_STRING;
int /*<<< orphan*/ freezero(unsigned char *, unsigned int);
int i2d_ASN1_OCTET_STRING(TYPE_1__ *, unsigned char **);
unsigned char *malloc(int);
int RSA_sign_ASN1_OCTET_STRING(int type, const unsigned char *m,
                               unsigned int m_len, unsigned char *sigret,
                               unsigned int *siglen, RSA *rsa) {
  ASN1_OCTET_STRING sig;
  int i, j, ret = 1;
  unsigned char *p, *s;
  sig.type = V_ASN1_OCTET_STRING;
  sig.length = m_len;
  sig.data = (unsigned char *)m;
  i = i2d_ASN1_OCTET_STRING(&sig, NULL);
  j = RSA_size(rsa);
  if (i > (j - RSA_PKCS1_PADDING_SIZE)) {
    RSAerror(RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
    return 0;
  }
  s = malloc(j + 1);
  if (s == NULL) {
    RSAerror(ERR_R_MALLOC_FAILURE);
    return 0;
  }
  p = s;
  i2d_ASN1_OCTET_STRING(&sig, &p);
  i = RSA_private_encrypt(i, s, sigret, rsa, RSA_PKCS1_PADDING);
  if (i <= 0)
    ret = 0;
  else
    *siglen = i;
  freezero(s, (unsigned int)j + 1);
  return ret;
}


x86 assembly (clang, -O0):
<RSA_sign_ASN1_OCTET_STRING>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    %r9,-0x30(%rbp)
movl   $0x1,-0x54(%rbp)
mov    0x2e3c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1050 <i2d_ASN1_OCTET_STRING@plt>
mov    %eax,-0x4c(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <RSA_size@plt>
mov    %eax,-0x50(%rbp)
mov    -0x4c(%rbp),%eax
mov    -0x50(%rbp),%ecx
mov    0x2e18(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jle    11ec <RSA_sign_ASN1_OCTET_STRING+0x8c>
mov    0x2df7(%rip),%rax
mov    (%rax),%edi
call   1080 <RSAerror@plt>
movl   $0x0,-0x4(%rbp)
jmp    128a <RSA_sign_ASN1_OCTET_STRING+0x12a>
mov    -0x50(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    1220 <RSA_sign_ASN1_OCTET_STRING+0xc0>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1080 <RSAerror@plt>
movl   $0x0,-0x4(%rbp)
jmp    128a <RSA_sign_ASN1_OCTET_STRING+0x12a>
mov    -0x68(%rbp),%rax
mov    %rax,-0x60(%rbp)
lea    -0x48(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1050 <i2d_ASN1_OCTET_STRING@plt>
mov    -0x4c(%rbp),%edi
mov    -0x68(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    0x2d6d(%rip),%rax
mov    (%rax),%r8d
call   1070 <RSA_private_encrypt@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jg     126c <RSA_sign_ASN1_OCTET_STRING+0x10c>
movl   $0x0,-0x54(%rbp)
jmp    1275 <RSA_sign_ASN1_OCTET_STRING+0x115>
mov    -0x4c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x68(%rbp),%rdi
mov    -0x50(%rbp),%esi
add    $0x1,%esi
call   1030 <freezero@plt>
mov    -0x54(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RSA_sign_ASN1_OCTET_STRING>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r9,%r12
mov    %r8,%r13
mov    %rcx,%r15
mov    0x2e4a(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x20(%rsp)
mov    %edx,0x10(%rsp)
mov    %rsi,0x18(%rsp)
xor    %ebx,%ebx
lea    0x10(%rsp),%rdi
xor    %esi,%esi
call   1050 <i2d_ASN1_OCTET_STRING@plt>
mov    %eax,%r14d
mov    %r12,%rdi
call   1040 <RSA_size@plt>
mov    %eax,%ebp
mov    0x2e31(%rip),%rax
mov    %ebp,%ecx
sub    (%rax),%ecx
cmp    %ecx,%r14d
jle    11c8 <RSA_sign_ASN1_OCTET_STRING+0x68>
mov    0x2e11(%rip),%rax
mov    (%rax),%edi
call   1080 <RSAerror@plt>
jmp    1237 <RSA_sign_ASN1_OCTET_STRING+0xd7>
mov    %r13,%rbx
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1219 <RSA_sign_ASN1_OCTET_STRING+0xb9>
mov    %rax,%r13
mov    %rax,0x8(%rsp)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <i2d_ASN1_OCTET_STRING@plt>
mov    0x2dc0(%rip),%rax
mov    (%rax),%r8d
mov    %r14d,%edi
mov    %r13,%rsi
mov    %r15,%rdx
mov    %r12,%rcx
call   1070 <RSA_private_encrypt@plt>
test   %eax,%eax
jle    122b <RSA_sign_ASN1_OCTET_STRING+0xcb>
mov    %eax,(%rbx)
mov    $0x1,%ebx
jmp    122d <RSA_sign_ASN1_OCTET_STRING+0xcd>
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
call   1080 <RSAerror@plt>
xor    %ebx,%ebx
jmp    1237 <RSA_sign_ASN1_OCTET_STRING+0xd7>
xor    %ebx,%ebx
mov    %r13,%rdi
mov    %ebp,%esi
call   1030 <freezero@plt>
mov    %ebx,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.TYPE_3__ = type { i32, i8*, i32 }
@V_ASN1_OCTET_STRING = global i32 0
@RSA_PKCS1_PADDING_SIZE = global i32 0
@RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@RSA_PKCS1_PADDING = global i32 0
define i32 @RSA_sign_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 %struct.TYPE_3__
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i8*
%19 = alloca i8*
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 1, i32* %17
%20 = load i32, i32* @V_ASN1_OCTET_STRING
%21 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %14, i32 0, i32 2
store i32 %20, i32* %21
%22 = load i32, i32* %10
%23 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %14, i32 0, i32 0
store i32 %22, i32* %23
%24 = load i8*, i8** %9
%25 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %14, i32 0, i32 1
store i8* %24, i8** %25
%26 = call i32 @i2d_ASN1_OCTET_STRING(%struct.TYPE_3__* %14, i8** null)
store i32 %26, i32* %15
%27 = load i32*, i32** %13
%28 = call i32 @RSA_size(i32* %27)
store i32 %28, i32* %16
%29 = load i32, i32* %15
%30 = load i32, i32* %16
%31 = load i32, i32* @RSA_PKCS1_PADDING_SIZE
%32 = sub nsw i32 %30, %31
%33 = icmp sgt i32 %29, %32
br i1 %33, label %34, label %37
34: ; preds = %6
%35 = load i32, i32* @RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY
%36 = call i32 @RSAerror(i32 %35)
store i32 0, i32* %7
br label %67
37: ; preds = %6
%38 = load i32, i32* %16
%39 = add nsw i32 %38, 1
%40 = call i8* @malloc(i32 %39)
store i8* %40, i8** %19
%41 = load i8*, i8** %19
%42 = icmp eq i8* %41, null
br i1 %42, label %43, label %46
43: ; preds = %37
%44 = load i32, i32* @ERR_R_MALLOC_FAILURE
%45 = call i32 @RSAerror(i32 %44)
store i32 0, i32* %7
br label %67
46: ; preds = %37
%47 = load i8*, i8** %19
store i8* %47, i8** %18
%48 = call i32 @i2d_ASN1_OCTET_STRING(%struct.TYPE_3__* %14, i8** %18)
%49 = load i32, i32* %15
%50 = load i8*, i8** %19
%51 = load i8*, i8** %11
%52 = load i32*, i32** %13
%53 = load i32, i32* @RSA_PKCS1_PADDING
%54 = call i32 @RSA_private_encrypt(i32 %49, i8* %50, i8* %51, i32* %52, i32 %53)
store i32 %54, i32* %15
%55 = load i32, i32* %15
%56 = icmp sle i32 %55, 0
br i1 %56, label %57, label %58
57: ; preds = %46
store i32 0, i32* %17
br label %61
58: ; preds = %46
%59 = load i32, i32* %15
%60 = load i32*, i32** %12
store i32 %59, i32* %60
br label %61
61: ; preds = %58, %57
%62 = load i8*, i8** %19
%63 = load i32, i32* %16
%64 = add i32 %63, 1
%65 = call i32 @freezero(i8* %62, i32 %64)
%66 = load i32, i32* %17
store i32 %66, i32* %7
br label %67
67: ; preds = %61, %43, %34
%68 = load i32, i32* %7
ret i32 %68
}

LLVM IR (-O2):
%struct.TYPE_3__ = type { i32, i8*, i32 }
@V_ASN1_OCTET_STRING = global i32 0
@RSA_PKCS1_PADDING_SIZE = global i32 0
@RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY = global i32 0
@ERR_R_MALLOC_FAILURE = global i32 0
@RSA_PKCS1_PADDING = global i32 0
define i32 @RSA_sign_ASN1_OCTET_STRING(i32 %0, i8* %1, i32 %2, i8* %3, i32* nocapture writeonly %4, i32* %5) {
%7 = alloca %struct.TYPE_3__
%8 = alloca i8*
%9 = bitcast %struct.TYPE_3__* %7 to i8*
%10 = bitcast i8** %8 to i8*
%11 = load i32, i32* @V_ASN1_OCTET_STRING
%12 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i64 0, i32 2
store i32 %11, i32* %12
%13 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i64 0, i32 0
store i32 %2, i32* %13
%14 = getelementptr inbounds %struct.TYPE_3__, %struct.TYPE_3__* %7, i64 0, i32 1
store i8* %1, i8** %14
%15 = call i32 @i2d_ASN1_OCTET_STRING(%struct.TYPE_3__* %7, i8** null)
%16 = call i32 @RSA_size(i32* %5)
%17 = load i32, i32* @RSA_PKCS1_PADDING_SIZE
%18 = sub nsw i32 %16, %17
%19 = icmp sgt i32 %15, %18
br i1 %19, label %20, label %23
20: ; preds = %6
%21 = load i32, i32* @RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY
%22 = call i32 @RSAerror(i32 %21)
br label %39
23: ; preds = %6
%24 = add nsw i32 %16, 1
%25 = call i8* @malloc(i32 %24)
%26 = icmp eq i8* %25, null
br i1 %26, label %27, label %30
27: ; preds = %23
%28 = load i32, i32* @ERR_R_MALLOC_FAILURE
%29 = call i32 @RSAerror(i32 %28)
br label %39
30: ; preds = %23
store i8* %25, i8** %8
%31 = call i32 @i2d_ASN1_OCTET_STRING(%struct.TYPE_3__* %7, i8** %8)
%32 = load i32, i32* @RSA_PKCS1_PADDING
%33 = call i32 @RSA_private_encrypt(i32 %15, i8* %25, i8* %3, i32* %5, i32 %32)
%34 = icmp slt i32 %33, 1
br i1 %34, label %36, label %35
35: ; preds = %30
store i32 %33, i32* %4
br label %36
36: ; preds = %30, %35
%37 = phi i32 [ 1, %35 ], [ 0, %30 ]
%38 = call i32 @freezero(i8* %25, i32 %24)
br label %39
39: ; preds = %36, %27, %20
%40 = phi i32 [ 0, %20 ], [ 0, %27 ], [ %37, %36 ]
ret i32 %40
}