#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*/ time_t;
struct tm {
  int dummy;
};

/* Variables and functions */
size_t SIZE_MAX;
int /*<<< orphan*/ free(char *);
struct tm *localtime(int /*<<< orphan*/ *);
char *malloc(size_t);
size_t strftime(char *, size_t, char const *, struct tm *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
char *str_timestamp(const char *format) {
  char *buffer = NULL;
  struct tm *tm;
  time_t t;
  time(&t);
  tm = localtime(&t);
  size_t buffsize = strftime(NULL, SIZE_MAX, format, tm);
  if (buffsize > 0) {
    buffer = malloc(++buffsize);
    if (buffer == NULL)
      return NULL;
    size_t len = strftime(buffer, buffsize, format, tm);
    if (len == 0 && buffer[0] != '\0')
      goto on_error_strftime;
  }
  return buffer;
on_error_strftime:
  free(buffer);
  return NULL;
}


x86 assembly (clang, -O0):
<str_timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x24(%rbp),%rdi
call   1050 <time@plt>
lea    -0x24(%rbp),%rdi
call   1040 <localtime@plt>
mov    %rax,-0x20(%rbp)
mov    0x2e57(%rip),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
call   1070 <strftime@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    120f <str_timestamp+0xbf>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    %rdi,-0x30(%rbp)
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d1 <str_timestamp+0x81>
movq   $0x0,-0x8(%rbp)
jmp    122d <str_timestamp+0xdd>
mov    -0x18(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1070 <strftime@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    120a <str_timestamp+0xba>
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     120a <str_timestamp+0xba>
jmp    121c <str_timestamp+0xcc>
jmp    120f <str_timestamp+0xbf>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    122d <str_timestamp+0xdd>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_timestamp>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <time@plt>
mov    %rbx,%rdi
call   1040 <localtime@plt>
mov    %rax,%r15
mov    0x2e5e(%rip),%rax
mov    (%rax),%rsi
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r14,%rdx
mov    %r15,%rcx
call   1070 <strftime@plt>
test   %rax,%rax
je     11d1 <str_timestamp+0x81>
mov    %rax,%rbx
add    $0x1,%rbx
mov    %rbx,%rdi
call   1060 <malloc@plt>
test   %rax,%rax
je     11ce <str_timestamp+0x7e>
mov    %rax,%r12
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1070 <strftime@plt>
test   %rax,%rax
jne    11d1 <str_timestamp+0x81>
cmpb   $0x0,(%r12)
je     11d1 <str_timestamp+0x81>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r12d,%r12d
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@SIZE_MAX = global i64 0
define i8* @str_timestamp(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.tm*
%6 = alloca i32
%7 = alloca i64
%8 = alloca i64
store i8* %0, i8** %3
store i8* null, i8** %4
%9 = call i32 @time(i32* %6)
%10 = call %struct.tm* @localtime(i32* %6)
store %struct.tm* %10, %struct.tm** %5
%11 = load i64, i64* @SIZE_MAX
%12 = load i8*, i8** %3
%13 = load %struct.tm*, %struct.tm** %5
%14 = call i64 @strftime(i8* null, i64 %11, i8* %12, %struct.tm* %13)
store i64 %14, i64* %7
%15 = load i64, i64* %7
%16 = icmp ugt i64 %15, 0
br i1 %16, label %17, label %40
17: ; preds = %1
%18 = load i64, i64* %7
%19 = add i64 %18, 1
store i64 %19, i64* %7
%20 = call i8* @malloc(i64 %19)
store i8* %20, i8** %4
%21 = load i8*, i8** %4
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %24
23: ; preds = %17
store i8* null, i8** %2
br label %45
24: ; preds = %17
%25 = load i8*, i8** %4
%26 = load i64, i64* %7
%27 = load i8*, i8** %3
%28 = load %struct.tm*, %struct.tm** %5
%29 = call i64 @strftime(i8* %25, i64 %26, i8* %27, %struct.tm* %28)
store i64 %29, i64* %8
%30 = load i64, i64* %8
%31 = icmp eq i64 %30, 0
br i1 %31, label %32, label %39
32: ; preds = %24
%33 = load i8*, i8** %4
%34 = getelementptr inbounds i8, i8* %33, i64 0
%35 = load i8, i8* %34
%36 = sext i8 %35 to i32
%37 = icmp ne i32 %36, 0
br i1 %37, label %38, label %39
38: ; preds = %32
br label %42
39: ; preds = %32, %24
br label %40
40: ; preds = %39, %1
%41 = load i8*, i8** %4
store i8* %41, i8** %2
br label %45
42: ; preds = %38
%43 = load i8*, i8** %4
%44 = call i32 @free(i8* %43)
store i8* null, i8** %2
br label %45
45: ; preds = %42, %40, %23
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@SIZE_MAX = global i64 0
define i8* @str_timestamp(i8* %0) {
%2 = alloca i32
%3 = bitcast i32* %2 to i8*
%4 = call i32 @time(i32* %2)
%5 = call %struct.tm* @localtime(i32* %2)
%6 = load i64, i64* @SIZE_MAX
%7 = call i64 @strftime(i8* null, i64 %6, i8* %0, %struct.tm* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %21, label %9
9: ; preds = %1
%10 = add i64 %7, 1
%11 = call i8* @malloc(i64 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %21, label %13
13: ; preds = %9
%14 = call i64 @strftime(i8* %11, i64 %10, i8* %0, %struct.tm* %5)
%15 = icmp eq i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %13
%17 = load i8, i8* %11
%18 = icmp eq i8 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %16
%20 = call i32 @free(i8* %11)
br label %21
21: ; preds = %16, %13, %1, %9, %19
%22 = phi i8* [ null, %19 ], [ null, %9 ], [ null, %1 ], [ %11, %13 ], [ %11, %16 ]
ret i8* %22
}