#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 */
char *calloc(int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int rand();
int /*<<< orphan*/ stderr;
char *random_nchar_string(int n) {
  char *s;
  int i;
  if (!(s = calloc(n + 1, sizeof(char)))) {
    fprintf(stderr, ("random_nchar_string: Out of memory!\n"));
    return NULL;
  }
  for (i = 0; i < n; i++)
    *(s + i) = (char)('A' + rand() % 25);
  return s;
}


x86 assembly (clang, -O0):
<random_nchar_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
mov    -0xc(%rbp),%edi
add    $0x1,%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    117b <random_nchar_string+0x4b>
mov    0x2e80(%rip),%rax
mov    (%rax),%edi
lea    0xe97(%rip),%rsi
call   1040 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c1 <random_nchar_string+0x91>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11b9 <random_nchar_string+0x89>
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x19,%ecx
cltd
idiv   %ecx
add    $0x41,%edx
mov    -0x18(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1182 <random_nchar_string+0x52>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_nchar_string>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%r14d
lea    0x1(%r14),%edi
mov    $0x1,%esi
call   1030 <calloc@plt>
mov    %rax,%r15
test   %rax,%rax
je     1196 <random_nchar_string+0x66>
test   %r14d,%r14d
jle    11ab <random_nchar_string+0x7b>
mov    %r14d,%r14d
xor    %ebx,%ebx
nopl   0x0(%rax,%rax,1)
xor    %eax,%eax
call   1050 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%ecx
lea    (%rcx,%rcx,4),%ecx
sub    %ecx,%eax
add    $0x41,%al
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1160 <random_nchar_string+0x30>
jmp    11ab <random_nchar_string+0x7b>
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
lea    0xe5a(%rip),%rsi
call   1040 <fprintf@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stderr = global i32 0
@.str = [37 x i8] c"random_nchar_string: Out of memory!\0A\00"
define i8* @random_nchar_string(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
store i32 %0, i32* %3
%6 = load i32, i32* %3
%7 = add nsw i32 %6, 1
%8 = call i8* @calloc(i32 %7, i32 1)
store i8* %8, i8** %4
%9 = icmp ne i8* %8, null
br i1 %9, label %13, label %10
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %2
br label %32
13: ; preds = %1
store i32 0, i32* %5
br label %14
14: ; preds = %27, %13
%15 = load i32, i32* %5
%16 = load i32, i32* %3
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %30
18: ; preds = %14
%19 = call i32 (...) @rand()
%20 = srem i32 %19, 25
%21 = add nsw i32 65, %20
%22 = trunc i32 %21 to i8
%23 = load i8*, i8** %4
%24 = load i32, i32* %5
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 %22, i8* %26
br label %27
27: ; preds = %18
%28 = load i32, i32* %5
%29 = add nsw i32 %28, 1
store i32 %29, i32* %5
br label %14
30: ; preds = %14
%31 = load i8*, i8** %4
store i8* %31, i8** %2
br label %32
32: ; preds = %30, %10
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [37 x i8] c"random_nchar_string: Out of memory!\0A\00"
define noalias i8* @random_nchar_string(i32 %0) {
%2 = add nsw i32 %0, 1
%3 = tail call i8* @calloc(i32 %2, i32 1)
%4 = icmp eq i8* %3, null
br i1 %4, label %9, label %5
5: ; preds = %1
%6 = icmp sgt i32 %0, 0
br i1 %6, label %7, label %21
7: ; preds = %5
%8 = zext i32 %0 to i64
br label %12
9: ; preds = %1
%10 = load i32, i32* @stderr
%11 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str, i64 0, i64 0))
br label %21
12: ; preds = %7, %12
%13 = phi i64 [ 0, %7 ], [ %19, %12 ]
%14 = tail call i32 (...) @rand()
%15 = srem i32 %14, 25
%16 = trunc i32 %15 to i8
%17 = add nsw i8 %16, 65
%18 = getelementptr inbounds i8, i8* %3, i64 %13
store i8 %17, i8* %18
%19 = add nuw nsw i64 %13, 1
%20 = icmp eq i64 %19, %8
br i1 %20, label %21, label %12
21: ; preds = %12, %5, %9
ret i8* %3
}