#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_F_RSA_SIGN_ASN1_OCTET_STRING;
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*/ RSAerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ V_ASN1_OCTET_STRING;
int /*<<< orphan*/ explicit_bzero(unsigned char *, unsigned int);
int /*<<< orphan*/ free(unsigned char *);
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)) {
    RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING, RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY);
    return 0;
  }
  s = malloc(j + 1);
  if (s == NULL) {
    RSAerr(RSA_F_RSA_SIGN_ASN1_OCTET_STRING, 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;
  explicit_bzero(s, (unsigned int)j + 1);
  free(s);
  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    0x2e24(%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    0x2e00(%rip),%rdx
sub    (%rdx),%ecx
cmp    %ecx,%eax
jle    1205 <RSA_sign_ASN1_OCTET_STRING+0x95>
mov    0x2dff(%rip),%rax
mov    (%rax),%edi
mov    0x2dd6(%rip),%rax
mov    (%rax),%esi
call   1080 <RSAerr@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <RSA_sign_ASN1_OCTET_STRING+0x145>
mov    -0x50(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x68(%rbp)
jne    1242 <RSA_sign_ASN1_OCTET_STRING+0xd2>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%esi
call   1080 <RSAerr@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b5 <RSA_sign_ASN1_OCTET_STRING+0x145>
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    0x2d43(%rip),%rax
mov    (%rax),%r8d
call   1070 <RSA_private_encrypt@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0x0,-0x4c(%rbp)
jg     128e <RSA_sign_ASN1_OCTET_STRING+0x11e>
movl   $0x0,-0x54(%rbp)
jmp    1297 <RSA_sign_ASN1_OCTET_STRING+0x127>
mov    -0x4c(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x68(%rbp),%rdi
mov    -0x50(%rbp),%esi
add    $0x1,%esi
call   1090 <explicit_bzero@plt>
mov    -0x68(%rbp),%rdi
call   1030 <free@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    0x2e32(%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    0x2e19(%rip),%rax
mov    %ebp,%ecx
sub    (%rax),%ecx
cmp    %ecx,%r14d
jle    11e4 <RSA_sign_ASN1_OCTET_STRING+0x74>
mov    0x2e19(%rip),%rax
mov    (%rax),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1080 <RSAerr@plt>
jmp    1264 <RSA_sign_ASN1_OCTET_STRING+0xf4>
mov    %r13,%rbx
add    $0x1,%ebp
mov    %ebp,%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1235 <RSA_sign_ASN1_OCTET_STRING+0xc5>
mov    %rax,%r13
mov    %rax,0x8(%rsp)
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
call   1050 <i2d_ASN1_OCTET_STRING@plt>
mov    0x2d9c(%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    1250 <RSA_sign_ASN1_OCTET_STRING+0xe0>
mov    %eax,(%rbx)
mov    $0x1,%ebx
jmp    1252 <RSA_sign_ASN1_OCTET_STRING+0xe2>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
mov    0x2d9b(%rip),%rax
mov    (%rax),%esi
call   1080 <RSAerr@plt>
xor    %ebx,%ebx
jmp    1264 <RSA_sign_ASN1_OCTET_STRING+0xf4>
xor    %ebx,%ebx
mov    %r13,%rdi
mov    %ebp,%esi
call   1090 <explicit_bzero@plt>
mov    %r13,%rdi
call   1030 <free@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_F_RSA_SIGN_ASN1_OCTET_STRING = 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 %38
34: ; preds = %6
%35 = load i32, i32* @RSA_F_RSA_SIGN_ASN1_OCTET_STRING
%36 = load i32, i32* @RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY
%37 = call i32 @RSAerr(i32 %35, i32 %36)
store i32 0, i32* %7
br label %71
38: ; preds = %6
%39 = load i32, i32* %16
%40 = add nsw i32 %39, 1
%41 = call i8* @malloc(i32 %40)
store i8* %41, i8** %19
%42 = load i8*, i8** %19
%43 = icmp eq i8* %42, null
br i1 %43, label %44, label %48
44: ; preds = %38
%45 = load i32, i32* @RSA_F_RSA_SIGN_ASN1_OCTET_STRING
%46 = load i32, i32* @ERR_R_MALLOC_FAILURE
%47 = call i32 @RSAerr(i32 %45, i32 %46)
store i32 0, i32* %7
br label %71
48: ; preds = %38
%49 = load i8*, i8** %19
store i8* %49, i8** %18
%50 = call i32 @i2d_ASN1_OCTET_STRING(%struct.TYPE_3__* %14, i8** %18)
%51 = load i32, i32* %15
%52 = load i8*, i8** %19
%53 = load i8*, i8** %11
%54 = load i32*, i32** %13
%55 = load i32, i32* @RSA_PKCS1_PADDING
%56 = call i32 @RSA_private_encrypt(i32 %51, i8* %52, i8* %53, i32* %54, i32 %55)
store i32 %56, i32* %15
%57 = load i32, i32* %15
%58 = icmp sle i32 %57, 0
br i1 %58, label %59, label %60
59: ; preds = %48
store i32 0, i32* %17
br label %63
60: ; preds = %48
%61 = load i32, i32* %15
%62 = load i32*, i32** %12
store i32 %61, i32* %62
br label %63
63: ; preds = %60, %59
%64 = load i8*, i8** %19
%65 = load i32, i32* %16
%66 = add i32 %65, 1
%67 = call i32 @explicit_bzero(i8* %64, i32 %66)
%68 = load i8*, i8** %19
%69 = call i32 @free(i8* %68)
%70 = load i32, i32* %17
store i32 %70, i32* %7
br label %71
71: ; preds = %63, %44, %34
%72 = load i32, i32* %7
ret i32 %72
}

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_F_RSA_SIGN_ASN1_OCTET_STRING = 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 %24
20: ; preds = %6
%21 = load i32, i32* @RSA_F_RSA_SIGN_ASN1_OCTET_STRING
%22 = load i32, i32* @RSA_R_DIGEST_TOO_BIG_FOR_RSA_KEY
%23 = call i32 @RSAerr(i32 %21, i32 %22)
br label %42
24: ; preds = %6
%25 = add nsw i32 %16, 1
%26 = call i8* @malloc(i32 %25)
%27 = icmp eq i8* %26, null
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* @RSA_F_RSA_SIGN_ASN1_OCTET_STRING
%30 = load i32, i32* @ERR_R_MALLOC_FAILURE
%31 = call i32 @RSAerr(i32 %29, i32 %30)
br label %42
32: ; preds = %24
store i8* %26, i8** %8
%33 = call i32 @i2d_ASN1_OCTET_STRING(%struct.TYPE_3__* %7, i8** %8)
%34 = load i32, i32* @RSA_PKCS1_PADDING
%35 = call i32 @RSA_private_encrypt(i32 %15, i8* %26, i8* %3, i32* %5, i32 %34)
%36 = icmp slt i32 %35, 1
br i1 %36, label %38, label %37
37: ; preds = %32
store i32 %35, i32* %4
br label %38
38: ; preds = %32, %37
%39 = phi i32 [ 1, %37 ], [ 0, %32 ]
%40 = call i32 @explicit_bzero(i8* %26, i32 %25)
%41 = call i32 @free(i8* %26)
br label %42
42: ; preds = %38, %28, %20
%43 = phi i32 [ 0, %20 ], [ 0, %28 ], [ %39, %38 ]
ret i32 %43
}