#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 scalar_t__ time_t;
struct tm {
  int dummy;
};
struct dbt {
  int dummy;
};

/* Variables and functions */
scalar_t__ atoi(char const *);
struct dbt *dbtget(scalar_t__);
struct dbt *dbtgetnxt(struct dbt *);
scalar_t__ dbtgett(struct dbt *);
int /*<<< orphan*/ error(int, char *, char const *, scalar_t__);
scalar_t__ labs(long);
int /*<<< orphan*/ memset(struct tm *, int /*<<< orphan*/, int);
scalar_t__ mktime(struct tm *);
int /*<<< orphan*/ strptime(char const *, char *, struct tm *);
struct dbt *timeparse(const char *stime) {
  time_t t = atoi(stime);
  struct dbt *dt = dbtget(t);
  if (!dt) {
    struct tm tm;
    struct dbt *dti = dt = dbtgetnxt(NULL);
    memset(&tm, 0, sizeof(struct tm));
    if (!dt)
      error(1, "not time found: '%s' -> %li\n", stime, t);
    if (!strptime(stime, "%y-%m-%d %H:%M", &tm) &&
        !strptime(stime, "%y-%m-%d", &tm) && !strptime(stime, "%y-%m", &tm) &&
        !strptime(stime, "%y", &tm))
      error(1, "time not parseable: '%s' -> %li\n", stime, t);
    t = mktime(&tm);
    while ((dti = dbtgetnxt(dti)))
      if (labs((long)dbtgett(dti) - (long)t) <
          labs((long)dbtgett(dt) - (long)t))
        dt = dti;
  }
  return dt;
}


