#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 timespec {
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ free(char *);
struct tm *localtime(int /*<<< orphan*/ *);
int /*<<< orphan*/ puts(char *);
char *realloc(char *, int);
int strftime(char *, int, char *, struct tm *);
int strlen(char *);
void printtime(char *format, struct timespec when) {
  char *str = NULL;
  struct tm *tm;
  int alloc = strlen(format), len = 0;
  tm = localtime(&when.tv_sec);
  do {
    alloc++;
    str = realloc(str, alloc);
    len = strftime(str, alloc, format, tm);
    if (!*format)
      break;
  } while (!len);
  puts(str);
  free(str);
}


x86 assembly (clang, -O0):
<printtime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %esi,-0x8(%rbp)
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x8(%rbp),%rdi
call   1040 <localtime@plt>
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
call   1070 <realloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    -0x10(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1080 <strftime@plt>
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11d9 <printtime+0x79>
jmp    11ef <printtime+0x8f>
jmp    11de <printtime+0x7e>
cmpl   $0x0,-0x28(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1197 <printtime+0x37>
mov    -0x18(%rbp),%rdi
call   1050 <puts@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<printtime>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r15
mov    %esi,(%rsp)
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1040 <localtime@plt>
mov    %rax,%r14
add    $0x1,%ebp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %ebp,%esi
call   1070 <realloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %ebp,%esi
mov    %r15,%rdx
mov    %r14,%rcx
call   1080 <strftime@plt>
cmpb   $0x0,(%r15)
je     11ba <printtime+0x5a>
add    $0x1,%ebp
test   %eax,%eax
je     1190 <printtime+0x30>
mov    %rbx,%rdi
call   1050 <puts@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timespec = type { i32 }
%struct.tm = type { i32 }
define void @printtime(i8* %0, i32 %1) {
%3 = alloca %struct.timespec
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca %struct.tm*
%7 = alloca i32
%8 = alloca i32
%9 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
store i32 %1, i32* %9
store i8* %0, i8** %4
store i8* null, i8** %5
%10 = load i8*, i8** %4
%11 = call i32 @strlen(i8* %10)
store i32 %11, i32* %7
store i32 0, i32* %8
%12 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%13 = call %struct.tm* @localtime(i32* %12)
store %struct.tm* %13, %struct.tm** %6
br label %14
14: ; preds = %30, %2
%15 = load i32, i32* %7
%16 = add nsw i32 %15, 1
store i32 %16, i32* %7
%17 = load i8*, i8** %5
%18 = load i32, i32* %7
%19 = call i8* @realloc(i8* %17, i32 %18)
store i8* %19, i8** %5
%20 = load i8*, i8** %5
%21 = load i32, i32* %7
%22 = load i8*, i8** %4
%23 = load %struct.tm*, %struct.tm** %6
%24 = call i32 @strftime(i8* %20, i32 %21, i8* %22, %struct.tm* %23)
store i32 %24, i32* %8
%25 = load i8*, i8** %4
%26 = load i8, i8* %25
%27 = icmp ne i8 %26, 0
br i1 %27, label %29, label %28
28: ; preds = %14
br label %34
29: ; preds = %14
br label %30
30: ; preds = %29
%31 = load i32, i32* %8
%32 = icmp ne i32 %31, 0
%33 = xor i1 %32, true
br i1 %33, label %14, label %34
34: ; preds = %30, %28
%35 = load i8*, i8** %5
%36 = call i32 @puts(i8* %35)
%37 = load i8*, i8** %5
%38 = call i32 @free(i8* %37)
ret void
}

LLVM IR (-O2):
%struct.timespec = type { i32 }
%struct.tm = type { i32 }
define void @printtime(i8* %0, i32 %1) {
%3 = alloca %struct.timespec
%4 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i64 0, i32 0
store i32 %1, i32* %4
%5 = tail call i32 @strlen(i8* %0)
%6 = call %struct.tm* @localtime(i32* %4)
br label %7
7: ; preds = %7, %2
%8 = phi i8* [ null, %2 ], [ %11, %7 ]
%9 = phi i32 [ %5, %2 ], [ %10, %7 ]
%10 = add nsw i32 %9, 1
%11 = call i8* @realloc(i8* %8, i32 %10)
%12 = call i32 @strftime(i8* %11, i32 %10, i8* %0, %struct.tm* %6)
%13 = load i8, i8* %0
%14 = icmp ne i8 %13, 0
%15 = icmp eq i32 %12, 0
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %7, label %17
17: ; preds = %7
%18 = call i32 @puts(i8* dereferenceable(1) %11)
%19 = call i32 @free(i8* %11)
ret void
}