#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 iTlvAddObj(unsigned char *, unsigned char *, int);
int iTlvGetObjByIndex(unsigned char *, int, unsigned char **);
int iTlvCopyBuffer(unsigned char *psTlvTargetBuffer,
                   unsigned char *psTlvSourceBuffer, int iConflictFlag) {
  int iRet, i;
  unsigned char *psTlvObject;
  for (i = 0;; i++) {
    iRet = iTlvGetObjByIndex(psTlvSourceBuffer, i, &psTlvObject);
    if (iRet == -10004)
      break;
    if (iRet < 0)
      return (iRet);
    iRet = iTlvAddObj(psTlvTargetBuffer, psTlvObject, iConflictFlag);
    if (iRet < 0)
      return (iRet);
  }
  return (i);
}


x86 assembly (clang, -O0):
<iTlvCopyBuffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
lea    -0x30(%rbp),%rdx
call   1030 <iTlvGetObjByIndex@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0xffffd8ec,-0x20(%rbp)
jne    115f <iTlvCopyBuffer+0x3f>
jmp    11af <iTlvCopyBuffer+0x8f>
cmpl   $0x0,-0x20(%rbp)
jge    1174 <iTlvCopyBuffer+0x54>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <iTlvCopyBuffer+0x95>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1040 <iTlvAddObj@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    119c <iTlvCopyBuffer+0x7c>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11b5 <iTlvCopyBuffer+0x95>
jmp    11a1 <iTlvCopyBuffer+0x81>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    113a <iTlvCopyBuffer+0x1a>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iTlvCopyBuffer>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r14d
mov    %rsi,%r13
mov    %rdi,%r15
xor    %ebp,%ebp
mov    %rsp,%rdx
mov    %rsi,%rdi
xor    %esi,%esi
call   1030 <iTlvGetObjByIndex@plt>
cmp    $0xffffd8ec,%eax
je     1186 <iTlvCopyBuffer+0x66>
xor    %ebx,%ebx
mov    %rsp,%r12
nop
mov    %eax,%ebp
test   %eax,%eax
js     1186 <iTlvCopyBuffer+0x66>
mov    (%rsp),%rsi
mov    %r15,%rdi
mov    %r14d,%edx
call   1040 <iTlvAddObj@plt>
test   %eax,%eax
js     1184 <iTlvCopyBuffer+0x64>
add    $0x1,%ebx
mov    %r13,%rdi
mov    %ebx,%esi
mov    %r12,%rdx
call   1030 <iTlvGetObjByIndex@plt>
mov    %ebx,%ebp
cmp    $0xffffd8ec,%eax
jne    1150 <iTlvCopyBuffer+0x30>
jmp    1186 <iTlvCopyBuffer+0x66>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @iTlvCopyBuffer(i8* %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %9
br label %11
11: ; preds = %33, %3
%12 = load i8*, i8** %6
%13 = load i32, i32* %9
%14 = call i32 @iTlvGetObjByIndex(i8* %12, i32 %13, i8** %10)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -10004
br i1 %16, label %17, label %18
17: ; preds = %11
br label %36
18: ; preds = %11
%19 = load i32, i32* %8
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %23
21: ; preds = %18
%22 = load i32, i32* %8
store i32 %22, i32* %4
br label %38
23: ; preds = %18
%24 = load i8*, i8** %5
%25 = load i8*, i8** %10
%26 = load i32, i32* %7
%27 = call i32 @iTlvAddObj(i8* %24, i8* %25, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %23
%31 = load i32, i32* %8
store i32 %31, i32* %4
br label %38
32: ; preds = %23
br label %33
33: ; preds = %32
%34 = load i32, i32* %9
%35 = add nsw i32 %34, 1
store i32 %35, i32* %9
br label %11
36: ; preds = %17
%37 = load i32, i32* %9
store i32 %37, i32* %4
br label %38
38: ; preds = %36, %30, %21
%39 = load i32, i32* %4
ret i32 %39
}

LLVM IR (-O2):
define i32 @iTlvCopyBuffer(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = bitcast i8** %4 to i8*
%6 = call i32 @iTlvGetObjByIndex(i8* %1, i32 0, i8** %4)
%7 = icmp eq i32 %6, -10004
br i1 %7, label %20, label %8
8: ; preds = %3, %16
%9 = phi i32 [ %18, %16 ], [ %6, %3 ]
%10 = phi i32 [ %17, %16 ], [ 0, %3 ]
%11 = icmp slt i32 %9, 0
br i1 %11, label %20, label %12
12: ; preds = %8
%13 = load i8*, i8** %4
%14 = call i32 @iTlvAddObj(i8* %0, i8* %13, i32 %2)
%15 = icmp slt i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %12
%17 = add nuw nsw i32 %10, 1
%18 = call i32 @iTlvGetObjByIndex(i8* %1, i32 %17, i8** %4)
%19 = icmp eq i32 %18, -10004
br i1 %19, label %20, label %8
20: ; preds = %8, %12, %16, %3
%21 = phi i32 [ 0, %3 ], [ %17, %16 ], [ %14, %12 ], [ %9, %8 ]
ret i32 %21
}