#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 perf {
  int dummy;
};

/* Variables and functions */
unsigned long long get_base_elapsed(struct perf *);
unsigned long long get_res();
unsigned long long estimate_perf_iterations(struct perf *p,
                                            unsigned long long runs,
                                            unsigned long long total) {
  total = total * runs;
  if (get_base_elapsed(p) > 0)
    return (total + get_base_elapsed(p) - 1) / get_base_elapsed(p);
  else
    return (total + get_res() - 1) / get_res();
}


x86 assembly (clang, -O0):
<estimate_perf_iterations>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x20(%rbp),%rax
imul   -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <get_base_elapsed@plt>
cmp    $0x0,%rax
jbe    1195 <estimate_perf_iterations+0x75>
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <get_base_elapsed@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
add    %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <get_base_elapsed@plt>
mov    %rax,%rcx
mov    -0x28(%rbp),%rax
xor    %edx,%edx
div    %rcx
mov    %rax,-0x8(%rbp)
jmp    11cd <estimate_perf_iterations+0xad>
mov    -0x20(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    $0x0,%al
call   1030 <get_res@plt>
mov    %rax,%rcx
mov    -0x40(%rbp),%rax
add    %rcx,%rax
sub    $0x1,%rax
mov    %rax,-0x38(%rbp)
mov    $0x0,%al
call   1030 <get_res@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
xor    %edx,%edx
div    %rcx
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<estimate_perf_iterations>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
imul   %rdx,%rbx
call   1040 <get_base_elapsed@plt>
test   %rax,%rax
je     1151 <estimate_perf_iterations+0x31>
mov    %r14,%rdi
call   1040 <get_base_elapsed@plt>
add    %rax,%rbx
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
call   1040 <get_base_elapsed@plt>
jmp    1166 <estimate_perf_iterations+0x46>
xor    %eax,%eax
call   1030 <get_res@plt>
add    %rax,%rbx
add    $0xffffffffffffffff,%rbx
xor    %eax,%eax
call   1030 <get_res@plt>
mov    %rax,%rcx
or     %rbx,%rax
shr    $0x20,%rax
je     117c <estimate_perf_iterations+0x5c>
mov    %rbx,%rax
xor    %edx,%edx
div    %rcx
jmp    1182 <estimate_perf_iterations+0x62>
mov    %ebx,%eax
xor    %edx,%edx
div    %ecx
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.perf = type { i32 }
define i64 @estimate_perf_iterations(%struct.perf* %0, i64 %1, i64 %2) {
%4 = alloca i64
%5 = alloca %struct.perf*
%6 = alloca i64
%7 = alloca i64
store %struct.perf* %0, %struct.perf** %5
store i64 %1, i64* %6
store i64 %2, i64* %7
%8 = load i64, i64* %7
%9 = load i64, i64* %6
%10 = mul i64 %8, %9
store i64 %10, i64* %7
%11 = load %struct.perf*, %struct.perf** %5
%12 = call i64 @get_base_elapsed(%struct.perf* %11)
%13 = icmp ugt i64 %12, 0
br i1 %13, label %14, label %23
14: ; preds = %3
%15 = load i64, i64* %7
%16 = load %struct.perf*, %struct.perf** %5
%17 = call i64 @get_base_elapsed(%struct.perf* %16)
%18 = add i64 %15, %17
%19 = sub i64 %18, 1
%20 = load %struct.perf*, %struct.perf** %5
%21 = call i64 @get_base_elapsed(%struct.perf* %20)
%22 = udiv i64 %19, %21
store i64 %22, i64* %4
br label %30
23: ; preds = %3
%24 = load i64, i64* %7
%25 = call i64 (...) @get_res()
%26 = add i64 %24, %25
%27 = sub i64 %26, 1
%28 = call i64 (...) @get_res()
%29 = udiv i64 %27, %28
store i64 %29, i64* %4
br label %30
30: ; preds = %23, %14
%31 = load i64, i64* %4
ret i64 %31
}

LLVM IR (-O2):
%struct.perf = type { i32 }
define i64 @estimate_perf_iterations(%struct.perf* %0, i64 %1, i64 %2) {
%4 = mul i64 %2, %1
%5 = tail call i64 @get_base_elapsed(%struct.perf* %0)
%6 = icmp eq i64 %5, 0
br i1 %6, label %13, label %7
7: ; preds = %3
%8 = tail call i64 @get_base_elapsed(%struct.perf* %0)
%9 = add i64 %4, -1
%10 = add i64 %9, %8
%11 = tail call i64 @get_base_elapsed(%struct.perf* %0)
%12 = udiv i64 %10, %11
br label %19
13: ; preds = %3
%14 = tail call i64 (...) @get_res()
%15 = add i64 %4, -1
%16 = add i64 %15, %14
%17 = tail call i64 (...) @get_res()
%18 = udiv i64 %16, %17
br label %19
19: ; preds = %13, %7
%20 = phi i64 [ %12, %7 ], [ %18, %13 ]
ret i64 %20
}