#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_long_unsigned_int_from_mem(unsigned int, char **,
                                           struct target *);
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_kexec_segment(int depth, unsigned int addr, char **dumped_params,
                       struct target *target) {
  char **dumped_type_params;
  unsigned int arm_tracing_buf = addr;
  unsigned int arm_tracing_bufsz = addr + 4;
  unsigned int arm_tracing_mem = addr + 8;
  unsigned int arm_tracing_memsz = addr + 12;
  int len = 0;
  if (depth < 0) {
    *dumped_params = malloc(0);
    return len;
  }
  dumped_type_params = malloc(4 * (sizeof(char *)));
  arm_tracing_buf = read_ptr_from_mem(arm_tracing_buf, target);
  if (depth == 0) {
    len += dump_ptr(arm_tracing_buf, &dumped_type_params[0]);
    len += dump_unsigned_int_from_mem(arm_tracing_bufsz, &dumped_type_params[1],
                                      target);
    len += dump_long_unsigned_int_from_mem(arm_tracing_mem,
                                           &dumped_type_params[2], target);
    len += dump_unsigned_int_from_mem(arm_tracing_memsz, &dumped_type_params[3],
                                      target);
    *dumped_params = copy_params(dumped_type_params, 4, &len);
    free_dumped_params(dumped_type_params, 4);
    return len;
  }
  if (depth >= 1) {
    len += dump_n_bytes_from_mem(arm_tracing_buf, &dumped_type_params[0],
                                 target, 256);
    len += dump_unsigned_int_from_mem(arm_tracing_bufsz, &dumped_type_params[1],
                                      target);
    len += dump_long_unsigned_int_from_mem(arm_tracing_mem,
                                           &dumped_type_params[2], target);
    len += dump_unsigned_int_from_mem(arm_tracing_memsz, &dumped_type_params[3],
                                      target);
  }
  *dumped_params = copy_params(dumped_type_params, 4, &len);
  free_dumped_params(dumped_type_params, 4);
  return len;
}


