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

/* Variables and functions */
int iDgiAddItem(unsigned char *, unsigned char *, int);
int iTlvMakeObject(unsigned char *, unsigned short, unsigned char *,
                   unsigned char *, int);
int iDgiAddItemByTLV(unsigned char *psDgiBuf, unsigned char *psTag,
                     unsigned int uiLength, unsigned char *psValue) {
  unsigned char sBuf[256];
  int iRet, iLen;
  if (uiLength > 249)
    return (2);
  iLen = iTlvMakeObject(psTag, (unsigned short)uiLength, psValue, sBuf,
                        sizeof(sBuf));
  if (iLen <= 0)
    return (3);
  iRet = iDgiAddItem(psDgiBuf, sBuf, iLen);
  if (iRet)
    return (1);
  return (0);
}


x86 assembly (clang, -O0):
<iDgiAddItemByTLV>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
cmpl   $0xf9,-0x1c(%rbp)
jbe    1153 <iDgiAddItemByTLV+0x33>
movl   $0x2,-0x4(%rbp)
jmp    11ce <iDgiAddItemByTLV+0xae>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%eax
mov    -0x28(%rbp),%rdx
lea    -0x130(%rbp),%rcx
mov    $0x100,%r8d
movzwl %ax,%esi
call   1040 <iTlvMakeObject@plt>
mov    %eax,-0x138(%rbp)
cmpl   $0x0,-0x138(%rbp)
jg     1192 <iDgiAddItemByTLV+0x72>
movl   $0x3,-0x4(%rbp)
jmp    11ce <iDgiAddItemByTLV+0xae>
mov    -0x10(%rbp),%rdi
lea    -0x130(%rbp),%rsi
mov    -0x138(%rbp),%edx
call   1030 <iDgiAddItem@plt>
mov    %eax,-0x134(%rbp)
cmpl   $0x0,-0x134(%rbp)
je     11c7 <iDgiAddItemByTLV+0xa7>
movl   $0x1,-0x4(%rbp)
jmp    11ce <iDgiAddItemByTLV+0xae>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iDgiAddItemByTLV>:
push   %rbx
sub    $0x100,%rsp
mov    $0x2,%eax
cmp    $0xf9,%edx
ja     1175 <iDgiAddItemByTLV+0x55>
mov    %rdi,%rbx
movzwl %dx,%eax
mov    %rsp,%r8
mov    %rsi,%rdi
mov    %eax,%esi
mov    %rcx,%rdx
mov    %r8,%rcx
mov    $0x100,%r8d
call   1040 <iTlvMakeObject@plt>
test   %eax,%eax
jle    1170 <iDgiAddItemByTLV+0x50>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    %eax,%edx
call   1030 <iDgiAddItem@plt>
mov    %eax,%ecx
xor    %eax,%eax
test   %ecx,%ecx
setne  %al
jmp    1175 <iDgiAddItemByTLV+0x55>
mov    $0x3,%eax
add    $0x100,%rsp
pop    %rbx
ret

LLVM IR (-O0):
define i32 @iDgiAddItemByTLV(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [256 x i8]
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i8* %3, i8** %9
%13 = load i32, i32* %8
%14 = icmp ugt i32 %13, 249
br i1 %14, label %15, label %16
15: ; preds = %4
store i32 2, i32* %5
br label %35
16: ; preds = %4
%17 = load i8*, i8** %7
%18 = load i32, i32* %8
%19 = trunc i32 %18 to i16
%20 = load i8*, i8** %9
%21 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%22 = call i32 @iTlvMakeObject(i8* %17, i16 zeroext %19, i8* %20, i8* %21, i32 256)
store i32 %22, i32* %12
%23 = load i32, i32* %12
%24 = icmp sle i32 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %16
store i32 3, i32* %5
br label %35
26: ; preds = %16
%27 = load i8*, i8** %6
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%29 = load i32, i32* %12
%30 = call i32 @iDgiAddItem(i8* %27, i8* %28, i32 %29)
store i32 %30, i32* %11
%31 = load i32, i32* %11
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %26
store i32 1, i32* %5
br label %35
34: ; preds = %26
store i32 0, i32* %5
br label %35
35: ; preds = %34, %33, %25, %15
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
define i32 @iDgiAddItemByTLV(i8* %0, i8* %1, i32 %2, i8* %3) {
%5 = alloca [256 x i8]
%6 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%7 = icmp ugt i32 %2, 249
br i1 %7, label %16, label %8
8: ; preds = %4
%9 = trunc i32 %2 to i16
%10 = call i32 @iTlvMakeObject(i8* %1, i16 zeroext %9, i8* %3, i8* %6, i32 256)
%11 = icmp slt i32 %10, 1
br i1 %11, label %16, label %12
12: ; preds = %8
%13 = call i32 @iDgiAddItem(i8* %0, i8* %6, i32 %10)
%14 = icmp ne i32 %13, 0
%15 = zext i1 %14 to i32
br label %16
16: ; preds = %12, %8, %4
%17 = phi i32 [ 2, %4 ], [ 3, %8 ], [ %15, %12 ]
ret i32 %17
}