#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 scalar_t__ uint64_t;

/* Variables and functions */
int /*<<< orphan*/ getTimeUs(scalar_t__ *);
int /*<<< orphan*/ sqrt(int);
int /*<<< orphan*/ usleep(int);
int forceCpuLoadSlice(int cpuLoad, int durationUs) {
  uint64_t workDuration;
  uint64_t begin;
  uint64_t end;
  uint64_t current;
  int i;
  getTimeUs(&begin);
  workDuration = (cpuLoad * durationUs) / 100;
  end = begin + workDuration;
  do {
    for (i = 0; i < 1000; i++)
      ;
    sqrt(durationUs);
    getTimeUs(&current);
  } while (current < end);
  usleep(durationUs - workDuration);
  return 0;
}


x86 assembly (clang, -O0):
<forceCpuLoadSlice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
call   1030 <getTimeUs@plt>
mov    -0x4(%rbp),%eax
imul   -0x8(%rbp),%eax
mov    $0x64,%ecx
cltd
idiv   %ecx
cltq
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x0,-0x2c(%rbp)
cmpl   $0x3e8,-0x2c(%rbp)
jge    118f <forceCpuLoadSlice+0x5f>
jmp    1181 <forceCpuLoadSlice+0x51>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    116f <forceCpuLoadSlice+0x3f>
mov    -0x8(%rbp),%edi
call   1040 <sqrt@plt>
lea    -0x28(%rbp),%rdi
call   1030 <getTimeUs@plt>
mov    -0x28(%rbp),%rax
cmp    -0x20(%rbp),%rax
jl     1168 <forceCpuLoadSlice+0x38>
movslq -0x8(%rbp),%rax
sub    -0x10(%rbp),%rax
mov    %eax,%edi
call   1050 <usleep@plt>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<forceCpuLoadSlice>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
lea    0x10(%rsp),%rdi
call   1030 <getTimeUs@plt>
imul   %ebx,%ebp
movslq %ebp,%rax
imul   $0x51eb851f,%rax,%r15
mov    %r15,%rax
shr    $0x3f,%rax
sar    $0x25,%r15
add    %eax,%r15d
movslq %r15d,%rbp
add    0x10(%rsp),%rbp
lea    0x8(%rsp),%r14
mov    %ebx,%edi
call   1040 <sqrt@plt>
mov    %r14,%rdi
call   1030 <getTimeUs@plt>
cmp    %rbp,0x8(%rsp)
jl     1170 <forceCpuLoadSlice+0x40>
sub    %r15d,%ebx
mov    %ebx,%edi
call   1050 <usleep@plt>
xor    %eax,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @forceCpuLoadSlice(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
%9 = alloca i32
store i32 %0, i32* %3
store i32 %1, i32* %4
%10 = call i32 @getTimeUs(i64* %6)
%11 = load i32, i32* %3
%12 = load i32, i32* %4
%13 = mul nsw i32 %11, %12
%14 = sdiv i32 %13, 100
%15 = sext i32 %14 to i64
store i64 %15, i64* %5
%16 = load i64, i64* %6
%17 = load i64, i64* %5
%18 = add nsw i64 %16, %17
store i64 %18, i64* %7
br label %19
19: ; preds = %31, %2
store i32 0, i32* %9
br label %20
20: ; preds = %24, %19
%21 = load i32, i32* %9
%22 = icmp slt i32 %21, 1000
br i1 %22, label %23, label %27
23: ; preds = %20
br label %24
24: ; preds = %23
%25 = load i32, i32* %9
%26 = add nsw i32 %25, 1
store i32 %26, i32* %9
br label %20
27: ; preds = %20
%28 = load i32, i32* %4
%29 = call i32 @sqrt(i32 %28)
%30 = call i32 @getTimeUs(i64* %8)
br label %31
31: ; preds = %27
%32 = load i64, i64* %8
%33 = load i64, i64* %7
%34 = icmp slt i64 %32, %33
br i1 %34, label %19, label %35
35: ; preds = %31
%36 = load i32, i32* %4
%37 = sext i32 %36 to i64
%38 = load i64, i64* %5
%39 = sub nsw i64 %37, %38
%40 = trunc i64 %39 to i32
%41 = call i32 @usleep(i32 %40)
ret i32 0
}

LLVM IR (-O2):
define i32 @forceCpuLoadSlice(i32 %0, i32 %1) {
%3 = alloca i64
%4 = alloca i64
%5 = bitcast i64* %3 to i8*
%6 = bitcast i64* %4 to i8*
%7 = call i32 @getTimeUs(i64* %3)
%8 = mul nsw i32 %1, %0
%9 = sdiv i32 %8, 100
%10 = sext i32 %9 to i64
%11 = load i64, i64* %3
%12 = add nsw i64 %11, %10
br label %13
13: ; preds = %13, %2
%14 = call i32 @sqrt(i32 %1)
%15 = call i32 @getTimeUs(i64* %4)
%16 = load i64, i64* %4
%17 = icmp slt i64 %16, %12
br i1 %17, label %13, label %18
18: ; preds = %13
%19 = sub i32 %1, %9
%20 = call i32 @usleep(i32 %19)
ret i32 0
}