#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 /*<<< orphan*/ uint8_t;
typedef size_t uint32_t;

/* Variables and functions */
scalar_t__ calloc(size_t, int);
size_t read_uint32(int /*<<< orphan*/ *);
uint32_t *read_backward_links(uint8_t *p, uint32_t num) {
  uint32_t i;
  uint32_t *bwd = (uint32_t *)calloc(num, sizeof(uint32_t));
  for (i = 0; i < num; ++i) {
    bwd[i] = read_uint32(p);
    p += sizeof(uint32_t);
  }
  return bwd;
}


x86 assembly (clang, -O0):
<read_backward_links>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    118d <read_backward_links+0x6d>
mov    -0x8(%rbp),%rdi
call   1030 <read_uint32@plt>
mov    %rax,%rdx
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x8(%rbp),%rax
add    $0x20,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    114a <read_backward_links+0x2a>
mov    -0x20(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_backward_links>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r14
mov    %rdi,%r12
mov    %rsi,%rdi
mov    $0x8,%esi
call   1040 <calloc@plt>
mov    %rax,%r15
test   %r14,%r14
je     1169 <read_backward_links+0x49>
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r12,%rdi
call   1030 <read_uint32@plt>
mov    %rax,(%r15,%rbx,8)
add    $0x20,%r12
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1150 <read_backward_links+0x30>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i64* @read_backward_links(i32* %0, i64 %1) {
%3 = alloca i32*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64*
store i32* %0, i32** %3
store i64 %1, i64* %4
%7 = load i64, i64* %4
%8 = call i64 @calloc(i64 %7, i32 8)
%9 = inttoptr i64 %8 to i64*
store i64* %9, i64** %6
store i64 0, i64* %5
br label %10
10: ; preds = %22, %2
%11 = load i64, i64* %5
%12 = load i64, i64* %4
%13 = icmp ult i64 %11, %12
br i1 %13, label %14, label %25
14: ; preds = %10
%15 = load i32*, i32** %3
%16 = call i64 @read_uint32(i32* %15)
%17 = load i64*, i64** %6
%18 = load i64, i64* %5
%19 = getelementptr inbounds i64, i64* %17, i64 %18
store i64 %16, i64* %19
%20 = load i32*, i32** %3
%21 = getelementptr inbounds i32, i32* %20, i64 8
store i32* %21, i32** %3
br label %22
22: ; preds = %14
%23 = load i64, i64* %5
%24 = add i64 %23, 1
store i64 %24, i64* %5
br label %10
25: ; preds = %10
%26 = load i64*, i64** %6
ret i64* %26
}

LLVM IR (-O2):
define i64* @read_backward_links(i32* %0, i64 %1) {
%3 = tail call i64 @calloc(i64 %1, i32 8)
%4 = inttoptr i64 %3 to i64*
%5 = icmp eq i64 %1, 0
br i1 %5, label %14, label %6
6: ; preds = %2, %6
%7 = phi i32* [ %11, %6 ], [ %0, %2 ]
%8 = phi i64 [ %12, %6 ], [ 0, %2 ]
%9 = tail call i64 @read_uint32(i32* %7)
%10 = getelementptr inbounds i64, i64* %4, i64 %8
store i64 %9, i64* %10
%11 = getelementptr inbounds i32, i32* %7, i64 8
%12 = add nuw i64 %8, 1
%13 = icmp eq i64 %12, %1
br i1 %13, label %14, label %6
14: ; preds = %6, %2
ret i64* %4
}