#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 {
  scalar_t__ tv_sec;
  scalar_t__ tv_usec;
};

/* Variables and functions */
int INT_MAX;
scalar_t__ __predict_true(int);
int /*<<< orphan*/ getmicrouptime(struct timeval *);
int /*<<< orphan*/ timersub(struct timeval *, struct timeval *,
                            struct timeval *);
int ppsratecheck(struct timeval *lasttime, int *curpps, int maxpps) {
  struct timeval tv, delta;
  int rv;
  getmicrouptime(&tv);
  timersub(&tv, lasttime, &delta);
  if ((lasttime->tv_sec == 0 && lasttime->tv_usec == 0) || delta.tv_sec >= 1) {
    *lasttime = tv;
    *curpps = 0;
  }
  if (maxpps < 0)
    rv = 1;
  else if (*curpps < maxpps)
    rv = 1;
  else
    rv = 0;
  if (__predict_true(*curpps != INT_MAX))
    *curpps = *curpps + 1;
  return (rv);
}


x86 assembly (clang, -O0):
<ppsratecheck>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
lea    -0x28(%rbp),%rdi
call   1030 <getmicrouptime@plt>
mov    -0x8(%rbp),%rsi
lea    -0x28(%rbp),%rdi
lea    -0x38(%rbp),%rdx
call   1050 <timersub@plt>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    117a <ppsratecheck+0x4a>
mov    -0x8(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     1185 <ppsratecheck+0x55>
cmpq   $0x1,-0x38(%rbp)
jl     11a2 <ppsratecheck+0x72>
mov    -0x8(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rcx
mov    %rcx,0x8(%rax)
mov    -0x10(%rbp),%rax
movl   $0x0,(%rax)
cmpl   $0x0,-0x14(%rbp)
jge    11b8 <ppsratecheck+0x88>
movl   $0x1,-0x3c(%rbp)
jmp    11df <ppsratecheck+0xaf>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
cmp    -0x14(%rbp),%eax
jge    11d3 <ppsratecheck+0xa3>
movl   $0x1,-0x3c(%rbp)
jmp    11da <ppsratecheck+0xaa>
movl   $0x0,-0x3c(%rbp)
jmp    11df <ppsratecheck+0xaf>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    0x2dfc(%rip),%rcx
cmp    (%rcx),%eax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <__predict_true@plt>
cmp    $0x0,%rax
je     1214 <ppsratecheck+0xe4>
mov    -0x10(%rbp),%rax
mov    (%rax),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x3c(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<ppsratecheck>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x28,%rsp
mov    %edx,%r14d
mov    %rsi,%r15
mov    %rdi,%rbp
lea    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1030 <getmicrouptime@plt>
lea    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    %rbp,%rsi
call   1050 <timersub@plt>
cmpq   $0x0,0x0(%rbp)
jne    116e <ppsratecheck+0x3e>
cmpq   $0x0,0x8(%rbp)
je     1176 <ppsratecheck+0x46>
cmpq   $0x0,0x8(%rsp)
jle    1186 <ppsratecheck+0x56>
movups 0x18(%rsp),%xmm0
movups %xmm0,0x0(%rbp)
movl   $0x0,(%r15)
mov    (%r15),%ebx
mov    0x2e58(%rip),%rax
xor    %edi,%edi
cmp    (%rax),%ebx
setne  %dil
call   1040 <__predict_true@plt>
test   %rax,%rax
je     11a6 <ppsratecheck+0x76>
addl   $0x1,(%r15)
test   %r14d,%r14d
sets   %al
cmp    %r14d,%ebx
setl   %cl
or     %al,%cl
movzbl %cl,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@INT_MAX = global i32 0
define i32 @ppsratecheck(%struct.timeval* %0, i32* %1, i32 %2) {
%4 = alloca %struct.timeval*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
%9 = alloca i32
store %struct.timeval* %0, %struct.timeval** %4
store i32* %1, i32** %5
store i32 %2, i32* %6
%10 = call i32 @getmicrouptime(%struct.timeval* %7)
%11 = load %struct.timeval*, %struct.timeval** %4
%12 = call i32 @timersub(%struct.timeval* %7, %struct.timeval* %11, %struct.timeval* %8)
%13 = load %struct.timeval*, %struct.timeval** %4
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %13, i32 0, i32 0
%15 = load i64, i64* %14
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %22
17: ; preds = %3
%18 = load %struct.timeval*, %struct.timeval** %4
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 1
%20 = load i64, i64* %19
%21 = icmp eq i64 %20, 0
br i1 %21, label %26, label %22
22: ; preds = %17, %3
%23 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%24 = load i64, i64* %23
%25 = icmp sge i64 %24, 1
br i1 %25, label %26, label %31
26: ; preds = %22, %17
%27 = load %struct.timeval*, %struct.timeval** %4
%28 = bitcast %struct.timeval* %27 to i8*
%29 = bitcast %struct.timeval* %7 to i8*
%30 = load i32*, i32** %5
store i32 0, i32* %30
br label %31
31: ; preds = %26, %22
%32 = load i32, i32* %6
%33 = icmp slt i32 %32, 0
br i1 %33, label %34, label %35
34: ; preds = %31
store i32 1, i32* %9
br label %43
35: ; preds = %31
%36 = load i32*, i32** %5
%37 = load i32, i32* %36
%38 = load i32, i32* %6
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %41
40: ; preds = %35
store i32 1, i32* %9
br label %42
41: ; preds = %35
store i32 0, i32* %9
br label %42
42: ; preds = %41, %40
br label %43
43: ; preds = %42, %34
%44 = load i32*, i32** %5
%45 = load i32, i32* %44
%46 = load i32, i32* @INT_MAX
%47 = icmp ne i32 %45, %46
%48 = zext i1 %47 to i32
%49 = call i64 @__predict_true(i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %56
51: ; preds = %43
%52 = load i32*, i32** %5
%53 = load i32, i32* %52
%54 = add nsw i32 %53, 1
%55 = load i32*, i32** %5
store i32 %54, i32* %55
br label %56
56: ; preds = %51, %43
%57 = load i32, i32* %9
ret i32 %57
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@INT_MAX = global i32 0
define i32 @ppsratecheck(%struct.timeval* %0, i32* nocapture %1, i32 %2) {
%4 = alloca %struct.timeval
%5 = alloca %struct.timeval
%6 = bitcast %struct.timeval* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = call i32 @getmicrouptime(%struct.timeval* %4)
%9 = call i32 @timersub(%struct.timeval* %4, %struct.timeval* %0, %struct.timeval* %5)
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%11 = load i64, i64* %10
%12 = icmp eq i64 %11, 0
br i1 %12, label %13, label %21
13: ; preds = %3
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%15 = load i64, i64* %14
%16 = icmp eq i64 %15, 0
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%18 = load i64, i64* %17
%19 = icmp sgt i64 %18, 0
%20 = select i1 %16, i1 true, i1 %19
br i1 %20, label %25, label %27
21: ; preds = %3
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
%23 = load i64, i64* %22
%24 = icmp sgt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %21, %13
%26 = bitcast %struct.timeval* %0 to i8*
store i32 0, i32* %1
br label %27
27: ; preds = %13, %25, %21
%28 = load i32, i32* %1
%29 = load i32, i32* @INT_MAX
%30 = icmp ne i32 %28, %29
%31 = zext i1 %30 to i32
%32 = call i64 @__predict_true(i32 %31)
%33 = icmp eq i64 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %27
%35 = load i32, i32* %1
%36 = add nsw i32 %35, 1
store i32 %36, i32* %1
br label %37
37: ; preds = %34, %27
%38 = icmp slt i32 %2, 0
%39 = icmp slt i32 %28, %2
%40 = select i1 %38, i1 true, i1 %39
%41 = zext i1 %40 to i32
ret i32 %41
}