char *timstr(time)
long time;
{
  int hours, minutes, seconds;
  static char buf[9];
  hours = time / 3600L;
  time -= (long)hours * 3600;
  minutes = time / 60;
  seconds = time - minutes * 60;
  sprintf(buf, "%02d:%02d:%02d", hours, minutes, seconds);
  return (buf);
}


x86 assembly (clang, -O0):
<timstr>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0xe10,%ecx
cqto
idiv   %rcx
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
imul   $0xe10,%rax,%rcx
mov    -0x8(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
mov    $0x3c,%ecx
cqto
idiv   %rcx
mov    %eax,-0x10(%rbp)
mov    -0x8(%rbp),%rax
imul   $0x3c,-0x10(%rbp),%ecx
movslq %ecx,%rcx
sub    %rcx,%rax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%edx
mov    -0x10(%rbp),%ecx
mov    -0x14(%rbp),%r8d
lea    0x2eb3(%rip),%rdi
lea    0xe83(%rip),%rsi
mov    $0x0,%al
call   1030 <sprintf@plt>
lea    0x2e9e(%rip),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timstr>:
push   %rbx
movabs $0x48d159e26af37c05,%rcx
59 d1 48
mov    %rdi,%rax
imul   %rcx
mov    %rdx,%rax
shr    $0x3f,%rax
sar    $0xa,%rdx
add    %rax,%rdx
movslq %edx,%r9
imul   $0xfffffffffffff1f0,%r9,%r8
add    %rdi,%r8
movabs $0x8888888888888889,%rcx
88 88 88
mov    %r8,%rax
imul   %rcx
mov    %rdx,%rcx
add    %r8,%rcx
mov    %rcx,%rax
shr    $0x3f,%rax
sar    $0x5,%rcx
add    %rax,%rcx
movabs $0x3c00000000,%rax
imul   %rcx,%rax
shr    $0x20,%rax
sub    %eax,%r8d
lea    0x2ead(%rip),%rbx
lea    0xe7d(%rip),%rsi
mov    %rbx,%rdi
mov    %r9d,%edx
xor    %eax,%eax
call   1030 <sprintf@plt>
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@timstr.buf = internal global [9 x i8] zeroinitializer
@.str = [15 x i8] c"%02d:%02d:%02d\00"
define i8* @timstr(i64 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
store i64 %0, i64* %2
%6 = load i64, i64* %2
%7 = sdiv i64 %6, 3600
%8 = trunc i64 %7 to i32
store i32 %8, i32* %3
%9 = load i32, i32* %3
%10 = sext i32 %9 to i64
%11 = mul nsw i64 %10, 3600
%12 = load i64, i64* %2
%13 = sub nsw i64 %12, %11
store i64 %13, i64* %2
%14 = load i64, i64* %2
%15 = sdiv i64 %14, 60
%16 = trunc i64 %15 to i32
store i32 %16, i32* %4
%17 = load i64, i64* %2
%18 = load i32, i32* %4
%19 = mul nsw i32 %18, 60
%20 = sext i32 %19 to i64
%21 = sub nsw i64 %17, %20
%22 = trunc i64 %21 to i32
store i32 %22, i32* %5
%23 = load i32, i32* %3
%24 = load i32, i32* %4
%25 = load i32, i32* %5
%26 = call i32 (i8*, i8*, ...) @sprintf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @timstr.buf, i64 0, i64 0), i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %23, i32 %24, i32 %25)
ret i8* getelementptr inbounds ([9 x i8], [9 x i8]* @timstr.buf, i64 0, i64 0)
}

LLVM IR (-O2):
@timstr.buf = internal global [9 x i8] zeroinitializer
@.str = [15 x i8] c"%02d:%02d:%02d\00"
define i8* @timstr(i64 %0) {
%2 = sdiv i64 %0, 3600
%3 = trunc i64 %2 to i32
%4 = shl i64 %2, 32
%5 = ashr exact i64 %4, 32
%6 = mul nsw i64 %5, -3600
%7 = add i64 %6, %0
%8 = sdiv i64 %7, 60
%9 = trunc i64 %8 to i32
%10 = mul i64 %8, 257698037760
%11 = lshr exact i64 %10, 32
%12 = sub i64 %7, %11
%13 = trunc i64 %12 to i32
%14 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @timstr.buf, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0), i32 %3, i32 %9, i32 %13)
ret i8* getelementptr inbounds ([9 x i8], [9 x i8]* @timstr.buf, i64 0, i64 0)
}