#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 time_t;
struct tm {
  int dummy;
};
typedef int /*<<< orphan*/ date;

/* Variables and functions */
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
struct tm *localtime(int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *strdup(char *);
scalar_t__ strftime(char *, int, char *, struct tm *);
int time(int /*<<< orphan*/ *);
char *fetchdate(void) {
  char date[255];
  time_t t;
  struct tm *tmp;
  if ((t = time(NULL)) == -1) {
    perror("time");
    return NULL;
  }
  if ((tmp = localtime(&t)) == NULL) {
    perror("localtime");
    return NULL;
  }
  if (strftime(date, sizeof(date), "%c", tmp) == 0) {
    (void)fputs("Could not retrieve date\n", stderr);
    return NULL;
  }
  return strdup(date);
}


x86 assembly (clang, -O0):
<fetchdate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
xor    %eax,%eax
mov    %eax,%edi
call   1050 <time@plt>
mov    %eax,-0x114(%rbp)
cmp    $0xffffffff,%eax
jne    119c <fetchdate+0x3c>
lea    0xe7b(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    122d <fetchdate+0xcd>
lea    -0x114(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x120(%rbp)
cmp    $0x0,%rax
jne    11d2 <fetchdate+0x72>
lea    0xe40(%rip),%rdi
call   1070 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    122d <fetchdate+0xcd>
lea    -0x110(%rbp),%rdi
mov    -0x120(%rbp),%rcx
mov    $0xff,%esi
lea    0xe1e(%rip),%rdx
call   1060 <strftime@plt>
cmp    $0x0,%rax
jne    121d <fetchdate+0xbd>
mov    0x2dde(%rip),%rax
mov    (%rax),%esi
lea    0xe02(%rip),%rdi
call   1040 <fputs@plt>
movq   $0x0,-0x8(%rbp)
jmp    122d <fetchdate+0xcd>
lea    -0x110(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fetchdate>:
sub    $0x118,%rsp
xor    %edi,%edi
call   1050 <time@plt>
mov    %eax,0xc(%rsp)
cmp    $0xffffffff,%eax
je     11cd <fetchdate+0x6d>
lea    0xc(%rsp),%rdi
call   1030 <localtime@plt>
test   %rax,%rax
je     11d6 <fetchdate+0x76>
lea    0xe7d(%rip),%rdx
lea    0x10(%rsp),%rdi
mov    $0xff,%esi
mov    %rax,%rcx
call   1060 <strftime@plt>
test   %rax,%rax
je     11b6 <fetchdate+0x56>
lea    0x10(%rsp),%rdi
call   1080 <strdup@plt>
add    $0x118,%rsp
ret
mov    0x2e23(%rip),%rax
mov    (%rax),%esi
lea    0xe47(%rip),%rdi
call   1040 <fputs@plt>
jmp    11e2 <fetchdate+0x82>
lea    0xe31(%rip),%rdi
jmp    11dd <fetchdate+0x7d>
lea    0xe23(%rip),%rdi
call   1070 <perror@plt>
xor    %eax,%eax
add    $0x118,%rsp
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
@.str = [5 x i8] c"time\00"
@.str.1 = [10 x i8] c"localtime\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [25 x i8] c"Could not retrieve date\0A\00"
@stderr = global i32 0
define i8* @fetchdate() {
%1 = alloca i8*
%2 = alloca [255 x i8]
%3 = alloca i32
%4 = alloca %struct.tm*
%5 = call i32 @time(i32* null)
store i32 %5, i32* %3
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* null, i8** %1
br label %25
9: ; preds = %0
%10 = call %struct.tm* @localtime(i32* %3)
store %struct.tm* %10, %struct.tm** %4
%11 = icmp eq %struct.tm* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %1
br label %25
14: ; preds = %9
%15 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%16 = load %struct.tm*, %struct.tm** %4
%17 = call i64 @strftime(i8* %15, i32 255, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %16)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %14
%20 = load i32, i32* @stderr
%21 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %20)
store i8* null, i8** %1
br label %25
22: ; preds = %14
%23 = getelementptr inbounds [255 x i8], [255 x i8]* %2, i64 0, i64 0
%24 = call i8* @strdup(i8* %23)
store i8* %24, i8** %1
br label %25
25: ; preds = %22, %19, %12, %7
%26 = load i8*, i8** %1
ret i8* %26
}

LLVM IR (-O2):
%struct.tm = type { i32 }
@.str = [5 x i8] c"time\00"
@.str.1 = [10 x i8] c"localtime\00"
@.str.2 = [3 x i8] c"%c\00"
@.str.3 = [25 x i8] c"Could not retrieve date\0A\00"
@stderr = global i32 0
define noalias i8* @fetchdate() {
%1 = alloca [255 x i8]
%2 = alloca i32
%3 = getelementptr inbounds [255 x i8], [255 x i8]* %1, i64 0, i64 0
%4 = bitcast i32* %2 to i8*
%5 = tail call i32 @time(i32* null)
store i32 %5, i32* %2
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %22
9: ; preds = %0
%10 = call %struct.tm* @localtime(i32* %2)
%11 = icmp eq %struct.tm* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %22
14: ; preds = %9
%15 = call i64 @strftime(i8* %3, i32 255, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), %struct.tm* %10)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %20
17: ; preds = %14
%18 = load i32, i32* @stderr
%19 = call i32 @fputs(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %18)
br label %22
20: ; preds = %14
%21 = call i8* @strdup(i8* %3)
br label %22
22: ; preds = %20, %17, %12, %7
%23 = phi i8* [ null, %7 ], [ null, %12 ], [ null, %17 ], [ %21, %20 ]
ret i8* %23
}