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

/* Variables and functions */
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int rand();
float sqrt(float);
float cpu_worker(int num_iter, struct timespec *delay_ptr) {
  int t = 0;
  float r;
  int i;
  for (i = 0; i < num_iter; i++) {
    r = rand() % 100;
    r = sqrt(r);
    t = r;
  }
  nanosleep(delay_ptr, NULL);
  return 0;
}


x86 assembly (clang, -O0):
<cpu_worker>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x4(%rbp),%eax
jge    1196 <cpu_worker+0x66>
mov    $0x0,%al
call   1050 <rand@plt>
mov    $0x64,%ecx
cltd
idiv   %ecx
cvtsi2ss %edx,%xmm0
movss  %xmm0,-0x18(%rbp)
movss  -0x18(%rbp),%xmm0
call   1040 <sqrt@plt>
movss  %xmm0,-0x18(%rbp)
cvttss2si -0x18(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    114d <cpu_worker+0x1d>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
xorps  %xmm0,%xmm0
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cpu_worker>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
test   %edi,%edi
jle    1182 <cpu_worker+0x52>
mov    %edi,%ebp
nopl   0x0(%rax)
xor    %eax,%eax
call   1050 <rand@plt>
cltq
imul   $0x51eb851f,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x25,%rcx
add    %edx,%ecx
imul   $0x64,%ecx,%ecx
sub    %ecx,%eax
js     116b <cpu_worker+0x3b>
add    $0xffffffff,%ebp
jne    1140 <cpu_worker+0x10>
jmp    1182 <cpu_worker+0x52>
xorps  %xmm0,%xmm0
cvtsi2ss %eax,%xmm0
ucomiss 0xe87(%rip),%xmm0
jae    1164 <cpu_worker+0x34>
call   1040 <sqrt@plt>
jmp    1164 <cpu_worker+0x34>
mov    %rbx,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
xorps  %xmm0,%xmm0
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
define float @cpu_worker(i32 %0, %struct.timespec* %1) {
%3 = alloca i32
%4 = alloca %struct.timespec*
%5 = alloca i32
%6 = alloca float
%7 = alloca i32
store i32 %0, i32* %3
store %struct.timespec* %1, %struct.timespec** %4
store i32 0, i32* %5
store i32 0, i32* %7
br label %8
8: ; preds = %20, %2
%9 = load i32, i32* %7
%10 = load i32, i32* %3
%11 = icmp slt i32 %9, %10
br i1 %11, label %12, label %23
12: ; preds = %8
%13 = call i32 (...) @rand()
%14 = srem i32 %13, 100
%15 = sitofp i32 %14 to float
store float %15, float* %6
%16 = load float, float* %6
%17 = call float @sqrt(float %16)
store float %17, float* %6
%18 = load float, float* %6
%19 = fptosi float %18 to i32
store i32 %19, i32* %5
br label %20
20: ; preds = %12
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
store i32 %22, i32* %7
br label %8
23: ; preds = %8
%24 = load %struct.timespec*, %struct.timespec** %4
%25 = call i32 @nanosleep(%struct.timespec* %24, i32* null)
ret float 0.000000e+00
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
define float @cpu_worker(i32 %0, %struct.timespec* %1) {
%3 = icmp sgt i32 %0, 0
br i1 %3, label %4, label %15
4: ; preds = %2, %12
%5 = phi i32 [ %13, %12 ], [ 0, %2 ]
%6 = tail call i32 (...) @rand()
%7 = srem i32 %6, 100
%8 = icmp slt i32 %7, 0
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = sitofp i32 %7 to float
%11 = tail call float @sqrt(float %10)
br label %12
12: ; preds = %4, %9
%13 = add nuw nsw i32 %5, 1
%14 = icmp eq i32 %13, %0
br i1 %14, label %15, label %4
15: ; preds = %12, %2
%16 = tail call i32 @nanosleep(%struct.timespec* %1, i32* null)
ret float 0.000000e+00
}