#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 tv_nsec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ nanosleep(struct timespec const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ test_and_verify(int);
void test_fade(int max) {
  int i;
  static const struct timespec ts = {.tv_sec = 0, .tv_nsec = 100 * 1000000};
  for (i = max; i > 0; i -= max / 10) {
    test_and_verify(i);
    nanosleep(&ts, NULL);
  }
  for (i = 0; i <= max; i += max / 10) {
    test_and_verify(i);
    nanosleep(&ts, NULL);
  }
}


x86 assembly (clang, -O0):
<test_fade>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jle    116d <test_fade+0x4d>
mov    -0x8(%rbp),%edi
call   1040 <test_and_verify@plt>
lea    0xeb6(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
mov    %eax,%ecx
mov    -0x8(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x8(%rbp)
jmp    1131 <test_fade+0x11>
movl   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%eax
cmp    -0x4(%rbp),%eax
jg     11ae <test_fade+0x8e>
mov    -0x8(%rbp),%edi
call   1040 <test_and_verify@plt>
lea    0xe71(%rip),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1030 <nanosleep@plt>
mov    -0x4(%rbp),%eax
mov    $0xa,%ecx
cltd
idiv   %ecx
add    -0x8(%rbp),%eax
mov    %eax,-0x8(%rbp)
jmp    1174 <test_fade+0x54>
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_fade>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r12d
mov    %edi,%r15d
test   %edi,%edi
jle    1167 <test_fade+0x47>
mov    $0xcccccccd,%ebp
imul   %r15,%rbp
shr    $0x23,%rbp
lea    0xeba(%rip),%r14
mov    %r12d,%ebx
nopl   0x0(%rax)
mov    %ebx,%edi
call   1040 <test_and_verify@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
sub    %ebp,%ebx
test   %ebx,%ebx
jg     1150 <test_fade+0x30>
test   %r12d,%r12d
js     11a8 <test_fade+0x88>
mov    $0xcccccccd,%ebp
imul   %r15,%rbp
shr    $0x23,%rbp
xor    %ebx,%ebx
lea    0xe7e(%rip),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
call   1040 <test_and_verify@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   1030 <nanosleep@plt>
add    %ebp,%ebx
cmp    %r12d,%ebx
jle    1190 <test_fade+0x70>
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@test_fade.ts = internal constant %struct.timespec { i32 100000000, i32 0 }
define void @test_fade(i32 %0) {
%2 = alloca i32
%3 = alloca i32
store i32 %0, i32* %2
%4 = load i32, i32* %2
store i32 %4, i32* %3
br label %5
5: ; preds = %12, %1
%6 = load i32, i32* %3
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %17
8: ; preds = %5
%9 = load i32, i32* %3
%10 = call i32 @test_and_verify(i32 %9)
%11 = call i32 @nanosleep(%struct.timespec* @test_fade.ts, i32* null)
br label %12
12: ; preds = %8
%13 = load i32, i32* %2
%14 = sdiv i32 %13, 10
%15 = load i32, i32* %3
%16 = sub nsw i32 %15, %14
store i32 %16, i32* %3
br label %5
17: ; preds = %5
store i32 0, i32* %3
br label %18
18: ; preds = %26, %17
%19 = load i32, i32* %3
%20 = load i32, i32* %2
%21 = icmp sle i32 %19, %20
br i1 %21, label %22, label %31
22: ; preds = %18
%23 = load i32, i32* %3
%24 = call i32 @test_and_verify(i32 %23)
%25 = call i32 @nanosleep(%struct.timespec* @test_fade.ts, i32* null)
br label %26
26: ; preds = %22
%27 = load i32, i32* %2
%28 = sdiv i32 %27, 10
%29 = load i32, i32* %3
%30 = add nsw i32 %29, %28
store i32 %30, i32* %3
br label %18
31: ; preds = %18
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@test_fade.ts = internal constant %struct.timespec { i32 100000000, i32 0 }
define void @test_fade(i32 %0) {
%2 = icmp sgt i32 %0, 0
br i1 %2, label %3, label %5
3: ; preds = %1
%4 = udiv i32 %0, 10
br label %9
5: ; preds = %9, %1
%6 = icmp slt i32 %0, 0
br i1 %6, label %21, label %7
7: ; preds = %5
%8 = udiv i32 %0, 10
br label %15
9: ; preds = %3, %9
%10 = phi i32 [ %0, %3 ], [ %13, %9 ]
%11 = tail call i32 @test_and_verify(i32 %10)
%12 = tail call i32 @nanosleep(%struct.timespec* @test_fade.ts, i32* null)
%13 = sub i32 %10, %4
%14 = icmp sgt i32 %13, 0
br i1 %14, label %9, label %5
15: ; preds = %7, %15
%16 = phi i32 [ 0, %7 ], [ %19, %15 ]
%17 = tail call i32 @test_and_verify(i32 %16)
%18 = tail call i32 @nanosleep(%struct.timespec* @test_fade.ts, i32* null)
%19 = add nuw nsw i32 %16, %8
%20 = icmp sgt i32 %19, %0
br i1 %20, label %21, label %15
21: ; preds = %15, %5
ret void
}