#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 */
int /*<<< orphan*/ RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE;
int /*<<< orphan*/ RSAerror(int /*<<< orphan*/);
int /*<<< orphan*/ arc4random_buf(unsigned char *, int);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, 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) {
    RSAerror(RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
    return 0;
  }
  p = (unsigned char *)to;
  *(p++) = 0;
  *(p++) = 2;
  j = tlen - 3 - flen;
  arc4random_buf(p, j);
  for (i = 0; i < j; i++) {
    while (*p == '\0')
      arc4random_buf(p, 1);
    p++;
  }
  *(p++) = '\0';
  memcpy(p, from, 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    1171 <RSA_padding_add_PKCS1_type_2+0x41>
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
call   1050 <RSAerror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1233 <RSA_padding_add_PKCS1_type_2+0x103>
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 <arc4random_buf@plt>
movl   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jge    120a <RSA_padding_add_PKCS1_type_2+0xda>
jmp    11cd <RSA_padding_add_PKCS1_type_2+0x9d>
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    11f0 <RSA_padding_add_PKCS1_type_2+0xc0>
mov    -0x38(%rbp),%rdi
mov    $0x1,%esi
call   1030 <arc4random_buf@plt>
jmp    11cd <RSA_padding_add_PKCS1_type_2+0x9d>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x28(%rbp)
jmp    11bc <RSA_padding_add_PKCS1_type_2+0x8c>
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    1156 <RSA_padding_add_PKCS1_type_2+0x26>
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
call   1050 <RSAerror@plt>
xor    %eax,%eax
jmp    11c8 <RSA_padding_add_PKCS1_type_2+0x98>
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 <arc4random_buf@plt>
test   %r12d,%r12d
jle    11ae <RSA_padding_add_PKCS1_type_2+0x7e>
xor    %ebp,%ebp
jmp    119d <RSA_padding_add_PKCS1_type_2+0x6d>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbx,%rdi
mov    $0x1,%esi
call   1030 <arc4random_buf@plt>
cmpb   $0x0,(%rbx)
je     1190 <RSA_padding_add_PKCS1_type_2+0x60>
add    $0x1,%rbx
add    $0x1,%ebp
cmp    %r12d,%ebp
jne    119d <RSA_padding_add_PKCS1_type_2+0x6d>
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_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 %20
17: ; preds = %4
%18 = load i32, i32* @RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
%19 = call i32 @RSAerror(i32 %18)
store i32 0, i32* %5
br label %59
20: ; preds = %4
%21 = load i8*, i8** %6
store i8* %21, i8** %12
%22 = load i8*, i8** %12
%23 = getelementptr inbounds i8, i8* %22, i32 1
store i8* %23, i8** %12
store i8 0, i8* %22
%24 = load i8*, i8** %12
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %12
store i8 2, i8* %24
%26 = load i32, i32* %7
%27 = sub nsw i32 %26, 3
%28 = load i32, i32* %9
%29 = sub nsw i32 %27, %28
store i32 %29, i32* %11
%30 = load i8*, i8** %12
%31 = load i32, i32* %11
%32 = call i32 @arc4random_buf(i8* %30, i32 %31)
store i32 0, i32* %10
br label %33
33: ; preds = %49, %20
%34 = load i32, i32* %10
%35 = load i32, i32* %11
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %52
37: ; preds = %33
br label %38
38: ; preds = %43, %37
%39 = load i8*, i8** %12
%40 = load i8, i8* %39
%41 = zext i8 %40 to i32
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i8*, i8** %12
%45 = call i32 @arc4random_buf(i8* %44, i32 1)
br label %38
46: ; preds = %38
%47 = load i8*, i8** %12
%48 = getelementptr inbounds i8, i8* %47, i32 1
store i8* %48, i8** %12
br label %49
49: ; preds = %46
%50 = load i32, i32* %10
%51 = add nsw i32 %50, 1
store i32 %51, i32* %10
br label %33
52: ; preds = %33
%53 = load i8*, i8** %12
%54 = getelementptr inbounds i8, i8* %53, i32 1
store i8* %54, i8** %12
store i8 0, i8* %53
%55 = load i8*, i8** %12
%56 = load i8*, i8** %8
%57 = load i32, i32* %9
%58 = call i32 @memcpy(i8* %55, i8* %56, i32 %57)
store i32 1, i32* %5
br label %59
59: ; preds = %52, %17
%60 = load i32, i32* %5
ret i32 %60
}

LLVM IR (-O2):
@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 %10
7: ; preds = %4
%8 = load i32, i32* @RSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE
%9 = tail call i32 @RSAerror(i32 %8)
br label %34
10: ; preds = %4
%11 = getelementptr inbounds i8, i8* %0, i64 1
store i8 0, i8* %0
%12 = getelementptr inbounds i8, i8* %0, i64 2
store i8 2, i8* %11
%13 = add nsw i32 %1, -3
%14 = sub i32 %13, %3
%15 = tail call i32 @arc4random_buf(i8* %12, i32 %14)
%16 = icmp sgt i32 %14, 0
br i1 %16, label %17, label %30
17: ; preds = %10, %26
%18 = phi i8* [ %27, %26 ], [ %12, %10 ]
%19 = phi i32 [ %28, %26 ], [ 0, %10 ]
%20 = load i8, i8* %18
%21 = icmp eq i8 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %17, %22
%23 = tail call i32 @arc4random_buf(i8* %18, i32 1)
%24 = load i8, i8* %18
%25 = icmp eq i8 %24, 0
br i1 %25, label %22, label %26
26: ; preds = %22, %17
%27 = getelementptr inbounds i8, i8* %18, i64 1
%28 = add nuw nsw i32 %19, 1
%29 = icmp eq i32 %28, %14
br i1 %29, label %30, label %17
30: ; preds = %26, %10
%31 = phi i8* [ %12, %10 ], [ %27, %26 ]
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8 0, i8* %31
%33 = tail call i32 @memcpy(i8* %32, i8* %2, i32 %3)
br label %34
34: ; preds = %30, %7
%35 = phi i32 [ 0, %7 ], [ 1, %30 ]
ret i32 %35
}