#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 size_t ssize_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int rand();
int /*<<< orphan*/ usleep(int);
size_t write(int, char const *, size_t);
void send_response(int c, const char *buffer, size_t len) {
  ssize_t n;
  while (len > 0) {
    n = (rand() % 99) + 1;
    if ((size_t)n > len)
      n = len;
    n = write(c, buffer, n);
    if (n < 0) {
      if (errno != EINTR) {
        perror("write");
        return;
      }
    } else {
      len -= n;
      buffer += n;
      usleep(10000);
    }
  }
}


x86 assembly (clang, -O0):
<send_response>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jbe    1206 <send_response+0xc6>
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x63,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
movslq %edx,%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x18(%rbp),%rax
jbe    118d <send_response+0x4d>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rdx
call   1030 <write@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jae    11dc <send_response+0x9c>
mov    0x2e2d(%rip),%rax
mov    (%rax),%rax
mov    0x2e1b(%rip),%rcx
cmp    (%rcx),%rax
je     11d7 <send_response+0x97>
lea    0xe33(%rip),%rdi
call   1040 <perror@plt>
jmp    1206 <send_response+0xc6>
jmp    1201 <send_response+0xc1>
mov    -0x20(%rbp),%rcx
mov    -0x18(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x18(%rbp)
mov    -0x20(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    $0x2710,%edi
call   1060 <usleep@plt>
jmp    1153 <send_response+0x13>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_response>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rdx,%rdx
je     11a4 <send_response+0x74>
mov    %rdx,%rbx
mov    %rsi,%r14
mov    %edi,%r15d
cs nopw 0x0(%rax,%rax,1)
nop
xor    %eax,%eax
call   1040 <rand@plt>
cltq
imul   $0xffffffffa57eb503,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x6,%ecx
add    %edx,%ecx
imul   $0x63,%ecx,%ecx
neg    %ecx
add    %ecx,%eax
add    $0x1,%eax
movslq %eax,%rdx
cmp    %rdx,%rbx
cmovb  %rbx,%rdx
mov    %r15d,%edi
mov    %r14,%rsi
call   1030 <write@plt>
mov    %rax,%rbp
mov    $0x2710,%edi
call   1050 <usleep@plt>
add    %rbp,%r14
sub    %rbp,%rbx
jne    1150 <send_response+0x20>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@.str = [6 x i8] c"write\00"
define void @send_response(i32 %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
store i32 %0, i32* %4
store i8* %1, i8** %5
store i64 %2, i64* %6
br label %8
8: ; preds = %43, %3
%9 = load i64, i64* %6
%10 = icmp ugt i64 %9, 0
br i1 %10, label %11, label %44
11: ; preds = %8
%12 = call i32 (...) @rand()
%13 = srem i32 %12, 99
%14 = add nsw i32 %13, 1
%15 = sext i32 %14 to i64
store i64 %15, i64* %7
%16 = load i64, i64* %7
%17 = load i64, i64* %6
%18 = icmp ugt i64 %16, %17
br i1 %18, label %19, label %21
19: ; preds = %11
%20 = load i64, i64* %6
store i64 %20, i64* %7
br label %21
21: ; preds = %19, %11
%22 = load i32, i32* %4
%23 = load i8*, i8** %5
%24 = load i64, i64* %7
%25 = call i64 @write(i32 %22, i8* %23, i64 %24)
store i64 %25, i64* %7
%26 = load i64, i64* %7
%27 = icmp ult i64 %26, 0
br i1 %27, label %28, label %35
28: ; preds = %21
%29 = load i64, i64* @errno
%30 = load i64, i64* @EINTR
%31 = icmp ne i64 %29, %30
br i1 %31, label %32, label %34
32: ; preds = %28
%33 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %44
34: ; preds = %28
br label %43
35: ; preds = %21
%36 = load i64, i64* %7
%37 = load i64, i64* %6
%38 = sub i64 %37, %36
store i64 %38, i64* %6
%39 = load i64, i64* %7
%40 = load i8*, i8** %5
%41 = getelementptr inbounds i8, i8* %40, i64 %39
store i8* %41, i8** %5
%42 = call i32 @usleep(i32 10000)
br label %43
43: ; preds = %35, %34
br label %8
44: ; preds = %32, %8
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
define void @send_response(i32 %0, i8* nocapture %1, i64 %2) {
%4 = icmp eq i64 %2, 0
br i1 %4, label %19, label %5
5: ; preds = %3, %5
%6 = phi i64 [ %17, %5 ], [ %2, %3 ]
%7 = phi i8* [ %16, %5 ], [ %1, %3 ]
%8 = tail call i32 (...) @rand()
%9 = srem i32 %8, 99
%10 = add nsw i32 %9, 1
%11 = sext i32 %10 to i64
%12 = icmp ult i64 %6, %11
%13 = select i1 %12, i64 %6, i64 %11
%14 = tail call i64 @write(i32 %0, i8* %7, i64 %13)
%15 = tail call i32 @usleep(i32 10000)
%16 = getelementptr inbounds i8, i8* %7, i64 %14
%17 = sub i64 %6, %14
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %5
19: ; preds = %5, %3
ret void
}