#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 int time_t;
struct timeval {
  int tv_usec;
  long tv_sec;
};
typedef long off_t;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int elapsed_time_count(struct timeval *);
int /*<<< orphan*/ errno;
scalar_t__ select(int /*<<< orphan*/, int /*<<< orphan*/ *,
                  int /*<<< orphan*/ *, int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ syslog(int /*<<< orphan*/, char *, int /*<<< orphan*/);
int /*<<< orphan*/ transfer_rate_sigmask(int);
void transfer_rate_throttle(off_t byte_count, struct timeval *tvsince,
                            off_t rate) {
  time_t ideal_time = 0, elapsed_time = 0;
  elapsed_time = elapsed_time_count(tvsince);
  ideal_time = byte_count * 1000L / rate;
  if (ideal_time > elapsed_time) {
    struct timeval tvdelay;
    tvdelay.tv_usec = (ideal_time - elapsed_time) * 1000;
    tvdelay.tv_sec = tvdelay.tv_usec / 1000000L;
    tvdelay.tv_usec = tvdelay.tv_usec % 1000000L;
    transfer_rate_sigmask(1);
    if (select(0, NULL, NULL, NULL, &tvdelay) < 0)
      syslog(LOG_ERR, "warning: unable to throttle bandwidth: %s",
             strerror(errno));
    transfer_rate_sigmask(0);
  }
  return;
}


x86 assembly (clang, -O0):
<transfer_rate_throttle>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <elapsed_time_count@plt>
mov    %eax,-0x20(%rbp)
imul   $0x3e8,-0x8(%rbp),%rax
cqto
idivq  -0x18(%rbp)
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    122a <transfer_rate_throttle+0xda>
mov    -0x1c(%rbp),%eax
sub    -0x20(%rbp),%eax
imul   $0x3e8,%eax,%eax
mov    %eax,-0x30(%rbp)
movslq -0x30(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %rax,-0x28(%rbp)
movslq -0x30(%rbp),%rax
mov    $0xf4240,%ecx
cqto
idiv   %rcx
mov    %edx,%eax
mov    %eax,-0x30(%rbp)
mov    $0x1,%edi
call   1060 <transfer_rate_sigmask@plt>
xor    %edi,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x30(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1040 <select@plt>
cmp    $0x0,%rax
jge    1223 <transfer_rate_throttle+0xd3>
mov    0x2dd1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
mov    -0x34(%rbp),%edi
mov    %eax,%edx
lea    0xde2(%rip),%rsi
call   1050 <syslog@plt>
xor    %edi,%edi
call   1060 <transfer_rate_sigmask@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<transfer_rate_throttle>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rdi,%rbx
mov    %rsi,%rdi
call   1030 <elapsed_time_count@plt>
mov    %eax,%ecx
imul   $0x3e8,%rbx,%rax
mov    %rax,%rdx
or     %r14,%rdx
shr    $0x20,%rdx
je     1188 <transfer_rate_throttle+0x38>
cqto
idiv   %r14
cmp    %eax,%ecx
jl     1195 <transfer_rate_throttle+0x45>
jmp    122c <transfer_rate_throttle+0xdc>
xor    %edx,%edx
div    %r14d
cmp    %eax,%ecx
jge    122c <transfer_rate_throttle+0xdc>
sub    %ecx,%eax
imul   $0x3e8,%eax,%ecx
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rax
add    %edx,%eax
cltq
mov    %rax,0x10(%rsp)
movslq %ecx,%rax
imul   $0x431bde83,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x32,%rcx
add    %edx,%ecx
imul   $0xf4240,%ecx,%ecx
sub    %ecx,%eax
mov    %eax,0x8(%rsp)
mov    $0x1,%edi
call   1060 <transfer_rate_sigmask@plt>
lea    0x8(%rsp),%r8
xor    %edi,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1040 <select@plt>
test   %rax,%rax
jns    1225 <transfer_rate_throttle+0xd5>
mov    0x2dcb(%rip),%rax
mov    (%rax),%ebx
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
call   1070 <strerror@plt>
lea    0xde4(%rip),%rsi
mov    %ebx,%edi
mov    %eax,%edx
call   1050 <syslog@plt>
xor    %edi,%edi
call   1060 <transfer_rate_sigmask@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@LOG_ERR = global i32 0
@.str = [42 x i8] c"warning: unable to throttle bandwidth: %s\00"
@errno = global i32 0
define void @transfer_rate_throttle(i64 %0, %struct.timeval* %1, i64 %2) {
%4 = alloca i64
%5 = alloca %struct.timeval*
%6 = alloca i64
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.timeval
store i64 %0, i64* %4
store %struct.timeval* %1, %struct.timeval** %5
store i64 %2, i64* %6
store i32 0, i32* %7
store i32 0, i32* %8
%10 = load %struct.timeval*, %struct.timeval** %5
%11 = call i32 @elapsed_time_count(%struct.timeval* %10)
store i32 %11, i32* %8
%12 = load i64, i64* %4
%13 = mul nsw i64 %12, 1000
%14 = load i64, i64* %6
%15 = sdiv i64 %13, %14
%16 = trunc i64 %15 to i32
store i32 %16, i32* %7
%17 = load i32, i32* %7
%18 = load i32, i32* %8
%19 = icmp sgt i32 %17, %18
br i1 %19, label %20, label %47
20: ; preds = %3
%21 = load i32, i32* %7
%22 = load i32, i32* %8
%23 = sub nsw i32 %21, %22
%24 = mul nsw i32 %23, 1000
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%27 = load i32, i32* %26
%28 = sext i32 %27 to i64
%29 = sdiv i64 %28, 1000000
%30 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 %29, i64* %30
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
%32 = load i32, i32* %31
%33 = sext i32 %32 to i64
%34 = srem i64 %33, 1000000
%35 = trunc i64 %34 to i32
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 %35, i32* %36
%37 = call i32 @transfer_rate_sigmask(i32 1)
%38 = call i64 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %9)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %20
%41 = load i32, i32* @LOG_ERR
%42 = load i32, i32* @errno
%43 = call i32 @strerror(i32 %42)
%44 = call i32 @syslog(i32 %41, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %43)
br label %45
45: ; preds = %40, %20
%46 = call i32 @transfer_rate_sigmask(i32 0)
br label %47
47: ; preds = %45, %3
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@LOG_ERR = global i32 0
@.str = [42 x i8] c"warning: unable to throttle bandwidth: %s\00"
@errno = global i32 0
define void @transfer_rate_throttle(i64 %0, %struct.timeval* %1, i64 %2) {
%4 = alloca %struct.timeval
%5 = tail call i32 @elapsed_time_count(%struct.timeval* %1)
%6 = mul nsw i64 %0, 1000
%7 = sdiv i64 %6, %2
%8 = trunc i64 %7 to i32
%9 = icmp slt i32 %5, %8
br i1 %9, label %10, label %29
10: ; preds = %3
%11 = bitcast %struct.timeval* %4 to i8*
%12 = sub nsw i32 %8, %5
%13 = mul nsw i32 %12, 1000
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%15 = sdiv i32 %12, 1000
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 1
store i64 %16, i64* %17
%18 = srem i32 %13, 1000000
store i32 %18, i32* %14
%19 = tail call i32 @transfer_rate_sigmask(i32 1)
%20 = call i64 @select(i32 0, i32* null, i32* null, i32* null, %struct.timeval* %4)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %10
%23 = load i32, i32* @LOG_ERR
%24 = load i32, i32* @errno
%25 = call i32 @strerror(i32 %24)
%26 = call i32 @syslog(i32 %23, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %22, %10
%28 = call i32 @transfer_rate_sigmask(i32 0)
br label %29
29: ; preds = %27, %3
ret void
}