#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 */
typedef scalar_t__ ssize_t;

/* Variables and functions */
int /*<<< orphan*/ perror(char *);
int rand();
scalar_t__ write(int const, char *, unsigned int);
size_t do_rnd_write(const int fd, char *buf, const size_t len) {
  unsigned int do_w;
  ssize_t bw;
  do_w = rand() & 0xffff;
  if (do_w == 0 || do_w > len)
    do_w = len;
  bw = write(fd, buf, do_w);
  if (bw < 0)
    perror("write");
  return bw;
}


x86 assembly (clang, -O0):
<do_rnd_write>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    $0x0,%al
call   1050 <rand@plt>
and    $0xffff,%eax
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1169 <do_rnd_write+0x39>
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%rax
jbe    1170 <do_rnd_write+0x40>
mov    -0x18(%rbp),%rax
mov    %eax,-0x1c(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <write@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jge    119a <do_rnd_write+0x6a>
lea    0xe6b(%rip),%rdi
call   1040 <perror@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_rnd_write>:
push   %rbp
push   %r14
push   %rbx
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%ebp
xor    %eax,%eax
call   1050 <rand@plt>
movzwl %ax,%eax
cmp    %rbx,%rax
mov    %eax,%edx
cmova  %ebx,%edx
test   %eax,%eax
cmove  %ebx,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <write@plt>
mov    %rax,%rbx
test   %rax,%rax
js     116d <do_rnd_write+0x3d>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe8c(%rip),%rdi
call   1040 <perror@plt>
jmp    1165 <do_rnd_write+0x35>

LLVM IR (-O0):
@.str = [6 x i8] c"write\00"
define i64 @do_rnd_write(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
%9 = call i32 (...) @rand()
%10 = and i32 %9, 65535
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, 0
br i1 %12, label %18, label %13
13: ; preds = %3
%14 = load i32, i32* %7
%15 = zext i32 %14 to i64
%16 = load i64, i64* %6
%17 = icmp ugt i64 %15, %16
br i1 %17, label %18, label %21
18: ; preds = %13, %3
%19 = load i64, i64* %6
%20 = trunc i64 %19 to i32
store i32 %20, i32* %7
br label %21
21: ; preds = %18, %13
%22 = load i32, i32* %4
%23 = load i8*, i8** %5
%24 = load i32, i32* %7
%25 = call i64 @write(i32 %22, i8* %23, i32 %24)
store i64 %25, i64* %8
%26 = load i64, i64* %8
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %21
%31 = load i64, i64* %8
ret i64 %31
}

LLVM IR (-O2):
@.str = [6 x i8] c"write\00"
define i64 @do_rnd_write(i32 %0, i8* nocapture %1, i64 %2) {
%4 = tail call i32 (...) @rand()
%5 = and i32 %4, 65535
%6 = icmp eq i32 %5, 0
%7 = zext i32 %5 to i64
%8 = icmp ugt i64 %7, %2
%9 = or i1 %6, %8
%10 = trunc i64 %2 to i32
%11 = select i1 %9, i32 %10, i32 %5
%12 = tail call i64 @write(i32 %0, i8* %1, i32 %11)
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %3
%15 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %16
16: ; preds = %14, %3
ret i64 %12
}