#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*/ ts;
typedef scalar_t__ time_t;
struct timespec {
  scalar_t__ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ CLOCK_REALTIME;
int clock_gettime(int /*<<< orphan*/, struct timespec *);
scalar_t__ ctime_r(scalar_t__ *, char *);
int errno;
int /*<<< orphan*/ memset(struct timespec *, int /*<<< orphan*/, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
const char *dbg_strdate(char *str) {
  struct timespec ts;
  time_t t;
  int ret;
  memset(&ts, 0, sizeof(ts));
  ret = clock_gettime(CLOCK_REALTIME, &ts);
  if (ret != 0)
    sprintf(str, "(clock_gettime error=%d)", errno);
  else {
    t = (time_t)ts.tv_sec;
    if (t == 0) {
      strcpy(str, "(undefined)");
    } else {
      char buf[1024];
      sprintf(str, "%.19s", (ctime_r(&t, buf) ? buf : "(ctime_r error)"));
    }
  }
  return str;
}


x86 assembly (clang, -O0):
<dbg_strdate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
lea    -0x10(%rbp),%rsi
call   1040 <clock_gettime@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     11ae <dbg_strdate+0x5e>
mov    -0x8(%rbp),%rdi
mov    0x2e47(%rip),%rax
mov    (%rax),%edx
lea    0xe5e(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    1242 <dbg_strdate+0xf2>
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11d6 <dbg_strdate+0x86>
mov    -0x8(%rbp),%rdi
lea    0xe4d(%rip),%rsi
call   1030 <strcpy@plt>
jmp    123d <dbg_strdate+0xed>
mov    -0x8(%rbp),%rax
mov    %rax,-0x428(%rbp)
lea    -0x420(%rbp),%rsi
lea    -0x18(%rbp),%rdi
call   1060 <ctime_r@plt>
cmp    $0x0,%rax
je     120e <dbg_strdate+0xbe>
lea    -0x420(%rbp),%rax
mov    %rax,-0x430(%rbp)
jmp    1221 <dbg_strdate+0xd1>
lea    0xe16(%rip),%rax
mov    %rax,-0x430(%rbp)
jmp    1221 <dbg_strdate+0xd1>
mov    -0x428(%rbp),%rdi
mov    -0x430(%rbp),%rdx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1070 <sprintf@plt>
jmp    1242 <dbg_strdate+0xf2>
mov    -0x8(%rbp),%rax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<dbg_strdate>:
push   %r14
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%r14
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1050 <memset@plt>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
call   1040 <clock_gettime@plt>
test   %eax,%eax
je     11a2 <dbg_strdate+0x52>
mov    0x2e53(%rip),%rax
mov    (%rax),%edx
lea    0xe6a(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <sprintf@plt>
jmp    11f0 <dbg_strdate+0xa0>
mov    0x8(%rsp),%rax
mov    %rax,(%rsp)
test   %rax,%rax
je     11e1 <dbg_strdate+0x91>
mov    %rsp,%rdi
lea    0x10(%rsp),%rbx
mov    %rbx,%rsi
call   1060 <ctime_r@plt>
test   %rax,%rax
lea    0xe61(%rip),%rdx
cmovne %rbx,%rdx
lea    0xe50(%rip),%rsi
mov    %r14,%rdi
xor    %eax,%eax
call   1070 <sprintf@plt>
jmp    11f0 <dbg_strdate+0xa0>
lea    0xe31(%rip),%rsi
mov    %r14,%rdi
call   1030 <strcpy@plt>
mov    %r14,%rax
add    $0x418,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timespec = type { i64 }
@CLOCK_REALTIME = global i32 0
@.str = [25 x i8] c"(clock_gettime error=%d)\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"(undefined)\00"
@.str.2 = [6 x i8] c"%.19s\00"
@.str.3 = [16 x i8] c"(ctime_r error)\00"
define i8* @dbg_strdate(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.timespec
%4 = alloca i64
%5 = alloca i32
%6 = alloca [1024 x i8]
store i8* %0, i8** %2
%7 = call i32 @memset(%struct.timespec* %3, i32 0, i32 8)
%8 = load i32, i32* @CLOCK_REALTIME
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %3)
store i32 %9, i32* %5
%10 = load i32, i32* %5
%11 = icmp ne i32 %10, 0
br i1 %11, label %12, label %16
12: ; preds = %1
%13 = load i8*, i8** %2
%14 = load i32, i32* @errno
%15 = call i32 (i8*, i8*, ...) @sprintf(i8* %13, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %14)
br label %36
16: ; preds = %1
%17 = getelementptr inbounds %struct.timespec, %struct.timespec* %3, i32 0, i32 0
%18 = load i64, i64* %17
store i64 %18, i64* %4
%19 = load i64, i64* %4
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %16
%22 = load i8*, i8** %2
%23 = call i32 @strcpy(i8* %22, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %35
24: ; preds = %16
%25 = load i8*, i8** %2
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%27 = call i64 @ctime_r(i64* %4, i8* %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
br label %32
31: ; preds = %24
br label %32
32: ; preds = %31, %29
%33 = phi i8* [ %30, %29 ], [ getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), %31 ]
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %33)
br label %35
35: ; preds = %32, %21
br label %36
36: ; preds = %35, %12
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
%struct.timespec = type { i64 }
@CLOCK_REALTIME = global i32 0
@.str = [25 x i8] c"(clock_gettime error=%d)\00"
@errno = global i32 0
@.str.1 = [12 x i8] c"(undefined)\00"
@.str.2 = [6 x i8] c"%.19s\00"
@.str.3 = [16 x i8] c"(ctime_r error)\00"
define i8* @dbg_strdate(i8* returned %0) {
%2 = alloca %struct.timespec
%3 = alloca i64
%4 = alloca [1024 x i8]
%5 = bitcast %struct.timespec* %2 to i8*
%6 = bitcast i64* %3 to i8*
%7 = call i32 @memset(%struct.timespec* %2, i32 0, i32 8)
%8 = load i32, i32* @CLOCK_REALTIME
%9 = call i32 @clock_gettime(i32 %8, %struct.timespec* %2)
%10 = icmp eq i32 %9, 0
br i1 %10, label %14, label %11
11: ; preds = %1
%12 = load i32, i32* @errno
%13 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i32 %12)
br label %26
14: ; preds = %1
%15 = getelementptr inbounds %struct.timespec, %struct.timespec* %2, i64 0, i32 0
%16 = load i64, i64* %15
store i64 %16, i64* %3
%17 = icmp eq i64 %16, 0
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0))
br label %26
20: ; preds = %14
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%22 = call i64 @ctime_r(i64* %3, i8* %21)
%23 = icmp eq i64 %22, 0
%24 = select i1 %23, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0), i8* %21
%25 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i8* %24)
br label %26
26: ; preds = %18, %20, %11
ret i8* %0
}