#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*/ chacha20_init(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ chacha20_update(int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/ *, size_t);
int chacha20_rng(uint8_t *out, size_t len, uint8_t key[32]) {
  uint32_t st[16];
  size_t off;
  chacha20_init(st, key);
  chacha20_update(&out[0], st);
  memcpy(key, out, 32);
  off = 0;
  while (len >= 64) {
    chacha20_update(&out[off], st);
    len -= 64;
    off += 64;
  }
  if (len > 0) {
    uint8_t tmp[64];
    chacha20_update(tmp, st);
    memcpy(&out[off], tmp, len);
  }
  return 0;
}


x86 assembly (clang, -O0):
<chacha20_rng>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
lea    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1030 <chacha20_init@plt>
mov    -0x8(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1040 <chacha20_update@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x20,%edx
call   1050 <memcpy@plt>
movq   $0x0,-0x68(%rbp)
cmpq   $0x40,-0x10(%rbp)
jb     11bb <chacha20_rng+0x8b>
mov    -0x8(%rbp),%rdi
mov    -0x68(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
lea    -0x60(%rbp),%rsi
call   1040 <chacha20_update@plt>
mov    -0x10(%rbp),%rax
sub    $0x40,%rax
mov    %rax,-0x10(%rbp)
mov    -0x68(%rbp),%rax
add    $0x40,%rax
mov    %rax,-0x68(%rbp)
jmp    117b <chacha20_rng+0x4b>
cmpq   $0x0,-0x10(%rbp)
jbe    11f5 <chacha20_rng+0xc5>
lea    -0x170(%rbp),%rdi
lea    -0x60(%rbp),%rsi
call   1040 <chacha20_update@plt>
mov    -0x8(%rbp),%rdi
mov    -0x68(%rbp),%rax
shl    $0x2,%rax
add    %rax,%rdi
lea    -0x170(%rbp),%rsi
mov    -0x10(%rbp),%rdx
call   1050 <memcpy@plt>
xor    %eax,%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chacha20_rng>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x140,%rsp
mov    %rdx,%rbx
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsp,%r15
mov    %r15,%rdi
mov    %rdx,%rsi
call   1030 <chacha20_init@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1040 <chacha20_update@plt>
mov    $0x20,%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <memcpy@plt>
xor    %r13d,%r13d
cmp    $0x40,%r12
jb     11ba <chacha20_rng+0x8a>
mov    %rsp,%r15
mov    %r14,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    %r15,%rsi
call   1040 <chacha20_update@plt>
add    $0x100,%rbx
lea    (%r12,%r13,1),%rax
add    $0xffffffffffffffc0,%rax
add    $0xffffffffffffffc0,%r13
cmp    $0x3f,%rax
ja     1190 <chacha20_rng+0x60>
add    %r13,%r12
neg    %r13
test   %r12,%r12
je     11de <chacha20_rng+0xae>
lea    0x40(%rsp),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <chacha20_update@plt>
lea    (%r14,%r13,4),%rdi
mov    %rbx,%rsi
mov    %r12,%rdx
call   1050 <memcpy@plt>
xor    %eax,%eax
add    $0x140,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i32 @chacha20_rng(i32* %0, i64 %1, i32* %2) {
%4 = alloca i32*
%5 = alloca i64
%6 = alloca i32*
%7 = alloca [16 x i32]
%8 = alloca i64
%9 = alloca [64 x i32]
store i32* %0, i32** %4
store i64 %1, i64* %5
store i32* %2, i32** %6
%10 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 0
%11 = load i32*, i32** %6
%12 = call i32 @chacha20_init(i32* %10, i32* %11)
%13 = load i32*, i32** %4
%14 = getelementptr inbounds i32, i32* %13, i64 0
%15 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 0
%16 = call i32 @chacha20_update(i32* %14, i32* %15)
%17 = load i32*, i32** %6
%18 = load i32*, i32** %4
%19 = call i32 @memcpy(i32* %17, i32* %18, i64 32)
store i64 0, i64* %8
br label %20
20: ; preds = %23, %3
%21 = load i64, i64* %5
%22 = icmp uge i64 %21, 64
br i1 %22, label %23, label %33
23: ; preds = %20
%24 = load i32*, i32** %4
%25 = load i64, i64* %8
%26 = getelementptr inbounds i32, i32* %24, i64 %25
%27 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 0
%28 = call i32 @chacha20_update(i32* %26, i32* %27)
%29 = load i64, i64* %5
%30 = sub i64 %29, 64
store i64 %30, i64* %5
%31 = load i64, i64* %8
%32 = add i64 %31, 64
store i64 %32, i64* %8
br label %20
33: ; preds = %20
%34 = load i64, i64* %5
%35 = icmp ugt i64 %34, 0
br i1 %35, label %36, label %46
36: ; preds = %33
%37 = getelementptr inbounds [64 x i32], [64 x i32]* %9, i64 0, i64 0
%38 = getelementptr inbounds [16 x i32], [16 x i32]* %7, i64 0, i64 0
%39 = call i32 @chacha20_update(i32* %37, i32* %38)
%40 = load i32*, i32** %4
%41 = load i64, i64* %8
%42 = getelementptr inbounds i32, i32* %40, i64 %41
%43 = getelementptr inbounds [64 x i32], [64 x i32]* %9, i64 0, i64 0
%44 = load i64, i64* %5
%45 = call i32 @memcpy(i32* %42, i32* %43, i64 %44)
br label %46
46: ; preds = %36, %33
ret i32 0
}

LLVM IR (-O2):
define i32 @chacha20_rng(i32* %0, i64 %1, i32* %2) {
%4 = alloca [16 x i32]
%5 = alloca [64 x i32]
%6 = bitcast [16 x i32]* %4 to i8*
%7 = getelementptr inbounds [16 x i32], [16 x i32]* %4, i64 0, i64 0
%8 = call i32 @chacha20_init(i32* %7, i32* %2)
%9 = call i32 @chacha20_update(i32* %0, i32* %7)
%10 = call i32 @memcpy(i32* %2, i32* %0, i64 32)
%11 = icmp ugt i64 %1, 63
br i1 %11, label %12, label %20
12: ; preds = %3, %12
%13 = phi i64 [ %18, %12 ], [ 0, %3 ]
%14 = phi i64 [ %17, %12 ], [ %1, %3 ]
%15 = getelementptr inbounds i32, i32* %0, i64 %13
%16 = call i32 @chacha20_update(i32* %15, i32* %7)
%17 = add i64 %14, -64
%18 = add i64 %13, 64
%19 = icmp ugt i64 %17, 63
br i1 %19, label %12, label %20
20: ; preds = %12, %3
%21 = phi i64 [ %1, %3 ], [ %17, %12 ]
%22 = phi i64 [ 0, %3 ], [ %18, %12 ]
%23 = icmp eq i64 %21, 0
br i1 %23, label %30, label %24
24: ; preds = %20
%25 = bitcast [64 x i32]* %5 to i8*
%26 = getelementptr inbounds [64 x i32], [64 x i32]* %5, i64 0, i64 0
%27 = call i32 @chacha20_update(i32* %26, i32* %7)
%28 = getelementptr inbounds i32, i32* %0, i64 %22
%29 = call i32 @memcpy(i32* %28, i32* %26, i64 %21)
br label %30
30: ; preds = %24, %20
ret i32 0
}