#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*/ fprintf(int /*<<< orphan*/, char *, char *, int, double,
                           double);
scalar_t__ is_leap(int);
int /*<<< orphan*/ snprintf(char *, int, char *, int, int, int);
int /*<<< orphan*/ stderr;
char *readstat_sav_date_string(double seconds, char *dest, int size) {
  int yr = 1582;
  int month = 0;
  int day = 1;
  int daysPerMonth[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  int daysPerMonthLeap[] = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  double secs = seconds;
  secs += 24710400.0;
  double days = secs / 86400.0;
  double err = days - (long long)days;
  if (err != 0.0) {
    fprintf(stderr, "%s:%d time not supported. seconds was %lf, err was %lf\n",
            "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
            "2022-01-22/02-19-57/repos/WizardMac/ReadStat/refs/heads/master/"
            "src/bin/util/readstat_sav_date.c",
            58, seconds, err);
    return NULL;
  }
  while (days > 0) {
    int days_in_year = is_leap(yr) ? 366 : 365;
    if (days >= days_in_year) {
      yr += 1;
      days -= days_in_year;
      continue;
    }
    int days_in_month =
        is_leap(yr) ? daysPerMonthLeap[month] : daysPerMonth[month];
    if (days >= days_in_month) {
      month += 1;
      days -= days_in_month;
      continue;
    }
    day += days;
    days = 0;
  }
  snprintf(dest, size, "%04d-%02d-%02d", yr, month + 1, day);
  return dest;
}


x86 assembly (clang, -O0):
<readstat_sav_date_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc0,%rsp
movsd  %xmm0,-0x10(%rbp)
mov    %rdi,-0x18(%rbp)
mov    %esi,-0x1c(%rbp)
movl   $0x62e,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x1,-0x28(%rbp)
lea    -0x60(%rbp),%rdi
lea    0xe99(%rip),%rsi
mov    $0x30,%edx
call   1060 <memcpy@plt>
lea    -0x90(%rbp),%rdi
lea    0xeb1(%rip),%rsi
mov    $0x30,%edx
call   1060 <memcpy@plt>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x98(%rbp)
ff
movsd  0xe5a(%rip),%xmm0
addsd  -0x98(%rbp),%xmm0
ff
movsd  %xmm0,-0x98(%rbp)
ff
movsd  -0x98(%rbp),%xmm0
ff
movsd  0xe32(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0xa0(%rbp)
ff
movsd  -0xa0(%rbp),%xmm0
ff
cvttsd2si -0xa0(%rbp),%rax
ff ff
cvtsi2sd %rax,%xmm1
subsd  %xmm1,%xmm0
movsd  %xmm0,-0xa8(%rbp)
ff
movsd  -0xa8(%rbp),%xmm0
ff
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    121c <readstat_sav_date_string+0xdc>
jp     121c <readstat_sav_date_string+0xdc>
jmp    1257 <readstat_sav_date_string+0x117>
mov    0x2dbd(%rip),%rax
mov    (%rax),%edi
movsd  -0x10(%rbp),%xmm0
movsd  -0xa8(%rbp),%xmm1
ff
lea    0xe37(%rip),%rsi
lea    0xe68(%rip),%rdx
mov    $0x3a,%ecx
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13af <readstat_sav_date_string+0x26f>
jmp    125c <readstat_sav_date_string+0x11c>
movsd  -0xa0(%rbp),%xmm0
ff
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1385 <readstat_sav_date_string+0x245>
mov    -0x20(%rbp),%edi
call   1040 <is_leap@plt>
mov    %rax,%rdx
mov    $0x16d,%eax
mov    $0x16e,%ecx
cmp    $0x0,%rdx
cmovne %ecx,%eax
mov    %eax,-0xac(%rbp)
movsd  -0xa0(%rbp),%xmm0
ff
cvtsi2sdl -0xac(%rbp),%xmm1
ff
ucomisd %xmm1,%xmm0
jb     12d7 <readstat_sav_date_string+0x197>
mov    -0x20(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x20(%rbp)
cvtsi2sdl -0xac(%rbp),%xmm1
ff
movsd  -0xa0(%rbp),%xmm0
ff
subsd  %xmm1,%xmm0
movsd  %xmm0,-0xa0(%rbp)
ff
jmp    125c <readstat_sav_date_string+0x11c>
mov    -0x20(%rbp),%edi
call   1040 <is_leap@plt>
cmp    $0x0,%rax
je     12ff <readstat_sav_date_string+0x1bf>
movslq -0x24(%rbp),%rax
mov    -0x90(%rbp,%rax,4),%eax
mov    %eax,-0xb4(%rbp)
jmp    130d <readstat_sav_date_string+0x1cd>
movslq -0x24(%rbp),%rax
mov    -0x60(%rbp,%rax,4),%eax
mov    %eax,-0xb4(%rbp)
mov    -0xb4(%rbp),%eax
mov    %eax,-0xb0(%rbp)
movsd  -0xa0(%rbp),%xmm0
ff
cvtsi2sdl -0xb0(%rbp),%xmm1
ff
ucomisd %xmm1,%xmm0
jb     135d <readstat_sav_date_string+0x21d>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
cvtsi2sdl -0xb0(%rbp),%xmm1
ff
movsd  -0xa0(%rbp),%xmm0
ff
subsd  %xmm1,%xmm0
movsd  %xmm0,-0xa0(%rbp)
ff
jmp    125c <readstat_sav_date_string+0x11c>
movsd  -0xa0(%rbp),%xmm1
ff
cvtsi2sdl -0x28(%rbp),%xmm0
addsd  %xmm1,%xmm0
cvttsd2si %xmm0,%eax
mov    %eax,-0x28(%rbp)
xorps  %xmm0,%xmm0
movsd  %xmm0,-0xa0(%rbp)
ff
jmp    125c <readstat_sav_date_string+0x11c>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
add    $0x1,%r8d
mov    -0x28(%rbp),%r9d
lea    0xda6(%rip),%rdx
call   1030 <snprintf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0xc0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readstat_sav_date_string>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movsd  0xeba(%rip),%xmm3
addsd  %xmm0,%xmm3
divsd  0xeb6(%rip),%xmm3
cvttsd2si %xmm3,%rax
cvtsi2sd %rax,%xmm2
movapd %xmm3,%xmm1
subsd  %xmm2,%xmm1
xorpd  %xmm2,%xmm2
ucomisd %xmm2,%xmm1
jne    1170 <readstat_sav_date_string+0x40>
jnp    1199 <readstat_sav_date_string+0x69>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
lea    0xf10(%rip),%rsi
lea    0xf41(%rip),%rdx
mov    $0x3a,%ecx
call   1050 <fprintf@plt>
xor    %r12d,%r12d
jmp    1273 <readstat_sav_date_string+0x143>
mov    %rdi,%r12
mov    $0x1,%r9d
ucomisd %xmm2,%xmm3
jbe    1236 <readstat_sav_date_string+0x106>
mov    %esi,0xc(%rsp)
xor    %ebp,%ebp
mov    $0x62e,%ebx
lea    0xe62(%rip),%r13
lea    0xe6b(%rip),%r14
lea    0xe94(%rip),%r15
jmp    11e1 <readstat_sav_date_string+0xb1>
xchg   %ax,%ax
add    $0x1,%ebx
subsd  %xmm0,%xmm3
ucomisd 0xe39(%rip),%xmm3
jbe    123f <readstat_sav_date_string+0x10f>
movsd  %xmm3,0x10(%rsp)
mov    %ebx,%edi
call   1040 <is_leap@plt>
movsd  0x10(%rsp),%xmm3
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
movsd  0x0(%r13,%rcx,8),%xmm0
ucomisd %xmm0,%xmm3
jae    11d0 <readstat_sav_date_string+0xa0>
mov    %ebx,%edi
call   1040 <is_leap@plt>
movsd  0x10(%rsp),%xmm3
test   %rax,%rax
movslq %ebp,%rax
mov    %r15,%rcx
cmove  %r14,%rcx
xorps  %xmm0,%xmm0
cvtsi2sdl (%rcx,%rax,4),%xmm0
ucomisd %xmm0,%xmm3
jb     124b <readstat_sav_date_string+0x11b>
add    $0x1,%ebp
jmp    11d3 <readstat_sav_date_string+0xa3>
mov    $0x62e,%ebx
xor    %ebp,%ebp
jmp    125c <readstat_sav_date_string+0x12c>
mov    0xc(%rsp),%esi
mov    $0x1,%r9d
jmp    125c <readstat_sav_date_string+0x12c>
addsd  0xdbd(%rip),%xmm3
cvttsd2si %xmm3,%r9d
mov    0xc(%rsp),%esi
add    $0x1,%ebp
lea    0xf02(%rip),%rdx
mov    %r12,%rdi
mov    %ebx,%ecx
mov    %ebp,%r8d
call   1030 <snprintf@plt>
mov    %r12,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.readstat_sav_date_string.daysPerMonth = [12 x i32] [i32 31, i32 28, i32 31, i32 30, i32 31, i32 30, i32 31, i32 31, i32 30, i32 31, i32 30, i32 31]
@__const.readstat_sav_date_string.daysPerMonthLeap = [12 x i32] [i32 31, i32 29, i32 31, i32 30, i32 31, i32 30, i32 31, i32 31, i32 30, i32 31, i32 30, i32 31]
@stderr = global i32 0
@.str = [56 x i8] c"%s:%d time not supported. seconds was %lf, err was %lf\0A\00"
@.str.1 = [160 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/WizardMac/ReadStat/refs/heads/master/src/bin/util/readstat_sav_date.c\00"
@.str.2 = [15 x i8] c"%04d-%02d-%02d\00"
define i8* @readstat_sav_date_string(double %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca double
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca [12 x i32]
%12 = alloca [12 x i32]
%13 = alloca double
%14 = alloca double
%15 = alloca double
%16 = alloca i32
%17 = alloca i32
store double %0, double* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 1582, i32* %8
store i32 0, i32* %9
store i32 1, i32* %10
%18 = bitcast [12 x i32]* %11 to i8*
%19 = bitcast [12 x i32]* %12 to i8*
%20 = load double, double* %5
store double %20, double* %13
%21 = load double, double* %13
%22 = fadd double %21, 2.471040e+07
store double %22, double* %13
%23 = load double, double* %13
%24 = fdiv double %23, 8.640000e+04
store double %24, double* %14
%25 = load double, double* %14
%26 = load double, double* %14
%27 = fptosi double %26 to i64
%28 = sitofp i64 %27 to double
%29 = fsub double %25, %28
store double %29, double* %15
%30 = load double, double* %15
%31 = fcmp une double %30, 0.000000e+00
br i1 %31, label %32, label %37
32: ; preds = %3
%33 = load i32, i32* @stderr
%34 = load double, double* %5
%35 = load double, double* %15
%36 = call i32 @fprintf(i32 %33, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.1, i64 0, i64 0), i32 58, double %34, double %35)
store i8* null, i8** %4
br label %100
37: ; preds = %3
br label %38
38: ; preds = %85, %78, %51, %37
%39 = load double, double* %14
%40 = fcmp ogt double %39, 0.000000e+00
br i1 %40, label %41, label %91
41: ; preds = %38
%42 = load i32, i32* %8
%43 = call i64 @is_leap(i32 %42)
%44 = icmp ne i64 %43, 0
%45 = zext i1 %44 to i64
%46 = select i1 %44, i32 366, i32 365
store i32 %46, i32* %16
%47 = load double, double* %14
%48 = load i32, i32* %16
%49 = sitofp i32 %48 to double
%50 = fcmp oge double %47, %49
br i1 %50, label %51, label %58
51: ; preds = %41
%52 = load i32, i32* %8
%53 = add nsw i32 %52, 1
store i32 %53, i32* %8
%54 = load i32, i32* %16
%55 = sitofp i32 %54 to double
%56 = load double, double* %14
%57 = fsub double %56, %55
store double %57, double* %14
br label %38
58: ; preds = %41
%59 = load i32, i32* %8
%60 = call i64 @is_leap(i32 %59)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %67
62: ; preds = %58
%63 = load i32, i32* %9
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds [12 x i32], [12 x i32]* %12, i64 0, i64 %64
%66 = load i32, i32* %65
br label %72
67: ; preds = %58
%68 = load i32, i32* %9
%69 = sext i32 %68 to i64
%70 = getelementptr inbounds [12 x i32], [12 x i32]* %11, i64 0, i64 %69
%71 = load i32, i32* %70
br label %72
72: ; preds = %67, %62
%73 = phi i32 [ %66, %62 ], [ %71, %67 ]
store i32 %73, i32* %17
%74 = load double, double* %14
%75 = load i32, i32* %17
%76 = sitofp i32 %75 to double
%77 = fcmp oge double %74, %76
br i1 %77, label %78, label %85
78: ; preds = %72
%79 = load i32, i32* %9
%80 = add nsw i32 %79, 1
store i32 %80, i32* %9
%81 = load i32, i32* %17
%82 = sitofp i32 %81 to double
%83 = load double, double* %14
%84 = fsub double %83, %82
store double %84, double* %14
br label %38
85: ; preds = %72
%86 = load double, double* %14
%87 = load i32, i32* %10
%88 = sitofp i32 %87 to double
%89 = fadd double %88, %86
%90 = fptosi double %89 to i32
store i32 %90, i32* %10
store double 0.000000e+00, double* %14
br label %38
91: ; preds = %38
%92 = load i8*, i8** %6
%93 = load i32, i32* %7
%94 = load i32, i32* %8
%95 = load i32, i32* %9
%96 = add nsw i32 %95, 1
%97 = load i32, i32* %10
%98 = call i32 @snprintf(i8* %92, i32 %93, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %94, i32 %96, i32 %97)
%99 = load i8*, i8** %6
store i8* %99, i8** %4
br label %100
100: ; preds = %91, %32
%101 = load i8*, i8** %4
ret i8* %101
}

LLVM IR (-O2):
@__const.readstat_sav_date_string.daysPerMonth = [12 x i32] [i32 31, i32 28, i32 31, i32 30, i32 31, i32 30, i32 31, i32 31, i32 30, i32 31, i32 30, i32 31]
@__const.readstat_sav_date_string.daysPerMonthLeap = [12 x i32] [i32 31, i32 29, i32 31, i32 30, i32 31, i32 30, i32 31, i32 31, i32 30, i32 31, i32 30, i32 31]
@stderr = global i32 0
@.str = [56 x i8] c"%s:%d time not supported. seconds was %lf, err was %lf\0A\00"
@.str.1 = [160 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/WizardMac/ReadStat/refs/heads/master/src/bin/util/readstat_sav_date.c\00"
@.str.2 = [15 x i8] c"%04d-%02d-%02d\00"
define i8* @readstat_sav_date_string(double %0, i8* %1, i32 %2) {
%4 = fadd double %0, 2.471040e+07
%5 = fdiv double %4, 8.640000e+04
%6 = fptosi double %5 to i64
%7 = sitofp i64 %6 to double
%8 = fsub double %5, %7
%9 = fcmp une double %8, 0.000000e+00
br i1 %9, label %12, label %10
10: ; preds = %3
%11 = fcmp ogt double %5, 0.000000e+00
br i1 %11, label %15, label %46
12: ; preds = %3
%13 = load i32, i32* @stderr
%14 = tail call i32 @fprintf(i32 %13, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([160 x i8], [160 x i8]* @.str.1, i64 0, i64 0), i32 58, double %0, double %8)
br label %52
15: ; preds = %10, %40
%16 = phi i32 [ %43, %40 ], [ 1582, %10 ]
%17 = phi i32 [ %42, %40 ], [ 0, %10 ]
%18 = phi double [ %44, %40 ], [ %5, %10 ]
%19 = tail call i64 @is_leap(i32 %16)
%20 = icmp eq i64 %19, 0
%21 = select i1 %20, double 3.650000e+02, double 3.660000e+02
%22 = fcmp ult double %18, %21
br i1 %22, label %25, label %23
23: ; preds = %15
%24 = add nsw i32 %16, 1
br label %40
25: ; preds = %15
%26 = tail call i64 @is_leap(i32 %16)
%27 = icmp eq i64 %26, 0
%28 = sext i32 %17 to i64
%29 = getelementptr inbounds [12 x i32], [12 x i32]* @__const.readstat_sav_date_string.daysPerMonthLeap, i64 0, i64 %28
%30 = getelementptr inbounds [12 x i32], [12 x i32]* @__const.readstat_sav_date_string.daysPerMonth, i64 0, i64 %28
%31 = select i1 %27, i32* %30, i32* %29
%32 = load i32, i32* %31
%33 = sitofp i32 %32 to double
%34 = fcmp ult double %18, %33
br i1 %34, label %37, label %35
35: ; preds = %25
%36 = add nsw i32 %17, 1
br label %40
37: ; preds = %25
%38 = fadd double %18, 1.000000e+00
%39 = fptosi double %38 to i32
br label %46
40: ; preds = %35, %23
%41 = phi double [ %33, %35 ], [ %21, %23 ]
%42 = phi i32 [ %36, %35 ], [ %17, %23 ]
%43 = phi i32 [ %16, %35 ], [ %24, %23 ]
%44 = fsub double %18, %41
%45 = fcmp ogt double %44, 0.000000e+00
br i1 %45, label %15, label %46
46: ; preds = %40, %37, %10
%47 = phi i32 [ 1, %10 ], [ %39, %37 ], [ 1, %40 ]
%48 = phi i32 [ 0, %10 ], [ %17, %37 ], [ %42, %40 ]
%49 = phi i32 [ 1582, %10 ], [ %16, %37 ], [ %43, %40 ]
%50 = add nsw i32 %48, 1
%51 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %49, i32 %50, i32 %47)
br label %52
52: ; preds = %46, %12
%53 = phi i8* [ null, %12 ], [ %1, %46 ]
ret i8* %53
}