#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 uint16_t;
struct tlv_iterator {
  int dummy;
};

/* Variables and functions */
char *malloc(int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
char *tlv_iter_get_data(struct tlv_iterator *);
int tlv_iter_get_len(struct tlv_iterator *);
int tlv_iter_decode_str(struct tlv_iterator *tlv_iter, char **str,
                        size_t *str_len) {
  const char *opt_data;
  uint16_t opt_len;
  char *tmp_str;
  opt_len = tlv_iter_get_len(tlv_iter);
  opt_data = tlv_iter_get_data(tlv_iter);
  tmp_str = malloc(opt_len + 1);
  if (tmp_str == NULL) {
    return (-1);
  }
  memcpy(tmp_str, opt_data, opt_len);
  tmp_str[opt_len] = '\0';
  *str = tmp_str;
  *str_len = opt_len;
  return (0);
}


x86 assembly (clang, -O0):
<tlv_iter_decode_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <tlv_iter_get_len@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <tlv_iter_get_data@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
add    $0x1,%edi
call   1060 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1193 <tlv_iter_decode_str+0x53>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <tlv_iter_decode_str+0x8c>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x2c(%rbp),%edx
call   1050 <memcpy@plt>
mov    -0x38(%rbp),%rax
movslq -0x2c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x38(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
movslq -0x2c(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tlv_iter_decode_str>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
call   1040 <tlv_iter_get_len@plt>
mov    %eax,%r13d
mov    %rbx,%rdi
call   1030 <tlv_iter_get_data@plt>
mov    %rax,%r12
lea    0x1(%r13),%edi
call   1060 <malloc@plt>
test   %rax,%rax
je     1195 <tlv_iter_decode_str+0x55>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
mov    %r13d,%edx
call   1050 <memcpy@plt>
movslq %r13d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,(%r15)
mov    %rax,(%r14)
xor    %eax,%eax
jmp    119a <tlv_iter_decode_str+0x5a>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tlv_iterator = type { i32 }
define i32 @tlv_iter_decode_str(%struct.tlv_iterator* %0, i8** %1, i64* %2) {
%4 = alloca i32
%5 = alloca %struct.tlv_iterator*
%6 = alloca i8**
%7 = alloca i64*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
store %struct.tlv_iterator* %0, %struct.tlv_iterator** %5
store i8** %1, i8*** %6
store i64* %2, i64** %7
%11 = load %struct.tlv_iterator*, %struct.tlv_iterator** %5
%12 = call i32 @tlv_iter_get_len(%struct.tlv_iterator* %11)
store i32 %12, i32* %9
%13 = load %struct.tlv_iterator*, %struct.tlv_iterator** %5
%14 = call i8* @tlv_iter_get_data(%struct.tlv_iterator* %13)
store i8* %14, i8** %8
%15 = load i32, i32* %9
%16 = add nsw i32 %15, 1
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %10
%18 = load i8*, i8** %10
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %3
store i32 -1, i32* %4
br label %35
21: ; preds = %3
%22 = load i8*, i8** %10
%23 = load i8*, i8** %8
%24 = load i32, i32* %9
%25 = call i32 @memcpy(i8* %22, i8* %23, i32 %24)
%26 = load i8*, i8** %10
%27 = load i32, i32* %9
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %26, i64 %28
store i8 0, i8* %29
%30 = load i8*, i8** %10
%31 = load i8**, i8*** %6
store i8* %30, i8** %31
%32 = load i32, i32* %9
%33 = sext i32 %32 to i64
%34 = load i64*, i64** %7
store i64 %33, i64* %34
store i32 0, i32* %4
br label %35
35: ; preds = %21, %20
%36 = load i32, i32* %4
ret i32 %36
}

LLVM IR (-O2):
%struct.tlv_iterator = type { i32 }
define i32 @tlv_iter_decode_str(%struct.tlv_iterator* %0, i8** nocapture writeonly %1, i64* nocapture writeonly %2) {
%4 = tail call i32 @tlv_iter_get_len(%struct.tlv_iterator* %0)
%5 = tail call i8* @tlv_iter_get_data(%struct.tlv_iterator* %0)
%6 = add nsw i32 %4, 1
%7 = tail call i8* @malloc(i32 %6)
%8 = icmp eq i8* %7, null
br i1 %8, label %13, label %9
9: ; preds = %3
%10 = tail call i32 @memcpy(i8* %7, i8* %5, i32 %4)
%11 = sext i32 %4 to i64
%12 = getelementptr inbounds i8, i8* %7, i64 %11
store i8 0, i8* %12
store i8* %7, i8** %1
store i64 %11, i64* %2
br label %13
13: ; preds = %3, %9
%14 = phi i32 [ 0, %9 ], [ -1, %3 ]
ret i32 %14
}