#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 */
typedef int ssize_t;

/* Variables and functions */
int /*<<< orphan*/ free(void *);
void *malloc(int);
int /*<<< orphan*/ memcpy(void *, void *, int);
int /*<<< orphan*/ memmove(void *, void *, int);
int exch(void *base, ssize_t nmemb, ssize_t size, ssize_t a, ssize_t b) {
  void *ap;
  void *bp;
  void *tp;
  (void)nmemb;
  tp = malloc(size);
  if (tp == NULL) {
    return -1;
  }
  ap = ((char *)base) + (a * size);
  bp = ((char *)base) + (b * size);
  memcpy(tp, ap, size);
  memmove(ap, bp, size);
  memcpy(bp, tp, size);
  free(tp);
  return 0;
}


x86 assembly (clang, -O0):
<exch>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x18(%rbp),%edi
call   1050 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    117c <exch+0x3c>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e6 <exch+0xa6>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
imul   -0x18(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
imul   -0x18(%rbp),%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x30(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1060 <memmove@plt>
mov    -0x30(%rbp),%rdi
mov    -0x38(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x38(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exch>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %r8d,%r12d
mov    %ecx,%ebp
mov    %edx,%ebx
mov    %rdi,%r15
mov    %edx,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11a7 <exch+0x67>
mov    %rax,%r14
imul   %ebx,%ebp
movslq %ebp,%rbp
add    %r15,%rbp
imul   %ebx,%r12d
movslq %r12d,%r12
add    %r15,%r12
mov    %rax,%rdi
mov    %rbp,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %rbp,%rdi
mov    %r12,%rsi
mov    %ebx,%edx
call   1060 <memmove@plt>
mov    %r12,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11ac <exch+0x6c>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @exch(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca i8*
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%15 = load i32, i32* %8
%16 = load i32, i32* %9
%17 = call i8* @malloc(i32 %16)
store i8* %17, i8** %14
%18 = load i8*, i8** %14
%19 = icmp eq i8* %18, null
br i1 %19, label %20, label %21
20: ; preds = %5
store i32 -1, i32* %6
br label %48
21: ; preds = %5
%22 = load i8*, i8** %7
%23 = load i32, i32* %10
%24 = load i32, i32* %9
%25 = mul nsw i32 %23, %24
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %22, i64 %26
store i8* %27, i8** %12
%28 = load i8*, i8** %7
%29 = load i32, i32* %11
%30 = load i32, i32* %9
%31 = mul nsw i32 %29, %30
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds i8, i8* %28, i64 %32
store i8* %33, i8** %13
%34 = load i8*, i8** %14
%35 = load i8*, i8** %12
%36 = load i32, i32* %9
%37 = call i32 @memcpy(i8* %34, i8* %35, i32 %36)
%38 = load i8*, i8** %12
%39 = load i8*, i8** %13
%40 = load i32, i32* %9
%41 = call i32 @memmove(i8* %38, i8* %39, i32 %40)
%42 = load i8*, i8** %13
%43 = load i8*, i8** %14
%44 = load i32, i32* %9
%45 = call i32 @memcpy(i8* %42, i8* %43, i32 %44)
%46 = load i8*, i8** %14
%47 = call i32 @free(i8* %46)
store i32 0, i32* %6
br label %48
48: ; preds = %21, %20
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
define i32 @exch(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = tail call i8* @malloc(i32 %2)
%7 = icmp eq i8* %6, null
br i1 %7, label %19, label %8
8: ; preds = %5
%9 = mul nsw i32 %3, %2
%10 = sext i32 %9 to i64
%11 = getelementptr inbounds i8, i8* %0, i64 %10
%12 = mul nsw i32 %4, %2
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %0, i64 %13
%15 = tail call i32 @memcpy(i8* %6, i8* %11, i32 %2)
%16 = tail call i32 @memmove(i8* %11, i8* %14, i32 %2)
%17 = tail call i32 @memcpy(i8* %14, i8* %6, i32 %2)
%18 = tail call i32 @free(i8* %6)
br label %19
19: ; preds = %5, %8
%20 = phi i32 [ 0, %8 ], [ -1, %5 ]
ret i32 %20
}