#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 */
struct tm {
  int dummy;
};
struct str {
  int dummy;
};
typedef int /*<<< orphan*/ buf0;

/* Variables and functions */
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ str_catn(struct str *, char *, int);
int /*<<< orphan*/ str_copyn(struct str *, char *, int);
int strftime(char *, int, char const *, struct tm *);
void str_formattime(struct str *sa, int cat, const char *fmt, struct tm *tm) {
  char buf0[0x0fff + 1], *buf;
  int buf_len;
  int n;
  buf = buf0;
  buf_len = sizeof(buf0);
  n = strftime(buf, buf_len, fmt, tm);
  if (n == 0 || n >= buf_len) {
    buf_len = 0x1fff + 1;
    assert((buf = malloc(buf_len)) != NULL);
    n = strftime(buf, buf_len, fmt, tm);
    if (n == 0 || n >= buf_len) {
      free(buf);
      buf_len = 0xffff + 1;
      assert((buf = malloc(buf_len)) != NULL);
      n = strftime(buf, buf_len, fmt, tm);
      if (n == 0 || n >= buf_len) {
        free(buf);
        buf_len = 0xfffff + 1;
        assert((buf = malloc(buf_len)) != NULL);
        n = strftime(buf, buf_len, fmt, tm);
        if (n == 0 || n >= buf_len) {
          free(buf);
          fprintf(stderr, "error: str_vformattime(): too large input (> %i)\n",
                  0xfffff + 1);
          exit(1);
        }
      }
    }
  }
  assert(buf != NULL);
  if (cat) {
    str_catn(sa, buf, n);
  } else {
    str_copyn(sa, buf, n);
  }
  if (buf && buf != buf0) {
    free(buf);
  }
}


