#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 /*<<< orphan*/ sTlvBuf;

/* Variables and functions */
int iTlvBatchAddObj(int, unsigned char *, unsigned char *, int,
                    int /*<<< orphan*/, int);
int iTlvCheckTlvObject(unsigned char *);
int /*<<< orphan*/ iTlvSetBuffer(unsigned char *, int);
int iTlvCheckDup(unsigned char *psTemplate, int iTemplateLen) {
  unsigned char sTlvBuf[400];
  int iRet;
  iTlvSetBuffer(sTlvBuf, sizeof(sTlvBuf));
  iRet = iTlvCheckTlvObject(psTemplate);
  if (iRet < 0 || iRet > iTemplateLen)
    return (-10009);
  iRet = iTlvBatchAddObj(1, sTlvBuf, psTemplate, iTemplateLen, 0, 1);
  if (iRet == -10003)
    return (-10009);
  if (iRet < 0)
    return (-10002);
  return (0);
}


x86 assembly (clang, -O0):
<iTlvCheckDup>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1c0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x1b0(%rbp),%rdi
mov    $0x190,%esi
call   1030 <iTlvSetBuffer@plt>
mov    -0x10(%rbp),%rdi
call   1040 <iTlvCheckTlvObject@plt>
mov    %eax,-0x1b4(%rbp)
cmpl   $0x0,-0x1b4(%rbp)
jl     117e <iTlvCheckDup+0x4e>
mov    -0x1b4(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    118a <iTlvCheckDup+0x5a>
movl   $0xffffd8e7,-0x4(%rbp)
jmp    11eb <iTlvCheckDup+0xbb>
lea    -0x1b0(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    $0x1,%r9d
xor    %r8d,%r8d
mov    %r9d,%edi
call   1050 <iTlvBatchAddObj@plt>
mov    %eax,-0x1b4(%rbp)
cmpl   $0xffffd8ed,-0x1b4(%rbp)
d8 ff ff
jne    11cb <iTlvCheckDup+0x9b>
movl   $0xffffd8e7,-0x4(%rbp)
jmp    11eb <iTlvCheckDup+0xbb>
cmpl   $0x0,-0x1b4(%rbp)
jge    11e4 <iTlvCheckDup+0xb4>
movl   $0xffffd8ee,-0x4(%rbp)
jmp    11eb <iTlvCheckDup+0xbb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x1c0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iTlvCheckDup>:
push   %rbp
push   %r14
push   %rbx
sub    $0x190,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x190,%esi
call   1030 <iTlvSetBuffer@plt>
mov    %rbx,%rdi
call   1040 <iTlvCheckTlvObject@plt>
mov    $0xffffd8e7,%r14d
test   %eax,%eax
js     1190 <iTlvCheckDup+0x60>
cmp    %ebp,%eax
jg     1190 <iTlvCheckDup+0x60>
mov    %rsp,%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
mov    %ebp,%ecx
xor    %r8d,%r8d
mov    $0x1,%r9d
call   1050 <iTlvBatchAddObj@plt>
cmp    $0xffffd8ed,%eax
je     1190 <iTlvCheckDup+0x60>
sar    $0x1f,%eax
and    $0xffffd8ee,%eax
mov    %eax,%r14d
mov    %r14d,%eax
add    $0x190,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @iTlvCheckDup(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [400 x i8]
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = getelementptr inbounds [400 x i8], [400 x i8]* %6, i64 0, i64 0
%9 = call i32 @iTlvSetBuffer(i8* %8, i32 400)
%10 = load i8*, i8** %4
%11 = call i32 @iTlvCheckTlvObject(i8* %10)
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = icmp slt i32 %12, 0
br i1 %13, label %18, label %14
14: ; preds = %2
%15 = load i32, i32* %7
%16 = load i32, i32* %5
%17 = icmp sgt i32 %15, %16
br i1 %17, label %18, label %19
18: ; preds = %14, %2
store i32 -10009, i32* %3
br label %32
19: ; preds = %14
%20 = getelementptr inbounds [400 x i8], [400 x i8]* %6, i64 0, i64 0
%21 = load i8*, i8** %4
%22 = load i32, i32* %5
%23 = call i32 @iTlvBatchAddObj(i32 1, i8* %20, i8* %21, i32 %22, i32 0, i32 1)
store i32 %23, i32* %7
%24 = load i32, i32* %7
%25 = icmp eq i32 %24, -10003
br i1 %25, label %26, label %27
26: ; preds = %19
store i32 -10009, i32* %3
br label %32
27: ; preds = %19
%28 = load i32, i32* %7
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %27
store i32 -10002, i32* %3
br label %32
31: ; preds = %27
store i32 0, i32* %3
br label %32
32: ; preds = %31, %30, %26, %18
%33 = load i32, i32* %3
ret i32 %33
}

LLVM IR (-O2):
define i32 @iTlvCheckDup(i8* %0, i32 %1) {
%3 = alloca [400 x i8]
%4 = getelementptr inbounds [400 x i8], [400 x i8]* %3, i64 0, i64 0
%5 = call i32 @iTlvSetBuffer(i8* %4, i32 400)
%6 = call i32 @iTlvCheckTlvObject(i8* %0)
%7 = icmp slt i32 %6, 0
%8 = icmp sgt i32 %6, %1
%9 = or i1 %7, %8
br i1 %9, label %16, label %10
10: ; preds = %2
%11 = call i32 @iTlvBatchAddObj(i32 1, i8* %4, i8* %0, i32 %1, i32 0, i32 1)
%12 = icmp eq i32 %11, -10003
br i1 %12, label %16, label %13
13: ; preds = %10
%14 = icmp slt i32 %11, 0
%15 = select i1 %14, i32 -10002, i32 0
br label %16
16: ; preds = %13, %10, %2
%17 = phi i32 [ -10009, %2 ], [ -10009, %10 ], [ %15, %13 ]
ret i32 %17
}