#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 */
struct timeval {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int rand();
char *rand_string(char *str, size_t size) {
  struct timeval tv_begin, tv_end;
  gettimeofday(&tv_begin, NULL);
  const char charset[] = "abcdefghijklmnopqrstuvwxyz0123456789%$£/.,;";
  if (size) {
    --size;
    for (size_t n = 0; n < size; n++) {
      int key = rand() % (int)(sizeof charset - 1);
      str[n] = charset[key];
    }
    str[size] = '\0';
  }
  return str;
}


x86 assembly (clang, -O0):
<rand_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
lea    -0x50(%rbp),%rdi
lea    0xea8(%rip),%rsi
mov    $0x2d,%edx
call   1040 <memcpy@plt>
cmpq   $0x0,-0x10(%rbp)
je     11d1 <rand_string+0xa1>
mov    -0x10(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x10(%rbp)
movq   $0x0,-0x58(%rbp)
mov    -0x58(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11c5 <rand_string+0x95>
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x2c,%ecx
cltd
idiv   %ecx
mov    %edx,-0x5c(%rbp)
movslq -0x5c(%rbp),%rax
mov    -0x50(%rbp,%rax,1),%dl
mov    -0x8(%rbp),%rax
mov    -0x58(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x58(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x58(%rbp)
jmp    1181 <rand_string+0x51>
mov    -0x8(%rbp),%rax
mov    -0x10(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r12
mov    %rdi,%r14
mov    %rsp,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %r12,%r12
je     118b <rand_string+0x6b>
add    $0xffffffffffffffff,%r12
je     1186 <rand_string+0x66>
lea    0xeb6(%rip),%r15
xor    %ebx,%ebx
nopl   0x0(%rax)
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0x2e8ba2e9,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
imul   $0x2c,%ecx,%ecx
sub    %ecx,%eax
cltq
movzbl (%rax,%r15,1),%eax
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r12
jne    1150 <rand_string+0x30>
movb   $0x0,(%r14,%r12,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32 }
@__const.rand_string.charset = [45 x i8] c"abcdefghijklmnopqrstuvwxyz0123456789%$\C2\A3/.,;\00"
define i8* @rand_string(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = alloca [45 x i8]
%8 = alloca i64
%9 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%10 = call i32 @gettimeofday(%struct.timeval* %5, i32* null)
%11 = bitcast [45 x i8]* %7 to i8*
%12 = load i64, i64* %4
%13 = icmp ne i64 %12, 0
br i1 %13, label %14, label %38
14: ; preds = %2
%15 = load i64, i64* %4
%16 = add i64 %15, -1
store i64 %16, i64* %4
store i64 0, i64* %8
br label %17
17: ; preds = %31, %14
%18 = load i64, i64* %8
%19 = load i64, i64* %4
%20 = icmp ult i64 %18, %19
br i1 %20, label %21, label %34
21: ; preds = %17
%22 = call i32 (...) @rand()
%23 = srem i32 %22, 44
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [45 x i8], [45 x i8]* %7, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = load i8*, i8** %3
%29 = load i64, i64* %8
%30 = getelementptr inbounds i8, i8* %28, i64 %29
store i8 %27, i8* %30
br label %31
31: ; preds = %21
%32 = load i64, i64* %8
%33 = add i64 %32, 1
store i64 %33, i64* %8
br label %17
34: ; preds = %17
%35 = load i8*, i8** %3
%36 = load i64, i64* %4
%37 = getelementptr inbounds i8, i8* %35, i64 %36
store i8 0, i8* %37
br label %38
38: ; preds = %34, %2
%39 = load i8*, i8** %3
ret i8* %39
}

LLVM IR (-O2):
%struct.timeval = type { i32 }
@__const.rand_string.charset = [45 x i8] c"abcdefghijklmnopqrstuvwxyz0123456789%$\C2\A3/.,;\00"
define i8* @rand_string(i8* returned writeonly %0, i64 %1) {
%3 = alloca %struct.timeval
%4 = bitcast %struct.timeval* %3 to i8*
%5 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%6 = icmp eq i64 %1, 0
br i1 %6, label %22, label %7
7: ; preds = %2
%8 = add i64 %1, -1
%9 = icmp eq i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %12, %7
%11 = getelementptr inbounds i8, i8* %0, i64 %8
store i8 0, i8* %11
br label %22
12: ; preds = %7, %12
%13 = phi i64 [ %20, %12 ], [ 0, %7 ]
%14 = tail call i32 (...) @rand()
%15 = srem i32 %14, 44
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds [45 x i8], [45 x i8]* @__const.rand_string.charset, i64 0, i64 %16
%18 = load i8, i8* %17
%19 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 %18, i8* %19
%20 = add nuw i64 %13, 1
%21 = icmp eq i64 %20, %8
br i1 %21, label %10, label %12
22: ; preds = %10, %2
ret i8* %0
}