#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 /*<<< orphan*/ time_t;

/* Variables and functions */
char *ctime(int /*<<< orphan*/ *);
double difftime(int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, int, char *);
int rand();
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ srand(int);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int main() {
  srand(1);
  for (;;) {
    double interval = 10;
    time_t start = time(NULL);
    int duration = rand() % 13;
    printf("%2d seconds of work started at %s", duration, ctime(&start));
    sleep(duration);
    time_t end = time(NULL);
    double elapsed = difftime(end, start);
    int seconds_to_sleep = (int)(interval - elapsed);
    if (seconds_to_sleep > 0) {
      sleep(seconds_to_sleep);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x1,%edi
call   1060 <srand@plt>
movsd  0xe6f(%rip),%xmm0
movsd  %xmm0,-0x10(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x14(%rbp)
mov    $0x0,%al
call   1090 <rand@plt>
mov    $0xd,%ecx
cltd
idiv   %ecx
mov    %edx,-0x18(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <ctime@plt>
mov    -0x30(%rbp),%esi
mov    %rax,%rdx
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%edi
call   1080 <sleep@plt>
xor    %eax,%eax
mov    %eax,%edi
call   1070 <time@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%edi
mov    -0x14(%rbp),%esi
call   1050 <difftime@plt>
movsd  %xmm0,-0x28(%rbp)
movsd  -0x10(%rbp),%xmm0
subsd  -0x28(%rbp),%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jle    121c <main+0xac>
mov    -0x2c(%rbp),%edi
call   1080 <sleep@plt>
jmp    1189 <main+0x19>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    $0x1,%edi
call   1060 <srand@plt>
lea    0xc(%rsp),%r14
lea    0xe79(%rip),%r15
nop
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
xor    %eax,%eax
call   1090 <rand@plt>
movslq %eax,%rbx
imul   $0x4ec4ec4f,%rbx,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
sar    $0x22,%rax
add    %ecx,%eax
lea    (%rax,%rax,2),%ecx
lea    (%rax,%rcx,4),%eax
sub    %eax,%ebx
mov    %r14,%rdi
call   1030 <ctime@plt>
mov    %r15,%rdi
mov    %ebx,%esi
mov    %rax,%rdx
call   1040 <printf@plt>
mov    %ebx,%edi
call   1080 <sleep@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    0xc(%rsp),%esi
mov    %eax,%edi
call   1050 <difftime@plt>
movsd  0xe09(%rip),%xmm1
subsd  %xmm0,%xmm1
cvttsd2si %xmm1,%edi
test   %edi,%edi
jle    1190 <main+0x20>
call   1080 <sleep@plt>
jmp    1190 <main+0x20>

LLVM IR (-O0):
@.str = [34 x i8] c"%2d seconds of work started at %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca double
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca double
%7 = alloca i32
store i32 0, i32* %1
%8 = call i32 @srand(i32 1)
br label %9
9: ; preds = %31, %0
store double 1.000000e+01, double* %2
%10 = call i32 @time(i32* null)
store i32 %10, i32* %3
%11 = call i32 (...) @rand()
%12 = srem i32 %11, 13
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = call i8* @ctime(i32* %3)
%15 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %13, i8* %14)
%16 = load i32, i32* %4
%17 = call i32 @sleep(i32 %16)
%18 = call i32 @time(i32* null)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = load i32, i32* %3
%21 = call double @difftime(i32 %19, i32 %20)
store double %21, double* %6
%22 = load double, double* %2
%23 = load double, double* %6
%24 = fsub double %22, %23
%25 = fptosi double %24 to i32
store i32 %25, i32* %7
%26 = load i32, i32* %7
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %9
%29 = load i32, i32* %7
%30 = call i32 @sleep(i32 %29)
br label %31
31: ; preds = %28, %9
br label %9
}

LLVM IR (-O2):
@.str = [34 x i8] c"%2d seconds of work started at %s\00"
define i32 @main() {
%1 = alloca i32
%2 = tail call i32 @srand(i32 1)
%3 = bitcast i32* %1 to i8*
br label %4
4: ; preds = %19, %0
%5 = call i32 @time(i32* null)
store i32 %5, i32* %1
%6 = call i32 (...) @rand()
%7 = srem i32 %6, 13
%8 = call i8* @ctime(i32* %1)
%9 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i32 %7, i8* %8)
%10 = call i32 @sleep(i32 %7)
%11 = call i32 @time(i32* null)
%12 = load i32, i32* %1
%13 = call double @difftime(i32 %11, i32 %12)
%14 = fsub double 1.000000e+01, %13
%15 = fptosi double %14 to i32
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %19
17: ; preds = %4
%18 = call i32 @sleep(i32 %15)
br label %19
19: ; preds = %17, %4
br label %4
}