#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_ptr(unsigned int, char **);
int /*<<< orphan*/ free_dumped_params(char **, int);
void *malloc(int);
unsigned int read_ptr_from_mem(unsigned int, struct target *);
int dump_robust_list(int depth, unsigned int addr, char **dumped_params,
                     struct target *target) {
  char **dumped_type_params;
  unsigned int arm_tracing_next = addr;
  int len = 0;
  if (depth < 0) {
    *dumped_params = malloc(0);
    return len;
  }
  dumped_type_params = malloc(1 * (sizeof(char *)));
  arm_tracing_next = read_ptr_from_mem(arm_tracing_next, target);
  if (depth == 0) {
    len += dump_ptr(arm_tracing_next, &dumped_type_params[0]);
    *dumped_params = copy_params(dumped_type_params, 1, &len);
    free_dumped_params(dumped_type_params, 1);
    return len;
  }
  if (depth >= 1) {
    len += dump_robust_list(depth - 1, arm_tracing_next, &dumped_type_params[0],
                            target);
  }
  *dumped_params = copy_params(dumped_type_params, 1, &len);
  free_dumped_params(dumped_type_params, 1);
  return len;
}


x86 assembly (clang, -O0):
<dump_robust_list>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%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)
movl   $0x0,-0x30(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11a9 <dump_robust_list+0x49>
xor    %edi,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1274 <dump_robust_list+0x114>
mov    $0x8,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1060 <read_ptr_from_mem@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    121e <dump_robust_list+0xbe>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1040 <dump_ptr@plt>
mov    %rax,%rcx
movslq -0x30(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
lea    -0x30(%rbp),%rdx
call   1050 <copy_params@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1030 <free_dumped_params@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1274 <dump_robust_list+0x114>
cmpl   $0x1,-0x8(%rbp)
jl     1244 <dump_robust_list+0xe4>
mov    -0x8(%rbp),%edi
sub    $0x1,%edi
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <dump_robust_list@plt>
add    -0x30(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
lea    -0x30(%rbp),%rdx
call   1050 <copy_params@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x1,%esi
call   1030 <free_dumped_params@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_robust_list>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdx,%r14
movl   $0x0,0xc(%rsp)
test   %edi,%edi
js     11b5 <dump_robust_list+0x55>
mov    %rcx,%r12
mov    %esi,%ebx
mov    %edi,%ebp
mov    $0x8,%edi
call   1080 <malloc@plt>
mov    %rax,%r15
mov    %ebx,%edi
mov    %r12,%rsi
call   1060 <read_ptr_from_mem@plt>
test   %ebp,%ebp
je     11c1 <dump_robust_list+0x61>
add    $0xffffffff,%ebp
mov    %ebp,%edi
mov    %eax,%esi
mov    %r15,%rdx
mov    %r12,%rcx
call   1070 <dump_robust_list@plt>
mov    %eax,0xc(%rsp)
jmp    11cf <dump_robust_list+0x6f>
xor    %edi,%edi
call   1080 <malloc@plt>
mov    %rax,(%r14)
jmp    11f1 <dump_robust_list+0x91>
mov    %eax,%edi
mov    %r15,%rsi
call   1040 <dump_ptr@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rdx
mov    %r15,%rdi
mov    $0x1,%esi
call   1050 <copy_params@plt>
mov    %rax,(%r14)
mov    %r15,%rdi
mov    $0x1,%esi
call   1030 <free_dumped_params@plt>
mov    0xc(%rsp),%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.target = type { i32 }
define i32 @dump_robust_list(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
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8** %2, i8*** %8
store %struct.target* %3, %struct.target** %9
%13 = load i32, i32* %7
store i32 %13, i32* %11
store i32 0, i32* %12
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %4
%17 = call i8* @malloc(i32 0)
%18 = load i8**, i8*** %8
store i8* %17, i8** %18
%19 = load i32, i32* %12
store i32 %19, i32* %5
br label %63
20: ; preds = %4
%21 = call i8* @malloc(i32 8)
%22 = bitcast i8* %21 to i8**
store i8** %22, i8*** %10
%23 = load i32, i32* %11
%24 = load %struct.target*, %struct.target** %9
%25 = call i32 @read_ptr_from_mem(i32 %23, %struct.target* %24)
store i32 %25, i32* %11
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %43
28: ; preds = %20
%29 = load i32, i32* %11
%30 = load i8**, i8*** %10
%31 = getelementptr inbounds i8*, i8** %30, i64 0
%32 = call i64 @dump_ptr(i32 %29, i8** %31)
%33 = load i32, i32* %12
%34 = sext i32 %33 to i64
%35 = add nsw i64 %34, %32
%36 = trunc i64 %35 to i32
store i32 %36, i32* %12
%37 = load i8**, i8*** %10
%38 = call i8* @copy_params(i8** %37, i32 1, i32* %12)
%39 = load i8**, i8*** %8
store i8* %38, i8** %39
%40 = load i8**, i8*** %10
%41 = call i32 @free_dumped_params(i8** %40, i32 1)
%42 = load i32, i32* %12
store i32 %42, i32* %5
br label %63
43: ; preds = %20
%44 = load i32, i32* %6
%45 = icmp sge i32 %44, 1
br i1 %45, label %46, label %56
46: ; preds = %43
%47 = load i32, i32* %6
%48 = sub nsw i32 %47, 1
%49 = load i32, i32* %11
%50 = load i8**, i8*** %10
%51 = getelementptr inbounds i8*, i8** %50, i64 0
%52 = load %struct.target*, %struct.target** %9
%53 = call i32 @dump_robust_list(i32 %48, i32 %49, i8** %51, %struct.target* %52)
%54 = load i32, i32* %12
%55 = add nsw i32 %54, %53
store i32 %55, i32* %12
br label %56
56: ; preds = %46, %43
%57 = load i8**, i8*** %10
%58 = call i8* @copy_params(i8** %57, i32 1, i32* %12)
%59 = load i8**, i8*** %8
store i8* %58, i8** %59
%60 = load i8**, i8*** %10
%61 = call i32 @free_dumped_params(i8** %60, i32 1)
%62 = load i32, i32* %12
store i32 %62, i32* %5
br label %63
63: ; preds = %56, %28, %16
%64 = load i32, i32* %5
ret i32 %64
}

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