#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 uint64_t;
struct timespec {
  unsigned long tv_sec;
  int tv_nsec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_PROCESS_CPUTIME_ID;
int /*<<< orphan*/ clock_gettime(int /*<<< orphan*/, struct timespec *);
int /*<<< orphan*/ cycles_test1();
int cycles_test2(uint64_t *t_us) {
  struct timespec ts;
  uint64_t t0;
  uint64_t t1;
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  t0 = 1000000000ul * ts.tv_sec + ts.tv_nsec;
  cycles_test1();
  clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts);
  t1 = 1000000000ul * ts.tv_sec + ts.tv_nsec;
  *t_us = (t1 - t0) / 1000;
  return 0;
}


x86 assembly (clang, -O0):
<cycles_test2>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2eb5(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0x3b9aca00,-0x18(%rbp),%rax
3b
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %eax,-0x1c(%rbp)
mov    $0x0,%al
call   1040 <cycles_test1@plt>
mov    0x2e8a(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rsi
call   1030 <clock_gettime@plt>
imul   $0x3b9aca00,-0x18(%rbp),%rax
3b
movslq -0x10(%rbp),%rcx
add    %rcx,%rax
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
sub    -0x1c(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cycles_test2>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%r14
mov    0x2eb2(%rip),%r15
mov    (%r15),%edi
mov    %rsp,%r12
mov    %r12,%rsi
call   1030 <clock_gettime@plt>
mov    (%rsp),%ebp
mov    0x8(%rsp),%ebx
xor    %eax,%eax
call   1040 <cycles_test1@plt>
mov    (%r15),%edi
mov    %r12,%rsi
call   1030 <clock_gettime@plt>
mov    (%rsp),%eax
sub    %ebp,%eax
mov    0x8(%rsp),%ecx
sub    %ebx,%ecx
imul   $0x3b9aca00,%eax,%eax
add    %ecx,%eax
cltq
imul   $0x10624dd3,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x26,%rax
add    %ecx,%eax
mov    %eax,(%r14)
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i64, i32 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
define i32 @cycles_test2(i32* %0) {
%2 = alloca i32*
%3 = alloca %struct.timespec
%4 = alloca i32
%5 = alloca i32
store i32* %0, i32** %2
%6 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%7 = call i32 @clock_gettime(i32 %6, %struct.timespec* %3)
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%9 = load i64, i64* %8
%10 = mul i64 1000000000, %9
%11 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%12 = load i32, i32* %11
%13 = sext i32 %12 to i64
%14 = add i64 %10, %13
%15 = trunc i64 %14 to i32
store i32 %15, i32* %4
%16 = call i32 (...) @cycles_test1()
%17 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%18 = call i32 @clock_gettime(i32 %17, %struct.timespec* %3)
%19 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%20 = load i64, i64* %19
%21 = mul i64 1000000000, %20
%22 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 1
%23 = load i32, i32* %22
%24 = sext i32 %23 to i64
%25 = add i64 %21, %24
%26 = trunc i64 %25 to i32
store i32 %26, i32* %5
%27 = load i32, i32* %5
%28 = load i32, i32* %4
%29 = sub nsw i32 %27, %28
%30 = sdiv i32 %29, 1000
%31 = load i32*, i32** %2
store i32 %30, i32* %31
ret i32 0
}

LLVM IR (-O2):
%struct.timespec = type { i64, i32 }
@CLOCK_PROCESS_CPUTIME_ID = global i32 0
define i32 @cycles_test2(i32* nocapture writeonly %0) {
%2 = alloca %struct.timespec
%3 = bitcast %struct.timespec* %2 to i8*
%4 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%5 = call i32 @clock_gettime(i32 %4, %struct.timespec* %2)
%6 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%7 = load i64, i64* %6
%8 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 1
%9 = load i32, i32* %8
%10 = trunc i64 %7 to i32
%11 = call i32 (...) @cycles_test1()
%12 = load i32, i32* @CLOCK_PROCESS_CPUTIME_ID
%13 = call i32 @clock_gettime(i32 %12, %struct.timespec* %2)
%14 = load i64, i64* %6
%15 = load i32, i32* %8
%16 = trunc i64 %14 to i32
%17 = sub i32 %16, %10
%18 = mul i32 %17, 1000000000
%19 = sub i32 %15, %9
%20 = add i32 %19, %18
%21 = sdiv i32 %20, 1000
store i32 %21, i32* %0
ret i32 0
}