#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 EIO;
struct tm *localtime(scalar_t__ *);
int strftime(char *, int, char *, struct tm *);
scalar_t__ time(scalar_t__ *);
int getcurtime(char *buf, int max) {
  time_t cur_time, rc;
  struct tm *cur_tm;
  int len;
  buf[0] = '\0';
  rc = time(&cur_time);
  if (rc == ((time_t)-1))
    return -EIO;
  cur_tm = localtime(&cur_time);
  if (!cur_tm)
    return -EIO;
  len = strftime(buf, max, "%b %d %T", cur_tm);
  return len;
}


x86 assembly (clang, -O0):
<getcurtime>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
lea    -0x20(%rbp),%rdi
call   1040 <time@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1171 <getcurtime+0x41>
xor    %eax,%eax
mov    0x2e79(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <getcurtime+0x8c>
lea    -0x20(%rbp),%rdi
call   1030 <localtime@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    119c <getcurtime+0x6c>
xor    %eax,%eax
mov    0x2e4e(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11bc <getcurtime+0x8c>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%rcx
lea    0xe52(%rip),%rdx
call   1050 <strftime@plt>
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcurtime>:
push   %rbp
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
movb   $0x0,(%rdi)
mov    %rsp,%rdi
call   1040 <time@plt>
cmp    $0xffffffffffffffff,%rax
je     116c <getcurtime+0x3c>
mov    %rsp,%rdi
call   1030 <localtime@plt>
test   %rax,%rax
je     116c <getcurtime+0x3c>
lea    0xea3(%rip),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
mov    %rax,%rcx
call   1050 <strftime@plt>
jmp    1177 <getcurtime+0x47>
mov    0x2e6d(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

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

LLVM IR (-O2):
%struct.tm = type { i32 }
@EIO = global i32 0
@.str = [9 x i8] c"%b %d %T\00"
define i32 @getcurtime(i8* %0, i32 %1) {
%3 = alloca i64
%4 = bitcast i64* %3 to i8*
store i8 0, i8* %0
%5 = call i64 @time(i64* %3)
%6 = icmp eq i64 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @EIO
%9 = sub nsw i32 0, %8
br label %18
10: ; preds = %2
%11 = call %struct.tm* @localtime(i64* %3)
%12 = icmp eq %struct.tm* %11, null
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = load i32, i32* @EIO
%15 = sub nsw i32 0, %14
br label %18
16: ; preds = %10
%17 = call i32 @strftime(i8* %0, i32 %1, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), %struct.tm* %11)
br label %18
18: ; preds = %16, %13, %7
%19 = phi i32 [ %9, %7 ], [ %17, %16 ], [ %15, %13 ]
ret i32 %19
}