#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 */
typedef int uint32_t;

/* 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) {
  uint32_t 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) {
      if (expected_eoc == ((uint32_t)-1)) {
        ASN1err(ASN1_F_ASN1_FIND_END, ERR_R_NESTED_ASN1_ERROR);
        return 0;
      }
      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    12ef <asn1_find_end+0x1bf>
movl   $0x1,-0x20(%rbp)
cmpq   $0x0,-0x18(%rbp)
jle    12b0 <asn1_find_end+0x180>
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    12b0 <asn1_find_end+0x180>
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    12ef <asn1_find_end+0x1bf>
cmpb   $0x0,-0x19(%rbp)
je     1289 <asn1_find_end+0x159>
cmpl   $0xffffffff,-0x20(%rbp)
jne    127b <asn1_find_end+0x14b>
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
mov    0x2d70(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ef <asn1_find_end+0x1bf>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1295 <asn1_find_end+0x165>
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     12dd <asn1_find_end+0x1ad>
mov    0x2d1f(%rip),%rax
mov    (%rax),%edi
mov    0x2d1e(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
movl   $0x0,-0x4(%rbp)
jmp    12ef <asn1_find_end+0x1bf>
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     11f7 <asn1_find_end+0xc7>
test   %rbx,%rbx
jle    1200 <asn1_find_end+0xd0>
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    11fc <asn1_find_end+0xcc>
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     1212 <asn1_find_end+0xe2>
add    $0xfffffffffffffffe,%rbx
test   %rbx,%rbx
jg     118c <asn1_find_end+0x5c>
jmp    11fc <asn1_find_end+0xcc>
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     1221 <asn1_find_end+0xf1>
cmpb   $0x0,0x7(%rsp)
je     1172 <asn1_find_end+0x42>
cmp    $0xffffffff,%ebp
je     1221 <asn1_find_end+0xf1>
add    $0x1,%ebp
mov    0x8(%rsp),%rax
jmp    1181 <asn1_find_end+0x51>
add    %rbx,%rax
jmp    1217 <asn1_find_end+0xe7>
test   %ebp,%ebp
je     1212 <asn1_find_end+0xe2>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
mov    0x2dd8(%rip),%rax
jmp    1231 <asn1_find_end+0x101>
mov    0x8(%rsp),%rax
mov    %rax,(%r14)
mov    $0x1,%eax
jmp    123a <asn1_find_end+0x10a>
mov    0x2db8(%rip),%rax
mov    (%rax),%edi
mov    0x2da7(%rip),%rax
mov    (%rax),%esi
call   1040 <ASN1err@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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