#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_F_RSA_PADDING_CHECK_PKCS1_TYPE_2;
int /*<<< orphan*/ RSA_R_BAD_PAD_BYTE_COUNT;
int /*<<< orphan*/ RSA_R_BLOCK_TYPE_IS_NOT_02;
int /*<<< orphan*/ RSA_R_DATA_TOO_LARGE;
int /*<<< orphan*/ RSA_R_NULL_BEFORE_BLOCK_MISSING;
int /*<<< orphan*/ RSAerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, int);
int RSA_padding_check_PKCS1_type_2(unsigned char *to, int tlen,
                                   const unsigned char *from, int flen,
                                   int num) {
  int i, j;
  const unsigned char *p;
  p = from;
  if (num != flen + 1 || *(p++) != 02) {
    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, RSA_R_BLOCK_TYPE_IS_NOT_02);
    return -1;
  }
  j = flen - 1;
  for (i = 0; i < j; i++)
    if (*(p++) == 0)
      break;
  if (i == j) {
    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2,
           RSA_R_NULL_BEFORE_BLOCK_MISSING);
    return -1;
  }
  if (i < 8) {
    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, RSA_R_BAD_PAD_BYTE_COUNT);
    return -1;
  }
  i++;
  j -= i;
  if (j > tlen) {
    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2, RSA_R_DATA_TOO_LARGE);
    return -1;
  }
  memcpy(to, p, j);
  return j;
}


