#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*/ getseed(void *);
int const rand();
int /*<<< orphan*/ srand(int /*<<< orphan*/);
void mkrandchar(void *buf, const int len, const unsigned char *set,
                const int setlen) {
  srand(getseed(buf));
  int i = 0;
  unsigned char *p;
  p = buf;
  int tmp;
  for (; i < len; i++) {
    tmp = rand() % setlen;
    *p++ = set[tmp];
  }
}


x86 assembly (clang, -O0):
<mkrandchar>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
call   1030 <getseed@plt>
mov    %eax,%edi
call   1040 <srand@plt>
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11a9 <mkrandchar+0x79>
mov    $0x0,%al
call   1050 <rand@plt>
cltd
idivl  -0x1c(%rbp)
mov    %edx,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,1),%cl
mov    -0x28(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x28(%rbp)
mov    %cl,(%rax)
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
jmp    1165 <mkrandchar+0x35>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mkrandchar>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %ecx,%r14d
mov    %rdx,%r12
mov    %esi,%r15d
mov    %rdi,%r13
call   1030 <getseed@plt>
mov    %eax,%edi
call   1040 <srand@plt>
test   %r15d,%r15d
jle    1180 <mkrandchar+0x50>
mov    %r15d,%ebx
xor    %ebp,%ebp
nopl   (%rax)
xor    %eax,%eax
call   1050 <rand@plt>
cltd
idiv   %r14d
movslq %edx,%rax
movzbl (%r12,%rax,1),%eax
mov    %al,0x0(%r13,%rbp,1)
add    $0x1,%rbp
cmp    %ebp,%ebx
jne    1160 <mkrandchar+0x30>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @mkrandchar(i8* %0, i32 %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%12 = load i8*, i8** %5
%13 = call i32 @getseed(i8* %12)
%14 = call i32 @srand(i32 %13)
store i32 0, i32* %9
%15 = load i8*, i8** %5
store i8* %15, i8** %10
br label %16
16: ; preds = %31, %4
%17 = load i32, i32* %9
%18 = load i32, i32* %6
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %34
20: ; preds = %16
%21 = call i32 (...) @rand()
%22 = load i32, i32* %8
%23 = srem i32 %21, %22
store i32 %23, i32* %11
%24 = load i8*, i8** %7
%25 = load i32, i32* %11
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
%29 = load i8*, i8** %10
%30 = getelementptr inbounds i8, i8* %29, i32 1
store i8* %30, i8** %10
store i8 %28, i8* %29
br label %31
31: ; preds = %20
%32 = load i32, i32* %9
%33 = add nsw i32 %32, 1
store i32 %33, i32* %9
br label %16
34: ; preds = %16
ret void
}

LLVM IR (-O2):
define void @mkrandchar(i8* %0, i32 %1, i8* nocapture %2, i32 %3) {
%5 = tail call i32 @getseed(i8* %0)
%6 = tail call i32 @srand(i32 %5)
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %19
8: ; preds = %4, %8
%9 = phi i32 [ %17, %8 ], [ 0, %4 ]
%10 = phi i8* [ %16, %8 ], [ %0, %4 ]
%11 = tail call i32 (...) @rand()
%12 = srem i32 %11, %3
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %2, i64 %13
%15 = load i8, i8* %14
%16 = getelementptr inbounds i8, i8* %10, i64 1
store i8 %15, i8* %10
%17 = add nuw nsw i32 %9, 1
%18 = icmp eq i32 %17, %1
br i1 %18, label %19, label %8
19: ; preds = %8, %4
ret void
}