#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*/ O_RDWR;
int /*<<< orphan*/ close(int);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ read(int, char *, unsigned long);
int /*<<< orphan*/ write(int, char *, unsigned long);
unsigned long memCpy_DMA(char *bufferIn, char *bufferOut, unsigned long elems,
                         size_t size, int dmaToUse) {
  int fd[4];
  fd[0] = open("/dev/axi_dma_0", O_RDWR);
  fd[1] = open("/dev/axi_dma_1", O_RDWR);
  fd[2] = open("/dev/axi_dma_2", O_RDWR);
  fd[3] = open("/dev/axi_dma_3", O_RDWR);
  unsigned long byteMoved = 0;
  unsigned long byteToMove = 0;
  int i;
  while (byteMoved != size * elems) {
    byteToMove =
        size * elems - byteMoved > 4000 ? 4000 : size * elems - byteMoved;
    for (i = 0; i < dmaToUse; i++) {
      write(fd[i], &bufferIn[byteMoved], byteToMove);
    }
    for (i = 0; i < dmaToUse; i++)
      read(fd[i], &bufferOut[byteMoved], byteToMove);
    byteMoved += byteToMove;
  }
  close(fd[0]);
  close(fd[1]);
  close(fd[2]);
  close(fd[3]);
  return elems * size * dmaToUse;
}


