#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 unsigned long long uint64_t;
typedef unsigned int time_t;
struct tm {
  int tm_mday;
  int tm_mon;
  int tm_year;
};

/* Variables and functions */
struct tm *gmtime(unsigned int *);
unsigned int mktime(struct tm *);
void add_beacon_timestamp(int *year, int *month, uint64_t timestamp) {
  unsigned days = timestamp / (((1000000ULL * 60ULL) * 60ULL) * 24ULL);
  struct tm tms = {.tm_mday = 1, .tm_mon = *month - 1, .tm_year = *year - 1900};
  time_t start = mktime(&tms);
  unsigned secs = days * (24 * 60 * 60);
  start += secs;
  struct tm *result = gmtime(&start);
  *year = result->tm_year + 1900;
  *month = result->tm_mon + 1;
}


x86 assembly (clang, -O0):
<add_beacon_timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movabs $0x141dd76000,%rcx
xor    %edx,%edx
div    %rcx
mov    %eax,-0x1c(%rbp)
movl   $0x1,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
sub    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%eax
sub    $0x76c,%eax
mov    %eax,-0x20(%rbp)
lea    -0x28(%rbp),%rdi
call   1030 <mktime@plt>
mov    %eax,-0x2c(%rbp)
imul   $0x15180,-0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
add    -0x2c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
lea    -0x2c(%rbp),%rdi
call   1040 <gmtime@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%ecx
add    $0x76c,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<add_beacon_timestamp>:
push   %r15
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %rdx,%rax
mov    %rsi,%r14
mov    %rdi,%r15
movabs $0x32e73fb956a1b897,%rcx
3f e7 32
mul    %rcx
mov    %rdx,%rbx
shr    $0x22,%rbx
movl   $0x1,0x10(%rsp)
mov    (%rsi),%eax
add    $0xffffffff,%eax
mov    %eax,0x14(%rsp)
mov    $0xfffff894,%eax
add    (%rdi),%eax
mov    %eax,0x18(%rsp)
lea    0x10(%rsp),%rdi
call   1030 <mktime@plt>
imul   $0x15180,%ebx,%ecx
add    %eax,%ecx
mov    %ecx,0xc(%rsp)
lea    0xc(%rsp),%rdi
call   1040 <gmtime@plt>
mov    $0x76c,%ecx
add    0x8(%rax),%ecx
mov    %ecx,(%r15)
mov    0x4(%rax),%eax
add    $0x1,%eax
mov    %eax,(%r14)
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32 }
define void @add_beacon_timestamp(i32* %0, i32* %1, i64 %2) {
%4 = alloca i32*
%5 = alloca i32*
%6 = alloca i64
%7 = alloca i32
%8 = alloca %struct.tm
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.tm*
store i32* %0, i32** %4
store i32* %1, i32** %5
store i64 %2, i64* %6
%12 = load i64, i64* %6
%13 = udiv i64 %12, 86400000000
%14 = trunc i64 %13 to i32
store i32 %14, i32* %7
%15 = getelementptr inbounds %struct.tm, %struct.tm* %8, i32 0, i32 0
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.tm, %struct.tm* %8, i32 0, i32 1
%17 = load i32*, i32** %5
%18 = load i32, i32* %17
%19 = sub nsw i32 %18, 1
store i32 %19, i32* %16
%20 = getelementptr inbounds %struct.tm, %struct.tm* %8, i32 0, i32 2
%21 = load i32*, i32** %4
%22 = load i32, i32* %21
%23 = sub nsw i32 %22, 1900
store i32 %23, i32* %20
%24 = call i32 @mktime(%struct.tm* %8)
store i32 %24, i32* %9
%25 = load i32, i32* %7
%26 = mul i32 %25, 86400
store i32 %26, i32* %10
%27 = load i32, i32* %10
%28 = load i32, i32* %9
%29 = add i32 %28, %27
store i32 %29, i32* %9
%30 = call %struct.tm* @gmtime(i32* %9)
store %struct.tm* %30, %struct.tm** %11
%31 = load %struct.tm*, %struct.tm** %11
%32 = getelementptr inbounds %struct.tm, %struct.tm* %31, i32 0, i32 2
%33 = load i32, i32* %32
%34 = add nsw i32 %33, 1900
%35 = load i32*, i32** %4
store i32 %34, i32* %35
%36 = load %struct.tm*, %struct.tm** %11
%37 = getelementptr inbounds %struct.tm, %struct.tm* %36, i32 0, i32 1
%38 = load i32, i32* %37
%39 = add nsw i32 %38, 1
%40 = load i32*, i32** %5
store i32 %39, i32* %40
ret void
}

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32 }
define void @add_beacon_timestamp(i32* nocapture %0, i32* nocapture %1, i64 %2) {
%4 = alloca %struct.tm
%5 = alloca i32
%6 = udiv i64 %2, 86400000000
%7 = trunc i64 %6 to i32
%8 = bitcast %struct.tm* %4 to i8*
%9 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 0
store i32 1, i32* %9
%10 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 1
%11 = load i32, i32* %1
%12 = add nsw i32 %11, -1
store i32 %12, i32* %10
%13 = getelementptr inbounds %struct.tm, %struct.tm* %4, i64 0, i32 2
%14 = load i32, i32* %0
%15 = add nsw i32 %14, -1900
store i32 %15, i32* %13
%16 = bitcast i32* %5 to i8*
%17 = call i32 @mktime(%struct.tm* %4)
%18 = mul i32 %7, 86400
%19 = add i32 %17, %18
store i32 %19, i32* %5
%20 = call %struct.tm* @gmtime(i32* %5)
%21 = getelementptr inbounds %struct.tm, %struct.tm* %20, i64 0, i32 2
%22 = load i32, i32* %21
%23 = add nsw i32 %22, 1900
store i32 %23, i32* %0
%24 = getelementptr inbounds %struct.tm, %struct.tm* %20, i64 0, i32 1
%25 = load i32, i32* %24
%26 = add nsw i32 %25, 1
store i32 %26, i32* %1
ret void
}