#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 tm_year;
  int tm_mon;
  int /*<<< orphan*/ tm_mday;
};
typedef int int64_t;

/* Variables and functions */
int /*<<< orphan*/ gmtime_r(scalar_t__ *, struct tm *);
int /*<<< orphan*/ llabs(long long);
size_t snprintf(char *, size_t, char *, char const *, ...);
scalar_t__ time(int /*<<< orphan*/ *);
size_t fmt_time(char *s, size_t size, const char *header, int64_t n,
                const char *trailer, int relative) {
  struct tm tm;
  time_t elapsed;
  time_t now;
  time_t t;
  size_t cc;
  t = (time_t)n;
  now = time(NULL);
  elapsed = now - t;
  gmtime_r(&t, &tm);
  cc = snprintf(s, size, "%s%04d-%02d-%02d", header, tm.tm_year + 1900,
                tm.tm_mon + 1, tm.tm_mday);
  if (relative) {
    cc += snprintf(
        &s[cc], size - cc, " (%lldy %lldm %lldd %lldh %s)",
        llabs((long long)elapsed / (int64_t)(365 * (int64_t)(24 * 60 * 60))),
        llabs(((long long)elapsed % (int64_t)(365 * (int64_t)(24 * 60 * 60))) /
              (int64_t)(30 * (int64_t)(24 * 60 * 60))),
        llabs(((long long)elapsed % (int64_t)(30 * (int64_t)(24 * 60 * 60))) /
              (int64_t)(24 * 60 * 60)),
        llabs(((long long)elapsed % (int64_t)(24 * 60 * 60)) /
              (int64_t)(60 * 60)),
        (now > t) ? "ago" : "ahead");
  }
  cc += snprintf(&s[cc], size - cc, "%s", trailer);
  return cc;
}


