#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 *malloc(unsigned int);
int rand();
char *rand_string(void) {
  unsigned len = 1 + (rand() % 3);
  char *ret = malloc(len + 1);
  unsigned i;
  for (i = 0; i < len; i++) {
    ret[i] = '0' + (rand() & 7);
  }
  ret[i] = 0;
  return ret;
}


x86 assembly (clang, -O0):
<rand_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    $0x0,%al
call   1040 <rand@plt>
mov    $0x3,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-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
jae    1186 <rand_string+0x66>
mov    $0x0,%al
call   1040 <rand@plt>
and    $0x7,%eax
add    $0x30,%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %dl,(%rax,%rcx,1)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    1153 <rand_string+0x33>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rand_string>:
push   %r15
push   %r14
push   %rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0x55555556,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
shr    $0x20,%rcx
add    %edx,%ecx
lea    (%rcx,%rcx,2),%ecx
mov    %eax,%ebx
sub    %ecx,%ebx
neg    %ecx
lea    (%rax,%rcx,1),%edi
add    $0x2,%edi
call   1030 <malloc@plt>
mov    %rax,%r15
inc    %ebx
je     1188 <rand_string+0x68>
mov    %ebx,%r14d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1040 <rand@plt>
and    $0x7,%al
or     $0x30,%al
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1170 <rand_string+0x50>
movb   $0x0,(%r15,%r14,1)
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

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