#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 useconds_t;
struct timeval {
  int tv_sec;
  int tv_usec;
};

/* Variables and functions */
scalar_t__ is_timeval_initialized(struct timeval *);
int /*<<< orphan*/ usleep(int);
void wait(struct timeval now, struct timeval *p_prev) {
  if (is_timeval_initialized(p_prev)) {
    *p_prev = now;
    return;
  }
  useconds_t dsec = now.tv_sec - p_prev->tv_sec;
  useconds_t dusec = dsec * 1000000 + (now.tv_usec - p_prev->tv_usec);
  if (dusec > 0) {
    usleep(dusec);
  }
  *p_prev = now;
}


x86 assembly (clang, -O0):
<wait>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <is_timeval_initialized@plt>
cmp    $0x0,%rax
je     1153 <wait+0x33>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    %rcx,(%rax)
jmp    1192 <wait+0x72>
mov    -0x8(%rbp),%eax
mov    -0x10(%rbp),%rcx
sub    (%rcx),%eax
mov    %eax,-0x14(%rbp)
imul   $0xf4240,-0x14(%rbp),%eax
mov    -0x4(%rbp),%ecx
mov    -0x10(%rbp),%rdx
sub    0x4(%rdx),%ecx
add    %ecx,%eax
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    1187 <wait+0x67>
mov    -0x18(%rbp),%edi
call   1040 <usleep@plt>
mov    -0x10(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    %rcx,(%rax)
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%rbx
mov    %rdi,%r14
mov    %rdi,%r15
shr    $0x20,%r15
mov    %rsi,%rdi
call   1030 <is_timeval_initialized@plt>
test   %rax,%rax
jne    1159 <wait+0x39>
mov    %r14d,%eax
sub    (%rbx),%eax
imul   $0xf4240,%eax,%eax
mov    %r15d,%edi
sub    0x4(%rbx),%edi
add    %eax,%edi
jle    1159 <wait+0x39>
call   1040 <usleep@plt>
mov    %r14d,(%rbx)
add    $0x4,%rbx
mov    %r15d,(%rbx)
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
define void @wait(i64 %0, %struct.timeval* %1) {
%3 = alloca %struct.timeval
%4 = alloca %struct.timeval*
%5 = alloca i32
%6 = alloca i32
%7 = bitcast %struct.timeval* %3 to i64*
store i64 %0, i64* %7
store %struct.timeval* %1, %struct.timeval** %4
%8 = load %struct.timeval*, %struct.timeval** %4
%9 = call i64 @is_timeval_initialized(%struct.timeval* %8)
%10 = icmp ne i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %2
%12 = load %struct.timeval*, %struct.timeval** %4
%13 = bitcast %struct.timeval* %12 to i8*
%14 = bitcast %struct.timeval* %3 to i8*
br label %40
15: ; preds = %2
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 0
%17 = load i32, i32* %16
%18 = load %struct.timeval*, %struct.timeval** %4
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %18, i32 0, i32 0
%20 = load i32, i32* %19
%21 = sub nsw i32 %17, %20
store i32 %21, i32* %5
%22 = load i32, i32* %5
%23 = mul nsw i32 %22, 1000000
%24 = getelementptr inbounds %struct.timeval, %struct.timeval* %3, i32 0, i32 1
%25 = load i32, i32* %24
%26 = load %struct.timeval*, %struct.timeval** %4
%27 = getelementptr inbounds %struct.timeval, %struct.timeval* %26, i32 0, i32 1
%28 = load i32, i32* %27
%29 = sub nsw i32 %25, %28
%30 = add nsw i32 %23, %29
store i32 %30, i32* %6
%31 = load i32, i32* %6
%32 = icmp sgt i32 %31, 0
br i1 %32, label %33, label %36
33: ; preds = %15
%34 = load i32, i32* %6
%35 = call i32 @usleep(i32 %34)
br label %36
36: ; preds = %33, %15
%37 = load %struct.timeval*, %struct.timeval** %4
%38 = bitcast %struct.timeval* %37 to i8*
%39 = bitcast %struct.timeval* %3 to i8*
br label %40
40: ; preds = %36, %11
ret void
}

LLVM IR (-O2):
%struct.timeval = type { i32, i32 }
define void @wait(i64 %0, %struct.timeval* %1) {
%3 = trunc i64 %0 to i32
%4 = lshr i64 %0, 32
%5 = trunc i64 %4 to i32
%6 = tail call i64 @is_timeval_initialized(%struct.timeval* %1)
%7 = icmp eq i64 %6, 0
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
br i1 %7, label %11, label %9
9: ; preds = %2
store i32 %3, i32* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
br label %23
11: ; preds = %2
%12 = load i32, i32* %8
%13 = sub nsw i32 %3, %12
%14 = mul nsw i32 %13, 1000000
%15 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%16 = load i32, i32* %15
%17 = sub nsw i32 %5, %16
%18 = add nsw i32 %14, %17
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %11
%21 = tail call i32 @usleep(i32 %18)
br label %22
22: ; preds = %20, %11
store i32 %3, i32* %8
br label %23
23: ; preds = %22, %9
%24 = phi i32* [ %15, %22 ], [ %10, %9 ]
store i32 %5, i32* %24
ret void
}