x86 assembly (clang, -O0):
<fmt_time>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
sub    $0x88,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %ecx,-0x24(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9d,-0x34(%rbp)
movslq -0x24(%rbp),%rax
mov    %rax,-0x58(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %rax,-0x50(%rbp)
mov    -0x50(%rbp),%rax
sub    -0x58(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    -0x58(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1040 <gmtime_r@plt>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rcx
mov    -0x40(%rbp),%r8d
add    $0x76c,%r8d
mov    -0x3c(%rbp),%r9d
add    $0x1,%r9d
mov    -0x38(%rbp),%eax
lea    0xe46(%rip),%rdx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1030 <snprintf@plt>
mov    %rax,-0x60(%rbp)
cmpl   $0x0,-0x34(%rbp)
je     12c8 <fmt_time+0x188>
mov    -0x10(%rbp),%rax
add    -0x60(%rbp),%rax
mov    %rax,-0x80(%rbp)
mov    -0x18(%rbp),%rax
sub    -0x60(%rbp),%rax
mov    %rax,-0x78(%rbp)
mov    -0x48(%rbp),%rax
mov    $0x1e13380,%ecx
cqto
idiv   %rcx
mov    %rax,%rdi
call   1050 <llabs@plt>
cltq
mov    %rax,-0x70(%rbp)
mov    -0x48(%rbp),%rax
mov    $0x1e13380,%ecx
cqto
idiv   %rcx
mov    %rdx,%rax
mov    $0x278d00,%ecx
cqto
idiv   %rcx
mov    %rax,%rdi
call   1050 <llabs@plt>
mov    %eax,-0x68(%rbp)
mov    -0x48(%rbp),%rax
mov    $0x278d00,%ecx
cqto
idiv   %rcx
mov    %rdx,%rax
mov    $0x15180,%ecx
cqto
idiv   %rcx
mov    %rax,%rdi
call   1050 <llabs@plt>
mov    %eax,-0x64(%rbp)
mov    -0x48(%rbp),%rax
mov    $0x15180,%ecx
cqto
idiv   %rcx
mov    %rdx,%rax
mov    $0xe10,%ecx
cqto
idiv   %rcx
mov    %rax,%rdi
call   1050 <llabs@plt>
mov    -0x80(%rbp),%rdi
mov    -0x78(%rbp),%rsi
mov    -0x70(%rbp),%rcx
mov    -0x68(%rbp),%r8d
mov    -0x64(%rbp),%r9d
mov    %eax,%r10d
mov    -0x50(%rbp),%r11
mov    -0x58(%rbp),%rbx
lea    0xd98(%rip),%rax
lea    0xd8d(%rip),%rdx
cmp    %rbx,%r11
cmovg  %rdx,%rax
lea    0xd61(%rip),%rdx
mov    %r10d,(%rsp)
mov    %rax,0x8(%rsp)
mov    $0x0,%al
call   1030 <snprintf@plt>
add    -0x60(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    -0x60(%rbp),%rsi
mov    -0x30(%rbp),%rcx
lea    0xd56(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
add    -0x60(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rax
add    $0x88,%rsp
pop    %rbx
pop    %rbp
ret

x86 assembly (clang, -O2):
<fmt_time>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
mov    %r9d,0x10(%rsp)
mov    %r8,0x40(%rsp)
mov    %rdx,%r13
mov    %rsi,%r12
mov    %rdi,%rbp
movslq %ecx,%rax
mov    %rax,0x8(%rsp)
mov    %rax,0x28(%rsp)
xor    %edi,%edi
call   1060 <time@plt>
mov    %rax,0x20(%rsp)
lea    0x28(%rsp),%rdi
lea    0x30(%rsp),%rsi
call   1040 <gmtime_r@plt>
mov    $0x76c,%r8d
add    0x30(%rsp),%r8d
mov    0x34(%rsp),%r9d
mov    0x38(%rsp),%ebx
add    $0x1,%r9d
sub    $0x8,%rsp
lea    0xe54(%rip),%rdx
mov    %rbp,%r14
mov    %rbp,%rdi
mov    %r12,%r15
mov    %r12,%rsi
mov    %r13,%rcx
xor    %eax,%eax
push   %rbx
call   1030 <snprintf@plt>
add    $0x10,%rsp
mov    %rax,%rbx
cmpl   $0x0,0x10(%rsp)
je     1343 <fmt_time+0x203>
mov    0x20(%rsp),%r13
sub    0x8(%rsp),%r13
mov    %r14,%r12
lea    (%r14,%rbx,1),%rax
mov    %rax,0x10(%rsp)
mov    %r15,%r14
sub    %rbx,%r14
movabs $0x883148f5cb1270b9,%rcx
48 31 88
mov    %r13,%rax
imul   %rcx
mov    %rdx,%rbp
add    %r13,%rbp
mov    %rbp,%rax
shr    $0x3f,%rax
sar    $0x18,%rbp
add    %rax,%rbp
mov    %rbp,%rdi
call   1050 <llabs@plt>
cltq
mov    %rax,0x8(%rsp)
imul   $0x1e13380,%ebp,%eax
mov    %r13d,%ecx
sub    %eax,%ecx
movslq %ecx,%rax
imul   $0x679024d1,%rax,%rax
mov    %rax,%rcx
shr    $0x3f,%rcx
shr    $0x20,%rax
sar    $0x14,%eax
add    %ecx,%eax
movslq %eax,%rdi
call   1050 <llabs@plt>
mov    %eax,0x1c(%rsp)
movabs $0x679024d03d1605b7,%rcx
24 90 67
mov    %r13,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0x14,%rdx
add    %rax,%rdx
imul   $0x278d00,%rdx,%rax
mov    %r13,%rcx
sub    %rax,%rcx
movslq %ecx,%rax
imul   $0xffffffffc22e4507,%rax,%rcx
shr    $0x20,%rcx
add    %ecx,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0x10,%eax
add    %ecx,%eax
movslq %eax,%rdi
call   1050 <llabs@plt>
mov    %eax,0x18(%rsp)
movabs $0x1845c8a0ce512957,%rcx
c8 45 18
mov    %r13,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0xd,%rdx
add    %rax,%rdx
imul   $0x15180,%rdx,%rax
sub    %rax,%r13
movslq %r13d,%rax
imul   $0xffffffff91a2b3c5,%rax,%rcx
shr    $0x20,%rcx
add    %ecx,%eax
mov    %eax,%ecx
shr    $0x1f,%ecx
sar    $0xb,%eax
add    %ecx,%eax
movslq %eax,%rdi
call   1050 <llabs@plt>
mov    %eax,%r10d
mov    0x20(%rsp),%rax
cmp    0x28(%rsp),%rax
lea    0xd2b(%rip),%rax
lea    0xd28(%rip),%rbp
cmovg  %rax,%rbp
lea    0xcfb(%rip),%rdx
mov    0x10(%rsp),%rdi
mov    %r14,%rsi
mov    0x8(%rsp),%rcx
mov    0x1c(%rsp),%r8d
mov    0x18(%rsp),%r9d
xor    %eax,%eax
push   %rbp
push   %r10
call   1030 <snprintf@plt>
add    $0x10,%rsp
add    %rax,%rbx
mov    %r12,%rdi
jmp    1346 <fmt_time+0x206>
mov    %r14,%rdi
add    %rbx,%rdi
sub    %rbx,%r15
lea    0xce6(%rip),%rdx
mov    %r15,%rsi
mov    0x40(%rsp),%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
add    %rbx,%rax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32, i32, i32 }
@.str = [17 x i8] c"%s%04d-%02d-%02d\00"
@.str.1 = [30 x i8] c" (%lldy %lldm %lldd %lldh %s)\00"
@.str.2 = [4 x i8] c"ago\00"
@.str.3 = [6 x i8] c"ahead\00"
@.str.4 = [3 x i8] c"%s\00"
define i64 @fmt_time(i8* %0, i64 %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca i8*
%8 = alloca i64
%9 = alloca i8*
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca %struct.tm
%14 = alloca i64
%15 = alloca i64
%16 = alloca i64
%17 = alloca i64
store i8* %0, i8** %7
store i64 %1, i64* %8
store i8* %2, i8** %9
store i32 %3, i32* %10
store i8* %4, i8** %11
store i32 %5, i32* %12
%18 = load i32, i32* %10
%19 = sext i32 %18 to i64
store i64 %19, i64* %16
%20 = call i64 @time(i32* null)
store i64 %20, i64* %15
%21 = load i64, i64* %15
%22 = load i64, i64* %16
%23 = sub nsw i64 %21, %22
store i64 %23, i64* %14
%24 = call i32 @gmtime_r(i64* %16, %struct.tm* %13)
%25 = load i8*, i8** %7
%26 = load i64, i64* %8
%27 = load i8*, i8** %9
%28 = getelementptr inbounds %struct.tm, %struct.tm* %13, i32 0, i32 0
%29 = load i32, i32* %28
%30 = add nsw i32 %29, 1900
%31 = getelementptr inbounds %struct.tm, %struct.tm* %13, i32 0, i32 1
%32 = load i32, i32* %31
%33 = add nsw i32 %32, 1
%34 = getelementptr inbounds %struct.tm, %struct.tm* %13, i32 0, i32 2
%35 = load i32, i32* %34
%36 = call i64 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %25, i64 %26, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %27, i32 %30, i32 %33, i32 %35)
store i64 %36, i64* %17
%37 = load i32, i32* %12
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %71
39: ; preds = %6
%40 = load i8*, i8** %7
%41 = load i64, i64* %17
%42 = getelementptr inbounds i8, i8* %40, i64 %41
%43 = load i64, i64* %8
%44 = load i64, i64* %17
%45 = sub i64 %43, %44
%46 = load i64, i64* %14
%47 = sdiv i64 %46, 31536000
%48 = call i32 @llabs(i64 %47)
%49 = sext i32 %48 to i64
%50 = inttoptr i64 %49 to i8*
%51 = load i64, i64* %14
%52 = srem i64 %51, 31536000
%53 = sdiv i64 %52, 2592000
%54 = call i32 @llabs(i64 %53)
%55 = load i64, i64* %14
%56 = srem i64 %55, 2592000
%57 = sdiv i64 %56, 86400
%58 = call i32 @llabs(i64 %57)
%59 = load i64, i64* %14
%60 = srem i64 %59, 86400
%61 = sdiv i64 %60, 3600
%62 = call i32 @llabs(i64 %61)
%63 = load i64, i64* %15
%64 = load i64, i64* %16
%65 = icmp sgt i64 %63, %64
%66 = zext i1 %65 to i64
%67 = select i1 %65, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%68 = call i64 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %42, i64 %45, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %50, i32 %54, i32 %58, i32 %62, i8* %67)
%69 = load i64, i64* %17
%70 = add i64 %69, %68
store i64 %70, i64* %17
br label %71
71: ; preds = %39, %6
%72 = load i8*, i8** %7
%73 = load i64, i64* %17
%74 = getelementptr inbounds i8, i8* %72, i64 %73
%75 = load i64, i64* %8
%76 = load i64, i64* %17
%77 = sub i64 %75, %76
%78 = load i8*, i8** %11
%79 = call i64 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %74, i64 %77, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %78)
%80 = load i64, i64* %17
%81 = add i64 %80, %79
store i64 %81, i64* %17
%82 = load i64, i64* %17
ret i64 %82
}

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32 }
@.str = [17 x i8] c"%s%04d-%02d-%02d\00"
@.str.1 = [30 x i8] c" (%lldy %lldm %lldd %lldh %s)\00"
@.str.2 = [4 x i8] c"ago\00"
@.str.3 = [6 x i8] c"ahead\00"
@.str.4 = [3 x i8] c"%s\00"
define i64 @fmt_time(i8* %0, i64 %1, i8* %2, i32 %3, i8* %4, i32 %5) {
%7 = alloca %struct.tm
%8 = alloca i64
%9 = bitcast %struct.tm* %7 to i8*
%10 = bitcast i64* %8 to i8*
%11 = sext i32 %3 to i64
store i64 %11, i64* %8
%12 = tail call i64 @time(i32* null)
%13 = sub nsw i64 %12, %11
%14 = call i32 @gmtime_r(i64* %8, %struct.tm* %7)
%15 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 0
%16 = load i32, i32* %15
%17 = add nsw i32 %16, 1900
%18 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 1
%19 = load i32, i32* %18
%20 = add nsw i32 %19, 1
%21 = getelementptr inbounds %struct.tm, %struct.tm* %7, i64 0, i32 2
%22 = load i32, i32* %21
%23 = call i64 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %0, i64 %1, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %2, i32 %17, i32 %20, i32 %22)
%24 = icmp eq i32 %5, 0
br i1 %24, label %52, label %25
25: ; preds = %6
%26 = getelementptr inbounds i8, i8* %0, i64 %23
%27 = sub i64 %1, %23
%28 = sdiv i64 %13, 31536000
%29 = call i32 @llabs(i64 %28)
%30 = sext i32 %29 to i64
%31 = inttoptr i64 %30 to i8*
%32 = srem i64 %13, 31536000
%33 = trunc i64 %32 to i32
%34 = sdiv i32 %33, 2592000
%35 = sext i32 %34 to i64
%36 = call i32 @llabs(i64 %35)
%37 = srem i64 %13, 2592000
%38 = trunc i64 %37 to i32
%39 = sdiv i32 %38, 86400
%40 = sext i32 %39 to i64
%41 = call i32 @llabs(i64 %40)
%42 = srem i64 %13, 86400
%43 = trunc i64 %42 to i32
%44 = sdiv i32 %43, 3600
%45 = sext i32 %44 to i64
%46 = call i32 @llabs(i64 %45)
%47 = load i64, i64* %8
%48 = icmp sgt i64 %12, %47
%49 = select i1 %48, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0)
%50 = call i64 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %26, i64 %27, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %31, i32 %36, i32 %41, i32 %46, i8* %49)
%51 = add i64 %50, %23
br label %52
52: ; preds = %25, %6
%53 = phi i64 [ %51, %25 ], [ %23, %6 ]
%54 = getelementptr inbounds i8, i8* %0, i64 %53
%55 = sub i64 %1, %53
%56 = call i64 (i8*, i64, i8*, i8*, ...) @snprintf(i8* %54, i64 %55, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %4)
%57 = add i64 %56, %53
ret i64 %57
}