#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 darray {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ bfree(void *);
void *bmalloc(size_t const);
void *darray_item(size_t const, struct darray *, size_t const);
int /*<<< orphan*/ memcpy(void *, void *, size_t const);
int /*<<< orphan*/ memmove(void *, void *, size_t const);
void darray_move_item(const size_t element_size, struct darray *dst,
                      const size_t from, const size_t to) {
  void *temp, *p_from, *p_to;
  if (from == to)
    return;
  temp = bmalloc(element_size);
  p_from = darray_item(element_size, dst, from);
  p_to = darray_item(element_size, dst, to);
  memcpy(temp, p_from, element_size);
  if (to < from)
    memmove(darray_item(element_size, dst, to + 1), p_to,
            element_size * (from - to));
  else
    memcpy(p_from, darray_item(element_size, dst, from + 1),
           element_size * (to - from));
  memcpy(p_to, temp, element_size);
  bfree(temp);
}


x86 assembly (clang, -O0):
<darray_move_item>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    117b <darray_move_item+0x2b>
jmp    125a <darray_move_item+0x10a>
mov    -0x8(%rbp),%rdi
call   1030 <bmalloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1060 <darray_item@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1060 <darray_item@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jae    1207 <darray_move_item+0xb7>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
add    $0x1,%rdx
call   1060 <darray_item@plt>
mov    %rax,%rdi
mov    -0x38(%rbp),%rsi
mov    -0x8(%rbp),%rdx
mov    -0x18(%rbp),%rax
sub    -0x20(%rbp),%rax
imul   %rax,%rdx
call   1070 <memmove@plt>
jmp    1240 <darray_move_item+0xf0>
mov    -0x30(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
add    $0x1,%rdx
call   1060 <darray_item@plt>
mov    -0x40(%rbp),%rdi
mov    %rax,%rsi
mov    -0x8(%rbp),%rdx
mov    -0x20(%rbp),%rax
sub    -0x18(%rbp),%rax
imul   %rax,%rdx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x8(%rbp),%rdx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1050 <bfree@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<darray_move_item>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%rbx
sub    %rcx,%rbx
jne    1175 <darray_move_item+0x25>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %rsi,%r13
mov    %rdi,%r12
call   1030 <bmalloc@plt>
mov    %rax,%r14
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbp,%rdx
call   1060 <darray_item@plt>
mov    %r13,%rsi
mov    %rax,%r13
mov    %r12,%rdi
mov    %rsi,0x8(%rsp)
mov    %r15,%rdx
call   1060 <darray_item@plt>
mov    %rax,0x10(%rsp)
mov    %r14,0x20(%rsp)
mov    %r14,%rdi
mov    %r13,0x18(%rsp)
mov    %r13,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %r15,%r14
sub    %rbp,%r14
jae    1202 <darray_move_item+0xb2>
add    $0x1,%r15
mov    %r12,%rdi
mov    0x8(%rsp),%rsi
mov    %r15,%rdx
call   1060 <darray_item@plt>
imul   %r12,%rbx
mov    %rax,%rdi
mov    0x10(%rsp),%rbp
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <memmove@plt>
mov    %rbp,%rdi
jmp    122f <darray_move_item+0xdf>
add    $0x1,%rbp
mov    %r12,%rdi
mov    0x8(%rsp),%rsi
mov    %rbp,%rdx
call   1060 <darray_item@plt>
imul   %r12,%r14
mov    0x18(%rsp),%rdi
mov    %rax,%rsi
mov    %r14,%rdx
call   1040 <memcpy@plt>
mov    0x10(%rsp),%rdi
mov    0x20(%rsp),%rbx
mov    %rbx,%rsi
mov    %r12,%rdx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <bfree@plt>

LLVM IR (-O0):
%struct.darray = type { i32 }
define void @darray_move_item(i64 %0, %struct.darray* %1, i64 %2, i64 %3) {
%5 = alloca i64
%6 = alloca %struct.darray*
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
store i64 %0, i64* %5
store %struct.darray* %1, %struct.darray** %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%12 = load i64, i64* %7
%13 = load i64, i64* %8
%14 = icmp eq i64 %12, %13
br i1 %14, label %15, label %16
15: ; preds = %4
br label %67
16: ; preds = %4
%17 = load i64, i64* %5
%18 = call i8* @bmalloc(i64 %17)
store i8* %18, i8** %9
%19 = load i64, i64* %5
%20 = load %struct.darray*, %struct.darray** %6
%21 = load i64, i64* %7
%22 = call i8* @darray_item(i64 %19, %struct.darray* %20, i64 %21)
store i8* %22, i8** %10
%23 = load i64, i64* %5
%24 = load %struct.darray*, %struct.darray** %6
%25 = load i64, i64* %8
%26 = call i8* @darray_item(i64 %23, %struct.darray* %24, i64 %25)
store i8* %26, i8** %11
%27 = load i8*, i8** %9
%28 = load i8*, i8** %10
%29 = load i64, i64* %5
%30 = call i32 @memcpy(i8* %27, i8* %28, i64 %29)
%31 = load i64, i64* %8
%32 = load i64, i64* %7
%33 = icmp ult i64 %31, %32
br i1 %33, label %34, label %47
34: ; preds = %16
%35 = load i64, i64* %5
%36 = load %struct.darray*, %struct.darray** %6
%37 = load i64, i64* %8
%38 = add i64 %37, 1
%39 = call i8* @darray_item(i64 %35, %struct.darray* %36, i64 %38)
%40 = load i8*, i8** %11
%41 = load i64, i64* %5
%42 = load i64, i64* %7
%43 = load i64, i64* %8
%44 = sub i64 %42, %43
%45 = mul i64 %41, %44
%46 = call i32 @memmove(i8* %39, i8* %40, i64 %45)
br label %60
47: ; preds = %16
%48 = load i8*, i8** %10
%49 = load i64, i64* %5
%50 = load %struct.darray*, %struct.darray** %6
%51 = load i64, i64* %7
%52 = add i64 %51, 1
%53 = call i8* @darray_item(i64 %49, %struct.darray* %50, i64 %52)
%54 = load i64, i64* %5
%55 = load i64, i64* %8
%56 = load i64, i64* %7
%57 = sub i64 %55, %56
%58 = mul i64 %54, %57
%59 = call i32 @memcpy(i8* %48, i8* %53, i64 %58)
br label %60
60: ; preds = %47, %34
%61 = load i8*, i8** %11
%62 = load i8*, i8** %9
%63 = load i64, i64* %5
%64 = call i32 @memcpy(i8* %61, i8* %62, i64 %63)
%65 = load i8*, i8** %9
%66 = call i32 @bfree(i8* %65)
br label %67
67: ; preds = %60, %15
ret void
}

LLVM IR (-O2):
%struct.darray = type { i32 }
define void @darray_move_item(i64 %0, %struct.darray* %1, i64 %2, i64 %3) {
%5 = icmp eq i64 %2, %3
br i1 %5, label %27, label %6
6: ; preds = %4
%7 = tail call i8* @bmalloc(i64 %0)
%8 = tail call i8* @darray_item(i64 %0, %struct.darray* %1, i64 %2)
%9 = tail call i8* @darray_item(i64 %0, %struct.darray* %1, i64 %3)
%10 = tail call i32 @memcpy(i8* %7, i8* %8, i64 %0)
%11 = icmp ult i64 %3, %2
br i1 %11, label %12, label %18
12: ; preds = %6
%13 = add nuw i64 %3, 1
%14 = tail call i8* @darray_item(i64 %0, %struct.darray* %1, i64 %13)
%15 = sub i64 %2, %3
%16 = mul i64 %15, %0
%17 = tail call i32 @memmove(i8* %14, i8* %9, i64 %16)
br label %24
18: ; preds = %6
%19 = add i64 %2, 1
%20 = tail call i8* @darray_item(i64 %0, %struct.darray* %1, i64 %19)
%21 = sub i64 %3, %2
%22 = mul i64 %21, %0
%23 = tail call i32 @memcpy(i8* %8, i8* %20, i64 %22)
br label %24
24: ; preds = %18, %12
%25 = tail call i32 @memcpy(i8* %9, i8* %7, i64 %0)
%26 = tail call i32 @bfree(i8* %7)
br label %27
27: ; preds = %4, %24
ret void
}