#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(int);
int rand();
char *randbuf(int len) {
  char *buf;
  int i;
  buf = (char *)malloc(len + 1);
  for (i = 0; i < len; i++) {
    buf[i] = 'a' + (rand() % 26);
  }
  buf[i] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<randbuf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%edi
add    $0x1,%edi
call   1030 <malloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1178 <randbuf+0x58>
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x1a,%ecx
cltd
idiv   %ecx
add    $0x61,%edx
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1141 <randbuf+0x21>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randbuf>:
push   %r15
push   %r14
push   %rbx
mov    %edi,%r14d
lea    0x1(%r14),%edi
call   1030 <malloc@plt>
mov    %rax,%r15
test   %r14d,%r14d
jle    1178 <randbuf+0x58>
mov    %r14d,%r14d
xor    %ebx,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0x4ec4ec4f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x23,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,4),%edx
lea    (%rdx,%rdx,4),%edx
add    %ecx,%edx
sub    %edx,%eax
add    $0x61,%al
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1140 <randbuf+0x20>
jmp    117b <randbuf+0x5b>
xor    %r14d,%r14d
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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