#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 */
struct timeval {
  unsigned int tv_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
struct timeval *malloc(int);
int /*<<< orphan*/ timeval_subtract(struct timeval *, struct timeval *,
                                    struct timeval *);
int /*<<< orphan*/ usleep(int);
void sleep_till_next_packet(unsigned int pps) {
  static struct timeval *lastvisit = NULL;
  struct timeval now, next, wait;
  unsigned int tbp;
  if (!pps)
    return;
  if (!lastvisit) {
    lastvisit = malloc(sizeof(struct timeval));
    gettimeofday(lastvisit, NULL);
    return;
  }
  next.tv_sec = lastvisit->tv_sec;
  next.tv_usec = lastvisit->tv_usec;
  tbp = 1000000 / pps;
  next.tv_usec += tbp;
  if (next.tv_usec > 999999) {
    next.tv_usec -= 1000000;
    next.tv_sec++;
  }
  gettimeofday(&now, NULL);
  if (!timeval_subtract(&wait, &next, &now))
    usleep(wait.tv_usec);
  gettimeofday(lastvisit, NULL);
}


x86 assembly (clang, -O0):
<sleep_till_next_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    115a <sleep_till_next_packet+0x1a>
jmp    121d <sleep_till_next_packet+0xdd>
cmpq   $0x0,0x2ee6(%rip)
jne    118e <sleep_till_next_packet+0x4e>
mov    $0x8,%edi
call   1050 <malloc@plt>
mov    %rax,0x2ecf(%rip)
mov    0x2ec8(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
jmp    121d <sleep_till_next_packet+0xdd>
mov    0x2eb3(%rip),%rax
mov    0x4(%rax),%eax
mov    %eax,-0x14(%rbp)
mov    0x2ea6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    $0xf4240,%eax
xor    %edx,%edx
divl   -0x4(%rbp)
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
cmpl   $0xf423f,-0x18(%rbp)
jbe    11de <sleep_till_next_packet+0x9e>
mov    -0x18(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
lea    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
lea    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x10(%rbp),%rdx
call   1040 <timeval_subtract@plt>
cmp    $0x0,%eax
jne    120d <sleep_till_next_packet+0xcd>
mov    -0x20(%rbp),%edi
call   1060 <usleep@plt>
mov    0x2e34(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <gettimeofday@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sleep_till_next_packet>:
push   %rbx
sub    $0x20,%rsp
test   %edi,%edi
je     11be <sleep_till_next_packet+0x7e>
mov    0x2ef8(%rip),%rsi
test   %rsi,%rsi
je     11c4 <sleep_till_next_packet+0x84>
mov    0x4(%rsi),%ecx
mov    %ecx,0xc(%rsp)
mov    $0xf4240,%eax
xor    %edx,%edx
div    %edi
add    (%rsi),%eax
mov    %eax,0x8(%rsp)
cmp    $0xf4240,%eax
jb     1182 <sleep_till_next_packet+0x42>
add    $0xfff0bdc0,%eax
mov    %eax,0x8(%rsp)
add    $0x1,%ecx
mov    %ecx,0xc(%rsp)
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
lea    0x10(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    %rbx,%rdx
call   1040 <timeval_subtract@plt>
test   %eax,%eax
jne    11b0 <sleep_till_next_packet+0x70>
mov    0x10(%rsp),%edi
call   1060 <usleep@plt>
mov    0x2e91(%rip),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
add    $0x20,%rsp
pop    %rbx
ret
mov    $0x8,%edi
call   1050 <malloc@plt>
mov    %rax,0x2e73(%rip)
mov    %rax,%rdi
xor    %esi,%esi
add    $0x20,%rsp
pop    %rbx
jmp    1030 <gettimeofday@plt>

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@sleep_till_next_packet.lastvisit = internal global %struct.timeval* null
define void @sleep_till_next_packet(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = alloca i32
store i32 %0, i32* %2
%7 = load i32, i32* %2
%8 = icmp ne i32 %7, 0
br i1 %8, label %10, label %9
9: ; preds = %1
br label %53
10: ; preds = %1
%11 = load %struct.timeval*, %struct.timeval** @sleep_till_next_packet.lastvisit
%12 = icmp ne %struct.timeval* %11, null
br i1 %12, label %17, label %13
13: ; preds = %10
%14 = call %struct.timeval* @malloc(i32 8)
store %struct.timeval* %14, %struct.timeval** @sleep_till_next_packet.lastvisit
%15 = load %struct.timeval*, %struct.timeval** @sleep_till_next_packet.lastvisit
%16 = call i32 @gettimeofday(%struct.timeval* %15, i32* null)
br label %53
17: ; preds = %10
%18 = load %struct.timeval*, %struct.timeval** @sleep_till_next_packet.lastvisit
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 1
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i32 %20, i32* %21
%22 = load %struct.timeval*, %struct.timeval** @sleep_till_next_packet.lastvisit
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %22, i32 0, i32 0
%24 = load i32, i32* %23
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 %24, i32* %25
%26 = load i32, i32* %2
%27 = udiv i32 1000000, %26
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%30 = load i32, i32* %29
%31 = add i32 %30, %28
store i32 %31, i32* %29
%32 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%33 = load i32, i32* %32
%34 = icmp ugt i32 %33, 999999
br i1 %34, label %35, label %42
35: ; preds = %17
%36 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
%37 = load i32, i32* %36
%38 = sub i32 %37, 1000000
store i32 %38, i32* %36
%39 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
%40 = load i32, i32* %39
%41 = add nsw i32 %40, 1
store i32 %41, i32* %39
br label %42
42: ; preds = %35, %17
%43 = call i32 @gettimeofday(%struct.timeval* %3, i32* null)
%44 = call i32 @timeval_subtract(%struct.timeval* %5, %struct.timeval* %4, %struct.timeval* %3)
%45 = icmp ne i32 %44, 0
br i1 %45, label %50, label %46
46: ; preds = %42
%47 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%48 = load i32, i32* %47
%49 = call i32 @usleep(i32 %48)
br label %50
50: ; preds = %46, %42
%51 = load %struct.timeval*, %struct.timeval** @sleep_till_next_packet.lastvisit
%52 = call i32 @gettimeofday(%struct.timeval* %51, i32* null)
br label %53
53: ; preds = %50, %13, %9
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
@sleep_till_next_packet.lastvisit = internal unnamed_addr global %struct.timeval* null
define void @sleep_till_next_packet(i32 %0) {
%2 = alloca %struct.timeval
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = bitcast %struct.timeval* %2 to i8*
%6 = bitcast %struct.timeval* %3 to i8*
%7 = bitcast %struct.timeval* %4 to i8*
%8 = icmp eq i32 %0, 0
br i1 %8, label %39, label %9
9: ; preds = %1
%10 = load %struct.timeval*, %struct.timeval** @sleep_till_next_packet.lastvisit
%11 = icmp eq %struct.timeval* %10, null
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = tail call %struct.timeval* @malloc(i32 8)
store %struct.timeval* %13, %struct.timeval** @sleep_till_next_packet.lastvisit
%14 = tail call i32 @gettimeofday(%struct.timeval* %13, i32* null)
br label %39
15: ; preds = %9
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i64 0, i32 1
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i32 %17, i32* %18
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %10, i64 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
%22 = udiv i32 1000000, %0
%23 = add i32 %22, %20
store i32 %23, i32* %21
%24 = icmp ugt i32 %23, 999999
br i1 %24, label %25, label %28
25: ; preds = %15
%26 = add i32 %23, -1000000
store i32 %26, i32* %21
%27 = add nsw i32 %17, 1
store i32 %27, i32* %18
br label %28
28: ; preds = %25, %15
%29 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%30 = call i32 @timeval_subtract(%struct.timeval* %4, %struct.timeval* %3, %struct.timeval* %2)
%31 = icmp eq i32 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %28
%33 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i64 0, i32 0
%34 = load i32, i32* %33
%35 = call i32 @usleep(i32 %34)
br label %36
36: ; preds = %32, %28
%37 = load %struct.timeval*, %struct.timeval** @sleep_till_next_packet.lastvisit
%38 = call i32 @gettimeofday(%struct.timeval* %37, i32* null)
br label %39
39: ; preds = %1, %36, %12
ret void
}