#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 */
scalar_t__ malloc(size_t);
char random_character();
char *random_string(size_t length) {
  char *string;
  char c;
  string = (char *)malloc(length + 1);
  int i;
  for (i = 0; i < length; i++) {
    c = random_character();
    string[i] = c;
  }
  string[length] = '\0';
  return string;
}


x86 assembly (clang, -O0):
<random_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movslq -0x18(%rbp),%rax
cmp    -0x8(%rbp),%rax
jae    1178 <random_string+0x58>
mov    $0x0,%al
call   1030 <random_character@plt>
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%dl
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1144 <random_string+0x24>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_string>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r14
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,%r15
test   %r14,%r14
je     1154 <random_string+0x34>
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <random_character@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1140 <random_string+0x20>
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @random_string(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i8
%5 = alloca i32
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = add i64 %6, 1
%8 = call i64 @malloc(i64 %7)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %3
store i32 0, i32* %5
br label %10
10: ; preds = %22, %1
%11 = load i32, i32* %5
%12 = sext i32 %11 to i64
%13 = load i64, i64* %2
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %25
15: ; preds = %10
%16 = call signext i8 (...) @random_character()
store i8 %16, i8* %4
%17 = load i8, i8* %4
%18 = load i8*, i8** %3
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds i8, i8* %18, i64 %20
store i8 %17, i8* %21
br label %22
22: ; preds = %15
%23 = load i32, i32* %5
%24 = add nsw i32 %23, 1
store i32 %24, i32* %5
br label %10
25: ; preds = %10
%26 = load i8*, i8** %3
%27 = load i64, i64* %2
%28 = getelementptr inbounds i8, i8* %26, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %3
ret i8* %29
}

LLVM IR (-O2):
define i8* @random_string(i64 %0) {
%2 = add i64 %0, 1
%3 = tail call i64 @malloc(i64 %2)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %0, 0
br i1 %5, label %12, label %6
6: ; preds = %1, %6
%7 = phi i64 [ %10, %6 ], [ 0, %1 ]
%8 = tail call signext i8 (...) @random_character()
%9 = getelementptr inbounds i8, i8* %4, i64 %7
store i8 %8, i8* %9
%10 = add nuw i64 %7, 1
%11 = icmp eq i64 %10, %0
br i1 %11, label %12, label %6
12: ; preds = %6, %1
%13 = getelementptr inbounds i8, i8* %4, i64 %0
store i8 0, i8* %13
ret i8* %4
}