#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 */

/* Variables and functions */
scalar_t__ RAND_bytes(unsigned char *, int);
int /*<<< orphan*/ RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2;
int /*<<< orphan*/ RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE;
int /*<<< orphan*/ RSAerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, unsigned int);
int RSA_padding_add_PKCS1_type_2(unsigned char *to, int tlen,
                                 const unsigned char *from, int flen) {
  int i, j;
  unsigned char *p;
  if (flen > (tlen - 11)) {
    RSAerr(RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2,
           RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
    return (0);
  }
  p = (unsigned char *)to;
  *(p++) = 0;
  *(p++) = 2;
  j = tlen - 3 - flen;
  if (RAND_bytes(p, j) <= 0)
    return (0);
  for (i = 0; i < j; i++) {
    if (*p == '\0')
      do {
        if (RAND_bytes(p, 1) <= 0)
          return (0);
      } while (*p == '\0');
    p++;
  }
  *(p++) = '\0';
  memcpy(p, from, (unsigned int)flen);
  return (1);
}


x86 assembly (clang, -O0):
<RSA_padding_add_PKCS1_type_2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    -0x24(%rbp),%eax
mov    -0x14(%rbp),%ecx
sub    $0xb,%ecx
cmp    %ecx,%eax
jle    117a <RSA_padding_add_PKCS1_type_2+0x4a>
mov    0x2e7a(%rip),%rax
mov    (%rax),%edi
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
call   1050 <RSAerr@plt>
movl   $0x0,-0x4(%rbp)
jmp    127d <RSA_padding_add_PKCS1_type_2+0x14d>
mov    -0x10(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x2,(%rax)
mov    -0x14(%rbp),%eax
sub    $0x3,%eax
sub    -0x24(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1030 <RAND_bytes@plt>
cmp    $0x0,%rax
jg     11d4 <RSA_padding_add_PKCS1_type_2+0xa4>
movl   $0x0,-0x4(%rbp)
jmp    127d <RSA_padding_add_PKCS1_type_2+0x14d>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    1254 <RSA_padding_add_PKCS1_type_2+0x124>
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    123a <RSA_padding_add_PKCS1_type_2+0x10a>
jmp    11fc <RSA_padding_add_PKCS1_type_2+0xcc>
mov    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   1030 <RAND_bytes@plt>
cmp    $0x0,%rax
jg     1220 <RSA_padding_add_PKCS1_type_2+0xf0>
movl   $0x0,-0x4(%rbp)
jmp    127d <RSA_padding_add_PKCS1_type_2+0x14d>
jmp    1225 <RSA_padding_add_PKCS1_type_2+0xf5>
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     11fc <RSA_padding_add_PKCS1_type_2+0xcc>
jmp    123a <RSA_padding_add_PKCS1_type_2+0x10a>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11db <RSA_padding_add_PKCS1_type_2+0xab>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movb   $0x0,(%rax)
mov    -0x38(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RSA_padding_add_PKCS1_type_2>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %esi,%r12d
lea    -0xb(%r12),%eax
cmp    %ecx,%eax
jge    115f <RSA_padding_add_PKCS1_type_2+0x2f>
mov    0x2e8d(%rip),%rax
mov    (%rax),%edi
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
call   1050 <RSAerr@plt>
xor    %eax,%eax
jmp    11d5 <RSA_padding_add_PKCS1_type_2+0xa5>
mov    %ecx,%r15d
mov    %rdx,%r14
mov    %rdi,%rbx
movw   $0x200,(%rdi)
add    $0x2,%rbx
sub    %ecx,%r12d
add    $0xfffffffd,%r12d
mov    %rbx,%rdi
mov    %r12d,%esi
call   1030 <RAND_bytes@plt>
test   %rax,%rax
jle    11b7 <RSA_padding_add_PKCS1_type_2+0x87>
test   %r12d,%r12d
jle    11bb <RSA_padding_add_PKCS1_type_2+0x8b>
xor    %ebp,%ebp
jmp    11a0 <RSA_padding_add_PKCS1_type_2+0x70>
add    $0x1,%rbx
add    $0x1,%ebp
cmp    %r12d,%ebp
je     11bb <RSA_padding_add_PKCS1_type_2+0x8b>
nopl   (%rax)
cmpb   $0x0,(%rbx)
jne    1191 <RSA_padding_add_PKCS1_type_2+0x61>
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <RAND_bytes@plt>
test   %rax,%rax
jg     11a0 <RSA_padding_add_PKCS1_type_2+0x70>
xor    %eax,%eax
jmp    11d5 <RSA_padding_add_PKCS1_type_2+0xa5>
movb   $0x0,(%rbx)
add    $0x1,%rbx
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15d,%edx
call   1040 <memcpy@plt>
mov    $0x1,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 = global i32 0
@RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE = global i32 0
define i32 @RSA_padding_add_PKCS1_type_2(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store i8* %0, i8** %6
store i32 %1, i32* %7
store i8* %2, i8** %8
store i32 %3, i32* %9
%13 = load i32, i32* %9
%14 = load i32, i32* %7
%15 = sub nsw i32 %14, 11
%16 = icmp sgt i32 %13, %15
br i1 %16, label %17, label %21
17: ; preds = %4
%18 = load i32, i32* @RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2
%19 = load i32, i32* @RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
%20 = call i32 @RSAerr(i32 %18, i32 %19)
store i32 0, i32* %5
br label %72
21: ; preds = %4
%22 = load i8*, i8** %6
store i8* %22, i8** %12
%23 = load i8*, i8** %12
%24 = getelementptr inbounds i8, i8* %23, i32 1
store i8* %24, i8** %12
store i8 0, i8* %23
%25 = load i8*, i8** %12
%26 = getelementptr inbounds i8, i8* %25, i32 1
store i8* %26, i8** %12
store i8 2, i8* %25
%27 = load i32, i32* %7
%28 = sub nsw i32 %27, 3
%29 = load i32, i32* %9
%30 = sub nsw i32 %28, %29
store i32 %30, i32* %11
%31 = load i8*, i8** %12
%32 = load i32, i32* %11
%33 = call i64 @RAND_bytes(i8* %31, i32 %32)
%34 = icmp sle i64 %33, 0
br i1 %34, label %35, label %36
35: ; preds = %21
store i32 0, i32* %5
br label %72
36: ; preds = %21
store i32 0, i32* %10
br label %37
37: ; preds = %62, %36
%38 = load i32, i32* %10
%39 = load i32, i32* %11
%40 = icmp slt i32 %38, %39
br i1 %40, label %41, label %65
41: ; preds = %37
%42 = load i8*, i8** %12
%43 = load i8, i8* %42
%44 = zext i8 %43 to i32
%45 = icmp eq i32 %44, 0
br i1 %45, label %46, label %59
46: ; preds = %41
br label %47
47: ; preds = %53, %46
%48 = load i8*, i8** %12
%49 = call i64 @RAND_bytes(i8* %48, i32 1)
%50 = icmp sle i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 0, i32* %5
br label %72
52: ; preds = %47
br label %53
53: ; preds = %52
%54 = load i8*, i8** %12
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = icmp eq i32 %56, 0
br i1 %57, label %47, label %58
58: ; preds = %53
br label %59
59: ; preds = %58, %41
%60 = load i8*, i8** %12
%61 = getelementptr inbounds i8, i8* %60, i32 1
store i8* %61, i8** %12
br label %62
62: ; preds = %59
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %10
br label %37
65: ; preds = %37
%66 = load i8*, i8** %12
%67 = getelementptr inbounds i8, i8* %66, i32 1
store i8* %67, i8** %12
store i8 0, i8* %66
%68 = load i8*, i8** %12
%69 = load i8*, i8** %8
%70 = load i32, i32* %9
%71 = call i32 @memcpy(i8* %68, i8* %69, i32 %70)
store i32 1, i32* %5
br label %72
72: ; preds = %65, %51, %35, %17
%73 = load i32, i32* %5
ret i32 %73
}

LLVM IR (-O2):
@RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2 = global i32 0
@RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE = global i32 0
define i32 @RSA_padding_add_PKCS1_type_2(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = add nsw i32 %1, -11
%6 = icmp slt i32 %5, %3
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i32, i32* @RSA_F_RSA_PADDING_ADD_PKCS1_TYPE_2
%9 = load i32, i32* @RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
%10 = tail call i32 @RSAerr(i32 %8, i32 %9)
br label %39
11: ; preds = %4
%12 = getelementptr inbounds i8, i8* %0, i64 1
store i8 0, i8* %0
%13 = getelementptr inbounds i8, i8* %0, i64 2
store i8 2, i8* %12
%14 = add nsw i32 %1, -3
%15 = sub i32 %14, %3
%16 = tail call i64 @RAND_bytes(i8* %13, i32 %15)
%17 = icmp slt i64 %16, 1
br i1 %17, label %39, label %18
18: ; preds = %11
%19 = icmp sgt i32 %15, 0
br i1 %19, label %20, label %35
20: ; preds = %18, %31
%21 = phi i8* [ %32, %31 ], [ %13, %18 ]
%22 = phi i32 [ %33, %31 ], [ 0, %18 ]
%23 = load i8, i8* %21
%24 = icmp eq i8 %23, 0
br i1 %24, label %25, label %31
25: ; preds = %20, %28
%26 = tail call i64 @RAND_bytes(i8* %21, i32 1)
%27 = icmp slt i64 %26, 1
br i1 %27, label %39, label %28
28: ; preds = %25
%29 = load i8, i8* %21
%30 = icmp eq i8 %29, 0
br i1 %30, label %25, label %31
31: ; preds = %28, %20
%32 = getelementptr inbounds i8, i8* %21, i64 1
%33 = add nuw nsw i32 %22, 1
%34 = icmp eq i32 %33, %15
br i1 %34, label %35, label %20
35: ; preds = %31, %18
%36 = phi i8* [ %13, %18 ], [ %32, %31 ]
%37 = getelementptr inbounds i8, i8* %36, i64 1
store i8 0, i8* %36
%38 = tail call i32 @memcpy(i8* %37, i8* %2, i32 %3)
br label %39
39: ; preds = %25, %11, %35, %7
%40 = phi i32 [ 0, %7 ], [ 1, %35 ], [ 0, %11 ], [ 0, %25 ]
ret i32 %40
}