#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 /*<<< orphan*/ memcpy(void *, int *, size_t);
void *rand();
void randomize_buffer(void *buf, size_t buflen) {
  int *p = (int *)buf;
  size_t words = buflen / sizeof(int);
  size_t leftover = buflen % sizeof(int);
  if (!buflen)
    return;
  while (words--)
    *p++ = rand();
  if (leftover) {
    int tmp = rand();
    memcpy(buf + buflen - leftover, &tmp, leftover);
  }
  return;
}


x86 assembly (clang, -O0):
<randomize_buffer>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
shr    $0x2,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
and    $0x3,%rax
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1160 <randomize_buffer+0x40>
jmp    11d5 <randomize_buffer+0xb5>
jmp    1165 <randomize_buffer+0x45>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0xffffffffffffffff,%rcx
mov    %rcx,-0x20(%rbp)
cmp    $0x0,%rax
je     119d <randomize_buffer+0x7d>
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x4,%rdx
mov    %rdx,-0x18(%rbp)
mov    %ecx,(%rax)
jmp    1165 <randomize_buffer+0x45>
cmpq   $0x0,-0x28(%rbp)
je     11d0 <randomize_buffer+0xb0>
mov    $0x0,%al
call   1040 <rand@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
add    -0x10(%rbp),%rdi
xor    %eax,%eax
sub    -0x28(%rbp),%rax
add    %rax,%rdi
mov    -0x28(%rbp),%rdx
lea    -0x2c(%rbp),%rsi
call   1030 <memcpy@plt>
jmp    11d5 <randomize_buffer+0xb5>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<randomize_buffer>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     118a <randomize_buffer+0x6a>
mov    %rsi,%r15
mov    %rdi,%r12
mov    %r15d,%r14d
and    $0x3,%r14d
cmp    $0x4,%rsi
jb     1164 <randomize_buffer+0x44>
mov    %r15,%r13
shr    $0x2,%r13
xor    %ebx,%ebx
xchg   %ax,%ax
xor    %eax,%eax
call   1040 <rand@plt>
mov    %eax,(%r12,%rbx,4)
add    $0x1,%rbx
cmp    %rbx,%r13
jne    1150 <randomize_buffer+0x30>
test   %r14,%r14
je     118a <randomize_buffer+0x6a>
xor    %eax,%eax
call   1040 <rand@plt>
mov    %eax,0xc(%rsp)
add    %r15,%r12
sub    %r14,%r12
lea    0xc(%rsp),%rsi
mov    %r12,%rdi
mov    %r14,%rdx
call   1030 <memcpy@plt>
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
define void @randomize_buffer(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i32
store i8* %0, i8** %3
store i64 %1, i64* %4
%9 = load i8*, i8** %3
%10 = bitcast i8* %9 to i32*
store i32* %10, i32** %5
%11 = load i64, i64* %4
%12 = udiv i64 %11, 4
store i64 %12, i64* %6
%13 = load i64, i64* %4
%14 = urem i64 %13, 4
store i64 %14, i64* %7
%15 = load i64, i64* %4
%16 = icmp ne i64 %15, 0
br i1 %16, label %18, label %17
17: ; preds = %2
br label %43
18: ; preds = %2
br label %19
19: ; preds = %23, %18
%20 = load i64, i64* %6
%21 = add i64 %20, -1
store i64 %21, i64* %6
%22 = icmp ne i64 %20, 0
br i1 %22, label %23, label %28
23: ; preds = %19
%24 = call i8* (...) @rand()
%25 = ptrtoint i8* %24 to i32
%26 = load i32*, i32** %5
%27 = getelementptr inbounds i32, i32* %26, i32 1
store i32* %27, i32** %5
store i32 %25, i32* %26
br label %19
28: ; preds = %19
%29 = load i64, i64* %7
%30 = icmp ne i64 %29, 0
br i1 %30, label %31, label %42
31: ; preds = %28
%32 = call i8* (...) @rand()
%33 = ptrtoint i8* %32 to i32
store i32 %33, i32* %8
%34 = load i8*, i8** %3
%35 = load i64, i64* %4
%36 = getelementptr i8, i8* %34, i64 %35
%37 = load i64, i64* %7
%38 = sub i64 0, %37
%39 = getelementptr i8, i8* %36, i64 %38
%40 = load i64, i64* %7
%41 = call i32 @memcpy(i8* %39, i32* %8, i64 %40)
br label %42
42: ; preds = %31, %28
br label %43
43: ; preds = %42, %17
ret void
}

LLVM IR (-O2):
define void @randomize_buffer(i8* %0, i64 %1) {
%3 = alloca i32
%4 = and i64 %1, 3
%5 = icmp eq i64 %1, 0
br i1 %5, label %31, label %6
6: ; preds = %2
%7 = icmp ult i64 %1, 4
br i1 %7, label %20, label %8
8: ; preds = %6
%9 = bitcast i8* %0 to i32*
%10 = lshr i64 %1, 2
br label %11
11: ; preds = %8, %11
%12 = phi i32* [ %18, %11 ], [ %9, %8 ]
%13 = phi i64 [ %14, %11 ], [ %10, %8 ]
%14 = add nsw i64 %13, -1
%15 = tail call i8* (...) @rand()
%16 = ptrtoint i8* %15 to i64
%17 = trunc i64 %16 to i32
%18 = getelementptr inbounds i32, i32* %12, i64 1
store i32 %17, i32* %12
%19 = icmp eq i64 %14, 0
br i1 %19, label %20, label %11
20: ; preds = %11, %6
%21 = icmp eq i64 %4, 0
br i1 %21, label %31, label %22
22: ; preds = %20
%23 = bitcast i32* %3 to i8*
%24 = tail call i8* (...) @rand()
%25 = ptrtoint i8* %24 to i64
%26 = trunc i64 %25 to i32
store i32 %26, i32* %3
%27 = getelementptr i8, i8* %0, i64 %1
%28 = sub nsw i64 0, %4
%29 = getelementptr i8, i8* %27, i64 %28
%30 = call i32 @memcpy(i8* %29, i32* %3, i64 %4)
br label %31
31: ; preds = %20, %22, %2
ret void
}