#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 */
unsigned int _uiTlvGetTagLen(unsigned char *);
int iTlvSearchObj(unsigned char *, int, int, char *, unsigned char **);
scalar_t__ memcmp(unsigned char *, unsigned char *, unsigned int);
int iTlvCheckOrder(unsigned char *psTemplate, int iTemplateLen,
                   unsigned char *psOrder) {
  int i;
  unsigned char *psTlvObj;
  int iTlvObjLen;
  unsigned int uiTagLen;
  for (i = 0;; i++) {
    iTlvObjLen = iTlvSearchObj(psTemplate, iTemplateLen, i, "\xFF", &psTlvObj);
    if (iTlvObjLen == -10002)
      return (-10002);
    if (iTlvObjLen == -10004)
      break;
    uiTagLen = _uiTlvGetTagLen(psTlvObj);
    if (memcmp(psTlvObj, psOrder, uiTagLen) != 0)
      continue;
    psOrder += uiTagLen;
    if (*psOrder == 0)
      break;
  }
  if (*psOrder != 0)
    return (-10009);
  return (0);
}


x86 assembly (clang, -O0):
<iTlvCheckOrder>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xea5(%rip),%rcx
lea    -0x30(%rbp),%r8
call   1030 <iTlvSearchObj@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0xffffd8ee,-0x34(%rbp)
jne    1180 <iTlvCheckOrder+0x50>
movl   $0xffffd8ee,-0x4(%rbp)
jmp    1218 <iTlvCheckOrder+0xe8>
cmpl   $0xffffd8ec,-0x34(%rbp)
jne    1192 <iTlvCheckOrder+0x62>
jmp    11f5 <iTlvCheckOrder+0xc5>
mov    -0x30(%rbp),%rdi
call   1050 <_uiTlvGetTagLen@plt>
mov    %eax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x38(%rbp),%edx
call   1040 <memcmp@plt>
cmp    $0x0,%rax
je     11bd <iTlvCheckOrder+0x8d>
jmp    11e7 <iTlvCheckOrder+0xb7>
mov    -0x38(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,%ecx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
jne    11e2 <iTlvCheckOrder+0xb2>
jmp    11f5 <iTlvCheckOrder+0xc5>
jmp    11e7 <iTlvCheckOrder+0xb7>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    114a <iTlvCheckOrder+0x1a>
mov    -0x20(%rbp),%rax
movzbl (%rax),%eax
cmp    $0x0,%eax
je     1211 <iTlvCheckOrder+0xe1>
movl   $0xffffd8e7,-0x4(%rbp)
jmp    1218 <iTlvCheckOrder+0xe8>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iTlvCheckOrder>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%rbx
mov    %esi,%r14d
mov    %rdi,%r15
xor    %ebp,%ebp
mov    %rsp,%r13
jmp    1153 <iTlvCheckOrder+0x23>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
mov    %r15,%rdi
mov    %r14d,%esi
mov    %ebp,%edx
lea    0xe9e(%rip),%rcx
mov    %r13,%r8
call   1030 <iTlvSearchObj@plt>
cmp    $0xffffd8ee,%eax
je     11b6 <iTlvCheckOrder+0x86>
cmp    $0xffffd8ec,%eax
je     11a5 <iTlvCheckOrder+0x75>
mov    (%rsp),%rdi
call   1050 <_uiTlvGetTagLen@plt>
mov    %eax,%r12d
mov    (%rsp),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <memcmp@plt>
test   %rax,%rax
jne    1150 <iTlvCheckOrder+0x20>
mov    %r12d,%eax
cmpb   $0x0,(%rbx,%rax,1)
je     11b4 <iTlvCheckOrder+0x84>
add    %rax,%rbx
jmp    1150 <iTlvCheckOrder+0x20>
xor    %ecx,%ecx
cmpb   $0x0,(%rbx)
mov    $0xffffd8e7,%eax
cmove  %ecx,%eax
jmp    11b6 <iTlvCheckOrder+0x86>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\FF\00"
define i32 @iTlvCheckOrder(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 0, i32* %8
br label %12
12: ; preds = %44, %3
%13 = load i8*, i8** %5
%14 = load i32, i32* %6
%15 = load i32, i32* %8
%16 = call i32 @iTlvSearchObj(i8* %13, i32 %14, i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %9)
store i32 %16, i32* %10
%17 = load i32, i32* %10
%18 = icmp eq i32 %17, -10002
br i1 %18, label %19, label %20
19: ; preds = %12
store i32 -10002, i32* %4
br label %54
20: ; preds = %12
%21 = load i32, i32* %10
%22 = icmp eq i32 %21, -10004
br i1 %22, label %23, label %24
23: ; preds = %20
br label %47
24: ; preds = %20
%25 = load i8*, i8** %9
%26 = call i32 @_uiTlvGetTagLen(i8* %25)
store i32 %26, i32* %11
%27 = load i8*, i8** %9
%28 = load i8*, i8** %7
%29 = load i32, i32* %11
%30 = call i64 @memcmp(i8* %27, i8* %28, i32 %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %33
32: ; preds = %24
br label %44
33: ; preds = %24
%34 = load i32, i32* %11
%35 = load i8*, i8** %7
%36 = zext i32 %34 to i64
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8* %37, i8** %7
%38 = load i8*, i8** %7
%39 = load i8, i8* %38
%40 = zext i8 %39 to i32
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %33
br label %47
43: ; preds = %33
br label %44
44: ; preds = %43, %32
%45 = load i32, i32* %8
%46 = add nsw i32 %45, 1
store i32 %46, i32* %8
br label %12
47: ; preds = %42, %23
%48 = load i8*, i8** %7
%49 = load i8, i8* %48
%50 = zext i8 %49 to i32
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %53
52: ; preds = %47
store i32 -10009, i32* %4
br label %54
53: ; preds = %47
store i32 0, i32* %4
br label %54
54: ; preds = %53, %52, %19
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@.str = [2 x i8] c"\FF\00"
define i32 @iTlvCheckOrder(i8* %0, i32 %1, i8* %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
br label %6
6: ; preds = %25, %3
%7 = phi i32 [ 0, %3 ], [ %27, %25 ]
%8 = phi i8* [ %2, %3 ], [ %26, %25 ]
%9 = call i32 @iTlvSearchObj(i8* %0, i32 %1, i32 %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8** %4)
switch i32 %9, label %14 [
i32 -10002, label %28
i32 -10004, label %10
]
10: ; preds = %6
%11 = load i8, i8* %8
%12 = icmp eq i8 %11, 0
%13 = select i1 %12, i32 0, i32 -10009
br label %28
14: ; preds = %6
%15 = load i8*, i8** %4
%16 = call i32 @_uiTlvGetTagLen(i8* %15)
%17 = load i8*, i8** %4
%18 = call i64 @memcmp(i8* %17, i8* %8, i32 %16)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %14
%21 = zext i32 %16 to i64
%22 = getelementptr inbounds i8, i8* %8, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %20, %14
%26 = phi i8* [ %8, %14 ], [ %22, %20 ]
%27 = add nuw nsw i32 %7, 1
br label %6
28: ; preds = %6, %20, %10
%29 = phi i32 [ %13, %10 ], [ %9, %6 ], [ 0, %20 ]
ret i32 %29
}