#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 */
double ceil(double);
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 = ceil(days) - 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/ivarref/ReadStat/refs/heads/master/src/"
            "spss/readstat_sav_date.c",
            59, 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    0xe89(%rip),%rsi
mov    $0x30,%edx
call   1070 <memcpy@plt>
lea    -0x90(%rbp),%rdi
lea    0xea1(%rip),%rsi
mov    $0x30,%edx
call   1070 <memcpy@plt>
movsd  -0x10(%rbp),%xmm0
movsd  %xmm0,-0x98(%rbp)
ff
movsd  0xe4a(%rip),%xmm0
addsd  -0x98(%rbp),%xmm0
ff
movsd  %xmm0,-0x98(%rbp)
ff
movsd  -0x98(%rbp),%xmm0
ff
movsd  0xe22(%rip),%xmm1
divsd  %xmm1,%xmm0
movsd  %xmm0,-0xa0(%rbp)
ff
movsd  -0xa0(%rbp),%xmm0
ff
call   1030 <ceil@plt>
subsd  -0xa0(%rbp),%xmm0
ff
movsd  %xmm0,-0xa8(%rbp)
ff
movsd  -0xa8(%rbp),%xmm0
ff
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jne    1227 <readstat_sav_date_string+0xd7>
jp     1227 <readstat_sav_date_string+0xd7>
jmp    1262 <readstat_sav_date_string+0x112>
mov    0x2db2(%rip),%rax
mov    (%rax),%edi
movsd  -0x10(%rbp),%xmm0
movsd  -0xa8(%rbp),%xmm1
ff
lea    0xe2c(%rip),%rsi
lea    0xe5d(%rip),%rdx
mov    $0x3b,%ecx
call   1060 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    13ba <readstat_sav_date_string+0x26a>
jmp    1267 <readstat_sav_date_string+0x117>
movsd  -0xa0(%rbp),%xmm0
ff
xorps  %xmm1,%xmm1
ucomisd %xmm1,%xmm0
jbe    1390 <readstat_sav_date_string+0x240>
mov    -0x20(%rbp),%edi
call   1050 <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     12e2 <readstat_sav_date_string+0x192>
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    1267 <readstat_sav_date_string+0x117>
mov    -0x20(%rbp),%edi
call   1050 <is_leap@plt>
cmp    $0x0,%rax
je     130a <readstat_sav_date_string+0x1ba>
movslq -0x24(%rbp),%rax
mov    -0x90(%rbp,%rax,4),%eax
mov    %eax,-0xb4(%rbp)
jmp    1318 <readstat_sav_date_string+0x1c8>
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     1368 <readstat_sav_date_string+0x218>
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    1267 <readstat_sav_date_string+0x117>
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    1267 <readstat_sav_date_string+0x117>
mov    -0x18(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%ecx
mov    -0x24(%rbp),%r8d
add    $0x1,%r8d
mov    -0x28(%rbp),%r9d
lea    0xd95(%rip),%rdx
call   1040 <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
mov    %esi,%r14d
mov    %rdi,%r12
movsd  0xea4(%rip),%xmm1
movsd  %xmm0,0x8(%rsp)
addsd  %xmm0,%xmm1
divsd  0xe9a(%rip),%xmm1
movsd  %xmm1,0x10(%rsp)
movapd %xmm1,%xmm0
call   1030 <ceil@plt>
movsd  0x10(%rsp),%xmm2
movapd %xmm0,%xmm1
subsd  %xmm2,%xmm1
xorpd  %xmm0,%xmm0
ucomisd %xmm0,%xmm1
jne    1197 <readstat_sav_date_string+0x57>
jnp    11c6 <readstat_sav_date_string+0x86>
mov    0x2e42(%rip),%rax
mov    (%rax),%edi
lea    0xee9(%rip),%rsi
lea    0xf1a(%rip),%rdx
mov    $0x3b,%ecx
movsd  0x8(%rsp),%xmm0
call   1060 <fprintf@plt>
xor    %r12d,%r12d
jmp    12a8 <readstat_sav_date_string+0x168>
mov    $0x1,%r9d
ucomisd %xmm0,%xmm2
jbe    1266 <readstat_sav_date_string+0x126>
mov    %r14d,0x8(%rsp)
xor    %ebp,%ebp
mov    $0x62e,%ebx
lea    0xe37(%rip),%r13
lea    0xe40(%rip),%r14
lea    0xe69(%rip),%r15
jmp    1211 <readstat_sav_date_string+0xd1>
nopl   0x0(%rax)
add    $0x1,%ebx
subsd  %xmm0,%xmm2
ucomisd 0xe09(%rip),%xmm2
jbe    126f <readstat_sav_date_string+0x12f>
movsd  %xmm2,0x10(%rsp)
mov    %ebx,%edi
call   1050 <is_leap@plt>
movsd  0x10(%rsp),%xmm2
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
movsd  0x0(%r13,%rcx,8),%xmm0
ucomisd %xmm0,%xmm2
jae    1200 <readstat_sav_date_string+0xc0>
mov    %ebx,%edi
call   1050 <is_leap@plt>
movsd  0x10(%rsp),%xmm2
test   %rax,%rax
movslq %ebp,%rax
mov    %r15,%rcx
cmove  %r14,%rcx
xorps  %xmm0,%xmm0
cvtsi2sdl (%rcx,%rax,4),%xmm0
ucomisd %xmm0,%xmm2
jb     127c <readstat_sav_date_string+0x13c>
add    $0x1,%ebp
jmp    1203 <readstat_sav_date_string+0xc3>
mov    $0x62e,%ebx
xor    %ebp,%ebp
jmp    128e <readstat_sav_date_string+0x14e>
mov    0x8(%rsp),%r14d
mov    $0x1,%r9d
jmp    128e <readstat_sav_date_string+0x14e>
addsd  0xd8c(%rip),%xmm2
cvttsd2si %xmm2,%r9d
mov    0x8(%rsp),%r14d
add    $0x1,%ebp
lea    0xeca(%rip),%rdx
mov    %r12,%rdi
mov    %r14d,%esi
mov    %ebx,%ecx
mov    %ebp,%r8d
call   1040 <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 = [154 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/ivarref/ReadStat/refs/heads/master/src/spss/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 = call double @llvm.ceil.f64(double %25)
%27 = load double, double* %14
%28 = fsub double %26, %27
store double %28, double* %15
%29 = load double, double* %15
%30 = fcmp une double %29, 0.000000e+00
br i1 %30, label %31, label %36
31: ; preds = %3
%32 = load i32, i32* @stderr
%33 = load double, double* %5
%34 = load double, double* %15
%35 = call i32 @fprintf(i32 %32, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([154 x i8], [154 x i8]* @.str.1, i64 0, i64 0), i32 59, double %33, double %34)
store i8* null, i8** %4
br label %99
36: ; preds = %3
br label %37
37: ; preds = %84, %77, %50, %36
%38 = load double, double* %14
%39 = fcmp ogt double %38, 0.000000e+00
br i1 %39, label %40, label %90
40: ; preds = %37
%41 = load i32, i32* %8
%42 = call i64 @is_leap(i32 %41)
%43 = icmp ne i64 %42, 0
%44 = zext i1 %43 to i64
%45 = select i1 %43, i32 366, i32 365
store i32 %45, i32* %16
%46 = load double, double* %14
%47 = load i32, i32* %16
%48 = sitofp i32 %47 to double
%49 = fcmp oge double %46, %48
br i1 %49, label %50, label %57
50: ; preds = %40
%51 = load i32, i32* %8
%52 = add nsw i32 %51, 1
store i32 %52, i32* %8
%53 = load i32, i32* %16
%54 = sitofp i32 %53 to double
%55 = load double, double* %14
%56 = fsub double %55, %54
store double %56, double* %14
br label %37
57: ; preds = %40
%58 = load i32, i32* %8
%59 = call i64 @is_leap(i32 %58)
%60 = icmp ne i64 %59, 0
br i1 %60, label %61, label %66
61: ; preds = %57
%62 = load i32, i32* %9
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [12 x i32], [12 x i32]* %12, i64 0, i64 %63
%65 = load i32, i32* %64
br label %71
66: ; preds = %57
%67 = load i32, i32* %9
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds [12 x i32], [12 x i32]* %11, i64 0, i64 %68
%70 = load i32, i32* %69
br label %71
71: ; preds = %66, %61
%72 = phi i32 [ %65, %61 ], [ %70, %66 ]
store i32 %72, i32* %17
%73 = load double, double* %14
%74 = load i32, i32* %17
%75 = sitofp i32 %74 to double
%76 = fcmp oge double %73, %75
br i1 %76, label %77, label %84
77: ; preds = %71
%78 = load i32, i32* %9
%79 = add nsw i32 %78, 1
store i32 %79, i32* %9
%80 = load i32, i32* %17
%81 = sitofp i32 %80 to double
%82 = load double, double* %14
%83 = fsub double %82, %81
store double %83, double* %14
br label %37
84: ; preds = %71
%85 = load double, double* %14
%86 = load i32, i32* %10
%87 = sitofp i32 %86 to double
%88 = fadd double %87, %85
%89 = fptosi double %88 to i32
store i32 %89, i32* %10
store double 0.000000e+00, double* %14
br label %37
90: ; preds = %37
%91 = load i8*, i8** %6
%92 = load i32, i32* %7
%93 = load i32, i32* %8
%94 = load i32, i32* %9
%95 = add nsw i32 %94, 1
%96 = load i32, i32* %10
%97 = call i32 @snprintf(i8* %91, i32 %92, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %93, i32 %95, i32 %96)
%98 = load i8*, i8** %6
store i8* %98, i8** %4
br label %99
99: ; preds = %90, %31
%100 = load i8*, i8** %4
ret i8* %100
}

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 = [154 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/ivarref/ReadStat/refs/heads/master/src/spss/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 = tail call double @llvm.ceil.f64(double %5)
%7 = fsub double %6, %5
%8 = fcmp une double %7, 0.000000e+00
br i1 %8, label %11, label %9
9: ; preds = %3
%10 = fcmp ogt double %5, 0.000000e+00
br i1 %10, label %14, label %45
11: ; preds = %3
%12 = load i32, i32* @stderr
%13 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([56 x i8], [56 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([154 x i8], [154 x i8]* @.str.1, i64 0, i64 0), i32 59, double %0, double %7)
br label %51
14: ; preds = %9, %39
%15 = phi i32 [ %42, %39 ], [ 1582, %9 ]
%16 = phi i32 [ %41, %39 ], [ 0, %9 ]
%17 = phi double [ %43, %39 ], [ %5, %9 ]
%18 = tail call i64 @is_leap(i32 %15)
%19 = icmp eq i64 %18, 0
%20 = select i1 %19, double 3.650000e+02, double 3.660000e+02
%21 = fcmp ult double %17, %20
br i1 %21, label %24, label %22
22: ; preds = %14
%23 = add nsw i32 %15, 1
br label %39
24: ; preds = %14
%25 = tail call i64 @is_leap(i32 %15)
%26 = icmp eq i64 %25, 0
%27 = sext i32 %16 to i64
%28 = getelementptr inbounds [12 x i32], [12 x i32]* @__const.readstat_sav_date_string.daysPerMonthLeap, i64 0, i64 %27
%29 = getelementptr inbounds [12 x i32], [12 x i32]* @__const.readstat_sav_date_string.daysPerMonth, i64 0, i64 %27
%30 = select i1 %26, i32* %29, i32* %28
%31 = load i32, i32* %30
%32 = sitofp i32 %31 to double
%33 = fcmp ult double %17, %32
br i1 %33, label %36, label %34
34: ; preds = %24
%35 = add nsw i32 %16, 1
br label %39
36: ; preds = %24
%37 = fadd double %17, 1.000000e+00
%38 = fptosi double %37 to i32
br label %45
39: ; preds = %34, %22
%40 = phi double [ %32, %34 ], [ %20, %22 ]
%41 = phi i32 [ %35, %34 ], [ %16, %22 ]
%42 = phi i32 [ %15, %34 ], [ %23, %22 ]
%43 = fsub double %17, %40
%44 = fcmp ogt double %43, 0.000000e+00
br i1 %44, label %14, label %45
45: ; preds = %39, %36, %9
%46 = phi i32 [ 1, %9 ], [ %38, %36 ], [ 1, %39 ]
%47 = phi i32 [ 0, %9 ], [ %16, %36 ], [ %41, %39 ]
%48 = phi i32 [ 1582, %9 ], [ %15, %36 ], [ %42, %39 ]
%49 = add nsw i32 %47, 1
%50 = tail call i32 @snprintf(i8* %1, i32 %2, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i32 %48, i32 %49, i32 %46)
br label %51
51: ; preds = %45, %11
%52 = phi i8* [ null, %11 ], [ %1, %45 ]
ret i8* %52
}