#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*/ assert(void *);
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, void *, size_t);
int /*<<< orphan*/ memmove(char *, char *, size_t);
void swap(void *base, size_t i, size_t j, size_t size) {
  void *tmp = malloc(size);
  assert(tmp);
  memcpy(tmp, (char *)base + i * size, size);
  memmove((char *)base + i * size, (char *)base + j * size, size);
  memcpy((char *)base + j * size, tmp, size);
  free(tmp);
}


x86 assembly (clang, -O0):
<swap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <assert@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    -0x10(%rbp),%rax
imul   -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
imul   -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x8(%rbp),%rsi
mov    -0x18(%rbp),%rax
imul   -0x20(%rbp),%rax
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   1070 <memmove@plt>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%rax
imul   -0x20(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1050 <memcpy@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swap>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r14
mov    %rdx,%r12
mov    %rsi,%rbx
mov    %rdi,%r13
mov    %rcx,%rdi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1040 <assert@plt>
imul   %r14,%rbx
add    %r13,%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
imul   %r14,%r12
add    %r13,%r12
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %r14,%rdx
call   1070 <memmove@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
call   1050 <memcpy@plt>
mov    %r15,%rdi
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
jmp    1030 <free@plt>

LLVM IR (-O0):
define void @swap(i8* %0, i64 %1, i64 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i8*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
store i64 %3, i64* %8
%10 = load i64, i64* %8
%11 = call i8* @malloc(i64 %10)
store i8* %11, i8** %9
%12 = load i8*, i8** %9
%13 = call i32 @assert(i8* %12)
%14 = load i8*, i8** %9
%15 = load i8*, i8** %5
%16 = load i64, i64* %6
%17 = load i64, i64* %8
%18 = mul i64 %16, %17
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = load i64, i64* %8
%21 = call i32 @memcpy(i8* %14, i8* %19, i64 %20)
%22 = load i8*, i8** %5
%23 = load i64, i64* %6
%24 = load i64, i64* %8
%25 = mul i64 %23, %24
%26 = getelementptr inbounds i8, i8* %22, i64 %25
%27 = load i8*, i8** %5
%28 = load i64, i64* %7
%29 = load i64, i64* %8
%30 = mul i64 %28, %29
%31 = getelementptr inbounds i8, i8* %27, i64 %30
%32 = load i64, i64* %8
%33 = call i32 @memmove(i8* %26, i8* %31, i64 %32)
%34 = load i8*, i8** %5
%35 = load i64, i64* %7
%36 = load i64, i64* %8
%37 = mul i64 %35, %36
%38 = getelementptr inbounds i8, i8* %34, i64 %37
%39 = load i8*, i8** %9
%40 = load i64, i64* %8
%41 = call i32 @memcpy(i8* %38, i8* %39, i64 %40)
%42 = load i8*, i8** %9
%43 = call i32 @free(i8* %42)
ret void
}

LLVM IR (-O2):
define void @swap(i8* %0, i64 %1, i64 %2, i64 %3) {
%5 = tail call i8* @malloc(i64 %3)
%6 = tail call i32 @assert(i8* %5)
%7 = mul i64 %3, %1
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = tail call i32 @memcpy(i8* %5, i8* %8, i64 %3)
%10 = mul i64 %3, %2
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = tail call i32 @memmove(i8* %8, i8* %11, i64 %3)
%13 = tail call i32 @memcpy(i8* %11, i8* %5, i64 %3)
%14 = tail call i32 @free(i8* %5)
ret void
}