#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 /*<<< orphan*/ uint8_t;
typedef int /*<<< orphan*/ uint32_t;

/* Variables and functions */
int /*<<< orphan*/ XOR(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ salsa20_core(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
void scryptBlockMix(int r, uint8_t *B) {
  uint8_t *Y = malloc(128 * r);
  uint8_t X[64];
  uint8_t T[64];
  memcpy(X, &B[64 * (2 * r - 1)], 64);
  for (int i = 0; i < 2 * r; i++) {
    memcpy(T, B + 64 * i, 64);
    XOR(T, X, 64);
    salsa20_core((uint32_t *)X, (uint32_t *)T);
    if (i % 2 == 0)
      memcpy(Y + (64 * i / 2), X, 64);
    else
      memcpy(Y + (64 * (i / 2) + 64 * r), X, 64);
  }
  memcpy(B, Y, 128 * r);
  free(Y);
}


x86 assembly (clang, -O0):
<scryptBlockMix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x230,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x4(%rbp),%edi
shl    $0x7,%edi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x4(%rbp),%eax
shl    $0x1,%eax
sub    $0x1,%eax
shl    $0x6,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
movl   $0x0,-0x224(%rbp)
mov    -0x224(%rbp),%eax
mov    -0x4(%rbp),%ecx
shl    $0x1,%ecx
cmp    %ecx,%eax
jge    12a6 <scryptBlockMix+0x156>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x224(%rbp),%eax
shl    $0x6,%eax
cltq
shl    $0x2,%rax
add    %rax,%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
lea    -0x220(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0x40,%edx
call   1060 <XOR@plt>
lea    -0x120(%rbp),%rdi
lea    -0x220(%rbp),%rsi
call   1040 <salsa20_core@plt>
mov    -0x224(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cmp    $0x0,%edx
jne    1256 <scryptBlockMix+0x106>
mov    -0x18(%rbp),%rdi
mov    -0x224(%rbp),%eax
shl    $0x6,%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
cltq
shl    $0x2,%rax
add    %rax,%rdi
lea    -0x120(%rbp),%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
jmp    128d <scryptBlockMix+0x13d>
mov    -0x18(%rbp),%rdi
mov    -0x224(%rbp),%eax
mov    $0x2,%ecx
cltd
idiv   %ecx
shl    $0x6,%eax
mov    -0x4(%rbp),%ecx
shl    $0x6,%ecx
add    %ecx,%eax
cltq
shl    $0x2,%rax
add    %rax,%rdi
lea    -0x120(%rbp),%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
jmp    1292 <scryptBlockMix+0x142>
mov    -0x224(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x224(%rbp)
jmp    11a5 <scryptBlockMix+0x55>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%edx
shl    $0x7,%edx
call   1050 <memcpy@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x230,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scryptBlockMix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x228,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    %edi,%ebp
shl    $0x7,%ebp
mov    %ebp,%edi
call   1070 <malloc@plt>
mov    %rax,0x8(%rsp)
mov    %rbp,0x10(%rsp)
lea    -0x40(%rbp),%eax
cltq
mov    %r14,(%rsp)
lea    (%r14,%rax,4),%rsi
lea    0x20(%rsp),%rdi
mov    $0x40,%edx
call   1050 <memcpy@plt>
test   %ebx,%ebx
jle    1251 <scryptBlockMix+0x101>
mov    %rbx,%rax
mov    %rbx,%rcx
add    %eax,%eax
shl    $0x6,%ecx
mov    %rcx,0x18(%rsp)
cmp    $0x2,%eax
mov    $0x1,%r15d
cmovge %eax,%r15d
shl    $0x5,%r15
xor    %r12d,%r12d
lea    0x120(%rsp),%rbx
lea    0x20(%rsp),%rbp
mov    (%rsp),%r14
xor    %r13d,%r13d
jmp    1218 <scryptBlockMix+0xc8>
nopw   0x0(%rax,%rax,1)
mov    %r13d,%eax
and    $0x7fffffc0,%eax
add    0x18(%rsp),%eax
cltq
mov    0x8(%rsp),%rcx
lea    (%rcx,%rax,4),%rdi
mov    %rbp,%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
add    $0x20,%r13
add    $0x100,%r14
add    $0x1,%r12d
cmp    %r13,%r15
je     1251 <scryptBlockMix+0x101>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    $0x40,%edx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x40,%edx
call   1060 <XOR@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <salsa20_core@plt>
test   $0x1,%r12b
jne    11e0 <scryptBlockMix+0x90>
mov    %r13d,%eax
and    $0xffffffe0,%eax
jmp    11ee <scryptBlockMix+0x9e>
mov    (%rsp),%rdi
mov    0x8(%rsp),%rbx
mov    %rbx,%rsi
mov    0x10(%rsp),%rdx
call   1050 <memcpy@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x228,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @scryptBlockMix(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca [64 x i32]
%7 = alloca [64 x i32]
%8 = alloca i32
store i32 %0, i32* %3
store i32* %1, i32** %4
%9 = load i32, i32* %3
%10 = mul nsw i32 128, %9
%11 = call i32* @malloc(i32 %10)
store i32* %11, i32** %5
%12 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 0
%13 = load i32*, i32** %4
%14 = load i32, i32* %3
%15 = mul nsw i32 2, %14
%16 = sub nsw i32 %15, 1
%17 = mul nsw i32 64, %16
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i32, i32* %13, i64 %18
%20 = call i32 @memcpy(i32* %12, i32* %19, i32 64)
store i32 0, i32* %8
br label %21
21: ; preds = %65, %2
%22 = load i32, i32* %8
%23 = load i32, i32* %3
%24 = mul nsw i32 2, %23
%25 = icmp slt i32 %22, %24
br i1 %25, label %26, label %68
26: ; preds = %21
%27 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 0
%28 = load i32*, i32** %4
%29 = load i32, i32* %8
%30 = mul nsw i32 64, %29
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i32, i32* %28, i64 %31
%33 = call i32 @memcpy(i32* %27, i32* %32, i32 64)
%34 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 0
%35 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 0
%36 = call i32 @XOR(i32* %34, i32* %35, i32 64)
%37 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 0
%38 = getelementptr inbounds [64 x i32], [64 x i32]* %7, i64 0, i64 0
%39 = call i32 @salsa20_core(i32* %37, i32* %38)
%40 = load i32, i32* %8
%41 = srem i32 %40, 2
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %52
43: ; preds = %26
%44 = load i32*, i32** %5
%45 = load i32, i32* %8
%46 = mul nsw i32 64, %45
%47 = sdiv i32 %46, 2
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i32, i32* %44, i64 %48
%50 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 0
%51 = call i32 @memcpy(i32* %49, i32* %50, i32 64)
br label %64
52: ; preds = %26
%53 = load i32*, i32** %5
%54 = load i32, i32* %8
%55 = sdiv i32 %54, 2
%56 = mul nsw i32 64, %55
%57 = load i32, i32* %3
%58 = mul nsw i32 64, %57
%59 = add nsw i32 %56, %58
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i32, i32* %53, i64 %60
%62 = getelementptr inbounds [64 x i32], [64 x i32]* %6, i64 0, i64 0
%63 = call i32 @memcpy(i32* %61, i32* %62, i32 64)
br label %64
64: ; preds = %52, %43
br label %65
65: ; preds = %64
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
br label %21
68: ; preds = %21
%69 = load i32*, i32** %4
%70 = load i32*, i32** %5
%71 = load i32, i32* %3
%72 = mul nsw i32 128, %71
%73 = call i32 @memcpy(i32* %69, i32* %70, i32 %72)
%74 = load i32*, i32** %5
%75 = call i32 @free(i32* %74)
ret void
}

LLVM IR (-O2):
define void @scryptBlockMix(i32 %0, i32* %1) {
%3 = alloca [64 x i32]
%4 = alloca [64 x i32]
%5 = shl nsw i32 %0, 7
%6 = tail call i32* @malloc(i32 %5)
%7 = bitcast [64 x i32]* %3 to i8*
%8 = bitcast [64 x i32]* %4 to i8*
%9 = getelementptr inbounds [64 x i32], [64 x i32]* %3, i64 0, i64 0
%10 = add i32 %5, -64
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i32, i32* %1, i64 %11
%13 = call i32 @memcpy(i32* %9, i32* %12, i32 64)
%14 = icmp sgt i32 %0, 0
br i1 %14, label %15, label %21
15: ; preds = %2
%16 = shl nuw i32 %0, 1
%17 = getelementptr inbounds [64 x i32], [64 x i32]* %4, i64 0, i64 0
%18 = shl nsw i32 %0, 6
%19 = call i32 @llvm.smax.i32(i32 %16, i32 1)
%20 = zext i32 %19 to i64
br label %24
21: ; preds = %42, %2
%22 = call i32 @memcpy(i32* %1, i32* %6, i32 %5)
%23 = call i32 @free(i32* %6)
ret void
24: ; preds = %15, %42
%25 = phi i64 [ 0, %15 ], [ %46, %42 ]
%26 = shl nsw i64 %25, 6
%27 = getelementptr inbounds i32, i32* %1, i64 %26
%28 = call i32 @memcpy(i32* %17, i32* %27, i32 64)
%29 = call i32 @XOR(i32* %17, i32* %9, i32 64)
%30 = call i32 @salsa20_core(i32* %9, i32* %17)
%31 = trunc i64 %25 to i32
%32 = and i32 %31, 1
%33 = icmp eq i32 %32, 0
br i1 %33, label %34, label %37
34: ; preds = %24
%35 = shl i64 %25, 5
%36 = and i64 %35, 4294967264
br label %42
37: ; preds = %24
%38 = shl nsw i32 %31, 5
%39 = and i32 %38, 2147483584
%40 = add nsw i32 %39, %18
%41 = sext i32 %40 to i64
br label %42
42: ; preds = %34, %37
%43 = phi i64 [ %36, %34 ], [ %41, %37 ]
%44 = getelementptr inbounds i32, i32* %6, i64 %43
%45 = call i32 @memcpy(i32* %44, i32* %9, i32 64)
%46 = add nuw nsw i64 %25, 1
%47 = icmp eq i64 %46, %20
br i1 %47, label %21, label %24
}