#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*/ ASN1_F_ASN1_FIND_END;
int /*<<< orphan*/ ASN1_R_MISSING_EOC;
int /*<<< orphan*/ ASN1err(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ ERR_R_NESTED_ASN1_ERROR;
scalar_t__ asn1_check_eoc(unsigned char const **, long);
int /*<<< orphan*/ asn1_check_tlen(long *, int /*<<< orphan*/ *,
                                   int /*<<< orphan*/ *, char *,
                                   int /*<<< orphan*/ *, unsigned char const **,
                                   long, int, int /*<<< orphan*/,
                                   int /*<<< orphan*/, int /*<<< orphan*/ *);
int asn1_find_end(const unsigned char **in, long len, char inf) {
  int expected_eoc;
  long plen;
  const unsigned char *p = *in, *q;
  if (inf == 0) {
    *in += len;
    return 1;
  }
  expected_eoc = 1;
  while (len > 0) {
    if (asn1_check_eoc(&p, len)) {
      expected_eoc--;
      if (expected_eoc == 0)
        break;
      len -= 2;
      continue;
    }
    q = p;
    if (!asn1_check_tlen(&plen, NULL, NULL, &inf, NULL, &p, len, -1, 0, 0,
                         NULL)) {
      ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
      return 0;
    }
    if (inf)
      expected_eoc++;
    else
      p += plen;
    len -= p - q;
  }
  if (expected_eoc) {
    ASN1err(ASN1_F_ASN1_FIND_END, ASN1_R_MISSING_EOC);
    return 0;
  }
  *in = p;
  return 1;
}


x86 assembly (clang, -O0):
<asn1_find_end>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x30(%rbp)
movsbl -0x19(%rbp),%eax
cmp    $0x0,%eax
jne    1177 <asn1_find_end+0x47>
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
jmp    12c2 <asn1_find_end+0x192>
movl   $0x1,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    1283 <asn1_find_end+0x153>
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1050 <asn1_check_eoc@plt>
cmp    $0x0,%rax
je     11c9 <asn1_find_end+0x99>
mov    -0x20(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jne    11b8 <asn1_find_end+0x88>
jmp    1283 <asn1_find_end+0x153>
mov    -0x18(%rbp),%rax
sub    $0x2,%rax
mov    %rax,-0x18(%rbp)
jmp    117e <asn1_find_end+0x4e>
mov    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x18(%rbp),%rax
lea    -0x28(%rbp),%rdi
xor    %ecx,%ecx
mov    %ecx,%r8d
lea    -0x19(%rbp),%rcx
lea    -0x30(%rbp),%r9
xor    %edx,%edx
mov    %r8,%rsi
mov    %r8,%rdx
mov    %rax,(%rsp)
movl   $0xffffffff,0x8(%rsp)
ff
movl   $0x0,0x10(%rsp)
movl   $0x0,0x18(%rsp)
movq   $0x0,0x20(%rsp)
call   1030 <asn1_check_tlen@plt>
cmp    $0x0,%eax
jne    1244 <asn1_find_end+0x114>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c2 <asn1_find_end+0x192>
cmpb   $0x0,-0x19(%rbp)
je     125c <asn1_find_end+0x12c>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1268 <asn1_find_end+0x138>
mov    -0x28(%rbp),%rax
add    -0x30(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rcx
mov    -0x38(%rbp),%rax
sub    %rax,%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    117e <asn1_find_end+0x4e>
cmpl   $0x0,-0x20(%rbp)
je     12b0 <asn1_find_end+0x180>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
mov    0x2d4b(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
movl   $0x0,-0x4(%rbp)
jmp    12c2 <asn1_find_end+0x192>
mov    -0x30(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<asn1_find_end>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %dl,0x7(%rsp)
mov    (%rdi),%rax
mov    %rax,0x8(%rsp)
test   %dl,%dl
je     11f2 <asn1_find_end+0xc2>
test   %rbx,%rbx
jle    11fb <asn1_find_end+0xcb>
mov    $0x1,%ebp
lea    0x8(%rsp),%r15
lea    0x7(%rsp),%r13
jmp    118c <asn1_find_end+0x5c>
mov    0x8(%rsp),%rax
add    0x10(%rsp),%rax
mov    %rax,0x8(%rsp)
add    %r12,%rbx
sub    %rax,%rbx
test   %rbx,%rbx
jle    11f7 <asn1_find_end+0xc7>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <asn1_check_eoc@plt>
test   %rax,%rax
je     11b0 <asn1_find_end+0x80>
add    $0xffffffff,%ebp
je     1216 <asn1_find_end+0xe6>
add    $0xfffffffffffffffe,%rbx
test   %rbx,%rbx
jg     118c <asn1_find_end+0x5c>
jmp    11f7 <asn1_find_end+0xc7>
nopl   0x0(%rax)
mov    0x8(%rsp),%r12
sub    $0x8,%rsp
lea    0x18(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
mov    %r13,%rcx
xor    %r8d,%r8d
mov    %r15,%r9
push   $0x0
push   $0x0
push   $0x0
push   $0xffffffffffffffff
push   %rbx
call   1030 <asn1_check_tlen@plt>
add    $0x30,%rsp
test   %eax,%eax
je     1232 <asn1_find_end+0x102>
cmpb   $0x0,0x7(%rsp)
je     1172 <asn1_find_end+0x42>
add    $0x1,%ebp
mov    0x8(%rsp),%rax
jmp    1181 <asn1_find_end+0x51>
add    %rbx,%rax
jmp    121b <asn1_find_end+0xeb>
test   %ebp,%ebp
je     1216 <asn1_find_end+0xe6>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
xor    %eax,%eax
jmp    1223 <asn1_find_end+0xf3>
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    0x2d96(%rip),%rax
jmp    120b <asn1_find_end+0xdb>

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

LLVM IR (-O2):
@ASN1_F_ASN1_FIND_END = global i32 0
@ERR_R_NESTED_ASN1_ERROR = global i32 0
@ASN1_R_MISSING_EOC = global i32 0
define i32 @asn1_find_end(i8** nocapture %0, i64 %1, i8 signext %2) {
%4 = alloca i8
%5 = alloca i64
%6 = alloca i8*
store i8 %2, i8* %4
%7 = bitcast i64* %5 to i8*
%8 = bitcast i8** %6 to i8*
%9 = load i8*, i8** %0
store i8* %9, i8** %6
%10 = icmp eq i8 %2, 0
br i1 %10, label %13, label %11
11: ; preds = %3
%12 = icmp sgt i64 %1, 0
br i1 %12, label %15, label %56
13: ; preds = %3
%14 = getelementptr inbounds i8, i8* %9, i64 %1
store i8* %14, i8** %0
br label %62
15: ; preds = %11, %25
%16 = phi i32 [ %27, %25 ], [ 1, %11 ]
%17 = phi i64 [ %26, %25 ], [ %1, %11 ]
%18 = call i64 @asn1_check_eoc(i8** %6, i64 %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %29, label %20
20: ; preds = %15
%21 = add nsw i32 %16, -1
%22 = icmp eq i32 %21, 0
br i1 %22, label %60, label %23
23: ; preds = %20
%24 = add nsw i64 %17, -2
br label %25
25: ; preds = %23, %47
%26 = phi i64 [ %24, %23 ], [ %53, %47 ]
%27 = phi i32 [ %21, %23 ], [ %49, %47 ]
%28 = icmp sgt i64 %26, 0
br i1 %28, label %15, label %54
29: ; preds = %15
%30 = load i8*, i8** %6
%31 = call i32 @asn1_check_tlen(i64* %5, i32* null, i32* null, i8* %4, i32* null, i8** %6, i64 %17, i32 -1, i32 0, i32 0, i32* null)
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %29
%34 = load i32, i32* @ASN1_F_ASN1_FIND_END
%35 = load i32, i32* @ERR_R_NESTED_ASN1_ERROR
%36 = call i32 @ASN1err(i32 %34, i32 %35)
br label %62
37: ; preds = %29
%38 = load i8, i8* %4
%39 = icmp eq i8 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %37
%41 = add nsw i32 %16, 1
%42 = load i8*, i8** %6
br label %47
43: ; preds = %37
%44 = load i64, i64* %5
%45 = load i8*, i8** %6
%46 = getelementptr inbounds i8, i8* %45, i64 %44
store i8* %46, i8** %6
br label %47
47: ; preds = %43, %40
%48 = phi i8* [ %42, %40 ], [ %46, %43 ]
%49 = phi i32 [ %41, %40 ], [ %16, %43 ]
%50 = ptrtoint i8* %48 to i64
%51 = ptrtoint i8* %30 to i64
%52 = add i64 %17, %51
%53 = sub i64 %52, %50
br label %25
54: ; preds = %25
%55 = icmp eq i32 %27, 0
br i1 %55, label %60, label %56
56: ; preds = %11, %54
%57 = load i32, i32* @ASN1_F_ASN1_FIND_END
%58 = load i32, i32* @ASN1_R_MISSING_EOC
%59 = call i32 @ASN1err(i32 %57, i32 %58)
br label %62
60: ; preds = %20, %54
%61 = load i8*, i8** %6
store i8* %61, i8** %0
br label %62
62: ; preds = %60, %56, %33, %13
%63 = phi i32 [ 1, %13 ], [ 0, %56 ], [ 1, %60 ], [ 0, %33 ]
ret i32 %63
}