#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*/ memcpy(void *, void *, size_t);
void *realloc(void *, size_t);
void swap(void *base, int i, int j, size_t size) {
  static void *temp = NULL;
  static size_t max_size = 0;
  void *elem1, *elem2;
  if (size > max_size) {
    temp = realloc(temp, size);
    max_size = size;
  }
  elem1 = ((char *)base) + (i * size);
  elem2 = ((char *)base) + (j * size);
  memcpy(temp, elem1, size);
  memcpy(elem1, elem2, size);
  memcpy(elem2, temp, size);
}


x86 assembly (clang, -O0):
<swap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
cmp    0x2eff(%rip),%rax
jbe    1169 <swap+0x49>
mov    0x2eea(%rip),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <realloc@plt>
mov    %rax,0x2eda(%rip)
mov    -0x18(%rbp),%rax
mov    %rax,0x2ed7(%rip)
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
imul   -0x18(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    0x2ea0(%rip),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    0x2e77(%rip),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <memcpy@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<swap>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rcx,%r12
mov    %edx,%r15d
mov    %esi,%ebp
mov    %rdi,%r14
mov    0x2efe(%rip),%rdi
cmp    %rcx,0x2eff(%rip)
jae    115c <swap+0x3c>
mov    %r12,%rsi
call   1040 <realloc@plt>
mov    %rax,%rdi
mov    %rax,0x2ee3(%rip)
mov    %r12,0x2ee4(%rip)
movslq %ebp,%rbp
imul   %r12,%rbp
add    %r14,%rbp
movslq %r15d,%rbx
imul   %r12,%rbx
add    %r14,%rbx
mov    %rbp,%rsi
mov    %r12,%rdx
call   1030 <memcpy@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1030 <memcpy@plt>
mov    0x2ea8(%rip),%rsi
mov    %rbx,%rdi
mov    %r12,%rdx
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1030 <memcpy@plt>

LLVM IR (-O0):
@swap.temp = internal global i8* null
@swap.max_size = internal global i64 0
define void @swap(i8* %0, i32 %1, i32 %2, i64 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i64 %3, i64* %8
%11 = load i64, i64* %8
%12 = load i64, i64* @swap.max_size
%13 = icmp ugt i64 %11, %12
br i1 %13, label %14, label %19
14: ; preds = %4
%15 = load i8*, i8** @swap.temp
%16 = load i64, i64* %8
%17 = call i8* @realloc(i8* %15, i64 %16)
store i8* %17, i8** @swap.temp
%18 = load i64, i64* %8
store i64 %18, i64* @swap.max_size
br label %19
19: ; preds = %14, %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = load i64, i64* %8
%24 = mul i64 %22, %23
%25 = getelementptr inbounds i8, i8* %20, i64 %24
store i8* %25, i8** %9
%26 = load i8*, i8** %5
%27 = load i32, i32* %7
%28 = sext i32 %27 to i64
%29 = load i64, i64* %8
%30 = mul i64 %28, %29
%31 = getelementptr inbounds i8, i8* %26, i64 %30
store i8* %31, i8** %10
%32 = load i8*, i8** @swap.temp
%33 = load i8*, i8** %9
%34 = load i64, i64* %8
%35 = call i32 @memcpy(i8* %32, i8* %33, i64 %34)
%36 = load i8*, i8** %9
%37 = load i8*, i8** %10
%38 = load i64, i64* %8
%39 = call i32 @memcpy(i8* %36, i8* %37, i64 %38)
%40 = load i8*, i8** %10
%41 = load i8*, i8** @swap.temp
%42 = load i64, i64* %8
%43 = call i32 @memcpy(i8* %40, i8* %41, i64 %42)
ret void
}

LLVM IR (-O2):
@swap.temp = internal unnamed_addr global i8* null
@swap.max_size = internal unnamed_addr global i64 0
define void @swap(i8* %0, i32 %1, i32 %2, i64 %3) {
%5 = load i64, i64* @swap.max_size
%6 = icmp ult i64 %5, %3
%7 = load i8*, i8** @swap.temp
br i1 %6, label %8, label %10
8: ; preds = %4
%9 = tail call i8* @realloc(i8* %7, i64 %3)
store i8* %9, i8** @swap.temp
store i64 %3, i64* @swap.max_size
br label %10
10: ; preds = %4, %8
%11 = phi i8* [ %9, %8 ], [ %7, %4 ]
%12 = sext i32 %1 to i64
%13 = mul i64 %12, %3
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = sext i32 %2 to i64
%16 = mul i64 %15, %3
%17 = getelementptr inbounds i8, i8* %0, i64 %16
%18 = tail call i32 @memcpy(i8* %11, i8* %14, i64 %3)
%19 = tail call i32 @memcpy(i8* %14, i8* %17, i64 %3)
%20 = load i8*, i8** @swap.temp
%21 = tail call i32 @memcpy(i8* %17, i8* %20, i64 %3)
ret void
}