#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 */
struct tm *localtime(int /*<<< orphan*/ *);
scalar_t__ strftime(char *, size_t, char const *, struct tm *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int al_get_datetime(char *buf, size_t len, const char *fmt) {
  time_t t = time(NULL);
  struct tm *timeinfo = localtime(&t);
  if (timeinfo == NULL) {
    return -1;
  }
  if (strftime(buf, len, fmt, timeinfo) == 0) {
    return -1;
  }
  buf[len - 1] = '\0';
  return 0;
}


x86 assembly (clang, -O0):
<al_get_datetime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %eax,-0x24(%rbp)
lea    -0x24(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1174 <al_get_datetime+0x44>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <al_get_datetime+0x86>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
call   1050 <strftime@plt>
cmp    $0x0,%rax
jne    119f <al_get_datetime+0x6f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11b6 <al_get_datetime+0x86>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<al_get_datetime>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rdi,%rbx
xor    %edi,%edi
call   1040 <time@plt>
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rdi
call   1030 <localtime@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     117d <al_get_datetime+0x4d>
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %r15,%rdx
mov    %rax,%rcx
call   1050 <strftime@plt>
test   %rax,%rax
je     117d <al_get_datetime+0x4d>
movb   $0x0,-0x1(%r14,%rbx,1)
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
define i32 @al_get_datetime(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.tm*
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
%10 = call i32 @time(i32* null)
store i32 %10, i32* %8
%11 = call %struct.tm* @localtime(i32* %8)
store %struct.tm* %11, %struct.tm** %9
%12 = load %struct.tm*, %struct.tm** %9
%13 = icmp eq %struct.tm* %12, null
br i1 %13, label %14, label %15
14: ; preds = %3
store i32 -1, i32* %4
br label %28
15: ; preds = %3
%16 = load i8*, i8** %5
%17 = load i64, i64* %6
%18 = load i8*, i8** %7
%19 = load %struct.tm*, %struct.tm** %9
%20 = call i64 @strftime(i8* %16, i64 %17, i8* %18, %struct.tm* %19)
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %15
store i32 -1, i32* %4
br label %28
23: ; preds = %15
%24 = load i8*, i8** %5
%25 = load i64, i64* %6
%26 = sub i64 %25, 1
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
store i32 0, i32* %4
br label %28
28: ; preds = %23, %22, %14
%29 = load i32, i32* %4
ret i32 %29
}

LLVM IR (-O2):
%struct.tm = type { i32 }
define i32 @al_get_datetime(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = bitcast i32* %4 to i8*
%6 = tail call i32 @time(i32* null)
store i32 %6, i32* %4
%7 = call %struct.tm* @localtime(i32* %4)
%8 = icmp eq %struct.tm* %7, null
br i1 %8, label %15, label %9
9: ; preds = %3
%10 = call i64 @strftime(i8* %0, i64 %1, i8* %2, %struct.tm* %7)
%11 = icmp eq i64 %10, 0
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = add i64 %1, -1
%14 = getelementptr inbounds i8, i8* %0, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %9, %3, %12
%16 = phi i32 [ 0, %12 ], [ -1, %3 ], [ -1, %9 ]
ret i32 %16
}