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

/* Variables and functions */
scalar_t__ ceil(double);
double modf(double, double *);
scalar_t__ round(double);
int /*<<< orphan*/ timeval_norm(struct timeval *);
void timeval_add_double(struct timeval *tv1, double tv2, int use_ceil) {
  double i, f;
  if (!tv1)
    return;
  f = modf(tv2, &i);
  tv1->tv_sec += i;
  if (use_ceil)
    tv1->tv_usec += ceil(f * 1000000);
  else
    tv1->tv_usec += round(f * 1000000);
  timeval_norm(tv1);
}


x86 assembly (clang, -O0):
<timeval_add_double>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movsd  %xmm0,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    1164 <timeval_add_double+0x24>
jmp    11e6 <timeval_add_double+0xa6>
movsd  -0x10(%rbp),%xmm0
lea    -0x20(%rbp),%rdi
call   1040 <modf@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x20(%rbp),%xmm0
mov    -0x8(%rbp),%rax
addsd  (%rax),%xmm0
movsd  %xmm0,(%rax)
cmpl   $0x0,-0x14(%rbp)
je     11ba <timeval_add_double+0x7a>
movsd  0xe66(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
call   1030 <ceil@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
movslq 0x8(%rax),%rcx
add    %rdx,%rcx
mov    %ecx,0x8(%rax)
jmp    11dd <timeval_add_double+0x9d>
movsd  0xe3e(%rip),%xmm0
mulsd  -0x28(%rbp),%xmm0
call   1060 <round@plt>
mov    %rax,%rdx
mov    -0x8(%rbp),%rax
movslq 0x8(%rax),%rcx
add    %rdx,%rcx
mov    %ecx,0x8(%rax)
mov    -0x8(%rbp),%rdi
call   1050 <timeval_norm@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_add_double>:
test   %rdi,%rdi
je     1175 <timeval_add_double+0x35>
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
mov    %rsp,%rdi
call   1040 <modf@plt>
movsd  (%rsp),%xmm1
addsd  (%rbx),%xmm1
movsd  %xmm1,(%rbx)
mulsd  0xe96(%rip),%xmm0
test   %ebp,%ebp
je     1176 <timeval_add_double+0x36>
call   1030 <ceil@plt>
jmp    117b <timeval_add_double+0x3b>
ret
call   1060 <round@plt>
add    %eax,0x8(%rbx)
mov    %rbx,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %rbp
jmp    1050 <timeval_norm@plt>

LLVM IR (-O0):
%struct.timeval = type { double, i32 }
define void @timeval_add_double(%struct.timeval* %0, double %1, i32 %2) {
%4 = alloca %struct.timeval*
%5 = alloca double
%6 = alloca i32
%7 = alloca double
%8 = alloca double
store %struct.timeval* %0, %struct.timeval** %4
store double %1, double* %5
store i32 %2, i32* %6
%9 = load %struct.timeval*, %struct.timeval** %4
%10 = icmp ne %struct.timeval* %9, null
br i1 %10, label %12, label %11
11: ; preds = %3
br label %45
12: ; preds = %3
%13 = load double, double* %5
%14 = call double @modf(double %13, double* %7)
store double %14, double* %8
%15 = load double, double* %7
%16 = load %struct.timeval*, %struct.timeval** %4
%17 = getelementptr inbounds %struct.timeval, %struct.timeval* %16, i32 0, i32 0
%18 = load double, double* %17
%19 = fadd double %18, %15
store double %19, double* %17
%20 = load i32, i32* %6
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %32
22: ; preds = %12
%23 = load double, double* %8
%24 = fmul double %23, 1.000000e+06
%25 = call i64 @ceil(double %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 = sext i32 %28 to i64
%30 = add nsw i64 %29, %25
%31 = trunc i64 %30 to i32
store i32 %31, i32* %27
br label %42
32: ; preds = %12
%33 = load double, double* %8
%34 = fmul double %33, 1.000000e+06
%35 = call i64 @round(double %34)
%36 = load %struct.timeval*, %struct.timeval** %4
%37 = getelementptr inbounds %struct.timeval, %struct.timeval* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = sext i32 %38 to i64
%40 = add nsw i64 %39, %35
%41 = trunc i64 %40 to i32
store i32 %41, i32* %37
br label %42
42: ; preds = %32, %22
%43 = load %struct.timeval*, %struct.timeval** %4
%44 = call i32 @timeval_norm(%struct.timeval* %43)
br label %45
45: ; preds = %42, %11
ret void
}

LLVM IR (-O2):
%struct.timeval = type { double, i32 }
define void @timeval_add_double(%struct.timeval* %0, double %1, i32 %2) {
%4 = alloca double
%5 = bitcast double* %4 to i8*
%6 = icmp eq %struct.timeval* %0, null
br i1 %6, label %26, label %7
7: ; preds = %3
%8 = call double @modf(double %1, double* %4)
%9 = load double, double* %4
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%11 = load double, double* %10
%12 = fadd double %9, %11
store double %12, double* %10
%13 = icmp eq i32 %2, 0
%14 = fmul double %8, 1.000000e+06
br i1 %13, label %17, label %15
15: ; preds = %7
%16 = tail call i64 @ceil(double %14)
br label %19
17: ; preds = %7
%18 = tail call i64 @round(double %14)
br label %19
19: ; preds = %17, %15
%20 = phi i64 [ %18, %17 ], [ %16, %15 ]
%21 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%22 = load i32, i32* %21
%23 = trunc i64 %20 to i32
%24 = add i32 %22, %23
store i32 %24, i32* %21
%25 = tail call i32 @timeval_norm(%struct.timeval* %0)
br label %26
26: ; preds = %3, %19
ret void
}