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

/* Variables and functions */
int EINVAL;
int errno;
struct tm *localtime(scalar_t__ *);
size_t strftime(char *, size_t, char *, struct tm *);
scalar_t__ time(int /*<<< orphan*/ *);
int timetag(char *buf, size_t size) {
  time_t t;
  struct tm *ltime;
  size_t len;
  if (!size)
    return -(errno = EINVAL);
  *buf = 0;
  t = time(NULL);
  if (t == ((time_t)-1))
    return -errno;
  ltime = localtime(&t);
  if (ltime == NULL)
    return -(errno = EINVAL);
  len = strftime(buf, size, "%F %T %z", ltime);
  if (!len)
    *buf = 0;
  return len;
}


x86 assembly (clang, -O0):
<timetag>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1169 <timetag+0x39>
mov    0x2e86(%rip),%rax
mov    (%rax),%ecx
mov    0x2e85(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <timetag+0xd6>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
xor    %eax,%eax
mov    %eax,%edi
call   1040 <time@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    119b <timetag+0x6b>
xor    %eax,%eax
mov    0x2e4f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <timetag+0xd6>
lea    -0x20(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11d1 <timetag+0xa1>
mov    0x2e1e(%rip),%rax
mov    (%rax),%ecx
mov    0x2e1d(%rip),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
sub    %ecx,%eax
mov    %eax,-0x4(%rbp)
jmp    1206 <timetag+0xd6>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rcx
lea    0xe1c(%rip),%rdx
call   1050 <strftime@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ff <timetag+0xcf>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x30(%rbp),%rax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<timetag>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
test   %rsi,%rsi
je     118d <timetag+0x5d>
mov    %rsi,%r15
mov    %rdi,%r14
movb   $0x0,(%rdi)
xor    %ebx,%ebx
xor    %edi,%edi
call   1040 <time@plt>
mov    %rax,0x8(%rsp)
cmp    $0xffffffffffffffff,%rax
je     11a3 <timetag+0x73>
lea    0x8(%rsp),%rdi
call   1030 <localtime@plt>
test   %rax,%rax
je     118d <timetag+0x5d>
lea    0xe8f(%rip),%rdx
mov    %r14,%rdi
mov    %r15,%rsi
mov    %rax,%rcx
call   1050 <strftime@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    11ac <timetag+0x7c>
movb   $0x0,(%r14)
jmp    11ac <timetag+0x7c>
mov    0x2e44(%rip),%rax
mov    (%rax),%ebx
mov    0x2e43(%rip),%rax
mov    %ebx,(%rax)
neg    %ebx
jmp    11ac <timetag+0x7c>
mov    0x2e36(%rip),%rax
sub    (%rax),%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"%F %T %z\00"
define i32 @timetag(i8* %0, i64 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i64
%7 = alloca %struct.tm*
%8 = alloca i64
store i8* %0, i8** %4
store i64 %1, i64* %5
%9 = load i64, i64* %5
%10 = icmp ne i64 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %2
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
%13 = sub nsw i32 0, %12
store i32 %13, i32* %3
br label %41
14: ; preds = %2
%15 = load i8*, i8** %4
store i8 0, i8* %15
%16 = call i64 @time(i32* null)
store i64 %16, i64* %6
%17 = load i64, i64* %6
%18 = icmp eq i64 %17, -1
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @errno
%21 = sub nsw i32 0, %20
store i32 %21, i32* %3
br label %41
22: ; preds = %14
%23 = call %struct.tm* @localtime(i64* %6)
store %struct.tm* %23, %struct.tm** %7
%24 = load %struct.tm*, %struct.tm** %7
%25 = icmp eq %struct.tm* %24, null
br i1 %25, label %26, label %29
26: ; preds = %22
%27 = load i32, i32* @EINVAL
store i32 %27, i32* @errno
%28 = sub nsw i32 0, %27
store i32 %28, i32* %3
br label %41
29: ; preds = %22
%30 = load i8*, i8** %4
%31 = load i64, i64* %5
%32 = load %struct.tm*, %struct.tm** %7
%33 = call i64 @strftime(i8* %30, i64 %31, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.tm* %32)
store i64 %33, i64* %8
%34 = load i64, i64* %8
%35 = icmp ne i64 %34, 0
br i1 %35, label %38, label %36
36: ; preds = %29
%37 = load i8*, i8** %4
store i8 0, i8* %37
br label %38
38: ; preds = %36, %29
%39 = load i64, i64* %8
%40 = trunc i64 %39 to i32
store i32 %40, i32* %3
br label %41
41: ; preds = %38, %26, %19, %11
%42 = load i32, i32* %3
ret i32 %42
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@EINVAL = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"%F %T %z\00"
define i32 @timetag(i8* %0, i64 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
%5 = icmp eq i64 %1, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
%8 = sub nsw i32 0, %7
br label %27
9: ; preds = %2
store i8 0, i8* %0
%10 = tail call i64 @time(i32* null)
store i64 %10, i64* %3
%11 = icmp eq i64 %10, -1
br i1 %11, label %12, label %15
12: ; preds = %9
%13 = load i32, i32* @errno
%14 = sub nsw i32 0, %13
br label %27
15: ; preds = %9
%16 = call %struct.tm* @localtime(i64* %3)
%17 = icmp eq %struct.tm* %16, null
br i1 %17, label %18, label %21
18: ; preds = %15
%19 = load i32, i32* @EINVAL
store i32 %19, i32* @errno
%20 = sub nsw i32 0, %19
br label %27
21: ; preds = %15
%22 = call i64 @strftime(i8* %0, i64 %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.tm* %16)
%23 = icmp eq i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %21
store i8 0, i8* %0
br label %25
25: ; preds = %24, %21
%26 = trunc i64 %22 to i32
br label %27
27: ; preds = %25, %18, %12, %6
%28 = phi i32 [ %14, %12 ], [ %20, %18 ], [ %26, %25 ], [ %8, %6 ]
ret i32 %28
}