#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ perror(char *);
int rand();
int /*<<< orphan*/ usleep(int);
int write(int, char const *, int);
void send_response(int c, const char *buffer, int len) {
  int n;
  while (len > 0) {
    n = (rand() % 99) + 1;
    if (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    %edx,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jle    11fa <send_response+0xba>
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x63,%ecx
cltd
idiv   %ecx
add    $0x1,%edx
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1183 <send_response+0x43>
mov    -0x14(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%edx
call   1030 <write@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jge    11cf <send_response+0x8f>
mov    0x2e3a(%rip),%rax
mov    (%rax),%rax
mov    0x2e28(%rip),%rcx
cmp    (%rcx),%rax
je     11ca <send_response+0x8a>
lea    0xe40(%rip),%rdi
call   1040 <perror@plt>
jmp    11fa <send_response+0xba>
jmp    11eb <send_response+0xab>
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    $0x2710,%edi
call   1060 <usleep@plt>
jmp    1152 <send_response+0x12>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<send_response>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
test   %edx,%edx
jle    11e4 <send_response+0xa4>
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
mov    0x2e82(%rip),%r15
mov    0x2e73(%rip),%r12
jmp    1185 <send_response+0x45>
nopw   0x0(%rax,%rax,1)
sub    %eax,%ebx
mov    %eax,%eax
add    %rax,%r14
mov    $0x2710,%edi
call   1060 <usleep@plt>
test   %ebx,%ebx
jle    11e4 <send_response+0xa4>
xor    %eax,%eax
call   1050 <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
mov    %eax,%esi
sub    %ecx,%esi
neg    %ecx
lea    (%rax,%rcx,1),%edx
add    $0x1,%edx
cmp    %ebx,%esi
cmovge %ebx,%edx
mov    %ebp,%edi
mov    %r14,%rsi
call   1030 <write@plt>
test   %eax,%eax
jns    1170 <send_response+0x30>
mov    (%r15),%rax
cmp    (%r12),%rax
je     1177 <send_response+0x37>
lea    0xe29(%rip),%rdi
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <perror@plt>
pop    %rbx
pop    %r12
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, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
br label %8
8: ; preds = %42, %3
%9 = load i32, i32* %6
%10 = icmp sgt i32 %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
store i32 %14, i32* %7
%15 = load i32, i32* %7
%16 = load i32, i32* %6
%17 = icmp sgt i32 %15, %16
br i1 %17, label %18, label %20
18: ; preds = %11
%19 = load i32, i32* %6
store i32 %19, i32* %7
br label %20
20: ; preds = %18, %11
%21 = load i32, i32* %4
%22 = load i8*, i8** %5
%23 = load i32, i32* %7
%24 = call i32 @write(i32 %21, i8* %22, i32 %23)
store i32 %24, i32* %7
%25 = load i32, i32* %7
%26 = icmp slt i32 %25, 0
br i1 %26, label %27, label %34
27: ; preds = %20
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINTR
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %33
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %44
33: ; preds = %27
br label %42
34: ; preds = %20
%35 = load i32, i32* %7
%36 = load i32, i32* %6
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %6
%38 = load i32, i32* %7
%39 = load i8*, i8** %5
%40 = sext i32 %38 to i64
%41 = getelementptr inbounds i8, i8* %39, i64 %40
store i8* %41, i8** %5
br label %42
42: ; preds = %34, %33
%43 = call i32 @usleep(i32 10000)
br label %8
44: ; preds = %31, %8
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@.str = [6 x i8] c"write\00"
define void @send_response(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %30
5: ; preds = %3, %25
%6 = phi i32 [ %27, %25 ], [ %2, %3 ]
%7 = phi i8* [ %26, %25 ], [ %1, %3 ]
%8 = tail call i32 (...) @rand()
%9 = srem i32 %8, 99
%10 = add nsw i32 %9, 1
%11 = icmp slt i32 %9, %6
%12 = select i1 %11, i32 %10, i32 %6
%13 = tail call i32 @write(i32 %0, i8* %7, i32 %12)
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %5
%16 = load i64, i64* @errno
%17 = load i64, i64* @EINTR
%18 = icmp eq i64 %16, %17
br i1 %18, label %25, label %19
19: ; preds = %15
%20 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
br label %30
21: ; preds = %5
%22 = sub nsw i32 %6, %13
%23 = zext i32 %13 to i64
%24 = getelementptr inbounds i8, i8* %7, i64 %23
br label %25
25: ; preds = %15, %21
%26 = phi i8* [ %7, %15 ], [ %24, %21 ]
%27 = phi i32 [ %6, %15 ], [ %22, %21 ]
%28 = tail call i32 @usleep(i32 10000)
%29 = icmp sgt i32 %27, 0
br i1 %29, label %5, label %30
30: ; preds = %25, %3, %19
ret void
}