#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 rand();
void random_printable_string_of_len(char *dest, int len) {
  char smallest = ' ', largest = '~';
  int num_chars = largest - smallest;
  for (int i = 0; i < len - 1; i++) {
    dest[i] = (rand() % num_chars) + smallest;
  }
  dest[len - 1] = '\0';
}


x86 assembly (clang, -O0):
<random_printable_string_of_len>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
movb   $0x20,-0xd(%rbp)
movb   $0x7e,-0xe(%rbp)
movsbl -0xe(%rbp),%eax
movsbl -0xd(%rbp),%ecx
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1176 <random_printable_string_of_len+0x66>
mov    $0x0,%al
call   1030 <rand@plt>
cltd
idivl  -0x14(%rbp)
movsbl -0xd(%rbp),%eax
add    %eax,%edx
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    113b <random_printable_string_of_len+0x2b>
mov    -0x8(%rbp),%rax
mov    -0xc(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_printable_string_of_len>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
lea    -0x1(%rsi),%r14d
cmp    $0x2,%esi
jl     1164 <random_printable_string_of_len+0x54>
mov    %r14d,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <rand@plt>
cltq
imul   $0xffffffffae4c415d,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
shr    $0x6,%ecx
add    %edx,%ecx
imul   $0x5e,%ecx,%ecx
sub    %ecx,%eax
add    $0x20,%al
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1130 <random_printable_string_of_len+0x20>
movslq %r14d,%rax
movb   $0x0,(%r15,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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