#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_R_MISSING_EOC;
int /*<<< orphan*/ ASN1error(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)) {
      ASN1error(ERR_R_NESTED_ASN1_ERROR);
      return 0;
    }
    if (inf)
      expected_eoc++;
    else
      p += plen;
    len -= p - q;
  }
  if (expected_eoc) {
    ASN1error(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    12b0 <asn1_find_end+0x180>
movl   $0x1,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    127a <asn1_find_end+0x14a>
mov    -0x18(%rbp),%rsi
lea    -0x30(%rbp),%rdi
call   1040 <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    127a <asn1_find_end+0x14a>
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    123b <asn1_find_end+0x10b>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
call   1050 <ASN1error@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <asn1_find_end+0x180>
cmpb   $0x0,-0x19(%rbp)
je     1253 <asn1_find_end+0x123>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    125f <asn1_find_end+0x12f>
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     129e <asn1_find_end+0x16e>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
call   1050 <ASN1error@plt>
movl   $0x0,-0x4(%rbp)
jmp    12b0 <asn1_find_end+0x180>
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   1040 <asn1_check_eoc@plt>
test   %rax,%rax
je     11b0 <asn1_find_end+0x80>
add    $0xffffffff,%ebp
je     120d <asn1_find_end+0xdd>
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     1229 <asn1_find_end+0xf9>
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    1212 <asn1_find_end+0xe2>
test   %ebp,%ebp
je     120d <asn1_find_end+0xdd>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1050 <ASN1error@plt>
xor    %eax,%eax
jmp    121a <asn1_find_end+0xea>
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    0x2db0(%rip),%rax
jmp    1202 <asn1_find_end+0xd2>

LLVM IR (-O0):
@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 %74
22: ; preds = %3
store i32 1, i32* %8
br label %23
23: ; preds = %57, %36, %22
%24 = load i64, i64* %6
%25 = icmp sgt i64 %24, 0
br i1 %25, label %26, label %65
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 %65
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 %47, label %44
44: ; preds = %39
%45 = load i32, i32* @ERR_R_NESTED_ASN1_ERROR
%46 = call i32 @ASN1error(i32 %45)
store i32 0, i32* %4
br label %74
47: ; preds = %39
%48 = load i8, i8* %7
%49 = icmp ne i8 %48, 0
br i1 %49, label %50, label %53
50: ; preds = %47
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
br label %57
53: ; preds = %47
%54 = load i64, i64* %9
%55 = load i8*, i8** %10
%56 = getelementptr inbounds i8, i8* %55, i64 %54
store i8* %56, i8** %10
br label %57
57: ; preds = %53, %50
%58 = load i8*, i8** %10
%59 = load i8*, i8** %11
%60 = ptrtoint i8* %58 to i64
%61 = ptrtoint i8* %59 to i64
%62 = sub i64 %60, %61
%63 = load i64, i64* %6
%64 = sub nsw i64 %63, %62
store i64 %64, i64* %6
br label %23
65: ; preds = %35, %23
%66 = load i32, i32* %8
%67 = icmp ne i32 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %65
%69 = load i32, i32* @ASN1_R_MISSING_EOC
%70 = call i32 @ASN1error(i32 %69)
store i32 0, i32* %4
br label %74
71: ; preds = %65
%72 = load i8*, i8** %10
%73 = load i8**, i8*** %5
store i8* %72, i8** %73
store i32 1, i32* %4
br label %74
74: ; preds = %71, %68, %44, %17
%75 = load i32, i32* %4
ret i32 %75
}

LLVM IR (-O2):
@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 %55
13: ; preds = %3
%14 = getelementptr inbounds i8, i8* %9, i64 %1
store i8* %14, i8** %0
br label %60
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 %58, label %23
23: ; preds = %20
%24 = add nsw i64 %17, -2
br label %25
25: ; preds = %23, %46
%26 = phi i64 [ %24, %23 ], [ %52, %46 ]
%27 = phi i32 [ %21, %23 ], [ %48, %46 ]
%28 = icmp sgt i64 %26, 0
br i1 %28, label %15, label %53
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 %36
33: ; preds = %29
%34 = load i32, i32* @ERR_R_NESTED_ASN1_ERROR
%35 = call i32 @ASN1error(i32 %34)
br label %60
36: ; preds = %29
%37 = load i8, i8* %4
%38 = icmp eq i8 %37, 0
br i1 %38, label %42, label %39
39: ; preds = %36
%40 = add nsw i32 %16, 1
%41 = load i8*, i8** %6
br label %46
42: ; preds = %36
%43 = load i64, i64* %5
%44 = load i8*, i8** %6
%45 = getelementptr inbounds i8, i8* %44, i64 %43
store i8* %45, i8** %6
br label %46
46: ; preds = %42, %39
%47 = phi i8* [ %41, %39 ], [ %45, %42 ]
%48 = phi i32 [ %40, %39 ], [ %16, %42 ]
%49 = ptrtoint i8* %47 to i64
%50 = ptrtoint i8* %30 to i64
%51 = add i64 %17, %50
%52 = sub i64 %51, %49
br label %25
53: ; preds = %25
%54 = icmp eq i32 %27, 0
br i1 %54, label %58, label %55
55: ; preds = %11, %53
%56 = load i32, i32* @ASN1_R_MISSING_EOC
%57 = call i32 @ASN1error(i32 %56)
br label %60
58: ; preds = %20, %53
%59 = load i8*, i8** %6
store i8* %59, i8** %0
br label %60
60: ; preds = %58, %55, %33, %13
%61 = phi i32 [ 1, %13 ], [ 0, %55 ], [ 1, %58 ], [ 0, %33 ]
ret i32 %61
}