x86 assembly (clang, -O0):
<memCpy_DMA>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
mov    0x2e85(%rip),%rax
mov    (%rax),%esi
lea    0xe94(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x40(%rbp)
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
lea    0xe8b(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2e55(%rip),%rax
mov    (%rax),%esi
lea    0xe82(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x38(%rbp)
mov    0x2e3d(%rip),%rax
mov    (%rax),%esi
lea    0xe79(%rip),%rdi
call   1060 <open@plt>
mov    %eax,-0x34(%rbp)
movq   $0x0,-0x48(%rbp)
movq   $0x0,-0x50(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x20(%rbp),%rcx
imul   -0x18(%rbp),%rcx
cmp    %rcx,%rax
je     12a7 <memCpy_DMA+0x167>
mov    -0x20(%rbp),%rax
imul   -0x18(%rbp),%rax
sub    -0x48(%rbp),%rax
cmp    $0xfa0,%rax
jbe    1209 <memCpy_DMA+0xc9>
mov    $0xfa0,%eax
mov    %rax,-0x60(%rbp)
jmp    121a <memCpy_DMA+0xda>
mov    -0x20(%rbp),%rax
imul   -0x18(%rbp),%rax
sub    -0x48(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    125c <memCpy_DMA+0x11c>
movslq -0x54(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edi
mov    -0x8(%rbp),%rsi
add    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1030 <write@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1229 <memCpy_DMA+0xe9>
movl   $0x0,-0x54(%rbp)
mov    -0x54(%rbp),%eax
cmp    -0x24(%rbp),%eax
jge    1296 <memCpy_DMA+0x156>
movslq -0x54(%rbp),%rax
mov    -0x40(%rbp,%rax,4),%edi
mov    -0x10(%rbp),%rsi
add    -0x48(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1050 <read@plt>
mov    -0x54(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x54(%rbp)
jmp    1263 <memCpy_DMA+0x123>
mov    -0x50(%rbp),%rax
add    -0x48(%rbp),%rax
mov    %rax,-0x48(%rbp)
jmp    11cc <memCpy_DMA+0x8c>
mov    -0x40(%rbp),%edi
call   1040 <close@plt>
mov    -0x3c(%rbp),%edi
call   1040 <close@plt>
mov    -0x38(%rbp),%edi
call   1040 <close@plt>
mov    -0x34(%rbp),%edi
call   1040 <close@plt>
mov    -0x18(%rbp),%rax
imul   -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
imul   %rcx,%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<memCpy_DMA>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r8d,0xc(%rsp)
mov    %rcx,%rbx
mov    %rdx,%r12
mov    %rsi,0x38(%rsp)
mov    %rdi,0x40(%rsp)
mov    0x2e7e(%rip),%rbp
mov    0x0(%rbp),%esi
lea    0xe8c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,0x1c(%rsp)
mov    %eax,0x20(%rsp)
mov    0x0(%rbp),%esi
lea    0xe84(%rip),%rdi
call   1060 <open@plt>
mov    %eax,0x18(%rsp)
mov    %eax,0x24(%rsp)
mov    0x0(%rbp),%esi
lea    0xe7c(%rip),%rdi
call   1060 <open@plt>
mov    %eax,0x14(%rsp)
mov    %eax,0x28(%rsp)
mov    0x0(%rbp),%esi
lea    0xe74(%rip),%rdi
call   1060 <open@plt>
mov    %eax,0x10(%rsp)
mov    %eax,0x2c(%rsp)
imul   %rbx,%r12
test   %r12,%r12
je     125a <memCpy_DMA+0x11a>
mov    0xc(%rsp),%r15d
xor    %r14d,%r14d
jmp    11e8 <memCpy_DMA+0xa8>
nopl   (%rax)
add    %rbx,%r14
cmp    %r14,%r12
je     125a <memCpy_DMA+0x11a>
mov    %r12,%rbx
sub    %r14,%rbx
cmp    $0xfa0,%rbx
mov    $0xfa0,%eax
cmovae %rax,%rbx
cmpl   $0x0,0xc(%rsp)
jle    11e0 <memCpy_DMA+0xa0>
mov    0x40(%rsp),%rax
lea    (%rax,%r14,1),%r13
xor    %ebp,%ebp
mov    0x20(%rsp,%rbp,4),%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1030 <write@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1210 <memCpy_DMA+0xd0>
cmpl   $0x0,0xc(%rsp)
jle    11e0 <memCpy_DMA+0xa0>
mov    0x38(%rsp),%rax
lea    (%rax,%r14,1),%r13
xor    %ebp,%ebp
nopw   0x0(%rax,%rax,1)
mov    0x20(%rsp,%rbp,4),%edi
mov    %r13,%rsi
mov    %rbx,%rdx
call   1050 <read@plt>
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1240 <memCpy_DMA+0x100>
jmp    11e0 <memCpy_DMA+0xa0>
mov    0x1c(%rsp),%edi
call   1040 <close@plt>
mov    0x18(%rsp),%edi
call   1040 <close@plt>
mov    0x14(%rsp),%edi
call   1040 <close@plt>
mov    0x10(%rsp),%edi
call   1040 <close@plt>
movslq 0xc(%rsp),%rax
imul   %rax,%r12
mov    %r12,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [15 x i8] c"/dev/axi_dma_0\00"
@O_RDWR = global i32 0
@.str.1 = [15 x i8] c"/dev/axi_dma_1\00"
@.str.2 = [15 x i8] c"/dev/axi_dma_2\00"
@.str.3 = [15 x i8] c"/dev/axi_dma_3\00"
define i64 @memCpy_DMA(i8* %0, i8* %1, i64 %2, i64 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i64
%10 = alloca i32
%11 = alloca [4 x i32]
%12 = alloca i64
%13 = alloca i64
%14 = alloca i32
store i8* %0, i8** %6
store i8* %1, i8** %7
store i64 %2, i64* %8
store i64 %3, i64* %9
store i32 %4, i32* %10
%15 = load i32, i32* @O_RDWR
%16 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %15)
%17 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 0
store i32 %16, i32* %17
%18 = load i32, i32* @O_RDWR
%19 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %18)
%20 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 1
store i32 %19, i32* %20
%21 = load i32, i32* @O_RDWR
%22 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 2
store i32 %22, i32* %23
%24 = load i32, i32* @O_RDWR
%25 = call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %24)
%26 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 3
store i32 %25, i32* %26
store i64 0, i64* %12
store i64 0, i64* %13
br label %27
27: ; preds = %84, %5
%28 = load i64, i64* %12
%29 = load i64, i64* %9
%30 = load i64, i64* %8
%31 = mul i64 %29, %30
%32 = icmp ne i64 %28, %31
br i1 %32, label %33, label %88
33: ; preds = %27
%34 = load i64, i64* %9
%35 = load i64, i64* %8
%36 = mul i64 %34, %35
%37 = load i64, i64* %12
%38 = sub i64 %36, %37
%39 = icmp ugt i64 %38, 4000
br i1 %39, label %40, label %41
40: ; preds = %33
br label %47
41: ; preds = %33
%42 = load i64, i64* %9
%43 = load i64, i64* %8
%44 = mul i64 %42, %43
%45 = load i64, i64* %12
%46 = sub i64 %44, %45
br label %47
47: ; preds = %41, %40
%48 = phi i64 [ 4000, %40 ], [ %46, %41 ]
store i64 %48, i64* %13
store i32 0, i32* %14
br label %49
49: ; preds = %63, %47
%50 = load i32, i32* %14
%51 = load i32, i32* %10
%52 = icmp slt i32 %50, %51
br i1 %52, label %53, label %66
53: ; preds = %49
%54 = load i32, i32* %14
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 %55
%57 = load i32, i32* %56
%58 = load i8*, i8** %6
%59 = load i64, i64* %12
%60 = getelementptr inbounds i8, i8* %58, i64 %59
%61 = load i64, i64* %13
%62 = call i32 @write(i32 %57, i8* %60, i64 %61)
br label %63
63: ; preds = %53
%64 = load i32, i32* %14
%65 = add nsw i32 %64, 1
store i32 %65, i32* %14
br label %49
66: ; preds = %49
store i32 0, i32* %14
br label %67
67: ; preds = %81, %66
%68 = load i32, i32* %14
%69 = load i32, i32* %10
%70 = icmp slt i32 %68, %69
br i1 %70, label %71, label %84
71: ; preds = %67
%72 = load i32, i32* %14
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 %73
%75 = load i32, i32* %74
%76 = load i8*, i8** %7
%77 = load i64, i64* %12
%78 = getelementptr inbounds i8, i8* %76, i64 %77
%79 = load i64, i64* %13
%80 = call i32 @read(i32 %75, i8* %78, i64 %79)
br label %81
81: ; preds = %71
%82 = load i32, i32* %14
%83 = add nsw i32 %82, 1
store i32 %83, i32* %14
br label %67
84: ; preds = %67
%85 = load i64, i64* %13
%86 = load i64, i64* %12
%87 = add i64 %86, %85
store i64 %87, i64* %12
br label %27
88: ; preds = %27
%89 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 0
%90 = load i32, i32* %89
%91 = call i32 @close(i32 %90)
%92 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 1
%93 = load i32, i32* %92
%94 = call i32 @close(i32 %93)
%95 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 2
%96 = load i32, i32* %95
%97 = call i32 @close(i32 %96)
%98 = getelementptr inbounds [4 x i32], [4 x i32]* %11, i64 0, i64 3
%99 = load i32, i32* %98
%100 = call i32 @close(i32 %99)
%101 = load i64, i64* %8
%102 = load i64, i64* %9
%103 = mul i64 %101, %102
%104 = load i32, i32* %10
%105 = sext i32 %104 to i64
%106 = mul i64 %103, %105
ret i64 %106
}

LLVM IR (-O2):
@.str = [15 x i8] c"/dev/axi_dma_0\00"
@O_RDWR = global i32 0
@.str.1 = [15 x i8] c"/dev/axi_dma_1\00"
@.str.2 = [15 x i8] c"/dev/axi_dma_2\00"
@.str.3 = [15 x i8] c"/dev/axi_dma_3\00"
define i64 @memCpy_DMA(i8* nocapture %0, i8* nocapture %1, i64 %2, i64 %3, i32 %4) {
%6 = alloca [4 x i32]
%7 = bitcast [4 x i32]* %6 to i8*
%8 = load i32, i32* @O_RDWR
%9 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 0
store i32 %9, i32* %10
%11 = load i32, i32* @O_RDWR
%12 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 1
store i32 %12, i32* %13
%14 = load i32, i32* @O_RDWR
%15 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 2
store i32 %15, i32* %16
%17 = load i32, i32* @O_RDWR
%18 = tail call i32 @open(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %17)
%19 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 3
store i32 %18, i32* %19
%20 = mul i64 %3, %2
%21 = icmp eq i64 %20, 0
br i1 %21, label %53, label %22
22: ; preds = %5
%23 = icmp sgt i32 %4, 0
%24 = zext i32 %4 to i64
%25 = zext i32 %4 to i64
br label %26
26: ; preds = %22, %50
%27 = phi i64 [ 0, %22 ], [ %51, %50 ]
%28 = sub i64 %20, %27
%29 = icmp ult i64 %28, 4000
%30 = select i1 %29, i64 %28, i64 4000
br i1 %23, label %31, label %50
31: ; preds = %26
%32 = getelementptr inbounds i8, i8* %0, i64 %27
br label %36
33: ; preds = %36
br i1 %23, label %34, label %50
34: ; preds = %33
%35 = getelementptr inbounds i8, i8* %1, i64 %27
br label %43
36: ; preds = %31, %36
%37 = phi i64 [ 0, %31 ], [ %41, %36 ]
%38 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 %37
%39 = load i32, i32* %38
%40 = tail call i32 @write(i32 %39, i8* %32, i64 %30)
%41 = add nuw nsw i64 %37, 1
%42 = icmp eq i64 %41, %24
br i1 %42, label %33, label %36
43: ; preds = %34, %43
%44 = phi i64 [ 0, %34 ], [ %48, %43 ]
%45 = getelementptr inbounds [4 x i32], [4 x i32]* %6, i64 0, i64 %44
%46 = load i32, i32* %45
%47 = tail call i32 @read(i32 %46, i8* %35, i64 %30)
%48 = add nuw nsw i64 %44, 1
%49 = icmp eq i64 %48, %25
br i1 %49, label %50, label %43
50: ; preds = %43, %26, %33
%51 = add i64 %30, %27
%52 = icmp eq i64 %20, %51
br i1 %52, label %53, label %26
53: ; preds = %50, %5
%54 = tail call i32 @close(i32 %9)
%55 = tail call i32 @close(i32 %12)
%56 = tail call i32 @close(i32 %15)
%57 = tail call i32 @close(i32 %18)
%58 = sext i32 %4 to i64
%59 = mul i64 %20, %58
ret i64 %59
}