#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 uint32_t;
typedef int uint16_t;

/* Variables and functions */
int rand_next();
void rand_str(char *str, int len) {
  while (len > 0) {
    if (len >= 4) {
      *((uint32_t *)str) = rand_next();
      str += sizeof(uint32_t);
      len -= sizeof(uint32_t);
    } else if (len >= 2) {
      *((uint16_t *)str) = rand_next() & 0xFFFF;
      str += sizeof(uint16_t);
      len -= sizeof(uint16_t);
    } else {
      *str++ = rand_next() & 0xFF;
      len--;
    }
  }
}


x86 assembly (clang, -O0):
<rand_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jle    11cb <rand_str+0xbb>
cmpl   $0x4,-0xc(%rbp)
jl     115e <rand_str+0x4e>
mov    $0x0,%al
call   1030 <rand_next@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
movslq -0xc(%rbp),%rax
sub    $0x4,%rax
mov    %eax,-0xc(%rbp)
jmp    11c6 <rand_str+0xb6>
cmpl   $0x2,-0xc(%rbp)
jl     1199 <rand_str+0x89>
mov    $0x0,%al
call   1030 <rand_next@plt>
mov    %eax,%ecx
and    $0xffff,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
add    $0x4,%rax
mov    %rax,-0x8(%rbp)
movslq -0xc(%rbp),%rax
sub    $0x4,%rax
mov    %eax,-0xc(%rbp)
jmp    11c1 <rand_str+0xb1>
mov    $0x0,%al
call   1030 <rand_next@plt>
and    $0xff,%eax
mov    %al,%cl
mov    -0x8(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x8(%rbp)
mov    %cl,(%rax)
mov    -0xc(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0xc(%rbp)
jmp    11c6 <rand_str+0xb6>
jmp    111f <rand_str+0xf>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_str>:
push   %rbp
push   %rbx
push   %rax
test   %esi,%esi
jle    114c <rand_str+0x3c>
mov    %esi,%ebp
mov    %rdi,%rbx
nopl   0x0(%rax)
xor    %eax,%eax
cmp    $0x3,%ebp
jbe    1139 <rand_str+0x29>
call   1030 <rand_next@plt>
mov    %eax,(%rbx)
add    $0x4,%rbx
add    $0xfffffffc,%ebp
jne    1120 <rand_str+0x10>
jmp    114c <rand_str+0x3c>
call   1030 <rand_next@plt>
cmp    $0x2,%ebp
jb     114a <rand_str+0x3a>
movzwl %ax,%eax
mov    %eax,(%rbx)
jmp    114c <rand_str+0x3c>
mov    %al,(%rbx)
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define void @rand_str(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
br label %5
5: ; preds = %44, %2
%6 = load i32, i32* %4
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %45
8: ; preds = %5
%9 = load i32, i32* %4
%10 = icmp sge i32 %9, 4
br i1 %10, label %11, label %21
11: ; preds = %8
%12 = call i32 (...) @rand_next()
%13 = load i8*, i8** %3
%14 = bitcast i8* %13 to i32*
store i32 %12, i32* %14
%15 = load i8*, i8** %3
%16 = getelementptr inbounds i8, i8* %15, i64 4
store i8* %16, i8** %3
%17 = load i32, i32* %4
%18 = sext i32 %17 to i64
%19 = sub i64 %18, 4
%20 = trunc i64 %19 to i32
store i32 %20, i32* %4
br label %44
21: ; preds = %8
%22 = load i32, i32* %4
%23 = icmp sge i32 %22, 2
br i1 %23, label %24, label %35
24: ; preds = %21
%25 = call i32 (...) @rand_next()
%26 = and i32 %25, 65535
%27 = load i8*, i8** %3
%28 = bitcast i8* %27 to i32*
store i32 %26, i32* %28
%29 = load i8*, i8** %3
%30 = getelementptr inbounds i8, i8* %29, i64 4
store i8* %30, i8** %3
%31 = load i32, i32* %4
%32 = sext i32 %31 to i64
%33 = sub i64 %32, 4
%34 = trunc i64 %33 to i32
store i32 %34, i32* %4
br label %43
35: ; preds = %21
%36 = call i32 (...) @rand_next()
%37 = and i32 %36, 255
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %3
%40 = getelementptr inbounds i8, i8* %39, i32 1
store i8* %40, i8** %3
store i8 %38, i8* %39
%41 = load i32, i32* %4
%42 = add nsw i32 %41, -1
store i32 %42, i32* %4
br label %43
43: ; preds = %35, %24
br label %44
44: ; preds = %43, %11
br label %5
45: ; preds = %5
ret void
}

LLVM IR (-O2):
define void @rand_str(i8* nocapture writeonly %0, i32 %1) {
%3 = icmp sgt i32 %1, 0
br i1 %3, label %4, label %22
4: ; preds = %2, %16
%5 = phi i32 [ %20, %16 ], [ %1, %2 ]
%6 = phi i8* [ %19, %16 ], [ %0, %2 ]
%7 = icmp ugt i32 %5, 3
br i1 %7, label %16, label %8
8: ; preds = %4
%9 = icmp ugt i32 %5, 1
%10 = tail call i32 (...) @rand_next()
br i1 %9, label %11, label %14
11: ; preds = %8
%12 = and i32 %10, 65535
%13 = bitcast i8* %6 to i32*
store i32 %12, i32* %13
br label %22
14: ; preds = %8
%15 = trunc i32 %10 to i8
store i8 %15, i8* %6
br label %22
16: ; preds = %4
%17 = tail call i32 (...) @rand_next()
%18 = bitcast i8* %6 to i32*
store i32 %17, i32* %18
%19 = getelementptr inbounds i8, i8* %6, i64 4
%20 = add nsw i32 %5, -4
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %4
22: ; preds = %16, %14, %11, %2
ret void
}