#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 time_t;

/* Variables and functions */
int O_APPEND;
int O_CREAT;
int O_EXCL;
int O_TRUNC;
int O_WRONLY;
int atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ gather_data(int);
int /*<<< orphan*/ localtime(int *);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strftime(char *, int, char *, int /*<<< orphan*/);
int time(int /*<<< orphan*/);
int main(int argc, char *argv[]) {
  time_t start_time;
  int interval = 30;
  int duration = 86400;
  int fd;
  char pathname[1024], *path;
  start_time = time(0);
  if (argc > 1) {
    interval = atoi(argv[1]);
    if (interval < 1) {
      fprintf(stderr, "Invalid interval (must be > 0)\n");
      exit(1);
    }
  }
  if (argc > 2) {
    duration = atoi(argv[2]);
    if (duration < interval) {
      fprintf(stderr, "Invalid duration (must be > %d)\n", interval);
      exit(1);
    }
  }
  if (argc > 3) {
    path = argv[3];
  } else {
    strftime(pathname, 1024, "%T_proc.dat", localtime(&start_time));
    path = pathname;
  }
  if (argc > 4) {
    fprintf(stderr, "Too many arguments\n");
    exit(1);
  }
  fd = open(path, O_WRONLY | O_TRUNC | O_APPEND | O_CREAT | O_EXCL);
  if (fd < 0) {
    perror("Cannot create output file");
    exit(1);
  }
  printf("Writing output to: %s\n", path);
  while (time(0) < start_time + duration) {
    int start = time(0);
    gather_data(fd);
    int end = time(0);
    sleep(interval - (end - start));
  }
  close(fd);
  exit(0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1e,-0x18(%rbp)
movl   $0x15180,-0x1c(%rbp)
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x1,-0x8(%rbp)
jle    123b <main+0x7b>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x1,-0x18(%rbp)
jge    1236 <main+0x76>
mov    0x2db4(%rip),%rax
mov    (%rax),%edi
lea    0xddb(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    123b <main+0x7b>
cmpl   $0x2,-0x8(%rbp)
jle    128a <main+0xca>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10b0 <atoi@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    1285 <main+0xc5>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%edx
lea    0xdac(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
jmp    128a <main+0xca>
cmpl   $0x3,-0x8(%rbp)
jle    12a8 <main+0xe8>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x428(%rbp)
jmp    12e7 <main+0x127>
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x14(%rbp),%rdi
call   1030 <localtime@plt>
mov    -0x438(%rbp),%rdi
mov    %eax,%ecx
mov    $0x400,%esi
lea    0xd6d(%rip),%rdx
call   1080 <strftime@plt>
lea    -0x420(%rbp),%rax
mov    %rax,-0x428(%rbp)
cmpl   $0x4,-0x8(%rbp)
jle    1312 <main+0x152>
mov    0x2cd8(%rip),%rax
mov    (%rax),%edi
lea    0xd4c(%rip),%rsi
mov    $0x0,%al
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x428(%rbp),%rdi
mov    0x2c98(%rip),%rax
mov    (%rax),%esi
mov    0x2cc7(%rip),%rax
or     (%rax),%esi
mov    0x2c7e(%rip),%rax
or     (%rax),%esi
mov    0x2cbd(%rip),%rax
or     (%rax),%esi
mov    0x2c84(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    136e <main+0x1ae>
lea    0xd02(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    -0x428(%rbp),%rsi
lea    0xcff(%rip),%rdi
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    -0x14(%rbp),%ecx
add    -0x1c(%rbp),%ecx
cmp    %ecx,%eax
jge    13d3 <main+0x213>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x20(%rbp),%edi
call   10c0 <gather_data@plt>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,-0x430(%rbp)
mov    -0x18(%rbp),%edi
mov    -0x430(%rbp),%eax
sub    -0x42c(%rbp),%eax
sub    %eax,%edi
call   10e0 <sleep@plt>
jmp    1381 <main+0x1c1>
mov    -0x20(%rbp),%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
mov    %rsi,%r15
mov    %edi,%ebx
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,0xc(%rsp)
mov    $0x1e,%r14d
mov    $0x15180,%r12d
cmp    $0x2,%ebx
jl     126f <main+0xaf>
mov    0x8(%r15),%rdi
call   10b0 <atoi@plt>
test   %eax,%eax
jg     121e <main+0x5e>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %eax,%r14d
cmp    $0x3,%ebx
jb     126f <main+0xaf>
mov    0x10(%r15),%rdi
call   10b0 <atoi@plt>
cmp    %r14d,%eax
jge    1258 <main+0x98>
mov    0x2d95(%rip),%rax
mov    (%rax),%edi
lea    0xddc(%rip),%rsi
mov    %r14d,%edx
xor    %eax,%eax
call   1060 <fprintf@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %eax,%r12d
cmp    $0x3,%ebx
jbe    126f <main+0xaf>
cmp    $0x4,%ebx
jne    1345 <main+0x185>
mov    0x18(%r15),%r15
jmp    1294 <main+0xd4>
lea    0x10(%rsp),%r15
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
lea    0xdbc(%rip),%rdx
mov    %r15,%rdi
mov    $0x400,%esi
mov    %eax,%ecx
call   1080 <strftime@plt>
mov    0x2d1d(%rip),%rax
mov    0x2d4e(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
mov    0x2d03(%rip),%rax
or     (%rax),%esi
mov    0x2d42(%rip),%rax
or     (%rax),%esi
mov    0x2d09(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
call   1090 <open@plt>
test   %eax,%eax
jns    12e3 <main+0x123>
lea    0xd8d(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10d0 <exit@plt>
mov    %eax,%ebx
lea    0xd8f(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
xor    %edi,%edi
call   1070 <time@plt>
mov    0xc(%rsp),%ecx
add    %r12d,%ecx
cmp    %ecx,%eax
jge    1337 <main+0x177>
xor    %edi,%edi
call   1070 <time@plt>
mov    %eax,%ebp
mov    %ebx,%edi
call   10c0 <gather_data@plt>
xor    %edi,%edi
call   1070 <time@plt>
add    %r14d,%ebp
sub    %eax,%ebp
mov    %ebp,%edi
call   10e0 <sleep@plt>
jmp    1300 <main+0x140>
mov    %ebx,%edi
call   1050 <close@plt>
xor    %edi,%edi
call   10d0 <exit@plt>
mov    0x2c84(%rip),%rax
mov    (%rax),%edi
lea    0xcf8(%rip),%rsi
jmp    120d <main+0x4d>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [32 x i8] c"Invalid interval (must be > 0)\0A\00"
@.str.1 = [33 x i8] c"Invalid duration (must be > %d)\0A\00"
@.str.2 = [12 x i8] c"%T_proc.dat\00"
@.str.3 = [20 x i8] c"Too many arguments\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.4 = [26 x i8] c"Cannot create output file\00"
@.str.5 = [23 x i8] c"Writing output to: %s\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 i32
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 30, i32* %7
store i32 86400, i32* %8
%14 = call i32 @time(i32 0)
store i32 %14, i32* %6
%15 = load i32, i32* %4
%16 = icmp sgt i32 %15, 1
br i1 %16, label %17, label %29
17: ; preds = %2
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 1
%20 = load i8*, i8** %19
%21 = call i32 @atoi(i8* %20)
store i32 %21, i32* %7
%22 = load i32, i32* %7
%23 = icmp slt i32 %22, 1
br i1 %23, label %24, label %28
24: ; preds = %17
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %17
br label %29
29: ; preds = %28, %2
%30 = load i32, i32* %4
%31 = icmp sgt i32 %30, 2
br i1 %31, label %32, label %46
32: ; preds = %29
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
%36 = call i32 @atoi(i8* %35)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = load i32, i32* %7
%39 = icmp slt i32 %37, %38
br i1 %39, label %40, label %45
40: ; preds = %32
%41 = load i32, i32* @stderr
%42 = load i32, i32* %7
%43 = call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %42)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %32
br label %46
46: ; preds = %45, %29
%47 = load i32, i32* %4
%48 = icmp sgt i32 %47, 3
br i1 %48, label %49, label %53
49: ; preds = %46
%50 = load i8**, i8*** %5
%51 = getelementptr inbounds i8*, i8** %50, i64 3
%52 = load i8*, i8** %51
store i8* %52, i8** %11
br label %58
53: ; preds = %46
%54 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%55 = call i32 @localtime(i32* %6)
%56 = call i32 @strftime(i8* %54, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %55)
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
store i8* %57, i8** %11
br label %58
58: ; preds = %53, %49
%59 = load i32, i32* %4
%60 = icmp sgt i32 %59, 4
br i1 %60, label %61, label %65
61: ; preds = %58
%62 = load i32, i32* @stderr
%63 = call i32 (i32, i8*, ...) @fprintf(i32 %62, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%64 = call i32 @exit(i32 1)
unreachable
65: ; preds = %58
%66 = load i8*, i8** %11
%67 = load i32, i32* @O_WRONLY
%68 = load i32, i32* @O_TRUNC
%69 = or i32 %67, %68
%70 = load i32, i32* @O_APPEND
%71 = or i32 %69, %70
%72 = load i32, i32* @O_CREAT
%73 = or i32 %71, %72
%74 = load i32, i32* @O_EXCL
%75 = or i32 %73, %74
%76 = call i32 @open(i8* %66, i32 %75)
store i32 %76, i32* %9
%77 = load i32, i32* %9
%78 = icmp slt i32 %77, 0
br i1 %78, label %79, label %82
79: ; preds = %65
%80 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%81 = call i32 @exit(i32 1)
unreachable
82: ; preds = %65
%83 = load i8*, i8** %11
%84 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %83)
br label %85
85: ; preds = %91, %82
%86 = call i32 @time(i32 0)
%87 = load i32, i32* %6
%88 = load i32, i32* %8
%89 = add nsw i32 %87, %88
%90 = icmp slt i32 %86, %89
br i1 %90, label %91, label %102
91: ; preds = %85
%92 = call i32 @time(i32 0)
store i32 %92, i32* %12
%93 = load i32, i32* %9
%94 = call i32 @gather_data(i32 %93)
%95 = call i32 @time(i32 0)
store i32 %95, i32* %13
%96 = load i32, i32* %7
%97 = load i32, i32* %13
%98 = load i32, i32* %12
%99 = sub nsw i32 %97, %98
%100 = sub nsw i32 %96, %99
%101 = call i32 @sleep(i32 %100)
br label %85
102: ; preds = %85
%103 = load i32, i32* %9
%104 = call i32 @close(i32 %103)
%105 = call i32 @exit(i32 0)
unreachable
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [32 x i8] c"Invalid interval (must be > 0)\0A\00"
@.str.1 = [33 x i8] c"Invalid duration (must be > %d)\0A\00"
@.str.2 = [12 x i8] c"%T_proc.dat\00"
@.str.3 = [20 x i8] c"Too many arguments\0A\00"
@O_WRONLY = global i32 0
@O_TRUNC = global i32 0
@O_APPEND = global i32 0
@O_CREAT = global i32 0
@O_EXCL = global i32 0
@.str.4 = [26 x i8] c"Cannot create output file\00"
@.str.5 = [23 x i8] c"Writing output to: %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i32
%4 = alloca [1024 x i8]
%5 = bitcast i32* %3 to i8*
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = tail call i32 @time(i32 0)
store i32 %7, i32* %3
%8 = icmp sgt i32 %0, 1
br i1 %8, label %9, label %31
9: ; preds = %2
%10 = getelementptr inbounds i8*, i8** %1, i64 1
%11 = load i8*, i8** %10
%12 = tail call i32 @atoi(i8* %11)
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %18
14: ; preds = %9
%15 = load i32, i32* @stderr
%16 = tail call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %9
%19 = icmp ugt i32 %0, 2
br i1 %19, label %20, label %31
20: ; preds = %18
%21 = getelementptr inbounds i8*, i8** %1, i64 2
%22 = load i8*, i8** %21
%23 = tail call i32 @atoi(i8* %22)
%24 = icmp slt i32 %23, %12
br i1 %24, label %25, label %29
25: ; preds = %20
%26 = load i32, i32* @stderr
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.1, i64 0, i64 0), i32 %12)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %20
%30 = icmp ugt i32 %0, 3
br i1 %30, label %36, label %31
31: ; preds = %29, %18, %2
%32 = phi i32 [ %23, %29 ], [ 86400, %18 ], [ 86400, %2 ]
%33 = phi i32 [ %12, %29 ], [ %12, %18 ], [ 30, %2 ]
%34 = call i32 @localtime(i32* %3)
%35 = call i32 @strftime(i8* %6, i32 1024, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %34)
br label %44
36: ; preds = %29
%37 = getelementptr inbounds i8*, i8** %1, i64 3
%38 = load i8*, i8** %37
%39 = icmp eq i32 %0, 4
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i32, i32* @stderr
%42 = tail call i32 (i32, i8*, ...) @fprintf(i32 %41, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.3, i64 0, i64 0))
%43 = tail call i32 @exit(i32 1)
unreachable
44: ; preds = %31, %36
%45 = phi i8* [ %6, %31 ], [ %38, %36 ]
%46 = phi i32 [ %33, %31 ], [ %12, %36 ]
%47 = phi i32 [ %32, %31 ], [ %23, %36 ]
%48 = load i32, i32* @O_WRONLY
%49 = load i32, i32* @O_TRUNC
%50 = or i32 %49, %48
%51 = load i32, i32* @O_APPEND
%52 = or i32 %50, %51
%53 = load i32, i32* @O_CREAT
%54 = or i32 %52, %53
%55 = load i32, i32* @O_EXCL
%56 = or i32 %54, %55
%57 = call i32 @open(i8* %45, i32 %56)
%58 = icmp slt i32 %57, 0
br i1 %58, label %59, label %62
59: ; preds = %44
%60 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
%61 = call i32 @exit(i32 1)
unreachable
62: ; preds = %44
%63 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.5, i64 0, i64 0), i8* %45)
%64 = call i32 @time(i32 0)
%65 = load i32, i32* %3
%66 = add nsw i32 %65, %47
%67 = icmp slt i32 %64, %66
br i1 %67, label %68, label %79
68: ; preds = %62, %68
%69 = call i32 @time(i32 0)
%70 = call i32 @gather_data(i32 %57)
%71 = call i32 @time(i32 0)
%72 = add i32 %69, %46
%73 = sub i32 %72, %71
%74 = call i32 @sleep(i32 %73)
%75 = call i32 @time(i32 0)
%76 = load i32, i32* %3
%77 = add nsw i32 %76, %47
%78 = icmp slt i32 %75, %77
br i1 %78, label %68, label %79
79: ; preds = %68, %62
%80 = call i32 @close(i32 %57)
%81 = call i32 @exit(i32 0)
unreachable
}