#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 SSIZE_MAX;
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 *logging_get_timestamp(const char *format) {
  struct tm *tm;
  time_t t;
  time(&t);
  tm = localtime(&t);
  char *buffer = NULL;
  size_t buffsize = strftime(NULL, SSIZE_MAX, format, tm);
  if (buffsize > 0) {
    buffer = malloc(++buffsize);
    if (!buffer)
      return NULL;
    strftime(buffer, buffsize + 1, format, tm);
  }
  return buffer;
}


x86 assembly (clang, -O0):
<logging_get_timestamp>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x1c(%rbp),%rdi
call   1040 <time@plt>
lea    -0x1c(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    0x2e77(%rip),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%edi
call   1060 <strftime@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jbe    11da <logging_get_timestamp+0x9a>
mov    -0x30(%rbp),%rdi
add    $0x1,%rdi
mov    %rdi,-0x30(%rbp)
call   1050 <malloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11c1 <logging_get_timestamp+0x81>
movq   $0x0,-0x8(%rbp)
jmp    11e2 <logging_get_timestamp+0xa2>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
add    $0x1,%rsi
mov    -0x10(%rbp),%rdx
mov    -0x18(%rbp),%rcx
call   1060 <strftime@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logging_get_timestamp>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0x4(%rsp),%rbx
mov    %rbx,%rdi
call   1040 <time@plt>
mov    %rbx,%rdi
call   1030 <localtime@plt>
mov    %rax,%r15
mov    0x2e7e(%rip),%rax
mov    (%rax),%rsi
xor    %r12d,%r12d
xor    %edi,%edi
mov    %r14,%rdx
mov    %r15,%rcx
call   1060 <strftime@plt>
test   %rax,%rax
je     11b0 <logging_get_timestamp+0x70>
mov    %rax,%rbx
lea    0x1(%rax),%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     11ad <logging_get_timestamp+0x6d>
mov    %rax,%r12
add    $0x2,%rbx
mov    %rax,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
mov    %r15,%rcx
call   1060 <strftime@plt>
jmp    11b0 <logging_get_timestamp+0x70>
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 }
@SSIZE_MAX = global i64 0
define i8* @logging_get_timestamp(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca %struct.tm*
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i64
store i8* %0, i8** %3
%8 = call i32 @time(i32* %5)
%9 = call %struct.tm* @localtime(i32* %5)
store %struct.tm* %9, %struct.tm** %4
store i8* null, i8** %6
%10 = load i64, i64* @SSIZE_MAX
%11 = load i8*, i8** %3
%12 = load %struct.tm*, %struct.tm** %4
%13 = call i64 @strftime(i8* null, i64 %10, i8* %11, %struct.tm* %12)
store i64 %13, i64* %7
%14 = load i64, i64* %7
%15 = icmp ugt i64 %14, 0
br i1 %15, label %16, label %30
16: ; preds = %1
%17 = load i64, i64* %7
%18 = add i64 %17, 1
store i64 %18, i64* %7
%19 = call i8* @malloc(i64 %18)
store i8* %19, i8** %6
%20 = load i8*, i8** %6
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %16
store i8* null, i8** %2
br label %32
23: ; preds = %16
%24 = load i8*, i8** %6
%25 = load i64, i64* %7
%26 = add i64 %25, 1
%27 = load i8*, i8** %3
%28 = load %struct.tm*, %struct.tm** %4
%29 = call i64 @strftime(i8* %24, i64 %26, i8* %27, %struct.tm* %28)
br label %30
30: ; preds = %23, %1
%31 = load i8*, i8** %6
store i8* %31, i8** %2
br label %32
32: ; preds = %30, %22
%33 = load i8*, i8** %2
ret i8* %33
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@SSIZE_MAX = global i64 0
define i8* @logging_get_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* @SSIZE_MAX
%7 = call i64 @strftime(i8* null, i64 %6, i8* %0, %struct.tm* %5)
%8 = icmp eq i64 %7, 0
br i1 %8, label %16, 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 %16, label %13
13: ; preds = %9
%14 = add i64 %7, 2
%15 = call i64 @strftime(i8* %11, i64 %14, i8* %0, %struct.tm* %5)
br label %16
16: ; preds = %1, %13, %9
%17 = phi i8* [ null, %9 ], [ %11, %13 ], [ null, %1 ]
ret i8* %17
}