#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*/ exit(int);
char getRandChar();
scalar_t__ malloc(size_t);
int /*<<< orphan*/ perror(char *);
char *getRandString(size_t n) {
  char *str = (char *)malloc(n * sizeof(char) + 1);
  if (str == NULL) {
    perror("malloc() failed in randString()");
    exit(1);
  }
  str[n] = '\0';
  while (n--)
    str[n] = getRandChar();
  return str;
}


x86 assembly (clang, -O0):
<getRandString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
shl    $0x0,%rdi
add    $0x1,%rdi
call   1040 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1182 <getRandString+0x42>
lea    0xe8d(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x8(%rbp)
cmp    $0x0,%rax
je     11c0 <getRandString+0x80>
mov    $0x0,%al
call   1030 <getRandChar@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
jmp    118e <getRandString+0x4e>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getRandString>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
add    $0x1,%rdi
call   1040 <malloc@plt>
test   %rax,%rax
je     118d <getRandString+0x4d>
mov    %rax,%r14
movb   $0x0,(%rax,%rbx,1)
test   %rbx,%rbx
je     1182 <getRandString+0x42>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1030 <getRandChar@plt>
mov    %al,-0x1(%r14,%rbx,1)
add    $0xffffffffffffffff,%rbx
jne    1170 <getRandString+0x30>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe6c(%rip),%rdi
call   1050 <perror@plt>
mov    $0x1,%edi
call   1060 <exit@plt>

LLVM IR (-O0):
@.str = [32 x i8] c"malloc() failed in randString()\00"
define i8* @getRandString(i64 %0) {
%2 = alloca i64
%3 = alloca i8*
store i64 %0, i64* %2
%4 = load i64, i64* %2
%5 = mul i64 %4, 1
%6 = add i64 %5, 1
%7 = call i64 @malloc(i64 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %3
%9 = load i8*, i8** %3
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %14
11: ; preds = %1
%12 = call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%13 = call i32 @exit(i32 1)
unreachable
14: ; preds = %1
%15 = load i8*, i8** %3
%16 = load i64, i64* %2
%17 = getelementptr inbounds i8, i8* %15, i64 %16
store i8 0, i8* %17
br label %18
18: ; preds = %22, %14
%19 = load i64, i64* %2
%20 = add i64 %19, -1
store i64 %20, i64* %2
%21 = icmp ne i64 %19, 0
br i1 %21, label %22, label %27
22: ; preds = %18
%23 = call signext i8 (...) @getRandChar()
%24 = load i8*, i8** %3
%25 = load i64, i64* %2
%26 = getelementptr inbounds i8, i8* %24, i64 %25
store i8 %23, i8* %26
br label %18
27: ; preds = %18
%28 = load i8*, i8** %3
ret i8* %28
}

LLVM IR (-O2):
@.str = [32 x i8] c"malloc() failed in randString()\00"
define i8* @getRandString(i64 %0) {
%2 = add i64 %0, 1
%3 = tail call i64 @malloc(i64 %2)
%4 = inttoptr i64 %3 to i8*
%5 = icmp eq i64 %3, 0
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = tail call i32 @perror(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %1
%10 = getelementptr inbounds i8, i8* %4, i64 %0
store i8 0, i8* %10
%11 = icmp eq i64 %0, 0
br i1 %11, label %18, label %12
12: ; preds = %9, %12
%13 = phi i64 [ %14, %12 ], [ %0, %9 ]
%14 = add i64 %13, -1
%15 = tail call signext i8 (...) @getRandChar()
%16 = getelementptr inbounds i8, i8* %4, i64 %14
store i8 %15, i8* %16
%17 = icmp eq i64 %14, 0
br i1 %17, label %18, label %12
18: ; preds = %12, %9
ret i8* %4
}