#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 */
struct target {
  int dummy;
};

/* Variables and functions */
char *copy_params(char **, int, int *);
scalar_t__ dump_n_bytes_from_mem(unsigned int, char **, struct target *, int);
scalar_t__ dump_ptr(unsigned int, char **);
scalar_t__ dump_unsigned_int_from_mem(unsigned int, char **, struct target *);
int /*<<< orphan*/ free_dumped_params(char **, int);
void *malloc(int);
unsigned int read_ptr_from_mem(unsigned int, struct target *);
int dump_iovec(int depth, unsigned int addr, char **dumped_params,
               struct target *target) {
  char **dumped_type_params;
  unsigned int arm_tracing_iov_base = addr;
  unsigned int arm_tracing_iov_len = addr + 4;
  int len = 0;
  if (depth < 0) {
    *dumped_params = malloc(0);
    return len;
  }
  dumped_type_params = malloc(2 * (sizeof(char *)));
  arm_tracing_iov_base = read_ptr_from_mem(arm_tracing_iov_base, target);
  if (depth == 0) {
    len += dump_ptr(arm_tracing_iov_base, &dumped_type_params[0]);
    len += dump_unsigned_int_from_mem(arm_tracing_iov_len,
                                      &dumped_type_params[1], target);
    *dumped_params = copy_params(dumped_type_params, 2, &len);
    free_dumped_params(dumped_type_params, 2);
    return len;
  }
  if (depth >= 1) {
    len += dump_n_bytes_from_mem(arm_tracing_iov_base, &dumped_type_params[0],
                                 target, 256);
    len += dump_unsigned_int_from_mem(arm_tracing_iov_len,
                                      &dumped_type_params[1], target);
  }
  *dumped_params = copy_params(dumped_type_params, 2, &len);
  free_dumped_params(dumped_type_params, 2);
  return len;
}