x86 assembly (clang, -O0):
<str_formattime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x1028(%rbp)
movl   $0x1000,-0x102c(%rbp)
mov    -0x1028(%rbp),%rdi
mov    -0x102c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1090 <strftime@plt>
mov    %eax,-0x1030(%rbp)
cmpl   $0x0,-0x1030(%rbp)
je     11f1 <str_formattime+0x71>
mov    -0x1030(%rbp),%eax
cmp    -0x102c(%rbp),%eax
jl     138c <str_formattime+0x20c>
movl   $0x2000,-0x102c(%rbp)
mov    -0x102c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x1028(%rbp),%rdi
mov    -0x102c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1090 <strftime@plt>
mov    %eax,-0x1030(%rbp)
cmpl   $0x0,-0x1030(%rbp)
je     125d <str_formattime+0xdd>
mov    -0x1030(%rbp),%eax
cmp    -0x102c(%rbp),%eax
jl     1387 <str_formattime+0x207>
mov    -0x1028(%rbp),%rdi
call   1030 <free@plt>
movl   $0x10000,-0x102c(%rbp)
mov    -0x102c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x1028(%rbp),%rdi
mov    -0x102c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1090 <strftime@plt>
mov    %eax,-0x1030(%rbp)
cmpl   $0x0,-0x1030(%rbp)
je     12d5 <str_formattime+0x155>
mov    -0x1030(%rbp),%eax
cmp    -0x102c(%rbp),%eax
jl     1382 <str_formattime+0x202>
mov    -0x1028(%rbp),%rdi
call   1030 <free@plt>
movl   $0x100000,-0x102c(%rbp)
mov    -0x102c(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x1028(%rbp)
cmp    $0x0,%rax
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
mov    -0x1028(%rbp),%rdi
mov    -0x102c(%rbp),%esi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1090 <strftime@plt>
mov    %eax,-0x1030(%rbp)
cmpl   $0x0,-0x1030(%rbp)
je     134d <str_formattime+0x1cd>
mov    -0x1030(%rbp),%eax
cmp    -0x102c(%rbp),%eax
jl     137d <str_formattime+0x1fd>
mov    -0x1028(%rbp),%rdi
call   1030 <free@plt>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
lea    0xc97(%rip),%rsi
mov    $0x100000,%edx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1382 <str_formattime+0x202>
jmp    1387 <str_formattime+0x207>
jmp    138c <str_formattime+0x20c>
cmpq   $0x0,-0x1028(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1040 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
je     13c6 <str_formattime+0x246>
mov    -0x8(%rbp),%rdi
mov    -0x1028(%rbp),%rsi
mov    -0x1030(%rbp),%edx
call   1060 <str_catn@plt>
jmp    13dc <str_formattime+0x25c>
mov    -0x8(%rbp),%rdi
mov    -0x1028(%rbp),%rsi
mov    -0x1030(%rbp),%edx
call   1080 <str_copyn@plt>
cmpq   $0x0,-0x1028(%rbp)
je     140d <str_formattime+0x28d>
mov    -0x1028(%rbp),%rax
lea    -0x1020(%rbp),%rcx
cmp    %rcx,%rax
je     140d <str_formattime+0x28d>
mov    -0x1028(%rbp),%rdi
call   1030 <free@plt>
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_formattime>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1008,%rsp
mov    %rcx,%r12
mov    %rdx,%r13
mov    %esi,%r15d
mov    %rdi,%r14
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   1090 <strftime@plt>
test   %eax,%eax
je     11be <str_formattime+0x3e>
mov    %eax,%ebp
cmp    $0x1000,%eax
jl     1283 <str_formattime+0x103>
mov    $0x2000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
mov    $0x2000,%esi
mov    %r13,%rdx
mov    %r12,%rcx
call   1090 <strftime@plt>
test   %eax,%eax
je     11fd <str_formattime+0x7d>
mov    %eax,%ebp
cmp    $0x2000,%eax
jl     1283 <str_formattime+0x103>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x10000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
mov    $0x10000,%esi
mov    %r13,%rdx
mov    %r12,%rcx
call   1090 <strftime@plt>
test   %eax,%eax
je     1240 <str_formattime+0xc0>
mov    %eax,%ebp
cmp    $0x10000,%eax
jl     1283 <str_formattime+0x103>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0x100000,%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
xor    %edi,%edi
test   %rax,%rax
setne  %dil
call   1040 <assert@plt>
mov    %rbx,%rdi
mov    $0x100000,%esi
mov    %r13,%rdx
mov    %r12,%rcx
call   1090 <strftime@plt>
test   %eax,%eax
je     12d6 <str_formattime+0x156>
mov    %eax,%ebp
cmp    $0x100000,%eax
jge    12d6 <str_formattime+0x156>
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1040 <assert@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
test   %r15d,%r15d
je     12aa <str_formattime+0x12a>
call   1060 <str_catn@plt>
test   %rbx,%rbx
jne    12b4 <str_formattime+0x134>
jmp    12c4 <str_formattime+0x144>
call   1080 <str_copyn@plt>
test   %rbx,%rbx
je     12c4 <str_formattime+0x144>
mov    %rsp,%rax
cmp    %rax,%rbx
je     12c4 <str_formattime+0x144>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x1008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %rbx,%rdi
call   1030 <free@plt>
mov    0x2cfb(%rip),%rax
mov    (%rax),%edi
lea    0xd12(%rip),%rsi
mov    $0x100000,%edx
call   1050 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.str = type { i32 }
%struct.tm = type { i32 }
@stderr = global i32 0
@.str = [50 x i8] c"error: str_vformattime(): too large input (> %i)\0A\00"
define void @str_formattime(%struct.str* %0, i32 %1, i8* %2, %struct.tm* %3) {
%5 = alloca %struct.str*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca %struct.tm*
%9 = alloca [4096 x i8]
%10 = alloca i8*
%11 = alloca i32
%12 = alloca i32
store %struct.str* %0, %struct.str** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
store %struct.tm* %3, %struct.tm** %8
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
store i8* %13, i8** %10
store i32 4096, i32* %11
%14 = load i8*, i8** %10
%15 = load i32, i32* %11
%16 = load i8*, i8** %7
%17 = load %struct.tm*, %struct.tm** %8
%18 = call i32 @strftime(i8* %14, i32 %15, i8* %16, %struct.tm* %17)
store i32 %18, i32* %12
%19 = load i32, i32* %12
%20 = icmp eq i32 %19, 0
br i1 %20, label %25, label %21
21: ; preds = %4
%22 = load i32, i32* %12
%23 = load i32, i32* %11
%24 = icmp sge i32 %22, %23
br i1 %24, label %25, label %89
25: ; preds = %21, %4
store i32 8192, i32* %11
%26 = load i32, i32* %11
%27 = call i8* @malloc(i32 %26)
store i8* %27, i8** %10
%28 = icmp ne i8* %27, null
%29 = zext i1 %28 to i32
%30 = call i32 @assert(i32 %29)
%31 = load i8*, i8** %10
%32 = load i32, i32* %11
%33 = load i8*, i8** %7
%34 = load %struct.tm*, %struct.tm** %8
%35 = call i32 @strftime(i8* %31, i32 %32, i8* %33, %struct.tm* %34)
store i32 %35, i32* %12
%36 = load i32, i32* %12
%37 = icmp eq i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %25
%39 = load i32, i32* %12
%40 = load i32, i32* %11
%41 = icmp sge i32 %39, %40
br i1 %41, label %42, label %88
42: ; preds = %38, %25
%43 = load i8*, i8** %10
%44 = call i32 @free(i8* %43)
store i32 65536, i32* %11
%45 = load i32, i32* %11
%46 = call i8* @malloc(i32 %45)
store i8* %46, i8** %10
%47 = icmp ne i8* %46, null
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = load i8*, i8** %10
%51 = load i32, i32* %11
%52 = load i8*, i8** %7
%53 = load %struct.tm*, %struct.tm** %8
%54 = call i32 @strftime(i8* %50, i32 %51, i8* %52, %struct.tm* %53)
store i32 %54, i32* %12
%55 = load i32, i32* %12
%56 = icmp eq i32 %55, 0
br i1 %56, label %61, label %57
57: ; preds = %42
%58 = load i32, i32* %12
%59 = load i32, i32* %11
%60 = icmp sge i32 %58, %59
br i1 %60, label %61, label %87
61: ; preds = %57, %42
%62 = load i8*, i8** %10
%63 = call i32 @free(i8* %62)
store i32 1048576, i32* %11
%64 = load i32, i32* %11
%65 = call i8* @malloc(i32 %64)
store i8* %65, i8** %10
%66 = icmp ne i8* %65, null
%67 = zext i1 %66 to i32
%68 = call i32 @assert(i32 %67)
%69 = load i8*, i8** %10
%70 = load i32, i32* %11
%71 = load i8*, i8** %7
%72 = load %struct.tm*, %struct.tm** %8
%73 = call i32 @strftime(i8* %69, i32 %70, i8* %71, %struct.tm* %72)
store i32 %73, i32* %12
%74 = load i32, i32* %12
%75 = icmp eq i32 %74, 0
br i1 %75, label %80, label %76
76: ; preds = %61
%77 = load i32, i32* %12
%78 = load i32, i32* %11
%79 = icmp sge i32 %77, %78
br i1 %79, label %80, label %86
80: ; preds = %76, %61
%81 = load i8*, i8** %10
%82 = call i32 @free(i8* %81)
%83 = load i32, i32* @stderr
%84 = call i32 @fprintf(i32 %83, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 1048576)
%85 = call i32 @exit(i32 1)
unreachable
86: ; preds = %76
br label %87
87: ; preds = %86, %57
br label %88
88: ; preds = %87, %38
br label %89
89: ; preds = %88, %21
%90 = load i8*, i8** %10
%91 = icmp ne i8* %90, null
%92 = zext i1 %91 to i32
%93 = call i32 @assert(i32 %92)
%94 = load i32, i32* %6
%95 = icmp ne i32 %94, 0
br i1 %95, label %96, label %101
96: ; preds = %89
%97 = load %struct.str*, %struct.str** %5
%98 = load i8*, i8** %10
%99 = load i32, i32* %12
%100 = call i32 @str_catn(%struct.str* %97, i8* %98, i32 %99)
br label %106
101: ; preds = %89
%102 = load %struct.str*, %struct.str** %5
%103 = load i8*, i8** %10
%104 = load i32, i32* %12
%105 = call i32 @str_copyn(%struct.str* %102, i8* %103, i32 %104)
br label %106
106: ; preds = %101, %96
%107 = load i8*, i8** %10
%108 = icmp ne i8* %107, null
br i1 %108, label %109, label %116
109: ; preds = %106
%110 = load i8*, i8** %10
%111 = getelementptr inbounds [4096 x i8], [4096 x i8]* %9, i64 0, i64 0
%112 = icmp ne i8* %110, %111
br i1 %112, label %113, label %116
113: ; preds = %109
%114 = load i8*, i8** %10
%115 = call i32 @free(i8* %114)
br label %116
116: ; preds = %113, %109, %106
ret void
}

LLVM IR (-O2):
%struct.str = type { i32 }
%struct.tm = type { i32 }
@stderr = global i32 0
@.str = [50 x i8] c"error: str_vformattime(): too large input (> %i)\0A\00"
define void @str_formattime(%struct.str* %0, i32 %1, i8* %2, %struct.tm* %3) {
%5 = alloca [4096 x i8]
%6 = getelementptr inbounds [4096 x i8], [4096 x i8]* %5, i64 0, i64 0
%7 = call i32 @strftime(i8* %6, i32 4096, i8* %2, %struct.tm* %3)
%8 = icmp eq i32 %7, 0
%9 = icmp sgt i32 %7, 4095
%10 = or i1 %8, %9
br i1 %10, label %11, label %45
11: ; preds = %4
%12 = call dereferenceable_or_null(8192) i8* @malloc(i32 8192)
%13 = icmp ne i8* %12, null
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = call i32 @strftime(i8* %12, i32 8192, i8* %2, %struct.tm* %3)
%17 = icmp eq i32 %16, 0
%18 = icmp sgt i32 %16, 8191
%19 = or i1 %17, %18
br i1 %19, label %20, label %45
20: ; preds = %11
%21 = call i32 @free(i8* %12)
%22 = call dereferenceable_or_null(65536) i8* @malloc(i32 65536)
%23 = icmp ne i8* %22, null
%24 = zext i1 %23 to i32
%25 = call i32 @assert(i32 %24)
%26 = call i32 @strftime(i8* %22, i32 65536, i8* %2, %struct.tm* %3)
%27 = icmp eq i32 %26, 0
%28 = icmp sgt i32 %26, 65535
%29 = or i1 %27, %28
br i1 %29, label %30, label %45
30: ; preds = %20
%31 = call i32 @free(i8* %22)
%32 = call dereferenceable_or_null(1048576) i8* @malloc(i32 1048576)
%33 = icmp ne i8* %32, null
%34 = zext i1 %33 to i32
%35 = call i32 @assert(i32 %34)
%36 = call i32 @strftime(i8* %32, i32 1048576, i8* %2, %struct.tm* %3)
%37 = icmp eq i32 %36, 0
%38 = icmp sgt i32 %36, 1048575
%39 = or i1 %37, %38
br i1 %39, label %40, label %45
40: ; preds = %30
%41 = call i32 @free(i8* %32)
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0), i32 1048576)
%44 = call i32 @exit(i32 1)
unreachable
45: ; preds = %30, %20, %11, %4
%46 = phi i8* [ %6, %4 ], [ %12, %11 ], [ %22, %20 ], [ %32, %30 ]
%47 = phi i32 [ %7, %4 ], [ %16, %11 ], [ %26, %20 ], [ %36, %30 ]
%48 = icmp ne i8* %46, null
%49 = zext i1 %48 to i32
%50 = call i32 @assert(i32 %49)
%51 = icmp eq i32 %1, 0
br i1 %51, label %54, label %52
52: ; preds = %45
%53 = call i32 @str_catn(%struct.str* %0, i8* %46, i32 %47)
br label %56
54: ; preds = %45
%55 = call i32 @str_copyn(%struct.str* %0, i8* %46, i32 %47)
br label %56
56: ; preds = %54, %52
%57 = icmp ne i8* %46, %6
%58 = and i1 %48, %57
br i1 %58, label %59, label %61
59: ; preds = %56
%60 = call i32 @free(i8* %46)
br label %61
61: ; preds = %59, %56
ret void
}