#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 {
  scalar_t__ tv_usec;
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ gettimeofday(struct timeval *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ usleep(int);
int main() {
  int i = 0;
  long max = 0;
  long min = 99999999;
  long avg = 0;
  long x = 0;
  struct timeval time_start, time_stop;
  for (i = 0; i < 8192; i++) {
    gettimeofday(&time_start, NULL);
    usleep(1024);
    gettimeofday(&time_stop, NULL);
    x = (long)(((long)time_stop.tv_sec * 1000000) + time_stop.tv_usec) -
        (long)(((long)time_start.tv_sec * 1000000) + time_start.tv_usec) - 1024;
    if (x > max) {
      max = x;
    } else if (x < min) {
      min = x;
    }
    avg = (avg + x) / 2;
  }
  printf("Contex Switching Test Result\n");
  printf("----------------------------\n");
  printf("Max ctx_switch latency: %ld us\n", max);
  printf("Min ctx_switch latency: %ld us\n", min);
  printf("Avg ctx_switch latency: %ld us\n", avg);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x5f5e0ff,-0x18(%rbp)
05
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movl   $0x0,-0x8(%rbp)
cmpl   $0x2000,-0x8(%rbp)
jge    121d <main+0xed>
lea    -0x38(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
mov    $0x400,%edi
call   1050 <usleep@plt>
lea    -0x48(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <gettimeofday@plt>
imul   $0xf4240,-0x40(%rbp),%rax
add    -0x48(%rbp),%rax
imul   $0xf4240,-0x30(%rbp),%rcx
add    -0x38(%rbp),%rcx
sub    %rcx,%rax
sub    $0x400,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jle    11de <main+0xae>
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    11f9 <main+0xc9>
mov    -0x28(%rbp),%rax
cmp    -0x18(%rbp),%rax
jge    11f4 <main+0xc4>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    11f9 <main+0xc9>
mov    -0x20(%rbp),%rax
add    -0x28(%rbp),%rax
mov    $0x2,%ecx
cqto
idiv   %rcx
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
jmp    116d <main+0x3d>
lea    0xddc(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x10(%rbp),%rsi
lea    0xdf8(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x18(%rbp),%rsi
lea    0xe06(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x20(%rbp),%rsi
lea    0xe14(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    $0x5f5e0ff,%r14d
mov    $0x2000,%ebp
xor    %ebx,%ebx
lea    0x8(%rsp),%r13
xor    %r15d,%r15d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r14,%r12
lea    0x18(%rsp),%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    $0x400,%edi
call   1060 <usleep@plt>
mov    %r13,%rdi
xor    %esi,%esi
call   1050 <gettimeofday@plt>
mov    0x10(%rsp),%rax
sub    0x20(%rsp),%rax
mov    0x8(%rsp),%rcx
imul   $0xf4240,%rax,%rax
sub    0x18(%rsp),%rcx
lea    (%rcx,%rax,1),%rdx
add    $0xfffffffffffffc00,%rdx
cmp    %r14,%rdx
cmovl  %rdx,%r14
cmp    %r15,%rdx
cmovg  %rdx,%r15
lea    (%rcx,%rax,1),%rax
cmovg  %r12,%r14
add    %rbx,%rax
add    $0xfffffffffffffc00,%rax
mov    %rax,%rbx
shr    $0x3f,%rbx
add    %rax,%rbx
sar    %rbx
add    $0xffffffff,%ebp
jne    1170 <main+0x30>
lea    0xe6f(%rip),%rdi
call   1030 <puts@plt>
lea    0xe80(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf7(%rip),%rdi
mov    %r15,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe06(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xe15(%rip),%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i64, i64 }
@.str = [30 x i8] c"Contex Switching Test Result\0A\00"
@.str.1 = [30 x i8] c"----------------------------\0A\00"
@.str.2 = [32 x i8] c"Max ctx_switch latency: %ld us\0A\00"
@.str.3 = [32 x i8] c"Min ctx_switch latency: %ld us\0A\00"
@.str.4 = [32 x i8] c"Avg ctx_switch latency: %ld us\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca %struct.timeval
%8 = alloca %struct.timeval
store i32 0, i32* %1
store i32 0, i32* %2
store i64 0, i64* %3
store i64 99999999, i64* %4
store i64 0, i64* %5
store i64 0, i64* %6
store i32 0, i32* %2
br label %9
9: ; preds = %47, %0
%10 = load i32, i32* %2
%11 = icmp slt i32 %10, 8192
br i1 %11, label %12, label %50
12: ; preds = %9
%13 = call i32 @gettimeofday(%struct.timeval* %7, i32* null)
%14 = call i32 @usleep(i32 1024)
%15 = call i32 @gettimeofday(%struct.timeval* %8, i32* null)
%16 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 1
%17 = load i64, i64* %16
%18 = mul nsw i64 %17, 1000000
%19 = getelementptr inbounds %struct.timeval, %struct.timeval* %8, i32 0, i32 0
%20 = load i64, i64* %19
%21 = add nsw i64 %18, %20
%22 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 1
%23 = load i64, i64* %22
%24 = mul nsw i64 %23, 1000000
%25 = getelementptr inbounds %struct.timeval, %struct.timeval* %7, i32 0, i32 0
%26 = load i64, i64* %25
%27 = add nsw i64 %24, %26
%28 = sub nsw i64 %21, %27
%29 = sub nsw i64 %28, 1024
store i64 %29, i64* %6
%30 = load i64, i64* %6
%31 = load i64, i64* %3
%32 = icmp sgt i64 %30, %31
br i1 %32, label %33, label %35
33: ; preds = %12
%34 = load i64, i64* %6
store i64 %34, i64* %3
br label %42
35: ; preds = %12
%36 = load i64, i64* %6
%37 = load i64, i64* %4
%38 = icmp slt i64 %36, %37
br i1 %38, label %39, label %41
39: ; preds = %35
%40 = load i64, i64* %6
store i64 %40, i64* %4
br label %41
41: ; preds = %39, %35
br label %42
42: ; preds = %41, %33
%43 = load i64, i64* %5
%44 = load i64, i64* %6
%45 = add nsw i64 %43, %44
%46 = sdiv i64 %45, 2
store i64 %46, i64* %5
br label %47
47: ; preds = %42
%48 = load i32, i32* %2
%49 = add nsw i32 %48, 1
store i32 %49, i32* %2
br label %9
50: ; preds = %9
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0))
%52 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%53 = load i64, i64* %3
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %53)
%55 = load i64, i64* %4
%56 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %55)
%57 = load i64, i64* %5
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i64 %57)
ret i32 0
}

LLVM IR (-O2):
%struct.timeval = type { i64, i64 }
@.str.2 = [32 x i8] c"Max ctx_switch latency: %ld us\0A\00"
@.str.3 = [32 x i8] c"Min ctx_switch latency: %ld us\0A\00"
@.str.4 = [32 x i8] c"Avg ctx_switch latency: %ld us\0A\00"
@str = [29 x i8] c"Contex Switching Test Result\00"
@str.5 = [29 x i8] c"----------------------------\00"
define i32 @main() {
%1 = alloca %struct.timeval
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %1 to i8*
%4 = bitcast %struct.timeval* %2 to i8*
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
%7 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 1
%8 = getelementptr inbounds %struct.timeval, %struct.timeval* %1, i64 0, i32 0
br label %9
9: ; preds = %0, %9
%10 = phi i64 [ 0, %0 ], [ %32, %9 ]
%11 = phi i64 [ 99999999, %0 ], [ %30, %9 ]
%12 = phi i64 [ 0, %0 ], [ %29, %9 ]
%13 = phi i32 [ 0, %0 ], [ %33, %9 ]
%14 = call i32 @gettimeofday(%struct.timeval* %1, i32* null)
%15 = tail call i32 @usleep(i32 1024)
%16 = call i32 @gettimeofday(%struct.timeval* %2, i32* null)
%17 = load i64, i64* %5
%18 = load i64, i64* %6
%19 = load i64, i64* %7
%20 = load i64, i64* %8
%21 = sub i64 %17, %19
%22 = mul i64 %21, 1000000
%23 = add i64 %18, -1024
%24 = sub i64 %23, %20
%25 = add i64 %24, %22
%26 = icmp sgt i64 %25, %12
%27 = icmp slt i64 %25, %11
%28 = select i1 %27, i64 %25, i64 %11
%29 = select i1 %26, i64 %25, i64 %12
%30 = select i1 %26, i64 %11, i64 %28
%31 = add nsw i64 %25, %10
%32 = sdiv i64 %31, 2
%33 = add nuw nsw i32 %13, 1
%34 = icmp eq i32 %33, 8192
br i1 %34, label %35, label %9
35: ; preds = %9
%36 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%37 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str.5, i64 0, i64 0))
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.2, i64 0, i64 0), i64 %29)
%39 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i64 %30)
%40 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([32 x i8], [32 x i8]* @.str.4, i64 0, i64 0), i64 %32)
ret i32 0
}