x86 assembly (clang, -O0):
<RSA_padding_check_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    %r8d,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    116e <RSA_padding_check_PKCS1_type_2+0x4e>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movzbl (%rax),%eax
cmp    $0x2,%eax
je     1191 <RSA_padding_check_PKCS1_type_2+0x71>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
mov    0x2e6a(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <RSA_padding_check_PKCS1_type_2+0x175>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    11e0 <RSA_padding_check_PKCS1_type_2+0xc0>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    11cd <RSA_padding_check_PKCS1_type_2+0xad>
jmp    11e0 <RSA_padding_check_PKCS1_type_2+0xc0>
jmp    11d2 <RSA_padding_check_PKCS1_type_2+0xb2>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a1 <RSA_padding_check_PKCS1_type_2+0x81>
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    120f <RSA_padding_check_PKCS1_type_2+0xef>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <RSA_padding_check_PKCS1_type_2+0x175>
cmpl   $0x8,-0x2c(%rbp)
jge    123c <RSA_padding_check_PKCS1_type_2+0x11c>
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <RSA_padding_check_PKCS1_type_2+0x175>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    127f <RSA_padding_check_PKCS1_type_2+0x15f>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
mov    0x2d64(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1295 <RSA_padding_check_PKCS1_type_2+0x175>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x30(%rbp),%edx
call   1030 <memcpy@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<RSA_padding_check_PKCS1_type_2>:
push   %rbx
mov    %ecx,%ebx
lea    0x1(%rbx),%eax
cmp    %r8d,%eax
jne    1153 <RSA_padding_check_PKCS1_type_2+0x33>
cmpb   $0x2,(%rdx)
jne    1153 <RSA_padding_check_PKCS1_type_2+0x33>
lea    -0x1(%rbx),%ecx
cmp    $0x2,%ebx
jl     1173 <RSA_padding_check_PKCS1_type_2+0x53>
add    $0x2,%rdx
xor    %eax,%eax
xchg   %ax,%ax
cmpb   $0x0,-0x1(%rdx)
je     1189 <RSA_padding_check_PKCS1_type_2+0x69>
add    $0x1,%eax
add    $0x1,%rdx
cmp    %eax,%ecx
jne    1140 <RSA_padding_check_PKCS1_type_2+0x20>
jmp    118d <RSA_padding_check_PKCS1_type_2+0x6d>
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret
test   %ecx,%ecx
je     118d <RSA_padding_check_PKCS1_type_2+0x6d>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
mov    0x2e41(%rip),%rax
jmp    1163 <RSA_padding_check_PKCS1_type_2+0x43>
cmp    %eax,%ecx
jne    119f <RSA_padding_check_PKCS1_type_2+0x7f>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    0x2e43(%rip),%rax
jmp    1163 <RSA_padding_check_PKCS1_type_2+0x43>
cmp    $0x7,%eax
jbe    1177 <RSA_padding_check_PKCS1_type_2+0x57>
sub    %eax,%ebx
add    $0xfffffffe,%ebx
cmp    %esi,%ebx
jle    11bf <RSA_padding_check_PKCS1_type_2+0x9f>
mov    0x2e0c(%rip),%rax
mov    (%rax),%edi
mov    0x2e13(%rip),%rax
jmp    1163 <RSA_padding_check_PKCS1_type_2+0x43>
mov    %rdx,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
jmp    116f <RSA_padding_check_PKCS1_type_2+0x4f>

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

LLVM IR (-O2):
@RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2 = global i32 0
@RSA_R_BLOCK_TYPE_IS_NOT_02 = global i32 0
@RSA_R_NULL_BEFORE_BLOCK_MISSING = global i32 0
@RSA_R_BAD_PAD_BYTE_COUNT = global i32 0
@RSA_R_DATA_TOO_LARGE = global i32 0
define i32 @RSA_padding_check_PKCS1_type_2(i8* %0, i32 %1, i8* %2, i32 %3, i32 %4) {
%6 = add nsw i32 %3, 1
%7 = icmp eq i32 %6, %4
br i1 %7, label %8, label %11
8: ; preds = %5
%9 = load i8, i8* %2
%10 = icmp eq i8 %9, 2
br i1 %10, label %15, label %11
11: ; preds = %8, %5
%12 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2
%13 = load i32, i32* @RSA_R_BLOCK_TYPE_IS_NOT_02
%14 = tail call i32 @RSAerr(i32 %12, i32 %13)
br label %53
15: ; preds = %8
%16 = add i32 %3, -1
%17 = icmp sgt i32 %3, 1
br i1 %17, label %18, label %31
18: ; preds = %15
%19 = getelementptr inbounds i8, i8* %2, i64 1
br label %20
20: ; preds = %18, %26
%21 = phi i8* [ %23, %26 ], [ %19, %18 ]
%22 = phi i32 [ %27, %26 ], [ 0, %18 ]
%23 = getelementptr inbounds i8, i8* %21, i64 1
%24 = load i8, i8* %21
%25 = icmp eq i8 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %20
%27 = add nuw nsw i32 %22, 1
%28 = icmp eq i32 %27, %16
br i1 %28, label %33, label %20
29: ; preds = %20
%30 = icmp eq i32 %22, %16
br i1 %30, label %33, label %37
31: ; preds = %15
%32 = icmp eq i32 %16, 0
br i1 %32, label %33, label %39
33: ; preds = %26, %31, %29
%34 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2
%35 = load i32, i32* @RSA_R_NULL_BEFORE_BLOCK_MISSING
%36 = tail call i32 @RSAerr(i32 %34, i32 %35)
br label %53
37: ; preds = %29
%38 = icmp ult i32 %22, 8
br i1 %38, label %39, label %43
39: ; preds = %31, %37
%40 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2
%41 = load i32, i32* @RSA_R_BAD_PAD_BYTE_COUNT
%42 = tail call i32 @RSAerr(i32 %40, i32 %41)
br label %53
43: ; preds = %37
%44 = xor i32 %22, -1
%45 = add i32 %16, %44
%46 = icmp sgt i32 %45, %1
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_2
%49 = load i32, i32* @RSA_R_DATA_TOO_LARGE
%50 = tail call i32 @RSAerr(i32 %48, i32 %49)
br label %53
51: ; preds = %43
%52 = tail call i32 @memcpy(i8* %0, i8* %23, i32 %45)
br label %53
53: ; preds = %51, %47, %39, %33, %11
%54 = phi i32 [ -1, %11 ], [ -1, %33 ], [ -1, %39 ], [ -1, %47 ], [ %45, %51 ]
ret i32 %54
}