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

/* Variables and functions */
int /*<<< orphan*/ ber_free_elements(struct ber_element *);
int /*<<< orphan*/ ber_link_elements(struct ber_element *,
                                     struct ber_element *);
struct ber_element *ber_unlink_elements(struct ber_element *);
void ber_replace_elements(struct ber_element *prev, struct ber_element *new) {
  struct ber_element *ber, *next;
  ber = ber_unlink_elements(prev);
  next = ber_unlink_elements(ber);
  ber_link_elements(new, next);
  ber_link_elements(prev, new);
  ber_free_elements(ber);
}


x86 assembly (clang, -O0):
<ber_replace_elements>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <ber_unlink_elements@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <ber_unlink_elements@plt>
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <ber_link_elements@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1040 <ber_link_elements@plt>
mov    -0x18(%rbp),%rdi
call   1030 <ber_free_elements@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ber_replace_elements>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1050 <ber_unlink_elements@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <ber_unlink_elements@plt>
mov    %r14,%rdi
mov    %rax,%rsi
call   1040 <ber_link_elements@plt>
mov    %r15,%rdi
mov    %r14,%rsi
call   1040 <ber_link_elements@plt>
mov    %rbx,%rdi
pop    %rbx
pop    %r14
pop    %r15
jmp    1030 <ber_free_elements@plt>

LLVM IR (-O0):
%struct.ber_element = type { i32 }
define void @ber_replace_elements(%struct.ber_element* %0, %struct.ber_element* %1) {
%3 = alloca %struct.ber_element*
%4 = alloca %struct.ber_element*
%5 = alloca %struct.ber_element*
%6 = alloca %struct.ber_element*
store %struct.ber_element* %0, %struct.ber_element** %3
store %struct.ber_element* %1, %struct.ber_element** %4
%7 = load %struct.ber_element*, %struct.ber_element** %3
%8 = call %struct.ber_element* @ber_unlink_elements(%struct.ber_element* %7)
store %struct.ber_element* %8, %struct.ber_element** %5
%9 = load %struct.ber_element*, %struct.ber_element** %5
%10 = call %struct.ber_element* @ber_unlink_elements(%struct.ber_element* %9)
store %struct.ber_element* %10, %struct.ber_element** %6
%11 = load %struct.ber_element*, %struct.ber_element** %4
%12 = load %struct.ber_element*, %struct.ber_element** %6
%13 = call i32 @ber_link_elements(%struct.ber_element* %11, %struct.ber_element* %12)
%14 = load %struct.ber_element*, %struct.ber_element** %3
%15 = load %struct.ber_element*, %struct.ber_element** %4
%16 = call i32 @ber_link_elements(%struct.ber_element* %14, %struct.ber_element* %15)
%17 = load %struct.ber_element*, %struct.ber_element** %5
%18 = call i32 @ber_free_elements(%struct.ber_element* %17)
ret void
}

LLVM IR (-O2):
%struct.ber_element = type { i32 }
define void @ber_replace_elements(%struct.ber_element* %0, %struct.ber_element* %1) {
%3 = tail call %struct.ber_element* @ber_unlink_elements(%struct.ber_element* %0)
%4 = tail call %struct.ber_element* @ber_unlink_elements(%struct.ber_element* %3)
%5 = tail call i32 @ber_link_elements(%struct.ber_element* %1, %struct.ber_element* %4)
%6 = tail call i32 @ber_link_elements(%struct.ber_element* %0, %struct.ber_element* %1)
%7 = tail call i32 @ber_free_elements(%struct.ber_element* %3)
ret void
}