#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*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ nanosleep(struct timespec *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *, int);
int /*<<< orphan*/ stdout;
int main(int argc, char **argv) {
  struct timespec tsp = {.tv_sec = 0, .tv_nsec = 500 * 1000 * 1000};
  while (true) {
    static int i = 0;
    printf("\r%d", i);
    fflush(stdout);
    nanosleep(&tsp, NULL);
    i++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0xeb3(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    0x2ee5(%rip),%esi
lea    0xeaa(%rip),%rdi
call   1030 <printf@plt>
mov    0x2e76(%rip),%rax
mov    (%rax),%edi
call   1050 <fflush@plt>
lea    -0x18(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1040 <nanosleep@plt>
mov    0x2eb8(%rip),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    %eax,0x2eac(%rip)
jmp    1151 <main+0x21>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
movq   $0x1dcd6500,0x8(%rsp)
cd 1d
mov    0x2ef4(%rip),%esi
lea    0xeb1(%rip),%r14
mov    0x2e8a(%rip),%r15
lea    0x8(%rsp),%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <printf@plt>
mov    (%r15),%edi
call   1050 <fflush@plt>
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <nanosleep@plt>
mov    0x2ebc(%rip),%esi
add    $0x1,%esi
mov    %esi,0x2eb3(%rip)
jmp    1160 <main+0x30>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@__const.main.tsp = %struct.timespec { i32 500000000, i32 0 }
@main.i = internal global i32 0
@.str = [4 x i8] c"\0D%d\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.timespec
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%7 = bitcast %struct.timespec* %6 to i8*
br label %8
8: ; preds = %2, %8
%9 = load i32, i32* @main.i
%10 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = load i32, i32* @stdout
%12 = call i32 @fflush(i32 %11)
%13 = call i32 @nanosleep(%struct.timespec* %6, i32* null)
%14 = load i32, i32* @main.i
%15 = add nsw i32 %14, 1
store i32 %15, i32* @main.i
br label %8
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@main.i = internal unnamed_addr global i32 0
@.str = [4 x i8] c"\0D%d\00"
@stdout = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timespec*
%5 = bitcast i64* %3 to i8*
store i64 500000000, i64* %3
%6 = load i32, i32* @main.i
br label %7
7: ; preds = %7, %2
%8 = phi i32 [ %14, %7 ], [ %6, %2 ]
%9 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 %8)
%10 = load i32, i32* @stdout
%11 = call i32 @fflush(i32 %10)
%12 = call i32 @nanosleep(%struct.timespec* %4, i32* null)
%13 = load i32, i32* @main.i
%14 = add nsw i32 %13, 1
store i32 %14, i32* @main.i
br label %7
}