#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 */

/* Variables and functions */
int /*<<< orphan*/ memmove(double *, double *, size_t);
int merge_cycles(double *buffer, size_t *count, size_t size,
                 int (*compare)(void const *, void const *)) {
  double *head, *tail;
  size_t c, n, elem;
  elem = size / sizeof(double);
  c = *count;
  if (c > 1) {
    head = buffer;
    for (n = c - 1; n > 0; --n) {
      tail = head + elem;
      if (compare(head, tail) == 0) {
        head[2] += tail[2];
        --c;
        memmove(tail, tail + elem, (n - 1) * size);
      } else {
        head += elem;
      }
    }
    *count = c;
  }
  return 0;
}


x86 assembly (clang, -O0):
<merge_cycles>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x18(%rbp),%rax
shr    $0x3,%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x1,-0x38(%rbp)
jbe    1211 <merge_cycles+0x101>
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x38(%rbp),%rax
sub    $0x1,%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
jbe    1206 <merge_cycles+0xf6>
mov    -0x28(%rbp),%rax
mov    -0x48(%rbp),%rcx
shl    $0x3,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x20(%rbp),%rax
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   *%rax
cmp    $0x0,%eax
jne    11e0 <merge_cycles+0xd0>
mov    -0x30(%rbp),%rax
movsd  0x10(%rax),%xmm0
mov    -0x28(%rbp),%rax
addsd  0x10(%rax),%xmm0
movsd  %xmm0,0x10(%rax)
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x48(%rbp),%rax
shl    $0x3,%rax
add    %rax,%rsi
mov    -0x40(%rbp),%rdx
sub    $0x1,%rdx
imul   -0x18(%rbp),%rdx
call   1030 <memmove@plt>
jmp    11f0 <merge_cycles+0xe0>
mov    -0x48(%rbp),%rax
shl    $0x3,%rax
add    -0x28(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11f5 <merge_cycles+0xe5>
mov    -0x40(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x40(%rbp)
jmp    115e <merge_cycles+0x4e>
mov    -0x38(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<merge_cycles>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rcx,0x10(%rsp)
mov    (%rsi),%rax
cmp    $0x2,%rax
jb     11c1 <merge_cycles+0xb1>
mov    %rdx,%r14
mov    %rdi,%r13
mov    %rsi,0x8(%rsp)
mov    %rdx,%r12
shr    $0x3,%r12
lea    -0x1(%rax),%r15
mov    %rax,(%rsp)
lea    -0x2(%rax),%rbx
imul   %rdx,%rbx
jmp    1197 <merge_cycles+0x87>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsd  0x10(%r13,%r12,8),%xmm0
addsd  0x10(%r13),%xmm0
movsd  %xmm0,0x10(%r13)
addq   $0xffffffffffffffff,(%rsp)
lea    0x0(,%r12,8),%rsi
add    %rbp,%rsi
mov    %rbp,%rdi
mov    %rbx,%rdx
call   1030 <memmove@plt>
sub    %r14,%rbx
add    $0xffffffffffffffff,%r15
je     11b5 <merge_cycles+0xa5>
lea    0x0(,%r12,8),%rbp
add    %r13,%rbp
mov    %r13,%rdi
mov    %rbp,%rsi
call   *0x10(%rsp)
test   %eax,%eax
je     1160 <merge_cycles+0x50>
mov    %rbp,%r13
jmp    118e <merge_cycles+0x7e>
mov    0x8(%rsp),%rax
mov    (%rsp),%rcx
mov    %rcx,(%rax)
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @merge_cycles(double* %0, i64* nocapture %1, i64 %2, i32 (i8*, i8*)* nocapture %3) {
%5 = lshr i64 %2, 3
%6 = load i64, i64* %1
%7 = icmp ugt i64 %6, 1
br i1 %7, label %8, label %37
8: ; preds = %4
%9 = add i64 %6, -1
br label %10
10: ; preds = %8, %31
%11 = phi i64 [ %34, %31 ], [ %9, %8 ]
%12 = phi double* [ %33, %31 ], [ %0, %8 ]
%13 = phi i64 [ %11, %31 ], [ %6, %8 ]
%14 = phi i64 [ %32, %31 ], [ %6, %8 ]
%15 = getelementptr inbounds double, double* %12, i64 %5
%16 = bitcast double* %12 to i8*
%17 = bitcast double* %15 to i8*
%18 = tail call i32 %3(i8* %16, i8* %17)
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %31
20: ; preds = %10
%21 = getelementptr inbounds double, double* %15, i64 2
%22 = load double, double* %21
%23 = getelementptr inbounds double, double* %12, i64 2
%24 = load double, double* %23
%25 = fadd double %22, %24
store double %25, double* %23
%26 = add i64 %14, -1
%27 = getelementptr inbounds double, double* %15, i64 %5
%28 = add i64 %13, -2
%29 = mul i64 %28, %2
%30 = tail call i32 @memmove(double* %15, double* %27, i64 %29)
br label %31
31: ; preds = %10, %20
%32 = phi i64 [ %26, %20 ], [ %14, %10 ]
%33 = phi double* [ %12, %20 ], [ %15, %10 ]
%34 = add i64 %11, -1
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %10
36: ; preds = %31
store i64 %32, i64* %1
br label %37
37: ; preds = %36, %4
ret i32 0
}