x86 assembly (clang, -O0):
<dump_kexec_segment>:
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)
mov    -0xc(%rbp),%eax
add    $0x8,%eax
mov    %eax,-0x34(%rbp)
mov    -0xc(%rbp),%eax
add    $0xc,%eax
mov    %eax,-0x38(%rbp)
movl   $0x0,-0x3c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11e4 <dump_kexec_segment+0x64>
xor    %edi,%edi
call   1090 <malloc@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    137b <dump_kexec_segment+0x1fb>
mov    $0x20,%edi
call   1090 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x2c(%rbp),%edi
mov    -0x20(%rbp),%rsi
call   1080 <read_ptr_from_mem@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x8(%rbp)
jne    12bc <dump_kexec_segment+0x13c>
mov    -0x2c(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1060 <dump_ptr@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x8,%rsi
mov    -0x20(%rbp),%rdx
call   10a0 <dump_unsigned_int_from_mem@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x10,%rsi
mov    -0x20(%rbp),%rdx
call   1040 <dump_long_unsigned_int_from_mem@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x18,%rsi
mov    -0x20(%rbp),%rdx
call   10a0 <dump_unsigned_int_from_mem@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x4,%esi
lea    -0x3c(%rbp),%rdx
call   1070 <copy_params@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x4,%esi
call   1050 <free_dumped_params@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    137b <dump_kexec_segment+0x1fb>
cmpl   $0x1,-0x8(%rbp)
jl     134b <dump_kexec_segment+0x1cb>
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 -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x30(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x8,%rsi
mov    -0x20(%rbp),%rdx
call   10a0 <dump_unsigned_int_from_mem@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x10,%rsi
mov    -0x20(%rbp),%rdx
call   1040 <dump_long_unsigned_int_from_mem@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x38(%rbp),%edi
mov    -0x28(%rbp),%rsi
add    $0x18,%rsi
mov    -0x20(%rbp),%rdx
call   10a0 <dump_unsigned_int_from_mem@plt>
mov    %rax,%rcx
movslq -0x3c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x28(%rbp),%rdi
mov    $0x4,%esi
lea    -0x3c(%rbp),%rdx
call   1070 <copy_params@plt>
mov    %rax,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x28(%rbp),%rdi
mov    $0x4,%esi
call   1050 <free_dumped_params@plt>
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dump_kexec_segment>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%rbx
movl   $0x0,0x4(%rsp)
test   %edi,%edi
js     1235 <dump_kexec_segment+0xb5>
mov    %rcx,%r13
mov    %esi,%r14d
mov    %edi,%r12d
mov    %rbx,0x10(%rsp)
lea    0x4(%r14),%ebx
lea    0x8(%r14),%eax
mov    %eax,0x8(%rsp)
lea    0xc(%r14),%eax
mov    %eax,0xc(%rsp)
mov    $0x20,%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    %r14d,%edi
mov    %r13,%rsi
call   1080 <read_ptr_from_mem@plt>
mov    %eax,%edi
mov    %r15,%rsi
test   %r12d,%r12d
je     1241 <dump_kexec_segment+0xc1>
mov    %r13,%rdx
mov    $0x100,%ecx
call   1030 <dump_n_bytes_from_mem@plt>
mov    %rax,%r14
lea    0x8(%r15),%rsi
mov    %ebx,%edi
mov    %r13,%rdx
call   10a0 <dump_unsigned_int_from_mem@plt>
mov    %rax,%rbp
add    %r14d,%ebp
lea    0x10(%r15),%rsi
mov    0x8(%rsp),%edi
mov    %r13,%rdx
call   1040 <dump_long_unsigned_int_from_mem@plt>
mov    %rax,%rbx
add    %ebp,%ebx
mov    %r15,%rsi
add    $0x18,%rsi
mov    0xc(%rsp),%edi
mov    %r13,%rdx
call   10a0 <dump_unsigned_int_from_mem@plt>
add    %ebx,%eax
jmp    1289 <dump_kexec_segment+0x109>
xor    %edi,%edi
call   1090 <malloc@plt>
mov    %rax,(%rbx)
jmp    12b4 <dump_kexec_segment+0x134>
call   1060 <dump_ptr@plt>
mov    %rax,%r14
lea    0x8(%r15),%rsi
mov    %ebx,%edi
mov    %r13,%rdx
call   10a0 <dump_unsigned_int_from_mem@plt>
mov    %rax,%rbp
add    %r14d,%ebp
lea    0x10(%r15),%rsi
mov    0x8(%rsp),%edi
mov    %r13,%rdx
call   1040 <dump_long_unsigned_int_from_mem@plt>
mov    %rax,%r14
add    %ebp,%r14d
mov    %r15,%rsi
add    $0x18,%rsi
mov    0xc(%rsp),%edi
mov    %r13,%rdx
call   10a0 <dump_unsigned_int_from_mem@plt>
add    %r14d,%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdx
mov    %r15,%rdi
mov    $0x4,%esi
call   1070 <copy_params@plt>
mov    0x10(%rsp),%rcx
mov    %rax,(%rcx)
mov    %r15,%rdi
mov    $0x4,%esi
call   1050 <free_dumped_params@plt>
mov    0x4(%rsp),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.target = type { i32 }
define i32 @dump_kexec_segment(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
%14 = alloca i32
%15 = alloca i32
store i32 %0, i32* %6
store i32 %1, i32* %7
store i8** %2, i8*** %8
store %struct.target* %3, %struct.target** %9
%16 = load i32, i32* %7
store i32 %16, i32* %11
%17 = load i32, i32* %7
%18 = add i32 %17, 4
store i32 %18, i32* %12
%19 = load i32, i32* %7
%20 = add i32 %19, 8
store i32 %20, i32* %13
%21 = load i32, i32* %7
%22 = add i32 %21, 12
store i32 %22, i32* %14
store i32 0, i32* %15
%23 = load i32, i32* %6
%24 = icmp slt i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %4
%26 = call i8* @malloc(i32 0)
%27 = load i8**, i8*** %8
store i8* %26, i8** %27
%28 = load i32, i32* %15
store i32 %28, i32* %5
br label %126
29: ; preds = %4
%30 = call i8* @malloc(i32 32)
%31 = bitcast i8* %30 to i8**
store i8** %31, i8*** %10
%32 = load i32, i32* %11
%33 = load %struct.target*, %struct.target** %9
%34 = call i32 @read_ptr_from_mem(i32 %32, %struct.target* %33)
store i32 %34, i32* %11
%35 = load i32, i32* %6
%36 = icmp eq i32 %35, 0
br i1 %36, label %37, label %79
37: ; preds = %29
%38 = load i32, i32* %11
%39 = load i8**, i8*** %10
%40 = getelementptr inbounds i8*, i8** %39, i64 0
%41 = call i64 @dump_ptr(i32 %38, i8** %40)
%42 = load i32, i32* %15
%43 = sext i32 %42 to i64
%44 = add nsw i64 %43, %41
%45 = trunc i64 %44 to i32
store i32 %45, i32* %15
%46 = load i32, i32* %12
%47 = load i8**, i8*** %10
%48 = getelementptr inbounds i8*, i8** %47, i64 1
%49 = load %struct.target*, %struct.target** %9
%50 = call i64 @dump_unsigned_int_from_mem(i32 %46, i8** %48, %struct.target* %49)
%51 = load i32, i32* %15
%52 = sext i32 %51 to i64
%53 = add nsw i64 %52, %50
%54 = trunc i64 %53 to i32
store i32 %54, i32* %15
%55 = load i32, i32* %13
%56 = load i8**, i8*** %10
%57 = getelementptr inbounds i8*, i8** %56, i64 2
%58 = load %struct.target*, %struct.target** %9
%59 = call i64 @dump_long_unsigned_int_from_mem(i32 %55, i8** %57, %struct.target* %58)
%60 = load i32, i32* %15
%61 = sext i32 %60 to i64
%62 = add nsw i64 %61, %59
%63 = trunc i64 %62 to i32
store i32 %63, i32* %15
%64 = load i32, i32* %14
%65 = load i8**, i8*** %10
%66 = getelementptr inbounds i8*, i8** %65, i64 3
%67 = load %struct.target*, %struct.target** %9
%68 = call i64 @dump_unsigned_int_from_mem(i32 %64, i8** %66, %struct.target* %67)
%69 = load i32, i32* %15
%70 = sext i32 %69 to i64
%71 = add nsw i64 %70, %68
%72 = trunc i64 %71 to i32
store i32 %72, i32* %15
%73 = load i8**, i8*** %10
%74 = call i8* @copy_params(i8** %73, i32 4, i32* %15)
%75 = load i8**, i8*** %8
store i8* %74, i8** %75
%76 = load i8**, i8*** %10
%77 = call i32 @free_dumped_params(i8** %76, i32 4)
%78 = load i32, i32* %15
store i32 %78, i32* %5
br label %126
79: ; preds = %29
%80 = load i32, i32* %6
%81 = icmp sge i32 %80, 1
br i1 %81, label %82, label %119
82: ; preds = %79
%83 = load i32, i32* %11
%84 = load i8**, i8*** %10
%85 = getelementptr inbounds i8*, i8** %84, i64 0
%86 = load %struct.target*, %struct.target** %9
%87 = call i64 @dump_n_bytes_from_mem(i32 %83, i8** %85, %struct.target* %86, i32 256)
%88 = load i32, i32* %15
%89 = sext i32 %88 to i64
%90 = add nsw i64 %89, %87
%91 = trunc i64 %90 to i32
store i32 %91, i32* %15
%92 = load i32, i32* %12
%93 = load i8**, i8*** %10
%94 = getelementptr inbounds i8*, i8** %93, i64 1
%95 = load %struct.target*, %struct.target** %9
%96 = call i64 @dump_unsigned_int_from_mem(i32 %92, i8** %94, %struct.target* %95)
%97 = load i32, i32* %15
%98 = sext i32 %97 to i64
%99 = add nsw i64 %98, %96
%100 = trunc i64 %99 to i32
store i32 %100, i32* %15
%101 = load i32, i32* %13
%102 = load i8**, i8*** %10
%103 = getelementptr inbounds i8*, i8** %102, i64 2
%104 = load %struct.target*, %struct.target** %9
%105 = call i64 @dump_long_unsigned_int_from_mem(i32 %101, i8** %103, %struct.target* %104)
%106 = load i32, i32* %15
%107 = sext i32 %106 to i64
%108 = add nsw i64 %107, %105
%109 = trunc i64 %108 to i32
store i32 %109, i32* %15
%110 = load i32, i32* %14
%111 = load i8**, i8*** %10
%112 = getelementptr inbounds i8*, i8** %111, i64 3
%113 = load %struct.target*, %struct.target** %9
%114 = call i64 @dump_unsigned_int_from_mem(i32 %110, i8** %112, %struct.target* %113)
%115 = load i32, i32* %15
%116 = sext i32 %115 to i64
%117 = add nsw i64 %116, %114
%118 = trunc i64 %117 to i32
store i32 %118, i32* %15
br label %119
119: ; preds = %82, %79
%120 = load i8**, i8*** %10
%121 = call i8* @copy_params(i8** %120, i32 4, i32* %15)
%122 = load i8**, i8*** %8
store i8* %121, i8** %122
%123 = load i8**, i8*** %10
%124 = call i32 @free_dumped_params(i8** %123, i32 4)
%125 = load i32, i32* %15
store i32 %125, i32* %5
br label %126
126: ; preds = %119, %37, %25
%127 = load i32, i32* %5
ret i32 %127
}

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