#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*/ uint8_t;
typedef double time_t;
typedef int /*<<< orphan*/ msgbuf;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ fwrite(int /*<<< orphan*/ *, size_t, int,
                          int /*<<< orphan*/ *);
int get_message_hour(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ *output_for_hour(char *, int, int);
size_t read_message(int /*<<< orphan*/ *, int /*<<< orphan*/ *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ *stdin;
char *strerror(int /*<<< orphan*/);
double time(int /*<<< orphan*/ *);
int main(int argc, char *argv[]) {
  int msgcnt = 0;
  int filecnt = 0;
  int msgpn = 0;
  int lastpn = 0;
  time_t start;
  int period = 3600;
  char *prefix;
  char *infn;
  uint8_t msgbuf[65536];
  size_t msglen;
  FILE *outfp = NULL;
  FILE *infp = NULL;
  if (argc < 3) {
    fprintf(stderr, "usage: %s period_sec out_prefix [in.ipfix] [< in.ipfix]\n",
            argv[0]);
    exit(1);
  }
  period = atoi(argv[1]);
  prefix = argv[2];
  if (argc == 4) {
    infn = argv[3];
    infp = fopen(infn, "rb");
    if (!infp) {
      fprintf(stderr, "could not open input file %s: %s", infn,
              strerror(errno));
      exit(1);
    }
  } else {
    infn = "-";
    infp = stdin;
  }
  start = time(NULL);
  while ((msglen = read_message(infp, msgbuf, sizeof(msgbuf)))) {
    msgpn = get_message_hour(msgbuf, period);
    if (msgpn > lastpn) {
      if (outfp) {
        fclose(outfp);
      }
      outfp = output_for_hour(prefix, msgpn, period);
      filecnt++;
      lastpn = msgpn;
    }
    fwrite(msgbuf, msglen, 1, outfp);
    msgcnt++;
  }
  time_t elapsed = time(NULL) - start;
  fprintf(stderr, "wrote %u messages to %u files in %ld sec (%.2f m/s).\n",
          msgcnt, filecnt, elapsed, (double)msgcnt / elapsed);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40070,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
movl   $0xe10,-0x2c(%rbp)
movq   $0x0,-0x40050(%rbp)
movq   $0x0,-0x40058(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    1234 <main+0x84>
mov    0x2dc5(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xddd(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rax
mov    %rax,-0x38(%rbp)
cmpl   $0x4,-0x8(%rbp)
jne    12e0 <main+0x130>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   1080 <fopen@plt>
mov    %rax,-0x40058(%rbp)
cmpq   $0x0,-0x40058(%rbp)
jne    12db <main+0x12b>
mov    0x2d46(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4006c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x40068(%rbp)
mov    0x2d24(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
mov    -0x4006c(%rbp),%edi
mov    -0x40068(%rbp),%rdx
mov    %rax,%rcx
lea    0xd72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
jmp    12fc <main+0x14c>
lea    0xd76(%rip),%rax
mov    %rax,-0x40(%rbp)
mov    0x2cf6(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x40058(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
movsd  %xmm0,-0x28(%rbp)
mov    -0x40058(%rbp),%rdi
lea    -0x40040(%rbp),%rsi
mov    $0x40000,%edx
call   10a0 <read_message@plt>
mov    %rax,-0x40048(%rbp)
cmp    $0x0,%rax
je     13bd <main+0x20d>
lea    -0x40040(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   1040 <get_message_hour@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x20(%rbp),%eax
jle    1390 <main+0x1e0>
cmpq   $0x0,-0x40050(%rbp)
je     136b <main+0x1bb>
mov    -0x40050(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x38(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x2c(%rbp),%edx
call   1070 <output_for_hour@plt>
mov    %rax,-0x40050(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x20(%rbp)
lea    -0x40040(%rbp),%rdi
mov    -0x40048(%rbp),%rsi
mov    -0x40050(%rbp),%rcx
mov    $0x1,%edx
call   10c0 <fwrite@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    130a <main+0x15a>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
subsd  -0x28(%rbp),%xmm0
movsd  %xmm0,-0x40060(%rbp)
ff
mov    0x2bfe(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%edx
mov    -0x18(%rbp),%ecx
movsd  -0x40060(%rbp),%xmm0
ff
cvtsi2sdl -0x14(%rbp),%xmm1
divsd  -0x40060(%rbp),%xmm1
ff
lea    0xc61(%rip),%rsi
mov    $0x2,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x40070,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x40038,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jle    1370 <main+0x1c0>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,0xc(%rsp)
mov    0x10(%rbx),%rax
mov    %rax,0x20(%rsp)
cmp    $0x4,%ebp
jne    123c <main+0x8c>
mov    0x18(%rbx),%rbx
lea    0xe44(%rip),%rsi
mov    %rbx,%rdi
call   1080 <fopen@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1246 <main+0x96>
mov    0x2dcc(%rip),%rax
mov    (%rax),%ebp
mov    0x2dbb(%rip),%rax
mov    (%rax),%edi
call   10d0 <strerror@plt>
lea    0xe19(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%rbp
xor    %eax,%eax
mov    %rax,0x10(%rsp)
xor    %edi,%edi
call   1060 <time@plt>
movsd  %xmm0,0x18(%rsp)
lea    0x30(%rsp),%rsi
mov    %rbp,%rdi
mov    $0x40000,%edx
call   10a0 <read_message@plt>
mov    $0x0,%r13d
test   %rax,%rax
je     1327 <main+0x177>
mov    %rax,%r14
xor    %r13d,%r13d
lea    0x30(%rsp),%rbx
xor    %r15d,%r15d
xor    %eax,%eax
mov    %rax,0x10(%rsp)
movl   $0x0,0x8(%rsp)
mov    %rbp,0x28(%rsp)
jmp    12d7 <main+0x127>
nop
mov    %r15,%rcx
mov    %rbp,%rbx
mov    %rbp,%rdi
mov    %r14,%rsi
mov    $0x1,%edx
mov    %rcx,%r15
call   10c0 <fwrite@plt>
add    $0x1,%r13d
mov    0x28(%rsp),%rdi
mov    %rbp,%rsi
mov    $0x40000,%edx
call   10a0 <read_message@plt>
mov    %rax,%r14
test   %rax,%rax
je     1327 <main+0x177>
mov    %rbx,%rbp
mov    %rbx,%rdi
mov    0xc(%rsp),%esi
call   1040 <get_message_hour@plt>
cmp    0x8(%rsp),%eax
jle    12a0 <main+0xf0>
mov    %eax,%r12d
test   %r15,%r15
je     12fc <main+0x14c>
mov    %r15,%rdi
call   1030 <fclose@plt>
mov    0x20(%rsp),%rdi
mov    %r12d,%esi
mov    0xc(%rsp),%edx
call   1070 <output_for_hour@plt>
mov    %rax,%rcx
mov    0x10(%rsp),%rax
add    $0x1,%eax
mov    %rax,0x10(%rsp)
mov    %r12d,0x8(%rsp)
jmp    12a3 <main+0xf3>
xor    %edi,%edi
call   1060 <time@plt>
subsd  0x18(%rsp),%xmm0
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
cvtsi2sd %r13d,%xmm1
divsd  %xmm0,%xmm1
lea    0xd10(%rip),%rsi
mov    %r13d,%edx
mov    0x10(%rsp),%rcx
mov    $0x2,%al
call   1050 <fprintf@plt>
xor    %eax,%eax
add    $0x40038,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2c61(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xc7d(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [57 x i8] c"usage: %s period_sec out_prefix [in.ipfix] [< in.ipfix]\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [33 x i8] c"could not open input file %s: %s\00"
@errno = global i32 0
@.str.3 = [2 x i8] c"-\00"
@stdin = global i32* null
@.str.4 = [54 x i8] c"wrote %u messages to %u files in %ld sec (%.2f m/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 double
%11 = alloca i32
%12 = alloca i8*
%13 = alloca i8*
%14 = alloca [65536 x i32]
%15 = alloca i64
%16 = alloca i32*
%17 = alloca i32*
%18 = alloca double
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
store i32 0, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 3600, i32* %11
store i32* null, i32** %16
store i32* null, i32** %17
%19 = load i32, i32* %4
%20 = icmp slt i32 %19, 3
br i1 %20, label %21, label %28
21: ; preds = %2
%22 = load i32, i32* @stderr
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 0
%25 = load i8*, i8** %24
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %22, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = call i32 @exit(i32 1)
unreachable
28: ; preds = %2
%29 = load i8**, i8*** %5
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8*, i8** %30
%32 = call i32 @atoi(i8* %31)
store i32 %32, i32* %11
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 2
%35 = load i8*, i8** %34
store i8* %35, i8** %12
%36 = load i32, i32* %4
%37 = icmp eq i32 %36, 4
br i1 %37, label %38, label %54
38: ; preds = %28
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 3
%41 = load i8*, i8** %40
store i8* %41, i8** %13
%42 = load i8*, i8** %13
%43 = call i32* @fopen(i8* %42, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
store i32* %43, i32** %17
%44 = load i32*, i32** %17
%45 = icmp ne i32* %44, null
br i1 %45, label %53, label %46
46: ; preds = %38
%47 = load i32, i32* @stderr
%48 = load i8*, i8** %13
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 (i32, i8*, ...) @fprintf(i32 %47, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %48, i8* %50)
%52 = call i32 @exit(i32 1)
unreachable
53: ; preds = %38
br label %56
54: ; preds = %28
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i8** %13
%55 = load i32*, i32** @stdin
store i32* %55, i32** %17
br label %56
56: ; preds = %54, %53
%57 = call double @time(i32* null)
store double %57, double* %10
br label %58
58: ; preds = %84, %56
%59 = load i32*, i32** %17
%60 = getelementptr inbounds [65536 x i32], [65536 x i32]* %14, i64 0, i64 0
%61 = call i64 @read_message(i32* %59, i32* %60, i32 262144)
store i64 %61, i64* %15
%62 = icmp ne i64 %61, 0
br i1 %62, label %63, label %91
63: ; preds = %58
%64 = getelementptr inbounds [65536 x i32], [65536 x i32]* %14, i64 0, i64 0
%65 = load i32, i32* %11
%66 = call i32 @get_message_hour(i32* %64, i32 %65)
store i32 %66, i32* %8
%67 = load i32, i32* %8
%68 = load i32, i32* %9
%69 = icmp sgt i32 %67, %68
br i1 %69, label %70, label %84
70: ; preds = %63
%71 = load i32*, i32** %16
%72 = icmp ne i32* %71, null
br i1 %72, label %73, label %76
73: ; preds = %70
%74 = load i32*, i32** %16
%75 = call i32 @fclose(i32* %74)
br label %76
76: ; preds = %73, %70
%77 = load i8*, i8** %12
%78 = load i32, i32* %8
%79 = load i32, i32* %11
%80 = call i32* @output_for_hour(i8* %77, i32 %78, i32 %79)
store i32* %80, i32** %16
%81 = load i32, i32* %7
%82 = add nsw i32 %81, 1
store i32 %82, i32* %7
%83 = load i32, i32* %8
store i32 %83, i32* %9
br label %84
84: ; preds = %76, %63
%85 = getelementptr inbounds [65536 x i32], [65536 x i32]* %14, i64 0, i64 0
%86 = load i64, i64* %15
%87 = load i32*, i32** %16
%88 = call i32 @fwrite(i32* %85, i64 %86, i32 1, i32* %87)
%89 = load i32, i32* %6
%90 = add nsw i32 %89, 1
store i32 %90, i32* %6
br label %58
91: ; preds = %58
%92 = call double @time(i32* null)
%93 = load double, double* %10
%94 = fsub double %92, %93
store double %94, double* %18
%95 = load i32, i32* @stderr
%96 = load i32, i32* %6
%97 = load i32, i32* %7
%98 = load double, double* %18
%99 = load i32, i32* %6
%100 = sitofp i32 %99 to double
%101 = load double, double* %18
%102 = fdiv double %100, %101
%103 = call i32 (i32, i8*, ...) @fprintf(i32 %95, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i32 %96, i32 %97, double %98, double %102)
ret i32 0
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [57 x i8] c"usage: %s period_sec out_prefix [in.ipfix] [< in.ipfix]\0A\00"
@.str.1 = [3 x i8] c"rb\00"
@.str.2 = [33 x i8] c"could not open input file %s: %s\00"
@errno = global i32 0
@stdin = global i32* null
@.str.4 = [54 x i8] c"wrote %u messages to %u files in %ld sec (%.2f m/s).\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [65536 x i32]
%4 = bitcast [65536 x i32]* %3 to i8*
%5 = icmp slt i32 %0, 3
br i1 %5, label %6, label %11
6: ; preds = %2
%7 = load i32, i32* @stderr
%8 = load i8*, i8** %1
%9 = tail call i32 (i32, i8*, ...) @fprintf(i32 %7, i8* getelementptr inbounds ([57 x i8], [57 x i8]* @.str, i64 0, i64 0), i8* %8)
%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 = getelementptr inbounds i8*, i8** %1, i64 2
%16 = load i8*, i8** %15
%17 = icmp eq i32 %0, 4
br i1 %17, label %18, label %29
18: ; preds = %11
%19 = getelementptr inbounds i8*, i8** %1, i64 3
%20 = load i8*, i8** %19
%21 = tail call i32* @fopen(i8* %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %31
23: ; preds = %18
%24 = load i32, i32* @stderr
%25 = load i32, i32* @errno
%26 = tail call i8* @strerror(i32 %25)
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %24, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i8* %20, i8* %26)
%28 = tail call i32 @exit(i32 1)
unreachable
29: ; preds = %11
%30 = load i32*, i32** @stdin
br label %31
31: ; preds = %18, %29
%32 = phi i32* [ %21, %18 ], [ %30, %29 ]
%33 = tail call double @time(i32* null)
%34 = getelementptr inbounds [65536 x i32], [65536 x i32]* %3, i64 0, i64 0
%35 = call i64 @read_message(i32* %32, i32* %34, i32 262144)
%36 = icmp eq i64 %35, 0
br i1 %36, label %60, label %37
37: ; preds = %31, %52
%38 = phi i64 [ %58, %52 ], [ %35, %31 ]
%39 = phi i32 [ %57, %52 ], [ 0, %31 ]
%40 = phi i32* [ %55, %52 ], [ null, %31 ]
%41 = phi i32 [ %54, %52 ], [ 0, %31 ]
%42 = phi i32 [ %53, %52 ], [ 0, %31 ]
%43 = call i32 @get_message_hour(i32* %34, i32 %14)
%44 = icmp sgt i32 %43, %42
br i1 %44, label %45, label %52
45: ; preds = %37
%46 = icmp eq i32* %40, null
br i1 %46, label %49, label %47
47: ; preds = %45
%48 = call i32 @fclose(i32* %40)
br label %49
49: ; preds = %47, %45
%50 = call i32* @output_for_hour(i8* %16, i32 %43, i32 %14)
%51 = add nsw i32 %41, 1
br label %52
52: ; preds = %49, %37
%53 = phi i32 [ %43, %49 ], [ %42, %37 ]
%54 = phi i32 [ %51, %49 ], [ %41, %37 ]
%55 = phi i32* [ %50, %49 ], [ %40, %37 ]
%56 = call i32 @fwrite(i32* %34, i64 %38, i32 1, i32* %55)
%57 = add nuw nsw i32 %39, 1
%58 = call i64 @read_message(i32* %32, i32* %34, i32 262144)
%59 = icmp eq i64 %58, 0
br i1 %59, label %60, label %37
60: ; preds = %52, %31
%61 = phi i32 [ 0, %31 ], [ %54, %52 ]
%62 = phi i32 [ 0, %31 ], [ %57, %52 ]
%63 = call double @time(i32* null)
%64 = fsub double %63, %33
%65 = load i32, i32* @stderr
%66 = sitofp i32 %62 to double
%67 = fdiv double %66, %64
%68 = call i32 (i32, i8*, ...) @fprintf(i32 %65, i8* getelementptr inbounds ([54 x i8], [54 x i8]* @.str.4, i64 0, i64 0), i32 %62, i32 %61, double %64, double %67)
ret i32 0
}