#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 {
  int tv_usec;
  scalar_t__ tv_sec;
};
typedef int /*<<< orphan*/ fd_set;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ gettimeofday(struct timeval *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ select(int, int /*<<< orphan*/ *, int /*<<< orphan*/ *,
                          int /*<<< orphan*/ *, struct timeval *);
int /*<<< orphan*/ stderr;
int timeval_subtract(struct timeval *, struct timeval *, struct timeval *);
void wait_timeval_ext(struct timeval *towait, struct timeval *lastburst) {
  static int cpu_warning_issued = 0;
  static int cpu_slowcycles = 0;
  struct timeval endtime;
  endtime.tv_sec = towait->tv_sec + lastburst->tv_sec;
  endtime.tv_usec = towait->tv_usec + lastburst->tv_usec;
  if (endtime.tv_usec > 1000000) {
    endtime.tv_usec -= 1000000;
    endtime.tv_sec++;
  }
  struct timeval curtime;
  if (gettimeofday(&curtime, 0) != 0)
    perror("failed to call gettimeofday");
  struct timeval difftime;
  if (timeval_subtract(&difftime, &endtime, &curtime) == 1) {
    cpu_slowcycles++;
    if (cpu_slowcycles > 100 && !cpu_warning_issued) {
      cpu_warning_issued = 1;
      fprintf(stderr, "WARNING: CPU is too slow to send packets in time, no "
                      "more warnings will be issued\n");
    }
  } else {
    select(1, (fd_set *)0, (fd_set *)0, (fd_set *)0, &difftime);
  }
  if (gettimeofday(lastburst, 0) != 0)
    perror("failed to call gettimeofday");
}


x86 assembly (clang, -O0):
<wait_timeval_ext>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x10(%rbp),%rcx
add    0x8(%rcx),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
mov    -0x10(%rbp),%rcx
add    (%rcx),%eax
mov    %eax,-0x20(%rbp)
cmpl   $0xf4240,-0x20(%rbp)
jle    11a7 <wait_timeval_ext+0x57>
mov    -0x20(%rbp),%eax
sub    $0xf4240,%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
je     11c8 <wait_timeval_ext+0x78>
lea    0xe3d(%rip),%rdi
call   1070 <perror@plt>
lea    -0x40(%rbp),%rdi
lea    -0x20(%rbp),%rsi
lea    -0x30(%rbp),%rdx
call   1050 <timeval_subtract@plt>
cmp    $0x1,%eax
jne    122f <wait_timeval_ext+0xdf>
mov    0x2e68(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2e5f(%rip)
cmpl   $0x64,0x2e58(%rip)
jle    122a <wait_timeval_ext+0xda>
cmpl   $0x0,0x2e47(%rip)
jne    122a <wait_timeval_ext+0xda>
movl   $0x1,0x2e37(%rip)
mov    0x2dc4(%rip),%rax
mov    (%rax),%edi
lea    0xdf7(%rip),%rsi
call   1040 <fprintf@plt>
jmp    1247 <wait_timeval_ext+0xf7>
mov    $0x1,%edi
xor    %eax,%eax
mov    %eax,%ecx
lea    -0x40(%rbp),%r8
mov    %rcx,%rsi
mov    %rcx,%rdx
call   1060 <select@plt>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
cmp    $0x0,%rax
je     1268 <wait_timeval_ext+0x118>
lea    0xd9d(%rip),%rdi
call   1070 <perror@plt>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_timeval_ext>:
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%rbx
mov    0x8(%rsi),%rax
add    0x8(%rdi),%rax
mov    %rax,0x8(%rsp)
mov    (%rsi),%ecx
add    (%rdi),%ecx
mov    %ecx,(%rsp)
cmp    $0xf4241,%ecx
jl     1186 <wait_timeval_ext+0x36>
add    $0xfff0bdc0,%ecx
mov    %ecx,(%rsp)
add    $0x1,%rax
mov    %rax,0x8(%rsp)
lea    0x20(%rsp),%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jne    1213 <wait_timeval_ext+0xc3>
lea    0x10(%rsp),%rdi
mov    %rsp,%rsi
lea    0x20(%rsp),%rdx
call   1050 <timeval_subtract@plt>
cmp    $0x1,%eax
jne    11e9 <wait_timeval_ext+0x99>
mov    0x2e9c(%rip),%eax
lea    0x1(%rax),%ecx
mov    %ecx,0x2e93(%rip)
cmp    $0x64,%eax
jl     11fe <wait_timeval_ext+0xae>
testb  $0x1,0x2e83(%rip)
jne    11fe <wait_timeval_ext+0xae>
movb   $0x1,0x2e7a(%rip)
mov    0x2e07(%rip),%rax
mov    (%rax),%edi
lea    0xe3a(%rip),%rsi
call   1040 <fprintf@plt>
jmp    11fe <wait_timeval_ext+0xae>
lea    0x10(%rsp),%r8
mov    $0x1,%edi
xor    %esi,%esi
xor    %edx,%edx
xor    %ecx,%ecx
call   1060 <select@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <gettimeofday@plt>
test   %rax,%rax
jne    1224 <wait_timeval_ext+0xd4>
add    $0x30,%rsp
pop    %rbx
ret
lea    0xde6(%rip),%rdi
call   1070 <perror@plt>
jmp    1197 <wait_timeval_ext+0x47>
lea    0xdd5(%rip),%rdi
call   1070 <perror@plt>
jmp    120d <wait_timeval_ext+0xbd>

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@wait_timeval_ext.cpu_warning_issued = internal global i32 0
@wait_timeval_ext.cpu_slowcycles = internal global i32 0
@.str = [28 x i8] c"failed to call gettimeofday\00"
@stderr = global i32 0
@.str.1 = [83 x i8] c"WARNING: CPU is too slow to send packets in time, no more warnings will be issued\0A\00"
define void @wait_timeval_ext(%struct.timeval* %0, %struct.timeval* %1) {
%3 = alloca %struct.timeval*
%4 = alloca %struct.timeval*
%5 = alloca %struct.timeval
%6 = alloca %struct.timeval
%7 = alloca %struct.timeval
store %struct.timeval* %0, %struct.timeval** %3
store %struct.timeval* %1, %struct.timeval** %4
%8 = load %struct.timeval*, %struct.timeval** %3
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%10 = load i64, i64* %9
%11 = load %struct.timeval*, %struct.timeval** %4
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %11, i32 0, i32 1
%13 = load i64, i64* %12
%14 = add nsw i64 %10, %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
store i64 %14, i64* %15
%16 = load %struct.timeval*, %struct.timeval** %3
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
%18 = load i32, i32* %17
%19 = load %struct.timeval*, %struct.timeval** %4
%20 = getelementptr inbounds %struct.timeval, %struct.timeval* %19, i32 0, i32 0
%21 = load i32, i32* %20
%22 = add nsw i32 %18, %21
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
store i32 %22, i32* %23
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%25 = load i32, i32* %24
%26 = icmp sgt i32 %25, 1000000
br i1 %26, label %27, label %34
27: ; preds = %2
%28 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 0
%29 = load i32, i32* %28
%30 = sub nsw i32 %29, 1000000
store i32 %30, i32* %28
%31 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i32 0, i32 1
%32 = load i64, i64* %31
%33 = add nsw i64 %32, 1
store i64 %33, i64* %31
br label %34
34: ; preds = %27, %2
%35 = call i64 @gettimeofday(%struct.timeval* %6, i32 0)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %34
%38 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %39
39: ; preds = %37, %34
%40 = call i32 @timeval_subtract(%struct.timeval* %7, %struct.timeval* %5, %struct.timeval* %6)
%41 = icmp eq i32 %40, 1
br i1 %41, label %42, label %54
42: ; preds = %39
%43 = load i32, i32* @wait_timeval_ext.cpu_slowcycles
%44 = add nsw i32 %43, 1
store i32 %44, i32* @wait_timeval_ext.cpu_slowcycles
%45 = load i32, i32* @wait_timeval_ext.cpu_slowcycles
%46 = icmp sgt i32 %45, 100
br i1 %46, label %47, label %53
47: ; preds = %42
%48 = load i32, i32* @wait_timeval_ext.cpu_warning_issued
%49 = icmp ne i32 %48, 0
br i1 %49, label %53, label %50
50: ; preds = %47
store i32 1, i32* @wait_timeval_ext.cpu_warning_issued
%51 = load i32, i32* @stderr
%52 = call i32 @fprintf(i32 %51, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.1, i64 0, i64 0))
br label %53
53: ; preds = %50, %47, %42
br label %56
54: ; preds = %39
%55 = call i32 @select(i32 1, i32* null, i32* null, i32* null, %struct.timeval* %7)
br label %56
56: ; preds = %54, %53
%57 = load %struct.timeval*, %struct.timeval** %4
%58 = call i64 @gettimeofday(%struct.timeval* %57, i32 0)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %62
62: ; preds = %60, %56
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@wait_timeval_ext.cpu_warning_issued = internal unnamed_addr global i1 false
@wait_timeval_ext.cpu_slowcycles = internal unnamed_addr global i32 0
@.str = [28 x i8] c"failed to call gettimeofday\00"
@stderr = global i32 0
@.str.1 = [83 x i8] c"WARNING: CPU is too slow to send packets in time, no more warnings will be issued\0A\00"
define void @wait_timeval_ext(%struct.timeval* nocapture %0, %struct.timeval* %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %3 to i8*
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%8 = load i64, i64* %7
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%10 = load i64, i64* %9
%11 = add nsw i64 %10, %8
%12 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 1
store i64 %11, i64* %12
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%14 = load i32, i32* %13
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%16 = load i32, i32* %15
%17 = add nsw i32 %16, %14
%18 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i64 0, i32 0
store i32 %17, i32* %18
%19 = icmp sgt i32 %17, 1000000
br i1 %19, label %20, label %23
20: ; preds = %2
%21 = add nsw i32 %17, -1000000
store i32 %21, i32* %18
%22 = add nsw i64 %11, 1
store i64 %22, i64* %12
br label %23
23: ; preds = %20, %2
%24 = bitcast %struct.timeval* %4 to i8*
%25 = call i64 @gettimeofday(%struct.timeval* %4, i32 0)
%26 = icmp eq i64 %25, 0
br i1 %26, label %29, label %27
27: ; preds = %23
%28 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %29
29: ; preds = %27, %23
%30 = bitcast %struct.timeval* %5 to i8*
%31 = call i32 @timeval_subtract(%struct.timeval* %5, %struct.timeval* %3, %struct.timeval* %4)
%32 = icmp eq i32 %31, 1
br i1 %32, label %33, label %42
33: ; preds = %29
%34 = load i32, i32* @wait_timeval_ext.cpu_slowcycles
%35 = add nsw i32 %34, 1
store i32 %35, i32* @wait_timeval_ext.cpu_slowcycles
%36 = icmp slt i32 %34, 100
%37 = load i1, i1* @wait_timeval_ext.cpu_warning_issued
%38 = select i1 %36, i1 true, i1 %37
br i1 %38, label %44, label %39
39: ; preds = %33
store i1 true, i1* @wait_timeval_ext.cpu_warning_issued
%40 = load i32, i32* @stderr
%41 = call i32 @fprintf(i32 %40, i8* getelementptr inbounds ([83 x i8], [83 x i8]* @.str.1, i64 0, i64 0))
br label %44
42: ; preds = %29
%43 = call i32 @select(i32 1, i32* null, i32* null, i32* null, %struct.timeval* %5)
br label %44
44: ; preds = %33, %39, %42
%45 = call i64 @gettimeofday(%struct.timeval* %1, i32 0)
%46 = icmp eq i64 %45, 0
br i1 %46, label %49, label %47
47: ; preds = %44
%48 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
ret void
}