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

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fflush(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, double, double);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, double *, double *);
int /*<<< orphan*/ nanosleep(struct timespec *, struct timespec *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ *stdout;
int main(int argc, char **argv) {
  if (argc < 2) {
    printf("Requires two arguments: delay time (ms) and filename\n");
    exit(1);
  }
  int milliseconds = atoi(argv[1]);
  FILE *f = (argc == 2) ? stdout : fopen(argv[2], "w");
  double initial_position = 0.0;
  double velocity = 1.0;
  if (argc == 4) {
    FILE *in_f = fopen(argv[3], "r");
    fscanf(in_f, "%lf %lf", &initial_position, &velocity);
  }
  struct timespec sleep_time, foo;
  sleep_time.tv_sec = milliseconds / 1000;
  sleep_time.tv_nsec = (milliseconds % 1000) * 1000000;
  int max_steps = 1000000;
  double position = initial_position;
  int i;
  for (i = 0; i < max_steps; i++) {
    position += velocity;
    fprintf(f, "%lf %lf\n", position, velocity);
    fflush(f);
    nanosleep(&sleep_time, &foo);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
jge    11b6 <main+0x36>
lea    0xe61(%rip),%rdi
call   1030 <printf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x2,-0x8(%rbp)
jne    11e3 <main+0x63>
mov    0x2e09(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x68(%rbp)
jmp    11fb <main+0x7b>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
lea    0xe4c(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x68(%rbp)
mov    -0x68(%rbp),%rax
mov    %rax,-0x20(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0x28(%rbp)
movsd  0xded(%rip),%xmm0
movsd  %xmm0,-0x30(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    1252 <main+0xd2>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rdi
lea    0xe0f(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
lea    0xdfd(%rip),%rsi
lea    -0x28(%rbp),%rdx
lea    -0x30(%rbp),%rcx
call   1050 <fscanf@plt>
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
mov    %eax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    $0x3e8,%ecx
cltd
idiv   %ecx
imul   $0xf4240,%edx,%eax
mov    %eax,-0x3c(%rbp)
movl   $0xf4240,-0x4c(%rbp)
movsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
movl   $0x0,-0x5c(%rbp)
mov    -0x5c(%rbp),%eax
cmp    -0x4c(%rbp),%eax
jge    12e5 <main+0x165>
movsd  -0x30(%rbp),%xmm0
addsd  -0x58(%rbp),%xmm0
movsd  %xmm0,-0x58(%rbp)
mov    -0x20(%rbp),%rdi
movsd  -0x58(%rbp),%xmm0
movsd  -0x30(%rbp),%xmm1
lea    0xd8e(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x20(%rbp),%rdi
call   1070 <fflush@plt>
lea    -0x40(%rbp),%rdi
lea    -0x48(%rbp),%rsi
call   1040 <nanosleep@plt>
mov    -0x5c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x5c(%rbp)
jmp    128c <main+0x10c>
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x30,%rsp
cmp    $0x1,%edi
jle    12ce <main+0x14e>
mov    %rsi,%r15
mov    %edi,%ebp
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%r14d
cmp    $0x2,%ebp
jne    11d3 <main+0x53>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rbx
movq   $0x0,0x10(%rsp)
movabs $0x3ff0000000000000,%rax
mov    %rax,0x8(%rsp)
xorpd  %xmm0,%xmm0
jmp    1236 <main+0xb6>
mov    0x10(%r15),%rdi
lea    0xe22(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,%rbx
movq   $0x0,0x10(%rsp)
movabs $0x3ff0000000000000,%rax
mov    %rax,0x8(%rsp)
xorpd  %xmm0,%xmm0
cmp    $0x4,%ebp
jne    1236 <main+0xb6>
mov    0x18(%r15),%rdi
lea    0xdf0(%rip),%rsi
call   1080 <fopen@plt>
lea    0xde6(%rip),%rsi
lea    0x10(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rax,%rdi
call   1050 <fscanf@plt>
movsd  0x10(%rsp),%xmm0
movslq %r14d,%rax
imul   $0x10624dd3,%rax,%rcx
mov    %rcx,%rdx
shr    $0x3f,%rdx
sar    $0x26,%rcx
add    %edx,%ecx
mov    %ecx,0x20(%rsp)
imul   $0x3e8,%ecx,%ecx
sub    %ecx,%eax
imul   $0xf4240,%eax,%eax
mov    %eax,0x24(%rsp)
mov    $0xf4240,%ebp
lea    0xd9d(%rip),%r14
lea    0x20(%rsp),%r15
lea    0x28(%rsp),%r12
nopl   0x0(%rax)
movsd  0x8(%rsp),%xmm1
addsd  %xmm1,%xmm0
movsd  %xmm0,0x18(%rsp)
mov    %rbx,%rdi
mov    %r14,%rsi
movsd  0x18(%rsp),%xmm0
call   1060 <fprintf@plt>
mov    %rbx,%rdi
call   1070 <fflush@plt>
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <nanosleep@plt>
movsd  0x18(%rsp),%xmm0
add    $0xffffffff,%ebp
jne    1280 <main+0x100>
xor    %eax,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd40(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.timespec = type { i32, i32 }
@.str = [54 x i8] c"Requires two arguments: delay time (ms) and filename\0A\00"
@stdout = global i32* null
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [8 x i8] c"%lf %lf\00"
@.str.4 = [9 x i8] c"%lf %lf\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32*
%8 = alloca double
%9 = alloca double
%10 = alloca i32*
%11 = alloca %struct.timespec
%12 = alloca %struct.timespec
%13 = alloca i32
%14 = alloca double
%15 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %16, 2
br i1 %17, label %18, label %21
18: ; preds = %2
%19 = call i32 @printf(i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str, i64 0, i64 0))
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %2
%22 = load i8**, i8*** %5
%23 = getelementptr inbounds i8*, i8** %22, i64 1
%24 = load i8*, i8** %23
%25 = call i32 @atoi(i8* %24)
store i32 %25, i32* %6
%26 = load i32, i32* %4
%27 = icmp eq i32 %26, 2
br i1 %27, label %28, label %30
28: ; preds = %21
%29 = load i32*, i32** @stdout
br label %35
30: ; preds = %21
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 2
%33 = load i8*, i8** %32
%34 = call i32* @fopen(i8* %33, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %35
35: ; preds = %30, %28
%36 = phi i32* [ %29, %28 ], [ %34, %30 ]
store i32* %36, i32** %7
store double 0.000000e+00, double* %8
store double 1.000000e+00, double* %9
%37 = load i32, i32* %4
%38 = icmp eq i32 %37, 4
br i1 %38, label %39, label %46
39: ; preds = %35
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 3
%42 = load i8*, i8** %41
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %43, i32** %10
%44 = load i32*, i32** %10
%45 = call i32 @fscanf(i32* %44, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double* %8, double* %9)
br label %46
46: ; preds = %39, %35
%47 = load i32, i32* %6
%48 = sdiv i32 %47, 1000
%49 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 0
store i32 %48, i32* %49
%50 = load i32, i32* %6
%51 = srem i32 %50, 1000
%52 = mul nsw i32 %51, 1000000
%53 = getelementptr inbounds %struct.timespec, %struct.timespec* %11, i32 0, i32 1
store i32 %52, i32* %53
store i32 1000000, i32* %13
%54 = load double, double* %8
store double %54, double* %14
store i32 0, i32* %15
br label %55
55: ; preds = %70, %46
%56 = load i32, i32* %15
%57 = load i32, i32* %13
%58 = icmp slt i32 %56, %57
br i1 %58, label %59, label %73
59: ; preds = %55
%60 = load double, double* %9
%61 = load double, double* %14
%62 = fadd double %61, %60
store double %62, double* %14
%63 = load i32*, i32** %7
%64 = load double, double* %14
%65 = load double, double* %9
%66 = call i32 @fprintf(i32* %63, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), double %64, double %65)
%67 = load i32*, i32** %7
%68 = call i32 @fflush(i32* %67)
%69 = call i32 @nanosleep(%struct.timespec* %11, %struct.timespec* %12)
br label %70
70: ; preds = %59
%71 = load i32, i32* %15
%72 = add nsw i32 %71, 1
store i32 %72, i32* %15
br label %55
73: ; preds = %55
%74 = load i32, i32* %3
ret i32 %74
}

LLVM IR (-O2):
%struct.timespec = type { i32, i32 }
@stdout = global i32* null
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [2 x i8] c"r\00"
@.str.3 = [8 x i8] c"%lf %lf\00"
@.str.4 = [9 x i8] c"%lf %lf\0A\00"
@str = [53 x i8] c"Requires two arguments: delay time (ms) and filename\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca double
%4 = alloca double
%5 = alloca %struct.timespec
%6 = alloca %struct.timespec
%7 = icmp slt i32 %0, 2
br i1 %7, label %8, label %11
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([53 x i8], [53 x i8]* @str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @atoi(i8* %13)
%15 = icmp eq i32 %0, 2
br i1 %15, label %16, label %20
16: ; preds = %11
%17 = load i32*, i32** @stdout
%18 = bitcast double* %3 to i8*
store double 0.000000e+00, double* %3
%19 = bitcast double* %4 to i8*
store double 1.000000e+00, double* %4
br label %33
20: ; preds = %11
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call i32* @fopen(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%24 = bitcast double* %3 to i8*
store double 0.000000e+00, double* %3
%25 = bitcast double* %4 to i8*
store double 1.000000e+00, double* %4
%26 = icmp eq i32 %0, 4
br i1 %26, label %27, label %33
27: ; preds = %20
%28 = getelementptr inbounds i8*, i8** %1, i64 3
%29 = load i8*, i8** %28
%30 = tail call i32* @fopen(i8* %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32 @fscanf(i32* %30, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0), double* %3, double* %4)
%32 = load double, double* %3
br label %33
33: ; preds = %16, %27, %20
%34 = phi double [ 0.000000e+00, %16 ], [ %32, %27 ], [ 0.000000e+00, %20 ]
%35 = phi i32* [ %17, %16 ], [ %23, %27 ], [ %23, %20 ]
%36 = bitcast %struct.timespec* %5 to i8*
%37 = bitcast %struct.timespec* %6 to i8*
%38 = sdiv i32 %14, 1000
%39 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 0
store i32 %38, i32* %39
%40 = srem i32 %14, 1000
%41 = mul nsw i32 %40, 1000000
%42 = getelementptr inbounds %struct.timespec, %struct.timespec* %5, i64 0, i32 1
store i32 %41, i32* %42
br label %43
43: ; preds = %33, %43
%44 = phi i32 [ 0, %33 ], [ %51, %43 ]
%45 = phi double [ %34, %33 ], [ %47, %43 ]
%46 = load double, double* %4
%47 = fadd double %45, %46
%48 = call i32 @fprintf(i32* %35, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), double %47, double %46)
%49 = call i32 @fflush(i32* %35)
%50 = call i32 @nanosleep(%struct.timespec* %5, %struct.timespec* %6)
%51 = add nuw nsw i32 %44, 1
%52 = icmp eq i32 %51, 1000000
br i1 %52, label %53, label %43
53: ; preds = %43
%54 = bitcast double* %3 to i8*
%55 = bitcast double* %4 to i8*
ret i32 0
}