#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_INVALID_PADDING;
int /*<<< orphan*/ RSA_R_NULL_BEFORE_BLOCK_MISSING;
int /*<<< orphan*/ RSAerr(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char const *, unsigned 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 < 11)
    return -1;
  if (num == flen) {
    if ((*p++) != 0x00) {
      RSAerr(RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1, RSA_R_INVALID_PADDING);
      return -1;
    }
    flen--;
  }
  if ((num != (flen + 1)) || (*(p++) != 0x01)) {
    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, (unsigned int)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)
cmpl   $0xb,-0x28(%rbp)
jge    1158 <RSA_padding_check_PKCS1_type_1+0x38>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <RSA_padding_check_PKCS1_type_1+0x21b>
mov    -0x28(%rbp),%eax
cmp    -0x24(%rbp),%eax
jne    11ab <RSA_padding_check_PKCS1_type_1+0x8b>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movzbl (%rax),%eax
cmp    $0x0,%eax
je     11a2 <RSA_padding_check_PKCS1_type_1+0x82>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <RSA_padding_check_PKCS1_type_1+0x21b>
mov    -0x24(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x24(%rbp)
mov    -0x28(%rbp),%eax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
cmp    %ecx,%eax
jne    11d7 <RSA_padding_check_PKCS1_type_1+0xb7>
mov    -0x38(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x38(%rbp)
movzbl (%rax),%eax
cmp    $0x1,%eax
je     11fa <RSA_padding_check_PKCS1_type_1+0xda>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
mov    0x2dc9(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <RSA_padding_check_PKCS1_type_1+0x21b>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jge    1286 <RSA_padding_check_PKCS1_type_1+0x166>
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
cmp    $0xff,%eax
je     126c <RSA_padding_check_PKCS1_type_1+0x14c>
mov    -0x38(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    1249 <RSA_padding_check_PKCS1_type_1+0x129>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
jmp    1286 <RSA_padding_check_PKCS1_type_1+0x166>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
mov    0x2d87(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <RSA_padding_check_PKCS1_type_1+0x21b>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    120a <RSA_padding_check_PKCS1_type_1+0xea>
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    12b5 <RSA_padding_check_PKCS1_type_1+0x195>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
mov    0x2d36(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <RSA_padding_check_PKCS1_type_1+0x21b>
cmpl   $0x8,-0x2c(%rbp)
jge    12e2 <RSA_padding_check_PKCS1_type_1+0x1c2>
mov    0x2d22(%rip),%rax
mov    (%rax),%edi
mov    0x2ce9(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <RSA_padding_check_PKCS1_type_1+0x21b>
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    1325 <RSA_padding_check_PKCS1_type_1+0x205>
mov    0x2cdf(%rip),%rax
mov    (%rax),%edi
mov    0x2cb6(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    133b <RSA_padding_check_PKCS1_type_1+0x21b>
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   %rbp
push   %rbx
push   %rax
mov    $0xffffffff,%ebp
cmp    $0xb,%r8d
jl     119e <RSA_padding_check_PKCS1_type_1+0x7e>
mov    %ecx,%ebx
cmp    %ecx,%r8d
jne    1154 <RSA_padding_check_PKCS1_type_1+0x34>
cmpb   $0x0,(%rdx)
je     114c <RSA_padding_check_PKCS1_type_1+0x2c>
mov    0x2ea7(%rip),%rax
mov    (%rax),%edi
mov    0x2e76(%rip),%rax
jmp    1197 <RSA_padding_check_PKCS1_type_1+0x77>
add    $0x1,%rdx
lea    -0x1(%r8),%ebx
lea    0x1(%rbx),%eax
cmp    %r8d,%eax
jne    1187 <RSA_padding_check_PKCS1_type_1+0x67>
cmpb   $0x1,(%rdx)
jne    1187 <RSA_padding_check_PKCS1_type_1+0x67>
lea    -0x1(%rbx),%r8d
cmp    $0x2,%ebx
jl     11a7 <RSA_padding_check_PKCS1_type_1+0x87>
add    $0x2,%rdx
xor    %eax,%eax
movzbl -0x1(%rdx),%ecx
cmp    $0xff,%cl
jne    11be <RSA_padding_check_PKCS1_type_1+0x9e>
add    $0x1,%eax
add    $0x1,%rdx
cmp    %eax,%r8d
jne    1170 <RSA_padding_check_PKCS1_type_1+0x50>
jmp    11c7 <RSA_padding_check_PKCS1_type_1+0xa7>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
call   1040 <RSAerr@plt>
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
test   %r8d,%r8d
je     11c7 <RSA_padding_check_PKCS1_type_1+0xa7>
mov    0x2e35(%rip),%rax
mov    (%rax),%edi
mov    0x2dfc(%rip),%rax
jmp    1197 <RSA_padding_check_PKCS1_type_1+0x77>
test   %cl,%cl
jne    11d9 <RSA_padding_check_PKCS1_type_1+0xb9>
cmp    %eax,%r8d
jne    11eb <RSA_padding_check_PKCS1_type_1+0xcb>
mov    0x2e1a(%rip),%rax
mov    (%rax),%edi
mov    0x2e01(%rip),%rax
jmp    1197 <RSA_padding_check_PKCS1_type_1+0x77>
mov    0x2e08(%rip),%rax
mov    (%rax),%edi
mov    0x2df7(%rip),%rax
jmp    1197 <RSA_padding_check_PKCS1_type_1+0x77>
cmp    $0x7,%eax
jbe    11ac <RSA_padding_check_PKCS1_type_1+0x8c>
sub    %eax,%ebx
add    $0xfffffffe,%ebx
cmp    %esi,%ebx
jle    120b <RSA_padding_check_PKCS1_type_1+0xeb>
mov    0x2de8(%rip),%rax
mov    (%rax),%edi
mov    0x2dbf(%rip),%rax
jmp    1197 <RSA_padding_check_PKCS1_type_1+0x77>
mov    %rdx,%rsi
mov    %ebx,%edx
call   1030 <memcpy@plt>
mov    %ebx,%ebp
jmp    119e <RSA_padding_check_PKCS1_type_1+0x7e>

LLVM IR (-O0):
@RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1 = global i32 0
@RSA_R_INVALID_PADDING = 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 = icmp slt i32 %16, 11
br i1 %17, label %18, label %19
18: ; preds = %5
store i32 -1, i32* %6
br label %115
19: ; preds = %5
%20 = load i32, i32* %11
%21 = load i32, i32* %10
%22 = icmp eq i32 %20, %21
br i1 %22, label %23, label %36
23: ; preds = %19
%24 = load i8*, i8** %14
%25 = getelementptr inbounds i8, i8* %24, i32 1
store i8* %25, i8** %14
%26 = load i8, i8* %24
%27 = zext i8 %26 to i32
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %23
%30 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%31 = load i32, i32* @RSA_R_INVALID_PADDING
%32 = call i32 @RSAerr(i32 %30, i32 %31)
store i32 -1, i32* %6
br label %115
33: ; preds = %23
%34 = load i32, i32* %10
%35 = add nsw i32 %34, -1
store i32 %35, i32* %10
br label %36
36: ; preds = %33, %19
%37 = load i32, i32* %11
%38 = load i32, i32* %10
%39 = add nsw i32 %38, 1
%40 = icmp ne i32 %37, %39
br i1 %40, label %47, label %41
41: ; preds = %36
%42 = load i8*, i8** %14
%43 = getelementptr inbounds i8, i8* %42, i32 1
store i8* %43, i8** %14
%44 = load i8, i8* %42
%45 = zext i8 %44 to i32
%46 = icmp ne i32 %45, 1
br i1 %46, label %47, label %51
47: ; preds = %41, %36
%48 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%49 = load i32, i32* @RSA_R_BLOCK_TYPE_IS_NOT_01
%50 = call i32 @RSAerr(i32 %48, i32 %49)
store i32 -1, i32* %6
br label %115
51: ; preds = %41
%52 = load i32, i32* %10
%53 = sub nsw i32 %52, 1
store i32 %53, i32* %13
store i32 0, i32* %12
br label %54
54: ; preds = %78, %51
%55 = load i32, i32* %12
%56 = load i32, i32* %13
%57 = icmp slt i32 %55, %56
br i1 %57, label %58, label %81
58: ; preds = %54
%59 = load i8*, i8** %14
%60 = load i8, i8* %59
%61 = zext i8 %60 to i32
%62 = icmp ne i32 %61, 255
br i1 %62, label %63, label %75
63: ; preds = %58
%64 = load i8*, i8** %14
%65 = load i8, i8* %64
%66 = zext i8 %65 to i32
%67 = icmp eq i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %63
%69 = load i8*, i8** %14
%70 = getelementptr inbounds i8, i8* %69, i32 1
store i8* %70, i8** %14
br label %81
71: ; preds = %63
%72 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%73 = load i32, i32* @RSA_R_BAD_FIXED_HEADER_DECRYPT
%74 = call i32 @RSAerr(i32 %72, i32 %73)
store i32 -1, i32* %6
br label %115
75: ; preds = %58
%76 = load i8*, i8** %14
%77 = getelementptr inbounds i8, i8* %76, i32 1
store i8* %77, i8** %14
br label %78
78: ; preds = %75
%79 = load i32, i32* %12
%80 = add nsw i32 %79, 1
store i32 %80, i32* %12
br label %54
81: ; preds = %68, %54
%82 = load i32, i32* %12
%83 = load i32, i32* %13
%84 = icmp eq i32 %82, %83
br i1 %84, label %85, label %89
85: ; preds = %81
%86 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%87 = load i32, i32* @RSA_R_NULL_BEFORE_BLOCK_MISSING
%88 = call i32 @RSAerr(i32 %86, i32 %87)
store i32 -1, i32* %6
br label %115
89: ; preds = %81
%90 = load i32, i32* %12
%91 = icmp slt i32 %90, 8
br i1 %91, label %92, label %96
92: ; preds = %89
%93 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%94 = load i32, i32* @RSA_R_BAD_PAD_BYTE_COUNT
%95 = call i32 @RSAerr(i32 %93, i32 %94)
store i32 -1, i32* %6
br label %115
96: ; preds = %89
%97 = load i32, i32* %12
%98 = add nsw i32 %97, 1
store i32 %98, i32* %12
%99 = load i32, i32* %12
%100 = load i32, i32* %13
%101 = sub nsw i32 %100, %99
store i32 %101, i32* %13
%102 = load i32, i32* %13
%103 = load i32, i32* %8
%104 = icmp sgt i32 %102, %103
br i1 %104, label %105, label %109
105: ; preds = %96
%106 = load i32, i32* @RSA_F_RSA_PADDING_CHECK_PKCS1_TYPE_1
%107 = load i32, i32* @RSA_R_DATA_TOO_LARGE
%108 = call i32 @RSAerr(i32 %106, i32 %107)
store i32 -1, i32* %6
br label %115
109: ; preds = %96
%110 = load i8*, i8** %7
%111 = load i8*, i8** %14
%112 = load i32, i32* %13
%113 = call i32 @memcpy(i8* %110, i8* %111, i32 %112)
%114 = load i32, i32* %13
store i32 %114, i32* %6
br label %115
115: ; preds = %109, %105, %92, %85, %71, %47, %29, %18
%116 = load i32, i32* %6
ret i32 %116
}

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