#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 */

/* Variables and functions */
int /*<<< orphan*/ beginTrans(int /*<<< orphan*/);
int /*<<< orphan*/ commitTrans();
int doCompress(int, int, int);
int getMinTs(int);
int getTime();
int /*<<< orphan*/ logMsg(int, char *, int, int, int, ...);
int /*<<< orphan*/ rollbackTrans();
int stub1(int);
int stub2(int);
int compressDbStage(int secKeepInterval, int oldDr, int newDr,
                    int (*fnRoundUp)(int)) {
  int keepBoundary = getTime() - secKeepInterval;
  int minTsRoundedUp = 0;
  int minTs = getMinTs(oldDr);
  if (minTs != 0) {
    minTsRoundedUp = (*fnRoundUp)(minTs);
  }
  int status = 1;
  int compressOk;
  logMsg(1, "In compressDbStage(%d,%d,%d), keep=%d, minTs=%d, minRounded=%d",
         secKeepInterval, oldDr, newDr, keepBoundary, minTs, minTsRoundedUp);
  beginTrans(0);
  while ((minTs != 0) && (minTsRoundedUp <= keepBoundary)) {
    logMsg(1, "compressDbStage loop: keep=%d, minTs=%d, minRounded=%d",
           keepBoundary, minTs, minTsRoundedUp);
    compressOk = doCompress(minTsRoundedUp, oldDr, newDr);
    if (!compressOk) {
      status = 0;
      break;
    }
    int prevMinTs = minTs;
    minTs = getMinTs(oldDr);
    if (minTs != 0) {
      if (minTs <= prevMinTs) {
        status = 0;
        logMsg(4, "getMinTs() error: oldDr=%d, prevMinTs=%d, minTs=%d,", oldDr,
               prevMinTs, minTs);
        break;
      }
      minTsRoundedUp = (*fnRoundUp)(minTs);
    }
  }
  if (status == 1) {
    commitTrans();
  } else {
    rollbackTrans();
  }
  return status;
}


