#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;

/* Variables and functions */
int /*<<< orphan*/ XOR(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ free(int /*<<< orphan*/ *);
int /*<<< orphan*/ free2D(int /*<<< orphan*/ **, int);
int integerify(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *malloc(int);
int /*<<< orphan*/ malloc2D(int /*<<< orphan*/ ***, int, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ scryptBlockMix(int, int /*<<< orphan*/ *);
void scryptROMix(int r, int n, uint8_t *B) {
  int j;
  uint8_t **V;
  uint8_t *X = malloc(r * 128);
  memcpy(X, B, r * 128);
  malloc2D(&V, n, r * 128);
  for (int i = 0; i < n; i++) {
    memcpy(V[i], X, 128 * r);
    scryptBlockMix(r, X);
  }
  for (int i = 0; i < n; i++) {
    j = integerify(X, r) % n;
    XOR(X, V[j], 128 * r);
    scryptBlockMix(r, X);
  }
  memcpy(B, X, r * 128);
  free2D(V, n);
  free(X);
}


x86 assembly (clang, -O0):
<scryptROMix>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %rdx,-0x10(%rbp)
mov    -0x4(%rbp),%edi
shl    $0x7,%edi
call   1080 <malloc@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x4(%rbp),%edx
shl    $0x7,%edx
call   1050 <memcpy@plt>
mov    -0x8(%rbp),%esi
mov    -0x4(%rbp),%edx
shl    $0x7,%edx
lea    -0x20(%rbp),%rdi
call   10a0 <malloc2D@plt>
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    120e <scryptROMix+0x8e>
mov    -0x20(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edx
shl    $0x7,%edx
call   1050 <memcpy@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1060 <scryptBlockMix@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11cd <scryptROMix+0x4d>
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1269 <scryptROMix+0xe9>
mov    -0x28(%rbp),%rdi
mov    -0x4(%rbp),%esi
call   1040 <integerify@plt>
cltd
idivl  -0x8(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
mov    -0x4(%rbp),%edx
shl    $0x7,%edx
call   1070 <XOR@plt>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
call   1060 <scryptBlockMix@plt>
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1215 <scryptROMix+0x95>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
mov    -0x4(%rbp),%edx
shl    $0x7,%edx
call   1050 <memcpy@plt>
mov    -0x20(%rbp),%rdi
mov    -0x8(%rbp),%esi
call   1090 <free2D@plt>
mov    -0x28(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<scryptROMix>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %esi,%r15d
mov    %edi,%r13d
mov    %edi,%r12d
shl    $0x7,%r12d
mov    %r12d,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
lea    0x8(%rsp),%rdi
mov    %r15d,%esi
mov    %r12d,%edx
call   10a0 <malloc2D@plt>
test   %r15d,%r15d
jle    1256 <scryptROMix+0xd6>
mov    %r14,0x10(%rsp)
mov    %r15d,%ebp
xor    %r14d,%r14d
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rax
mov    (%rax,%r14,8),%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1060 <scryptBlockMix@plt>
add    $0x1,%r14
cmp    %r14,%rbp
jne    11e0 <scryptROMix+0x60>
test   %r15d,%r15d
mov    0x10(%rsp),%r14
jle    1256 <scryptROMix+0xd6>
mov    %r15d,%ebp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r13d,%esi
call   1040 <integerify@plt>
cltd
idiv   %r15d
mov    0x8(%rsp),%rax
movslq %edx,%rcx
mov    (%rax,%rcx,8),%rsi
mov    %rbx,%rdi
mov    %r12d,%edx
call   1070 <XOR@plt>
mov    %r13d,%edi
mov    %rbx,%rsi
call   1060 <scryptBlockMix@plt>
add    $0xffffffff,%ebp
jne    1220 <scryptROMix+0xa0>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r12d,%edx
call   1050 <memcpy@plt>
mov    0x8(%rsp),%rdi
mov    %r15d,%esi
call   1090 <free2D@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define void @scryptROMix(i32 %0, i32 %1, i32* %2) {
%4 = alloca i32**
%5 = bitcast i32*** %4 to i8*
%6 = shl nsw i32 %0, 7
%7 = tail call i32* @malloc(i32 %6)
%8 = tail call i32 @memcpy(i32* %7, i32* %2, i32 %6)
%9 = call i32 @malloc2D(i32*** %4, i32 %1, i32 %6)
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %23
11: ; preds = %3
%12 = zext i32 %1 to i64
br label %14
13: ; preds = %14
br i1 %10, label %28, label %23
14: ; preds = %11, %14
%15 = phi i64 [ 0, %11 ], [ %21, %14 ]
%16 = load i32**, i32*** %4
%17 = getelementptr inbounds i32*, i32** %16, i64 %15
%18 = load i32*, i32** %17
%19 = call i32 @memcpy(i32* %18, i32* %7, i32 %6)
%20 = call i32 @scryptBlockMix(i32 %0, i32* %7)
%21 = add nuw nsw i64 %15, 1
%22 = icmp eq i64 %21, %12
br i1 %22, label %13, label %14
23: ; preds = %28, %3, %13
%24 = call i32 @memcpy(i32* %2, i32* %7, i32 %6)
%25 = load i32**, i32*** %4
%26 = call i32 @free2D(i32** %25, i32 %1)
%27 = call i32 @free(i32* %7)
ret void
28: ; preds = %13, %28
%29 = phi i32 [ %38, %28 ], [ 0, %13 ]
%30 = call i32 @integerify(i32* %7, i32 %0)
%31 = srem i32 %30, %1
%32 = load i32**, i32*** %4
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i32*, i32** %32, i64 %33
%35 = load i32*, i32** %34
%36 = call i32 @XOR(i32* %7, i32* %35, i32 %6)
%37 = call i32 @scryptBlockMix(i32 %0, i32* %7)
%38 = add nuw nsw i32 %29, 1
%39 = icmp eq i32 %38, %1
br i1 %39, label %23, label %28
}