x86 assembly (clang, -O0):
<dump_iovec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0xc(%rbp),%eax
add    $0x4,%eax
mov    %eax,-0x30(%rbp)
movl   $0x0,-0x34(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11c2 <dump_iovec+0x52>
xor    %edi,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d5 <dump_iovec+0x165>
mov    $0x10,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1070 <read_ptr_from_mem@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    1258 <dump_iovec+0xe8>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1050 <dump_ptr@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x8,%rsi
mov    -0x20(%rbp),%rdx
call   1090 <dump_unsigned_int_from_mem@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2,%esi
lea    -0x34(%rbp),%rdx
call   1060 <copy_params@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x2,%esi
call   1040 <free_dumped_params@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12d5 <dump_iovec+0x165>
cmpl   $0x1,-0x8(%rbp)
jl     12a5 <dump_iovec+0x135>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    $0x100,%ecx
call   1030 <dump_n_bytes_from_mem@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x8,%rsi
mov    -0x20(%rbp),%rdx
call   1090 <dump_unsigned_int_from_mem@plt>
mov    %rax,%rcx
movslq -0x34(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x34(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x2,%esi
lea    -0x34(%rbp),%rdx
call   1060 <copy_params@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x2,%esi
call   1040 <free_dumped_params@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_iovec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
movl   $0x0,0x4(%rsp)
test   %edi,%edi
js     11c4 <dump_iovec+0x54>
mov    %rcx,%r13
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x4(%rbx),%r12d
mov    $0x10,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %ebx,%edi
mov    %r13,%rsi
call   1070 <read_ptr_from_mem@plt>
mov    %eax,%edi
mov    %r15,%rsi
test   %ebp,%ebp
je     11d0 <dump_iovec+0x60>
mov    %r13,%rdx
mov    $0x100,%ecx
call   1030 <dump_n_bytes_from_mem@plt>
jmp    11d5 <dump_iovec+0x65>
xor    %edi,%edi
call   1080 <malloc@plt>
mov    %rax,(%r14)
jmp    1212 <dump_iovec+0xa2>
call   1050 <dump_ptr@plt>
mov    %rax,%rbx
mov    %r15,%rsi
add    $0x8,%rsi
mov    %r12d,%edi
mov    %r13,%rdx
call   1090 <dump_unsigned_int_from_mem@plt>
add    %ebx,%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
mov    $0x2,%esi
call   1060 <copy_params@plt>
mov    %rax,(%r14)
mov    %r15,%rdi
mov    $0x2,%esi
call   1040 <free_dumped_params@plt>
mov    0x4(%rsp),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.target = type { i32 }
define i32 @dump_iovec(i32 %0, i32 %1, i8** %2, %struct.target* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8**
%9 = alloca %struct.target*
%10 = alloca i8**
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8** %2, i8*** %8
store %struct.target* %3, %struct.target** %9
%14 = load i32, i32* %7
store i32 %14, i32* %11
%15 = load i32, i32* %7
%16 = add i32 %15, 4
store i32 %16, i32* %12
store i32 0, i32* %13
%17 = load i32, i32* %6
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %4
%20 = call i8* @malloc(i32 0)
%21 = load i8**, i8*** %8
store i8* %20, i8** %21
%22 = load i32, i32* %13
store i32 %22, i32* %5
br label %84
23: ; preds = %4
%24 = call i8* @malloc(i32 16)
%25 = bitcast i8* %24 to i8**
store i8** %25, i8*** %10
%26 = load i32, i32* %11
%27 = load %struct.target*, %struct.target** %9
%28 = call i32 @read_ptr_from_mem(i32 %26, %struct.target* %27)
store i32 %28, i32* %11
%29 = load i32, i32* %6
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %55
31: ; preds = %23
%32 = load i32, i32* %11
%33 = load i8**, i8*** %10
%34 = getelementptr inbounds i8*, i8** %33, i64 0
%35 = call i64 @dump_ptr(i32 %32, i8** %34)
%36 = load i32, i32* %13
%37 = sext i32 %36 to i64
%38 = add nsw i64 %37, %35
%39 = trunc i64 %38 to i32
store i32 %39, i32* %13
%40 = load i32, i32* %12
%41 = load i8**, i8*** %10
%42 = getelementptr inbounds i8*, i8** %41, i64 1
%43 = load %struct.target*, %struct.target** %9
%44 = call i64 @dump_unsigned_int_from_mem(i32 %40, i8** %42, %struct.target* %43)
%45 = load i32, i32* %13
%46 = sext i32 %45 to i64
%47 = add nsw i64 %46, %44
%48 = trunc i64 %47 to i32
store i32 %48, i32* %13
%49 = load i8**, i8*** %10
%50 = call i8* @copy_params(i8** %49, i32 2, i32* %13)
%51 = load i8**, i8*** %8
store i8* %50, i8** %51
%52 = load i8**, i8*** %10
%53 = call i32 @free_dumped_params(i8** %52, i32 2)
%54 = load i32, i32* %13
store i32 %54, i32* %5
br label %84
55: ; preds = %23
%56 = load i32, i32* %6
%57 = icmp sge i32 %56, 1
br i1 %57, label %58, label %77
58: ; preds = %55
%59 = load i32, i32* %11
%60 = load i8**, i8*** %10
%61 = getelementptr inbounds i8*, i8** %60, i64 0
%62 = load %struct.target*, %struct.target** %9
%63 = call i64 @dump_n_bytes_from_mem(i32 %59, i8** %61, %struct.target* %62, i32 256)
%64 = load i32, i32* %13
%65 = sext i32 %64 to i64
%66 = add nsw i64 %65, %63
%67 = trunc i64 %66 to i32
store i32 %67, i32* %13
%68 = load i32, i32* %12
%69 = load i8**, i8*** %10
%70 = getelementptr inbounds i8*, i8** %69, i64 1
%71 = load %struct.target*, %struct.target** %9
%72 = call i64 @dump_unsigned_int_from_mem(i32 %68, i8** %70, %struct.target* %71)
%73 = load i32, i32* %13
%74 = sext i32 %73 to i64
%75 = add nsw i64 %74, %72
%76 = trunc i64 %75 to i32
store i32 %76, i32* %13
br label %77
77: ; preds = %58, %55
%78 = load i8**, i8*** %10
%79 = call i8* @copy_params(i8** %78, i32 2, i32* %13)
%80 = load i8**, i8*** %8
store i8* %79, i8** %80
%81 = load i8**, i8*** %10
%82 = call i32 @free_dumped_params(i8** %81, i32 2)
%83 = load i32, i32* %13
store i32 %83, i32* %5
br label %84
84: ; preds = %77, %31, %19
%85 = load i32, i32* %5
ret i32 %85
}

LLVM IR (-O2):
%struct.target = type { i32 }
define i32 @dump_iovec(i32 %0, i32 %1, i8** nocapture writeonly %2, %struct.target* %3) {
%5 = alloca i32
%6 = add i32 %1, 4
%7 = bitcast i32* %5 to i8*
store i32 0, i32* %5
%8 = icmp slt i32 %0, 0
br i1 %8, label %9, label %11
9: ; preds = %4
%10 = tail call i8* @malloc(i32 0)
store i8* %10, i8** %2
br label %34
11: ; preds = %4
%12 = tail call dereferenceable_or_null(16) i8* @malloc(i32 16)
%13 = bitcast i8* %12 to i8**
%14 = tail call i32 @read_ptr_from_mem(i32 %1, %struct.target* %3)
%15 = icmp eq i32 %0, 0
br i1 %15, label %16, label %25
16: ; preds = %11
%17 = tail call i64 @dump_ptr(i32 %14, i8** %13)
%18 = trunc i64 %17 to i32
%19 = getelementptr inbounds i8*, i8** %13, i64 1
%20 = tail call i64 @dump_unsigned_int_from_mem(i32 %6, i8** %19, %struct.target* %3)
%21 = trunc i64 %20 to i32
%22 = add i32 %18, %21
store i32 %22, i32* %5
%23 = call i8* @copy_params(i8** %13, i32 2, i32* %5)
store i8* %23, i8** %2
%24 = call i32 @free_dumped_params(i8** %13, i32 2)
br label %34
25: ; preds = %11
%26 = tail call i64 @dump_n_bytes_from_mem(i32 %14, i8** %13, %struct.target* %3, i32 256)
%27 = trunc i64 %26 to i32
%28 = getelementptr inbounds i8*, i8** %13, i64 1
%29 = tail call i64 @dump_unsigned_int_from_mem(i32 %6, i8** %28, %struct.target* %3)
%30 = trunc i64 %29 to i32
%31 = add i32 %27, %30
store i32 %31, i32* %5
%32 = call i8* @copy_params(i8** %13, i32 2, i32* %5)
store i8* %32, i8** %2
%33 = call i32 @free_dumped_params(i8** %13, i32 2)
br label %34
34: ; preds = %25, %16, %9
%35 = load i32, i32* %5
ret i32 %35
}