#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;
};

/* Variables and functions */
struct timeval timeval_add(const struct timeval *a, const struct timeval *b) {
  struct timeval ret;
  ret.tv_sec = a->tv_sec + b->tv_sec;
  ret.tv_usec = a->tv_usec + b->tv_usec;
  if (ret.tv_usec > 1000000) {
    int nsec = (ret.tv_usec / 1000000);
    ret.tv_sec += nsec;
    ret.tv_usec -= (1000000ULL * nsec);
  }
  return ret;
}


x86 assembly (clang, -O0):
<timeval_add>:
push   %rbp
mov    %rsp,%rbp
mov    %rdi,-0x18(%rbp)
mov    %rsi,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    0x8(%rax),%rax
mov    -0x20(%rbp),%rcx
add    0x8(%rcx),%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    -0x20(%rbp),%rcx
add    (%rcx),%eax
mov    %eax,-0x10(%rbp)
cmpl   $0xf4240,-0x10(%rbp)
jle    116b <timeval_add+0x6b>
mov    -0x10(%rbp),%eax
mov    $0xf4240,%ecx
cltd
idiv   %ecx
mov    %eax,-0x24(%rbp)
movslq -0x24(%rbp),%rax
add    -0x8(%rbp),%rax
mov    %rax,-0x8(%rbp)
movslq -0x24(%rbp),%rax
imul   $0xf4240,%rax,%rcx
movslq -0x10(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%eax
mov    -0x8(%rbp),%rdx
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeval_add>:
mov    0x8(%rsi),%rdx
add    0x8(%rdi),%rdx
mov    (%rsi),%eax
add    (%rdi),%eax
cmp    $0xf4241,%eax
jl     112b <timeval_add+0x2b>
mov    %eax,%ecx
imul   $0x431bde83,%rcx,%rcx
shr    $0x32,%rcx
add    %rcx,%rdx
imul   $0xfff0bdc0,%ecx,%ecx
add    %ecx,%eax
ret

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
define { i32, i64 } @timeval_add(%struct.timeval* nocapture %0, %struct.timeval* nocapture %1) {
%3 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 1
%4 = load i64, i64* %3
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%6 = load i64, i64* %5
%7 = add nsw i64 %6, %4
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %0, i64 0, i32 0
%9 = load i32, i32* %8
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, %9
%13 = icmp sgt i32 %12, 1000000
br i1 %13, label %14, label %20
14: ; preds = %2
%15 = udiv i32 %12, 1000000
%16 = zext i32 %15 to i64
%17 = add nsw i64 %7, %16
%18 = mul nsw i32 %15, -1000000
%19 = add nsw i32 %18, %12
br label %20
20: ; preds = %14, %2
%21 = phi i32 [ %19, %14 ], [ %12, %2 ]
%22 = phi i64 [ %17, %14 ], [ %7, %2 ]
%23 = insertvalue { i32, i64 } poison, i32 %21, 0
%24 = insertvalue { i32, i64 } %23, i64 %22, 1
ret { i32, i64 } %24
}