#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 */

/* Variables and functions */
unsigned long long cpu_stats_get_cswch();
int /*<<< orphan*/ printf(char *, unsigned long long, ...);
int /*<<< orphan*/ sleep(unsigned int);
unsigned long long get_ctxtdelta(unsigned int count, unsigned int delay,
                                 signed char verbose) {
  int tog = 0;
  unsigned int i;
  unsigned long long nctxt[2], dnctxt = nctxt[0] = cpu_stats_get_cswch();
  if (verbose)
    printf("ctxt = %llu\n", dnctxt);
  for (i = 1; i < count; i++) {
    sleep(delay);
    tog = !tog;
    nctxt[tog] = cpu_stats_get_cswch();
    dnctxt = (nctxt[tog] - nctxt[!tog]) / delay;
    if (verbose)
      printf("ctxt = %llu --> %llu/s\n", nctxt[tog], dnctxt);
  }
  return dnctxt;
}


x86 assembly (clang, -O0):
<get_ctxtdelta>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %al,-0x9(%rbp)
movl   $0x0,-0x10(%rbp)
mov    $0x0,%al
call   1040 <cpu_stats_get_cswch@plt>
mov    %rax,-0x30(%rbp)
mov    %rax,-0x38(%rbp)
cmpb   $0x0,-0x9(%rbp)
je     1175 <get_ctxtdelta+0x45>
mov    -0x38(%rbp),%rsi
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x4(%rbp),%eax
jae    1219 <get_ctxtdelta+0xe9>
mov    -0x8(%rbp),%edi
call   1050 <sleep@plt>
cmpl   $0x0,-0x10(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x10(%rbp)
mov    $0x0,%al
call   1040 <cpu_stats_get_cswch@plt>
mov    %rax,%rcx
movslq -0x10(%rbp),%rax
mov    %rcx,-0x30(%rbp,%rax,8)
movslq -0x10(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rax
cmpl   $0x0,-0x10(%rbp)
setne  %cl
xor    $0xff,%cl
and    $0x1,%cl
movzbl %cl,%ecx
movslq %ecx,%rcx
sub    -0x30(%rbp,%rcx,8),%rax
mov    -0x8(%rbp),%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x38(%rbp)
cmpb   $0x0,-0x9(%rbp)
je     1206 <get_ctxtdelta+0xd6>
movslq -0x10(%rbp),%rax
mov    -0x30(%rbp,%rax,8),%rsi
mov    -0x38(%rbp),%rdx
lea    0xe0e(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    120b <get_ctxtdelta+0xdb>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    117c <get_ctxtdelta+0x4c>
mov    -0x38(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_ctxtdelta>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edx,%r12d
mov    %esi,%r14d
mov    %edi,%ebp
xor    %eax,%eax
call   1040 <cpu_stats_get_cswch@plt>
mov    %rax,%rbx
mov    %rax,(%rsp)
test   %r12b,%r12b
je     116a <get_ctxtdelta+0x3a>
lea    0xea0(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1030 <printf@plt>
cmp    $0x2,%ebp
jb     11ef <get_ctxtdelta+0xbf>
mov    %r14d,%r13d
add    $0xffffffff,%ebp
xor    %r15d,%r15d
jmp    1189 <get_ctxtdelta+0x59>
xchg   %ax,%ax
xor    $0x1,%r15d
add    $0xffffffff,%ebp
je     11ef <get_ctxtdelta+0xbf>
mov    %r14d,%edi
call   1050 <sleep@plt>
xor    %ebx,%ebx
test   %r15d,%r15d
sete   %bl
xor    %eax,%eax
call   1040 <cpu_stats_get_cswch@plt>
mov    %rax,%rsi
mov    %rax,(%rsp,%rbx,8)
mov    %r15d,%ecx
sub    (%rsp,%rcx,8),%rax
mov    %rax,%rcx
shr    $0x20,%rcx
je     11d0 <get_ctxtdelta+0xa0>
xor    %edx,%edx
div    %r13
mov    %rax,%rbx
test   %r12b,%r12b
jne    11dc <get_ctxtdelta+0xac>
jmp    1180 <get_ctxtdelta+0x50>
cs nopw 0x0(%rax,%rax,1)
xor    %edx,%edx
div    %r13d
mov    %eax,%ebx
test   %r12b,%r12b
je     1180 <get_ctxtdelta+0x50>
lea    0xe2a(%rip),%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <printf@plt>
jmp    1180 <get_ctxtdelta+0x50>
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"ctxt = %llu\0A\00"
@.str.1 = [24 x i8] c"ctxt = %llu --> %llu/s\0A\00"
define i64 @get_ctxtdelta(i32 %0, i32 %1, i8 signext %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8
%7 = alloca i32
%8 = alloca i32
%9 = alloca [2 x i64]
%10 = alloca i64
store i32 %0, i32* %4
store i32 %1, i32* %5
store i8 %2, i8* %6
store i32 0, i32* %7
%11 = call i64 (...) @cpu_stats_get_cswch()
%12 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 0
store i64 %11, i64* %12
store i64 %11, i64* %10
%13 = load i8, i8* %6
%14 = icmp ne i8 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %3
%16 = load i64, i64* %10
%17 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %16)
br label %18
18: ; preds = %15, %3
store i32 1, i32* %8
br label %19
19: ; preds = %59, %18
%20 = load i32, i32* %8
%21 = load i32, i32* %4
%22 = icmp ult i32 %20, %21
br i1 %22, label %23, label %62
23: ; preds = %19
%24 = load i32, i32* %5
%25 = call i32 @sleep(i32 %24)
%26 = load i32, i32* %7
%27 = icmp ne i32 %26, 0
%28 = xor i1 %27, true
%29 = zext i1 %28 to i32
store i32 %29, i32* %7
%30 = call i64 (...) @cpu_stats_get_cswch()
%31 = load i32, i32* %7
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 %32
store i64 %30, i64* %33
%34 = load i32, i32* %7
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 %35
%37 = load i64, i64* %36
%38 = load i32, i32* %7
%39 = icmp ne i32 %38, 0
%40 = xor i1 %39, true
%41 = zext i1 %40 to i32
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 %42
%44 = load i64, i64* %43
%45 = sub i64 %37, %44
%46 = load i32, i32* %5
%47 = zext i32 %46 to i64
%48 = udiv i64 %45, %47
store i64 %48, i64* %10
%49 = load i8, i8* %6
%50 = icmp ne i8 %49, 0
br i1 %50, label %51, label %58
51: ; preds = %23
%52 = load i32, i32* %7
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [2 x i64], [2 x i64]* %9, i64 0, i64 %53
%55 = load i64, i64* %54
%56 = load i64, i64* %10
%57 = call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %55, i64 %56)
br label %58
58: ; preds = %51, %23
br label %59
59: ; preds = %58
%60 = load i32, i32* %8
%61 = add i32 %60, 1
store i32 %61, i32* %8
br label %19
62: ; preds = %19
%63 = load i64, i64* %10
ret i64 %63
}

LLVM IR (-O2):
@.str = [13 x i8] c"ctxt = %llu\0A\00"
@.str.1 = [24 x i8] c"ctxt = %llu --> %llu/s\0A\00"
define i64 @get_ctxtdelta(i32 %0, i32 %1, i8 signext %2) {
%4 = alloca [2 x i64]
%5 = bitcast [2 x i64]* %4 to i8*
%6 = tail call i64 (...) @cpu_stats_get_cswch()
%7 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 0
store i64 %6, i64* %7
%8 = icmp eq i8 %2, 0
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i64 %6)
br label %11
11: ; preds = %9, %3
%12 = icmp ugt i32 %0, 1
br i1 %12, label %13, label %34
13: ; preds = %11
%14 = zext i32 %1 to i64
br label %15
15: ; preds = %13, %31
%16 = phi i32 [ 1, %13 ], [ %32, %31 ]
%17 = phi i32 [ 0, %13 ], [ %20, %31 ]
%18 = tail call i32 @sleep(i32 %1)
%19 = icmp eq i32 %17, 0
%20 = xor i32 %17, 1
%21 = tail call i64 (...) @cpu_stats_get_cswch()
%22 = zext i1 %19 to i64
%23 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 %22
store i64 %21, i64* %23
%24 = zext i32 %17 to i64
%25 = getelementptr inbounds [2 x i64], [2 x i64]* %4, i64 0, i64 %24
%26 = load i64, i64* %25
%27 = sub i64 %21, %26
%28 = udiv i64 %27, %14
br i1 %8, label %31, label %29
29: ; preds = %15
%30 = tail call i32 (i8*, i64, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i64 %21, i64 %28)
br label %31
31: ; preds = %15, %29
%32 = add nuw i32 %16, 1
%33 = icmp eq i32 %32, %0
br i1 %33, label %34, label %15
34: ; preds = %31, %11
%35 = phi i64 [ %6, %11 ], [ %28, %31 ]
ret i64 %35
}