#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 time_t;
struct tm {
  int tm_year;
  int tm_mon;
  int tm_mday;
  int tm_hour;
  int tm_min;
  int tm_sec;
};

/* Variables and functions */
struct tm *localtime(int *);
int /*<<< orphan*/ sprintf(char *, char *, int, int, int, ...);
char *strcpy(char *, char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *duration_str(int show_astr, time_t cur, time_t start, char *buf,
                   int len) {
  int hh, mm, ss;
  static char b[64];
  if (show_astr) {
    struct tm *tt = localtime(&cur);
    sprintf(b, "%04d/%02d/%02d %02d:%02d:%02d", tt->tm_year + 1900,
            tt->tm_mon + 1, tt->tm_mday, tt->tm_hour, tt->tm_min, tt->tm_sec);
  } else {
    time_t time = cur - start;
    ss = time % 60;
    time /= 60;
    mm = time % 60;
    time /= 60;
    hh = time;
    sprintf(b, "%d:%02d:%02d", hh, mm, ss);
  }
  if (!buf)
    return b;
  if (len <= 0)
    return strcpy(buf, b);
  strncpy(buf, b, len);
  buf[len - 1] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<duration_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0xc(%rbp)
mov    %esi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8d,-0x24(%rbp)
cmpl   $0x0,-0xc(%rbp)
je     11c7 <duration_str+0x87>
lea    -0x10(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rax
mov    (%rax),%edx
add    $0x76c,%edx
mov    -0x38(%rbp),%rax
mov    0x4(%rax),%ecx
add    $0x1,%ecx
mov    -0x38(%rbp),%rax
mov    0x8(%rax),%r8d
mov    -0x38(%rbp),%rax
mov    0xc(%rax),%r9d
mov    -0x38(%rbp),%rax
mov    0x10(%rax),%r10d
mov    -0x38(%rbp),%rax
mov    0x14(%rax),%eax
lea    0x2ea4(%rip),%rdi
lea    0xe4d(%rip),%rsi
mov    %r10d,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1060 <sprintf@plt>
jmp    122d <duration_str+0xed>
mov    -0x10(%rbp),%eax
sub    -0x14(%rbp),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    %edx,-0x30(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    %edx,-0x2c(%rbp)
mov    -0x3c(%rbp),%eax
mov    $0x3c,%ecx
cltd
idiv   %ecx
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%edx
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%r8d
lea    0x2e31(%rip),%rdi
lea    0xdf8(%rip),%rsi
mov    $0x0,%al
call   1060 <sprintf@plt>
cmpq   $0x0,-0x20(%rbp)
jne    1248 <duration_str+0x108>
lea    0x2e11(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    1297 <duration_str+0x157>
cmpl   $0x0,-0x24(%rbp)
jg     126b <duration_str+0x12b>
mov    -0x20(%rbp),%rdi
lea    0x2df3(%rip),%rsi
call   1050 <strcpy@plt>
mov    %rax,-0x8(%rbp)
jmp    1297 <duration_str+0x157>
mov    -0x20(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0x2dd7(%rip),%rsi
call   1040 <strncpy@plt>
mov    -0x20(%rbp),%rax
mov    -0x24(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<duration_str>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %esi,0xc(%rsp)
test   %edi,%edi
je     11cb <duration_str+0x8b>
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
mov    $0x76c,%edx
add    (%rax),%edx
mov    0x4(%rax),%ecx
mov    0x8(%rax),%r8d
add    $0x1,%ecx
mov    0xc(%rax),%r9d
mov    0x10(%rax),%r10d
mov    0x14(%rax),%ebx
lea    0x2ecc(%rip),%rdi
lea    0xe75(%rip),%rsi
xor    %eax,%eax
push   %rbx
push   %r10
call   1060 <sprintf@plt>
add    $0x10,%rsp
test   %r15,%r15
je     1244 <duration_str+0x104>
lea    0x2ea7(%rip),%rsi
mov    %r15,%rdi
test   %r14d,%r14d
jle    124d <duration_str+0x10d>
mov    %r14d,%edx
call   1040 <strncpy@plt>
add    $0xffffffff,%r14d
movb   $0x0,(%r15,%r14,1)
jmp    1252 <duration_str+0x112>
sub    %edx,%esi
movslq %esi,%rax
imul   $0xffffffff88888889,%rax,%rcx
shr    $0x20,%rcx
add    %eax,%ecx
mov    %ecx,%edx
shr    $0x1f,%edx
sar    $0x5,%ecx
add    %edx,%ecx
imul   $0x3c,%ecx,%esi
imul   $0xffffffff91a2b3c5,%rax,%rdx
shr    $0x20,%rdx
add    %eax,%edx
mov    %eax,%r8d
sub    %esi,%r8d
movslq %ecx,%rcx
imul   $0xffffffff88888889,%rcx,%rax
shr    $0x20,%rax
add    %ecx,%eax
mov    %eax,%esi
shr    $0x1f,%esi
sar    $0x5,%eax
add    %esi,%eax
imul   $0x3c,%eax,%eax
sub    %eax,%ecx
mov    %edx,%eax
shr    $0x1f,%eax
sar    $0xb,%edx
add    %eax,%edx
lea    0x2e23(%rip),%rdi
lea    0xdea(%rip),%rsi
xor    %eax,%eax
call   1060 <sprintf@plt>
test   %r15,%r15
jne    11a2 <duration_str+0x62>
lea    0x2e05(%rip),%r15
jmp    1252 <duration_str+0x112>
call   1050 <strcpy@plt>
mov    %r15,%rax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
%struct.tm = type { i32, i32, i32, i32, i32, i32 }
@duration_str.b = internal global [64 x i8] zeroinitializer
@.str = [30 x i8] c"%04d/%02d/%02d %02d:%02d:%02d\00"
@.str.1 = [13 x i8] c"%d:%02d:%02d\00"
define i8* @duration_str(i32 %0, i32 %1, i32 %2, i8* %3, i32 %4) {
%6 = alloca i32
store i32 %1, i32* %6
%7 = icmp eq i32 %0, 0
br i1 %7, label %25, label %8
8: ; preds = %5
%9 = call %struct.tm* @localtime(i32* %6)
%10 = getelementptr inbounds %struct.tm, %struct.tm* %9, i64 0, i32 0
%11 = load i32, i32* %10
%12 = add nsw i32 %11, 1900
%13 = getelementptr inbounds %struct.tm, %struct.tm* %9, i64 0, i32 1
%14 = load i32, i32* %13
%15 = add nsw i32 %14, 1
%16 = getelementptr inbounds %struct.tm, %struct.tm* %9, i64 0, i32 2
%17 = load i32, i32* %16
%18 = getelementptr inbounds %struct.tm, %struct.tm* %9, i64 0, i32 3
%19 = load i32, i32* %18
%20 = getelementptr inbounds %struct.tm, %struct.tm* %9, i64 0, i32 4
%21 = load i32, i32* %20
%22 = getelementptr inbounds %struct.tm, %struct.tm* %9, i64 0, i32 5
%23 = load i32, i32* %22
%24 = call i32 (i8*, i8*, i32, i32, i32, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @duration_str.b, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @.str, i64 0, i64 0), i32 %12, i32 %15, i32 %17, i32 %19, i32 %21, i32 %23)
br label %32
25: ; preds = %5
%26 = sub nsw i32 %1, %2
%27 = srem i32 %26, 60
%28 = sdiv i32 %26, 60
%29 = srem i32 %28, 60
%30 = sdiv i32 %26, 3600
%31 = tail call i32 (i8*, i8*, i32, i32, i32, ...) @sprintf(i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @duration_str.b, i64 0, i64 0), i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %30, i32 %29, i32 %27)
br label %32
32: ; preds = %25, %8
%33 = icmp eq i8* %3, null
br i1 %33, label %43, label %34
34: ; preds = %32
%35 = icmp slt i32 %4, 1
br i1 %35, label %36, label %38
36: ; preds = %34
%37 = call i8* @strcpy(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([64 x i8], [64 x i8]* @duration_str.b, i64 0, i64 0))
br label %43
38: ; preds = %34
%39 = call i32 @strncpy(i8* %3, i8* getelementptr inbounds ([64 x i8], [64 x i8]* @duration_str.b, i64 0, i64 0), i32 %4)
%40 = add nsw i32 %4, -1
%41 = zext i32 %40 to i64
%42 = getelementptr inbounds i8, i8* %3, i64 %41
store i8 0, i8* %42
br label %43
43: ; preds = %32, %38, %36
%44 = phi i8* [ %3, %36 ], [ %3, %38 ], [ getelementptr inbounds ([64 x i8], [64 x i8]* @duration_str.b, i64 0, i64 0), %32 ]
ret i8* %44
}