#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 rand();
int gen_rand_data(char *buf, int len) {
  int i;
  int generated_amount;
  generated_amount = (rand() % len) + 1;
  for (i = 0; i < generated_amount; i++) {
    buf[i] = (rand() % 255) + 1;
  }
  return generated_amount;
}


x86 assembly (clang, -O0):
<gen_rand_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    $0x0,%al
call   1030 <rand@plt>
cltd
idivl  -0xc(%rbp)
add    $0x1,%edx
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    116e <gen_rand_data+0x5e>
mov    $0x0,%al
call   1030 <rand@plt>
mov    $0xff,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    1137 <gen_rand_data+0x27>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_rand_data>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%r15
xor    %eax,%eax
call   1030 <rand@plt>
cltd
idiv   %ebp
lea    0x1(%rdx),%r14d
test   %edx,%edx
js     1172 <gen_rand_data+0x62>
mov    %r14d,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
xor    %eax,%eax
call   1030 <rand@plt>
cltq
imul   $0xffffffff80808081,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
shr    $0x7,%ecx
add    %edx,%ecx
add    %eax,%ecx
add    $0x1,%cl
mov    %cl,(%r15,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%rbp
jne    1140 <gen_rand_data+0x30>
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @gen_rand_data(i8* nocapture writeonly %0, i32 %1) {
%3 = tail call i32 (...) @rand()
%4 = srem i32 %3, %1
%5 = icmp slt i32 %4, 0
%6 = add i32 %4, 1
br i1 %5, label %18, label %7
7: ; preds = %2
%8 = zext i32 %6 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %16, %9 ]
%11 = tail call i32 (...) @rand()
%12 = srem i32 %11, 255
%13 = trunc i32 %12 to i8
%14 = add i8 %13, 1
%15 = getelementptr inbounds i8, i8* %0, 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, %2
ret i32 %6
}