#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(size_t, size_t);
int /*<<< orphan*/ memset(char *, char, size_t);
int random();
char *random_buf(size_t len, size_t typesiz) {
  char *buf = calloc(len + 1, typesiz);
  memset(buf, (random() % 255) + 1, len * typesiz);
  memset(buf + (len * typesiz), '\0', typesiz);
  return buf;
}


x86 assembly (clang, -O0):
<random_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
add    $0x1,%rdi
mov    -0x10(%rbp),%rsi
call   1040 <calloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1050 <random@plt>
mov    -0x20(%rbp),%rdi
mov    $0xff,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %dl,%al
mov    -0x8(%rbp),%rdx
imul   -0x10(%rbp),%rdx
movsbl %al,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rax
imul   -0x10(%rbp),%rax
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1030 <memset@plt>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<random_buf>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%rbx
add    $0x1,%rdi
call   1040 <calloc@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1050 <random@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
imul   %r14,%rbx
movsbl %cl,%esi
mov    %r15,%rdi
mov    %rbx,%rdx
call   1030 <memset@plt>
add    %r15,%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    %r14,%rdx
call   1030 <memset@plt>
mov    %r15,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define i8* @random_buf(i64 %0, i64 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
store i64 %0, i64* %3
store i64 %1, i64* %4
%6 = load i64, i64* %3
%7 = add i64 %6, 1
%8 = load i64, i64* %4
%9 = call i8* @calloc(i64 %7, i64 %8)
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = call i32 (...) @random()
%12 = srem i32 %11, 255
%13 = add nsw i32 %12, 1
%14 = trunc i32 %13 to i8
%15 = load i64, i64* %3
%16 = load i64, i64* %4
%17 = mul i64 %15, %16
%18 = call i32 @memset(i8* %10, i8 signext %14, i64 %17)
%19 = load i8*, i8** %5
%20 = load i64, i64* %3
%21 = load i64, i64* %4
%22 = mul i64 %20, %21
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i64, i64* %4
%25 = call i32 @memset(i8* %23, i8 signext 0, i64 %24)
%26 = load i8*, i8** %5
ret i8* %26
}

LLVM IR (-O2):
define i8* @random_buf(i64 %0, i64 %1) {
%3 = add i64 %0, 1
%4 = tail call i8* @calloc(i64 %3, i64 %1)
%5 = tail call i32 (...) @random()
%6 = srem i32 %5, 255
%7 = trunc i32 %6 to i8
%8 = add i8 %7, 1
%9 = mul i64 %1, %0
%10 = tail call i32 @memset(i8* %4, i8 signext %8, i64 %9)
%11 = getelementptr inbounds i8, i8* %4, i64 %9
%12 = tail call i32 @memset(i8* %11, i8 signext 0, i64 %1)
ret i8* %4
}