#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_1;
int /*<<< orphan*/ RSA_R_BAD_FIXED_HEADER_DECRYPT;
int /*<<< orphan*/ RSA_R_BAD_PAD_BYTE_COUNT;
int /*<<< orphan*/ RSA_R_BLOCK_TYPE_IS_NOT_01;
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_1(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++) != 01) {
    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_BLOCK_TYPE_IS_NOT_01);
    return -1;
  }
  j = flen - 1;
  for (i = 0; i < j; i++) {
    if (*p != 0xff) {
      if (*p == 0) {
        p++;
        break;
      } else {
        RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
               RSA_R_BAD_FIXED_HEADER_DECRYPT);
        return -1;
      }
    }
    p++;
  }
  if (i == j) {
    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1,
           RSA_R_NULL_BEFORE_BLOCK_MISSING);
    return -1;
  }
  if (i < 8) {
    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_BAD_PAD_BYTE_COUNT);
    return -1;
  }
  i++;
  j -= i;
  if (j > tlen) {
    RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_DATA_TOO_LARGE);
    return -1;
  }
  memcpy(to, p, j);
  return j;
}


x86 assembly (clang, -O0):
<RSA_padding_check_PKCS1_type_1>:
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_1+0x4e>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movzbl (%rax),%eax
cmp    $0x1,%eax
je     1191 <RSA_padding_check_PKCS1_type_1+0x71>
mov    0x2e73(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <RSA_padding_check_PKCS1_type_1+0x1b2>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    121d <RSA_padding_check_PKCS1_type_1+0xfd>
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
cmp    $0xff,%eax
je     1203 <RSA_padding_check_PKCS1_type_1+0xe3>
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    11e0 <RSA_padding_check_PKCS1_type_1+0xc0>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    121d <RSA_padding_check_PKCS1_type_1+0xfd>
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
mov    0x2df0(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <RSA_padding_check_PKCS1_type_1+0x1b2>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a1 <RSA_padding_check_PKCS1_type_1+0x81>
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    124c <RSA_padding_check_PKCS1_type_1+0x12c>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <RSA_padding_check_PKCS1_type_1+0x1b2>
cmpl   $0x8,-0x2c(%rbp)
jge    1279 <RSA_padding_check_PKCS1_type_1+0x159>
mov    0x2d8b(%rip),%rax
mov    (%rax),%edi
mov    0x2d5a(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <RSA_padding_check_PKCS1_type_1+0x1b2>
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    12bc <RSA_padding_check_PKCS1_type_1+0x19c>
mov    0x2d48(%rip),%rax
mov    (%rax),%edi
mov    0x2d1f(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <RSA_padding_check_PKCS1_type_1+0x1b2>
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_1>:
push   %rbx
mov    %ecx,%ebx
lea    0x1(%rbx),%eax
cmp    %r8d,%eax
jne    1157 <RSA_padding_check_PKCS1_type_1+0x37>
cmpb   $0x1,(%rdx)
jne    1157 <RSA_padding_check_PKCS1_type_1+0x37>
lea    -0x1(%rbx),%r8d
cmp    $0x2,%ebx
jl     1177 <RSA_padding_check_PKCS1_type_1+0x57>
add    $0x2,%rdx
xor    %eax,%eax
nop
movzbl -0x1(%rdx),%ecx
cmp    $0xff,%cl
jne    118e <RSA_padding_check_PKCS1_type_1+0x6e>
add    $0x1,%eax
add    $0x1,%rdx
cmp    %eax,%r8d
jne    1140 <RSA_padding_check_PKCS1_type_1+0x20>
jmp    1197 <RSA_padding_check_PKCS1_type_1+0x77>
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
pop    %rbx
ret
test   %r8d,%r8d
je     1197 <RSA_padding_check_PKCS1_type_1+0x77>
mov    0x2e65(%rip),%rax
mov    (%rax),%edi
mov    0x2e34(%rip),%rax
jmp    1167 <RSA_padding_check_PKCS1_type_1+0x47>
test   %cl,%cl
jne    11a9 <RSA_padding_check_PKCS1_type_1+0x89>
cmp    %eax,%r8d
jne    11bb <RSA_padding_check_PKCS1_type_1+0x9b>
mov    0x2e4a(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
jmp    1167 <RSA_padding_check_PKCS1_type_1+0x47>
mov    0x2e38(%rip),%rax
mov    (%rax),%edi
mov    0x2e27(%rip),%rax
jmp    1167 <RSA_padding_check_PKCS1_type_1+0x47>
cmp    $0x7,%eax
jbe    117c <RSA_padding_check_PKCS1_type_1+0x5c>
sub    %eax,%ebx
add    $0xfffffffe,%ebx
cmp    %esi,%ebx
jle    11db <RSA_padding_check_PKCS1_type_1+0xbb>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
mov    0x2def(%rip),%rax
jmp    1167 <RSA_padding_check_PKCS1_type_1+0x47>
mov    %rdx,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
jmp    1173 <RSA_padding_check_PKCS1_type_1+0x53>

LLVM IR (-O0):
@RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 = global i32 0
@RSA_R_BLOCK_TYPE_IS_NOT_01 = global i32 0
@RSA_R_BAD_FIXED_HEADER_DECRYPT = 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_1(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, 1
br i1 %25, label %26, label %30
26: ; preds = %20, %5
%27 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%28 = load i32, i32* @RSA_R_BLOCK_TYPE_IS_NOT_01
%29 = call i32 @RSAerr(i32 %27, i32 %28)
store i32 -1, i32* %6
br label %94
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 = %57, %30
%34 = load i32, i32* %12
%35 = load i32, i32* %13
%36 = icmp slt i32 %34, %35
br i1 %36, label %37, label %60
37: ; preds = %33
%38 = load i8*, i8** %14
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = icmp ne i32 %40, 255
br i1 %41, label %42, label %54
42: ; preds = %37
%43 = load i8*, i8** %14
%44 = load i8, i8* %43
%45 = zext i8 %44 to i32
%46 = icmp eq i32 %45, 0
br i1 %46, label %47, label %50
47: ; preds = %42
%48 = load i8*, i8** %14
%49 = getelementptr inbounds i8, i8* %48, i32 1
store i8* %49, i8** %14
br label %60
50: ; preds = %42
%51 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%52 = load i32, i32* @RSA_R_BAD_FIXED_HEADER_DECRYPT
%53 = call i32 @RSAerr(i32 %51, i32 %52)
store i32 -1, i32* %6
br label %94
54: ; preds = %37
%55 = load i8*, i8** %14
%56 = getelementptr inbounds i8, i8* %55, i32 1
store i8* %56, i8** %14
br label %57
57: ; preds = %54
%58 = load i32, i32* %12
%59 = add nsw i32 %58, 1
store i32 %59, i32* %12
br label %33
60: ; preds = %47, %33
%61 = load i32, i32* %12
%62 = load i32, i32* %13
%63 = icmp eq i32 %61, %62
br i1 %63, label %64, label %68
64: ; preds = %60
%65 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%66 = load i32, i32* @RSA_R_NULL_BEFORE_BLOCK_MISSING
%67 = call i32 @RSAerr(i32 %65, i32 %66)
store i32 -1, i32* %6
br label %94
68: ; preds = %60
%69 = load i32, i32* %12
%70 = icmp slt i32 %69, 8
br i1 %70, label %71, label %75
71: ; preds = %68
%72 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%73 = load i32, i32* @RSA_R_BAD_PAD_BYTE_COUNT
%74 = call i32 @RSAerr(i32 %72, i32 %73)
store i32 -1, i32* %6
br label %94
75: ; preds = %68
%76 = load i32, i32* %12
%77 = add nsw i32 %76, 1
store i32 %77, i32* %12
%78 = load i32, i32* %12
%79 = load i32, i32* %13
%80 = sub nsw i32 %79, %78
store i32 %80, i32* %13
%81 = load i32, i32* %13
%82 = load i32, i32* %8
%83 = icmp sgt i32 %81, %82
br i1 %83, label %84, label %88
84: ; preds = %75
%85 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%86 = load i32, i32* @RSA_R_DATA_TOO_LARGE
%87 = call i32 @RSAerr(i32 %85, i32 %86)
store i32 -1, i32* %6
br label %94
88: ; preds = %75
%89 = load i8*, i8** %7
%90 = load i8*, i8** %14
%91 = load i32, i32* %13
%92 = call i32 @memcpy(i8* %89, i8* %90, i32 %91)
%93 = load i32, i32* %13
store i32 %93, i32* %6
br label %94
94: ; preds = %88, %84, %71, %64, %50, %26
%95 = load i32, i32* %6
ret i32 %95
}

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