x86 assembly (clang, -O0):
<timeparse>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   10a0 <atoi@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <dbtget@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1307 <timeparse+0x177>
xor    %eax,%eax
mov    %eax,%edi
call   1080 <dbtgetnxt@plt>
mov    %rax,-0x18(%rbp)
mov    %rax,-0x28(%rbp)
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1050 <memset@plt>
cmpq   $0x0,-0x18(%rbp)
jne    1206 <timeparse+0x76>
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
lea    0xdff(%rip),%rsi
call   1090 <error@plt>
mov    -0x8(%rbp),%rdi
lea    0xe0c(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <strptime@plt>
cmp    $0x0,%eax
jne    1293 <timeparse+0x103>
mov    -0x8(%rbp),%rdi
lea    0xdfe(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <strptime@plt>
cmp    $0x0,%eax
jne    1293 <timeparse+0x103>
mov    -0x8(%rbp),%rdi
lea    0xdea(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <strptime@plt>
cmp    $0x0,%eax
jne    1293 <timeparse+0x103>
mov    -0x8(%rbp),%rdi
lea    0xdd3(%rip),%rsi
lea    -0x20(%rbp),%rdx
call   10b0 <strptime@plt>
cmp    $0x0,%eax
jne    1293 <timeparse+0x103>
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%edi
lea    0xdb0(%rip),%rsi
call   1090 <error@plt>
lea    -0x20(%rbp),%rdi
call   1060 <mktime@plt>
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <dbtgetnxt@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     1302 <timeparse+0x172>
mov    -0x28(%rbp),%rdi
call   1030 <dbtgett@plt>
mov    %rax,%rdi
sub    -0x10(%rbp),%rdi
call   1040 <labs@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
call   1030 <dbtgett@plt>
mov    %rax,%rdi
sub    -0x10(%rbp),%rdi
call   1040 <labs@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
cmp    %rcx,%rax
jge    12fd <timeparse+0x16d>
mov    -0x28(%rbp),%rax
mov    %rax,-0x18(%rbp)
jmp    12a0 <timeparse+0x110>
jmp    1307 <timeparse+0x177>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timeparse>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1090 <atoi@plt>
mov    %rax,%r15
mov    %rax,%rdi
call   1060 <dbtget@plt>
mov    %rax,%r12
test   %rax,%rax
jne    12b1 <timeparse+0x131>
xor    %edi,%edi
call   1070 <dbtgetnxt@plt>
mov    %rax,%r12
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x4,%edx
call   1040 <memset@plt>
test   %r12,%r12
jne    11dc <timeparse+0x5c>
lea    0xe34(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
mov    %r15,%rcx
call   1080 <error@plt>
lea    0xe3a(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   10a0 <strptime@plt>
test   %eax,%eax
jne    124b <timeparse+0xcb>
lea    0xe33(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   10a0 <strptime@plt>
test   %eax,%eax
jne    124b <timeparse+0xcb>
lea    0xe26(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   10a0 <strptime@plt>
test   %eax,%eax
jne    124b <timeparse+0xcb>
lea    0xe16(%rip),%rsi
mov    %rsp,%rdx
mov    %r14,%rdi
call   10a0 <strptime@plt>
test   %eax,%eax
jne    124b <timeparse+0xcb>
lea    0xe03(%rip),%rsi
mov    $0x1,%edi
mov    %r14,%rdx
mov    %r15,%rcx
call   1080 <error@plt>
mov    %rsp,%rdi
call   1050 <mktime@plt>
mov    %rax,%r14
mov    %r12,%rdi
call   1070 <dbtgetnxt@plt>
test   %rax,%rax
je     12b1 <timeparse+0x131>
mov    %rax,%r15
cs nopw 0x0(%rax,%rax,1)
mov    %r15,%rdi
call   1030 <dbtgett@plt>
sub    %r14,%rax
mov    %rax,%rbx
neg    %rbx
cmovs  %rax,%rbx
mov    %r12,%rdi
call   1030 <dbtgett@plt>
sub    %r14,%rax
mov    %rax,%rcx
neg    %rcx
cmovs  %rax,%rcx
cmp    %rcx,%rbx
cmovb  %r15,%r12
mov    %r15,%rdi
call   1070 <dbtgetnxt@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1270 <timeparse+0xf0>
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.dbt = type { i32 }
%struct.tm = type { i32 }
@.str = [29 x i8] c"not time found: '%s' -> %li\0A\00"
@.str.1 = [15 x i8] c"%y-%m-%d %H:%M\00"
@.str.2 = [9 x i8] c"%y-%m-%d\00"
@.str.3 = [6 x i8] c"%y-%m\00"
@.str.4 = [3 x i8] c"%y\00"
@.str.5 = [33 x i8] c"time not parseable: '%s' -> %li\0A\00"
define %struct.dbt* @timeparse(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca %struct.dbt*
%5 = alloca %struct.tm
%6 = alloca %struct.dbt*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i64 @atoi(i8* %7)
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = call %struct.dbt* @dbtget(i64 %9)
store %struct.dbt* %10, %struct.dbt** %4
%11 = load %struct.dbt*, %struct.dbt** %4
%12 = icmp ne %struct.dbt* %11, null
br i1 %12, label %64, label %13
13: ; preds = %1
%14 = call %struct.dbt* @dbtgetnxt(%struct.dbt* null)
store %struct.dbt* %14, %struct.dbt** %4
store %struct.dbt* %14, %struct.dbt** %6
%15 = call i32 @memset(%struct.tm* %5, i32 0, i32 4)
%16 = load %struct.dbt*, %struct.dbt** %4
%17 = icmp ne %struct.dbt* %16, null
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = load i8*, i8** %2
%20 = load i64, i64* %3
%21 = call i32 @error(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %19, i64 %20)
br label %22
22: ; preds = %18, %13
%23 = load i8*, i8** %2
%24 = call i32 @strptime(i8* %23, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %5)
%25 = icmp ne i32 %24, 0
br i1 %25, label %42, label %26
26: ; preds = %22
%27 = load i8*, i8** %2
%28 = call i32 @strptime(i8* %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %5)
%29 = icmp ne i32 %28, 0
br i1 %29, label %42, label %30
30: ; preds = %26
%31 = load i8*, i8** %2
%32 = call i32 @strptime(i8* %31, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %struct.tm* %5)
%33 = icmp ne i32 %32, 0
br i1 %33, label %42, label %34
34: ; preds = %30
%35 = load i8*, i8** %2
%36 = call i32 @strptime(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), %struct.tm* %5)
%37 = icmp ne i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = load i8*, i8** %2
%40 = load i64, i64* %3
%41 = call i32 @error(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %39, i64 %40)
br label %42
42: ; preds = %38, %34, %30, %26, %22
%43 = call i64 @mktime(%struct.tm* %5)
store i64 %43, i64* %3
br label %44
44: ; preds = %62, %42
%45 = load %struct.dbt*, %struct.dbt** %6
%46 = call %struct.dbt* @dbtgetnxt(%struct.dbt* %45)
store %struct.dbt* %46, %struct.dbt** %6
%47 = icmp ne %struct.dbt* %46, null
br i1 %47, label %48, label %63
48: ; preds = %44
%49 = load %struct.dbt*, %struct.dbt** %6
%50 = call i64 @dbtgett(%struct.dbt* %49)
%51 = load i64, i64* %3
%52 = sub nsw i64 %50, %51
%53 = call i64 @labs(i64 %52)
%54 = load %struct.dbt*, %struct.dbt** %4
%55 = call i64 @dbtgett(%struct.dbt* %54)
%56 = load i64, i64* %3
%57 = sub nsw i64 %55, %56
%58 = call i64 @labs(i64 %57)
%59 = icmp slt i64 %53, %58
br i1 %59, label %60, label %62
60: ; preds = %48
%61 = load %struct.dbt*, %struct.dbt** %6
store %struct.dbt* %61, %struct.dbt** %4
br label %62
62: ; preds = %60, %48
br label %44
63: ; preds = %44
br label %64
64: ; preds = %63, %1
%65 = load %struct.dbt*, %struct.dbt** %4
ret %struct.dbt* %65
}

LLVM IR (-O2):
%struct.dbt = type { i32 }
%struct.tm = type { i32 }
@.str = [29 x i8] c"not time found: '%s' -> %li\0A\00"
@.str.1 = [15 x i8] c"%y-%m-%d %H:%M\00"
@.str.2 = [9 x i8] c"%y-%m-%d\00"
@.str.3 = [6 x i8] c"%y-%m\00"
@.str.4 = [3 x i8] c"%y\00"
@.str.5 = [33 x i8] c"time not parseable: '%s' -> %li\0A\00"
define %struct.dbt* @timeparse(i8* %0) {
%2 = alloca %struct.tm
%3 = tail call i64 @atoi(i8* %0)
%4 = tail call %struct.dbt* @dbtget(i64 %3)
%5 = icmp eq %struct.dbt* %4, null
br i1 %5, label %6, label %46
6: ; preds = %1
%7 = bitcast %struct.tm* %2 to i8*
%8 = tail call %struct.dbt* @dbtgetnxt(%struct.dbt* null)
%9 = call i32 @memset(%struct.tm* %2, i32 0, i32 4)
%10 = icmp eq %struct.dbt* %8, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = call i32 @error(i32 1, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0), i8* %0, i64 %3)
br label %13
13: ; preds = %11, %6
%14 = call i32 @strptime(i8* %0, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), %struct.tm* %2)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %27
16: ; preds = %13
%17 = call i32 @strptime(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %2)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = call i32 @strptime(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), %struct.tm* %2)
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %19
%23 = call i32 @strptime(i8* %0, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), %struct.tm* %2)
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = call i32 @error(i32 1, i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.5, i64 0, i64 0), i8* %0, i64 %3)
br label %27
27: ; preds = %25, %22, %19, %16, %13
%28 = call i64 @mktime(%struct.tm* %2)
%29 = call %struct.dbt* @dbtgetnxt(%struct.dbt* %8)
%30 = icmp eq %struct.dbt* %29, null
br i1 %30, label %44, label %31
31: ; preds = %27, %31
%32 = phi %struct.dbt* [ %42, %31 ], [ %29, %27 ]
%33 = phi %struct.dbt* [ %41, %31 ], [ %8, %27 ]
%34 = call i64 @dbtgett(%struct.dbt* %32)
%35 = sub nsw i64 %34, %28
%36 = call i64 @llvm.abs.i64(i64 %35, i1 true)
%37 = call i64 @dbtgett(%struct.dbt* %33)
%38 = sub nsw i64 %37, %28
%39 = call i64 @llvm.abs.i64(i64 %38, i1 true)
%40 = icmp ult i64 %36, %39
%41 = select i1 %40, %struct.dbt* %32, %struct.dbt* %33
%42 = call %struct.dbt* @dbtgetnxt(%struct.dbt* %32)
%43 = icmp eq %struct.dbt* %42, null
br i1 %43, label %44, label %31
44: ; preds = %31, %27
%45 = phi %struct.dbt* [ %8, %27 ], [ %41, %31 ]
br label %46
46: ; preds = %44, %1
%47 = phi %struct.dbt* [ %4, %1 ], [ %45, %44 ]
ret %struct.dbt* %47
}