x86 assembly (clang, -O0):
<compressDbStage>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
mov    %edx,-0xc(%rbp)
mov    %rcx,-0x18(%rbp)
mov    $0x0,%al
call   1060 <getTime@plt>
sub    -0x4(%rbp),%eax
mov    %eax,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
mov    -0x8(%rbp),%edi
call   1090 <getMinTs@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     11ba <compressDbStage+0x4a>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%edi
call   *%rax
mov    %eax,-0x20(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x4(%rbp),%edx
mov    -0x8(%rbp),%ecx
mov    -0xc(%rbp),%r8d
mov    -0x1c(%rbp),%r9d
mov    -0x24(%rbp),%r10d
mov    -0x20(%rbp),%eax
mov    $0x1,%edi
lea    0xe1e(%rip),%rsi
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1080 <logMsg@plt>
xor    %edi,%edi
call   1070 <beginTrans@plt>
xor    %eax,%eax
cmpl   $0x0,-0x24(%rbp)
mov    %al,-0x31(%rbp)
je     1213 <compressDbStage+0xa3>
mov    -0x20(%rbp),%eax
cmp    -0x1c(%rbp),%eax
setle  %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1223 <compressDbStage+0xb3>
jmp    12c8 <compressDbStage+0x158>
mov    -0x1c(%rbp),%edx
mov    -0x24(%rbp),%ecx
mov    -0x20(%rbp),%r8d
mov    $0x1,%edi
lea    0xe06(%rip),%rsi
mov    $0x0,%al
call   1080 <logMsg@plt>
mov    -0x20(%rbp),%edi
mov    -0x8(%rbp),%esi
mov    -0xc(%rbp),%edx
call   1040 <doCompress@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    1267 <compressDbStage+0xf7>
movl   $0x0,-0x28(%rbp)
jmp    12c8 <compressDbStage+0x158>
mov    -0x24(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x8(%rbp),%edi
call   1090 <getMinTs@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
je     12c3 <compressDbStage+0x153>
mov    -0x24(%rbp),%eax
cmp    -0x30(%rbp),%eax
jg     12b7 <compressDbStage+0x147>
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x24(%rbp),%r8d
mov    $0x4,%edi
lea    0xdcb(%rip),%rsi
mov    $0x0,%al
call   1080 <logMsg@plt>
jmp    12c8 <compressDbStage+0x158>
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%edi
call   *%rax
mov    %eax,-0x20(%rbp)
jmp    11f8 <compressDbStage+0x88>
cmpl   $0x1,-0x28(%rbp)
jne    12de <compressDbStage+0x16e>
mov    $0x0,%al
call   1030 <commitTrans@plt>
jmp    12e5 <compressDbStage+0x175>
mov    $0x0,%al
call   1050 <rollbackTrans@plt>
mov    -0x28(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<compressDbStage>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%rbp
mov    %edx,0x4(%rsp)
mov    %esi,%r12d
mov    %edi,%r14d
xor    %r13d,%r13d
xor    %eax,%eax
call   1060 <getTime@plt>
mov    %eax,%r15d
sub    %r14d,%r15d
mov    %r12d,%edi
call   1090 <getMinTs@plt>
mov    %eax,%ebx
test   %eax,%eax
je     11ad <compressDbStage+0x3d>
mov    %ebx,%edi
call   *%rbp
mov    %eax,%r13d
lea    0xe4c(%rip),%rsi
mov    $0x1,%edi
mov    %r14d,%edx
mov    %r12d,%ecx
mov    0x4(%rsp),%r8d
mov    %r15d,%r9d
xor    %eax,%eax
push   %r13
push   %rbx
call   1080 <logMsg@plt>
add    $0x10,%rsp
xor    %edi,%edi
call   1070 <beginTrans@plt>
test   %ebx,%ebx
je     123f <compressDbStage+0xcf>
cmp    %r15d,%r13d
jg     123f <compressDbStage+0xcf>
mov    %rbp,%r14
nopl   0x0(%rax,%rax,1)
mov    $0x1,%edi
lea    0xe43(%rip),%rsi
mov    %r15d,%edx
mov    %ebx,%ecx
mov    %r13d,%r8d
xor    %eax,%eax
call   1080 <logMsg@plt>
mov    %r13d,%edi
mov    %r12d,%esi
mov    0x4(%rsp),%edx
call   1040 <doCompress@plt>
test   %eax,%eax
je     1268 <compressDbStage+0xf8>
mov    %r12d,%edi
call   1090 <getMinTs@plt>
test   %eax,%eax
je     123f <compressDbStage+0xcf>
mov    %eax,%ebp
cmp    %ebx,%eax
jle    124d <compressDbStage+0xdd>
mov    %ebp,%edi
call   *%r14
mov    %eax,%r13d
mov    %ebp,%ebx
cmp    %r15d,%eax
jle    11f0 <compressDbStage+0x80>
xor    %eax,%eax
call   1030 <commitTrans@plt>
mov    $0x1,%ebx
jmp    1271 <compressDbStage+0x101>
lea    0xe22(%rip),%rsi
mov    $0x4,%edi
mov    %r12d,%edx
mov    %ebx,%ecx
mov    %ebp,%r8d
xor    %eax,%eax
call   1080 <logMsg@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   1050 <rollbackTrans@plt>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [63 x i8] c"In compressDbStage(%d,%d,%d), keep=%d, minTs=%d, minRounded=%d\00"
@.str.1 = [55 x i8] c"compressDbStage loop: keep=%d, minTs=%d, minRounded=%d\00"
@.str.2 = [52 x i8] c"getMinTs() error: oldDr=%d, prevMinTs=%d, minTs=%d,\00"
define i32 @compressDbStage(i32 %0, i32 %1, i32 %2, i32 (i32)* %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32 (i32)*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 (i32)* %3, i32 (i32)** %8
%15 = call i32 (...) @getTime()
%16 = load i32, i32* %5
%17 = sub nsw i32 %15, %16
store i32 %17, i32* %9
store i32 0, i32* %10
%18 = load i32, i32* %6
%19 = call i32 @getMinTs(i32 %18)
store i32 %19, i32* %11
%20 = load i32, i32* %11
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %26
22: ; preds = %4
%23 = load i32 (i32)*, i32 (i32)** %8
%24 = load i32, i32* %11
%25 = call i32 %23(i32 %24)
store i32 %25, i32* %10
br label %26
26: ; preds = %22, %4
store i32 1, i32* %12
%27 = load i32, i32* %5
%28 = load i32, i32* %6
%29 = load i32, i32* %7
%30 = load i32, i32* %9
%31 = load i32, i32* %11
%32 = load i32, i32* %10
%33 = call i32 (i32, i8*, i32, i32, i32, ...) @logMsg(i32 1, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 %27, i32 %28, i32 %29, i32 %30, i32 %31, i32 %32)
%34 = call i32 @beginTrans(i32 0)
br label %35
35: ; preds = %75, %26
%36 = load i32, i32* %11
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %42
38: ; preds = %35
%39 = load i32, i32* %10
%40 = load i32, i32* %9
%41 = icmp sle i32 %39, %40
br label %42
42: ; preds = %38, %35
%43 = phi i1 [ false, %35 ], [ %41, %38 ]
br i1 %43, label %44, label %76
44: ; preds = %42
%45 = load i32, i32* %9
%46 = load i32, i32* %11
%47 = load i32, i32* %10
%48 = call i32 (i32, i8*, i32, i32, i32, ...) @logMsg(i32 1, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %45, i32 %46, i32 %47)
%49 = load i32, i32* %10
%50 = load i32, i32* %6
%51 = load i32, i32* %7
%52 = call i32 @doCompress(i32 %49, i32 %50, i32 %51)
store i32 %52, i32* %13
%53 = load i32, i32* %13
%54 = icmp ne i32 %53, 0
br i1 %54, label %56, label %55
55: ; preds = %44
store i32 0, i32* %12
br label %76
56: ; preds = %44
%57 = load i32, i32* %11
store i32 %57, i32* %14
%58 = load i32, i32* %6
%59 = call i32 @getMinTs(i32 %58)
store i32 %59, i32* %11
%60 = load i32, i32* %11
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %75
62: ; preds = %56
%63 = load i32, i32* %11
%64 = load i32, i32* %14
%65 = icmp sle i32 %63, %64
br i1 %65, label %66, label %71
66: ; preds = %62
store i32 0, i32* %12
%67 = load i32, i32* %6
%68 = load i32, i32* %14
%69 = load i32, i32* %11
%70 = call i32 (i32, i8*, i32, i32, i32, ...) @logMsg(i32 4, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %67, i32 %68, i32 %69)
br label %76
71: ; preds = %62
%72 = load i32 (i32)*, i32 (i32)** %8
%73 = load i32, i32* %11
%74 = call i32 %72(i32 %73)
store i32 %74, i32* %10
br label %75
75: ; preds = %71, %56
br label %35
76: ; preds = %66, %55, %42
%77 = load i32, i32* %12
%78 = icmp eq i32 %77, 1
br i1 %78, label %79, label %81
79: ; preds = %76
%80 = call i32 (...) @commitTrans()
br label %83
81: ; preds = %76
%82 = call i32 (...) @rollbackTrans()
br label %83
83: ; preds = %81, %79
%84 = load i32, i32* %12
ret i32 %84
}

LLVM IR (-O2):
@.str = [63 x i8] c"In compressDbStage(%d,%d,%d), keep=%d, minTs=%d, minRounded=%d\00"
@.str.1 = [55 x i8] c"compressDbStage loop: keep=%d, minTs=%d, minRounded=%d\00"
@.str.2 = [52 x i8] c"getMinTs() error: oldDr=%d, prevMinTs=%d, minTs=%d,\00"
define i32 @compressDbStage(i32 %0, i32 %1, i32 %2, i32 (i32)* nocapture %3) {
%5 = tail call i32 (...) @getTime()
%6 = sub nsw i32 %5, %0
%7 = tail call i32 @getMinTs(i32 %1)
%8 = icmp eq i32 %7, 0
br i1 %8, label %11, label %9
9: ; preds = %4
%10 = tail call i32 %3(i32 %7)
br label %11
11: ; preds = %9, %4
%12 = phi i32 [ %10, %9 ], [ 0, %4 ]
%13 = tail call i32 (i32, i8*, i32, i32, i32, ...) @logMsg(i32 1, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str, i64 0, i64 0), i32 %0, i32 %1, i32 %2, i32 %6, i32 %7, i32 %12)
%14 = tail call i32 @beginTrans(i32 0)
%15 = icmp ne i32 %7, 0
%16 = icmp sle i32 %12, %6
%17 = select i1 %15, i1 %16, i1 false
br i1 %17, label %18, label %34
18: ; preds = %11, %31
%19 = phi i32 [ %32, %31 ], [ %12, %11 ]
%20 = phi i32 [ %25, %31 ], [ %7, %11 ]
%21 = tail call i32 (i32, i8*, i32, i32, i32, ...) @logMsg(i32 1, i8* getelementptr inbounds ([55 x i8], [55 x i8]* @.str.1, i64 0, i64 0), i32 %6, i32 %20, i32 %19)
%22 = tail call i32 @doCompress(i32 %19, i32 %1, i32 %2)
%23 = icmp eq i32 %22, 0
br i1 %23, label %36, label %24
24: ; preds = %18
%25 = tail call i32 @getMinTs(i32 %1)
%26 = icmp eq i32 %25, 0
br i1 %26, label %34, label %27
27: ; preds = %24
%28 = icmp sgt i32 %25, %20
br i1 %28, label %31, label %29
29: ; preds = %27
%30 = tail call i32 (i32, i8*, i32, i32, i32, ...) @logMsg(i32 4, i8* getelementptr inbounds ([52 x i8], [52 x i8]* @.str.2, i64 0, i64 0), i32 %1, i32 %20, i32 %25)
br label %36
31: ; preds = %27
%32 = tail call i32 %3(i32 %25)
%33 = icmp sgt i32 %32, %6
br i1 %33, label %34, label %18
34: ; preds = %24, %31, %11
%35 = tail call i32 (...) @commitTrans()
br label %38
36: ; preds = %18, %29
%37 = tail call i32 (...) @rollbackTrans()
br label %38
38: ; preds = %36, %34
%39 = phi i32 [ 0, %36 ], [ 1, %34 ]
ret i32 %39
}