#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;
  }
  a -= 1;
  b -= 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    11f8 <exch+0xb8>
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x20(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x20(%rbp)
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,%ebp
mov    %ecx,%ebx
mov    %edx,%r12d
mov    %rdi,%r15
mov    %edx,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11b2 <exch+0x72>
mov    %rax,%r14
add    $0xffffffff,%ebx
add    $0xffffffff,%ebp
imul   %r12d,%ebx
movslq %ebx,%rbx
add    %r15,%rbx
imul   %r12d,%ebp
movslq %ebp,%rbp
add    %r15,%rbp
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    %r12d,%edx
call   1060 <memmove@plt>
mov    %rbp,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1040 <memcpy@plt>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    11b7 <exch+0x77>
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 %52
21: ; preds = %5
%22 = load i32, i32* %10
%23 = sub nsw i32 %22, 1
store i32 %23, i32* %10
%24 = load i32, i32* %11
%25 = sub nsw i32 %24, 1
store i32 %25, i32* %11
%26 = load i8*, i8** %7
%27 = load i32, i32* %10
%28 = load i32, i32* %9
%29 = mul nsw i32 %27, %28
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %26, i64 %30
store i8* %31, i8** %12
%32 = load i8*, i8** %7
%33 = load i32, i32* %11
%34 = load i32, i32* %9
%35 = mul nsw i32 %33, %34
%36 = sext i32 %35 to i64
%37 = getelementptr inbounds i8, i8* %32, i64 %36
store i8* %37, i8** %13
%38 = load i8*, i8** %14
%39 = load i8*, i8** %12
%40 = load i32, i32* %9
%41 = call i32 @memcpy(i8* %38, i8* %39, i32 %40)
%42 = load i8*, i8** %12
%43 = load i8*, i8** %13
%44 = load i32, i32* %9
%45 = call i32 @memmove(i8* %42, i8* %43, i32 %44)
%46 = load i8*, i8** %13
%47 = load i8*, i8** %14
%48 = load i32, i32* %9
%49 = call i32 @memcpy(i8* %46, i8* %47, i32 %48)
%50 = load i8*, i8** %14
%51 = call i32 @free(i8* %50)
store i32 0, i32* %6
br label %52
52: ; preds = %21, %20
%53 = load i32, i32* %6
ret i32 %53
}

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 %21, label %8
8: ; preds = %5
%9 = add nsw i32 %3, -1
%10 = add nsw i32 %4, -1
%11 = mul nsw i32 %9, %2
%12 = sext i32 %11 to i64
%13 = getelementptr inbounds i8, i8* %0, i64 %12
%14 = mul nsw i32 %10, %2
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = tail call i32 @memcpy(i8* %6, i8* %13, i32 %2)
%18 = tail call i32 @memmove(i8* %13, i8* %16, i32 %2)
%19 = tail call i32 @memcpy(i8* %16, i8* %6, i32 %2)
%20 = tail call i32 @free(i8* %6)
br label %21
21: ; preds = %5, %8
%22 = phi i32 [ 0, %8 ], [ -1, %5 ]
ret i32 %22
}