#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 int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ snprintf(char *, int, char *, unsigned long, ...);
const char *format_seconds(unsigned long timeval) {
  static char buf[128];
  unsigned long rest;
  unsigned long days, hours, minutes, seconds;
  size_t i = 0;
  days = timeval / 86400;
  rest = timeval % 86400;
  hours = rest / 3600;
  rest = rest % 3600;
  minutes = rest / 60;
  rest = rest % 60;
  seconds = (unsigned int)rest;
  if (days)
    i += snprintf(&buf[i], sizeof(buf) - i, "%ld+", days);
  if (hours)
    i += snprintf(&buf[i], sizeof(buf) - i, "%02ld:", hours);
  i += snprintf(&buf[i], sizeof(buf) - i, "%02ld:%02ld", minutes, seconds);
  return buf;
}


x86 assembly (clang, -O0):
<format_seconds>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x15180,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x15180,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xe10,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    $0xe10,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3c,%ecx
xor    %edx,%edx
div    %rcx
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    $0x3c,%ecx
xor    %edx,%edx
div    %rcx
mov    %rdx,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %eax,%eax
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11d5 <format_seconds+0xc5>
lea    0x2e94(%rip),%rdi
add    -0x38(%rbp),%rdi
mov    $0x80,%eax
sub    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x18(%rbp),%rcx
lea    0xe3a(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     1210 <format_seconds+0x100>
lea    0x2e59(%rip),%rdi
add    -0x38(%rbp),%rdi
mov    $0x80,%eax
sub    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x20(%rbp),%rcx
lea    0xe04(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0x2e29(%rip),%rdi
add    -0x38(%rbp),%rdi
mov    $0x80,%eax
sub    -0x38(%rbp),%rax
mov    %eax,%esi
mov    -0x28(%rbp),%rcx
mov    -0x30(%rbp),%r8
lea    0xdd7(%rip),%rdx
mov    $0x0,%al
call   1030 <snprintf@plt>
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
lea    0x2df5(%rip),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<format_seconds>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
movabs $0xc22e450672894ab7,%rcx
45 2e c2
mov    %rdi,%rax
mul    %rcx
mov    %rdx,%rcx
shr    $0x10,%rcx
imul   $0x15180,%rcx,%rax
mov    %rdi,%rbx
sub    %rax,%rbx
mov    %ebx,%r15d
mov    $0x91a2b3c5,%r12d
imul   %rbx,%r12
shr    $0x2b,%r12
imul   $0xe10,%r12d,%ebp
cmp    $0x15180,%rdi
jae    1162 <format_seconds+0x52>
xor    %r14d,%r14d
jmp    117f <format_seconds+0x6f>
lea    0x2ed7(%rip),%rdi
lea    0xe90(%rip),%rdx
mov    $0x80,%esi
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rax,%r14
sub    %ebp,%r15d
cmp    $0xe10,%rbx
jb     11b1 <format_seconds+0xa1>
lea    0x2eae(%rip),%rdi
add    %r14,%rdi
mov    $0x80,%esi
sub    %r14d,%esi
lea    0xe61(%rip),%rdx
mov    %r12,%rcx
xor    %eax,%eax
call   1030 <snprintf@plt>
add    %rax,%r14
movzwl %r15w,%eax
imul   $0x8889,%eax,%ecx
shr    $0x15,%ecx
imul   $0x3c,%ecx,%eax
sub    %eax,%r15d
movzwl %r15w,%r8d
lea    0x2e71(%rip),%rbx
lea    (%rbx,%r14,1),%rdi
mov    $0x80,%esi
sub    %r14d,%esi
lea    0xe2a(%rip),%rdx
xor    %eax,%eax
call   1030 <snprintf@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@format_seconds.buf = internal global [128 x i8] zeroinitializer
@.str = [5 x i8] c"%ld+\00"
@.str.1 = [7 x i8] c"%02ld:\00"
@.str.2 = [12 x i8] c"%02ld:%02ld\00"
define i8* @format_seconds(i64 %0) {
%2 = alloca i64
%3 = alloca i64
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i64
store i64 %0, i64* %2
store i64 0, i64* %8
%9 = load i64, i64* %2
%10 = udiv i64 %9, 86400
store i64 %10, i64* %4
%11 = load i64, i64* %2
%12 = urem i64 %11, 86400
store i64 %12, i64* %3
%13 = load i64, i64* %3
%14 = udiv i64 %13, 3600
store i64 %14, i64* %5
%15 = load i64, i64* %3
%16 = urem i64 %15, 3600
store i64 %16, i64* %3
%17 = load i64, i64* %3
%18 = udiv i64 %17, 60
store i64 %18, i64* %6
%19 = load i64, i64* %3
%20 = urem i64 %19, 60
store i64 %20, i64* %3
%21 = load i64, i64* %3
%22 = trunc i64 %21 to i32
%23 = zext i32 %22 to i64
store i64 %23, i64* %7
%24 = load i64, i64* %4
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %36
26: ; preds = %1
%27 = load i64, i64* %8
%28 = getelementptr inbounds [128 x i8], [128 x i8]* @format_seconds.buf, i64 0, i64 %27
%29 = load i64, i64* %8
%30 = sub i64 128, %29
%31 = trunc i64 %30 to i32
%32 = load i64, i64* %4
%33 = call i64 (i8*, i32, i8*, i64, ...) @snprintf(i8* %28, i32 %31, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %32)
%34 = load i64, i64* %8
%35 = add i64 %34, %33
store i64 %35, i64* %8
br label %36
36: ; preds = %26, %1
%37 = load i64, i64* %5
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %49
39: ; preds = %36
%40 = load i64, i64* %8
%41 = getelementptr inbounds [128 x i8], [128 x i8]* @format_seconds.buf, i64 0, i64 %40
%42 = load i64, i64* %8
%43 = sub i64 128, %42
%44 = trunc i64 %43 to i32
%45 = load i64, i64* %5
%46 = call i64 (i8*, i32, i8*, i64, ...) @snprintf(i8* %41, i32 %44, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %45)
%47 = load i64, i64* %8
%48 = add i64 %47, %46
store i64 %48, i64* %8
br label %49
49: ; preds = %39, %36
%50 = load i64, i64* %8
%51 = getelementptr inbounds [128 x i8], [128 x i8]* @format_seconds.buf, i64 0, i64 %50
%52 = load i64, i64* %8
%53 = sub i64 128, %52
%54 = trunc i64 %53 to i32
%55 = load i64, i64* %6
%56 = load i64, i64* %7
%57 = call i64 (i8*, i32, i8*, i64, ...) @snprintf(i8* %51, i32 %54, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %55, i64 %56)
%58 = load i64, i64* %8
%59 = add i64 %58, %57
store i64 %59, i64* %8
ret i8* getelementptr inbounds ([128 x i8], [128 x i8]* @format_seconds.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@format_seconds.buf = internal global [128 x i8] zeroinitializer
@.str = [5 x i8] c"%ld+\00"
@.str.1 = [7 x i8] c"%02ld:\00"
@.str.2 = [12 x i8] c"%02ld:%02ld\00"
define i8* @format_seconds(i64 %0) {
%2 = urem i64 %0, 86400
%3 = udiv i64 %0, 86400
%4 = trunc i64 %2 to i32
%5 = udiv i32 %4, 3600
%6 = urem i32 %4, 3600
%7 = zext i32 %5 to i64
%8 = icmp ult i64 %0, 86400
br i1 %8, label %11, label %9
9: ; preds = %1
%10 = tail call i64 (i8*, i32, i8*, i64, ...) @snprintf(i8* getelementptr inbounds ([128 x i8], [128 x i8]* @format_seconds.buf, i64 0, i64 0), i32 128, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i64 %3)
br label %11
11: ; preds = %9, %1
%12 = phi i64 [ %10, %9 ], [ 0, %1 ]
%13 = icmp ult i64 %2, 3600
br i1 %13, label %20, label %14
14: ; preds = %11
%15 = getelementptr inbounds [128 x i8], [128 x i8]* @format_seconds.buf, i64 0, i64 %12
%16 = trunc i64 %12 to i32
%17 = sub i32 128, %16
%18 = tail call i64 (i8*, i32, i8*, i64, ...) @snprintf(i8* %15, i32 %17, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.1, i64 0, i64 0), i64 %7)
%19 = add i64 %18, %12
br label %20
20: ; preds = %14, %11
%21 = phi i64 [ %19, %14 ], [ %12, %11 ]
%22 = trunc i32 %6 to i16
%23 = urem i16 %22, 60
%24 = zext i16 %23 to i64
%25 = udiv i16 %22, 60
%26 = zext i16 %25 to i64
%27 = getelementptr inbounds [128 x i8], [128 x i8]* @format_seconds.buf, i64 0, i64 %21
%28 = trunc i64 %21 to i32
%29 = sub i32 128, %28
%30 = tail call i64 (i8*, i32, i8*, i64, ...) @snprintf(i8* %27, i32 %29, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i64 %26, i64 %24)
ret i8* getelementptr inbounds ([128 x i8], [128 x i8]* @format_seconds.buf, i64 0, i64 0)
}