#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;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
size_t fwrite(char *, int, size_t, int /*<<< orphan*/ *);
char *gethome();
int /*<<< orphan*/ sprintf(char *, char *, int /*<<< orphan*/);
scalar_t__ sstrncat(char *, size_t *, int, char const *, int);
size_t strlen(char const *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
int update_localdate(void) {
  FILE *fp;
  size_t len;
  char const *homedir;
  char outdir[512];
  size_t written;
  char timestr[32];
  time_t curtime;
  homedir = gethome();
  if (homedir == NULL)
    return 1;
  len = 0;
  if (sstrncat(outdir, &len, 512, homedir, strlen(homedir)))
    return 1;
  if (sstrncat(outdir, &len, 512, "/.tldrc/date", (sizeof("/.tldrc/date") - 1)))
    return 1;
  fp = fopen(outdir, "w");
  if (!fp)
    return 1;
  curtime = time(NULL);
  sprintf(timestr, "%ld", curtime);
  written = fwrite(timestr, sizeof(char), strlen(timestr), fp);
  if (written < strlen(timestr))
    goto error;
  fclose(fp);
  return 0;
error:
  fclose(fp);
  return 1;
}


x86 assembly (clang, -O0):
<update_localdate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
mov    $0x0,%al
call   10a0 <gethome@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ad <update_localdate+0x2d>
movl   $0x1,-0x4(%rbp)
jmp    132a <update_localdate+0x1aa>
movq   $0x0,-0x18(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x268(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x260(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x268(%rbp),%rdi
mov    -0x260(%rbp),%rcx
mov    %eax,%r8d
lea    -0x18(%rbp),%rsi
mov    $0x200,%edx
call   1050 <sstrncat@plt>
cmp    $0x0,%rax
je     120c <update_localdate+0x8c>
movl   $0x1,-0x4(%rbp)
jmp    132a <update_localdate+0x1aa>
lea    -0x220(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0x200,%edx
lea    0xddd(%rip),%rcx
mov    $0xc,%r8d
call   1050 <sstrncat@plt>
cmp    $0x0,%rax
je     1244 <update_localdate+0xc4>
movl   $0x1,-0x4(%rbp)
jmp    132a <update_localdate+0x1aa>
lea    -0x220(%rbp),%rdi
lea    0xdbb(%rip),%rsi
call   1070 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1272 <update_localdate+0xf2>
movl   $0x1,-0x4(%rbp)
jmp    132a <update_localdate+0x1aa>
xor    %eax,%eax
mov    %eax,%edi
call   1060 <time@plt>
mov    %eax,-0x254(%rbp)
lea    -0x250(%rbp),%rdi
mov    -0x254(%rbp),%edx
lea    0xd7a(%rip),%rsi
call   1080 <sprintf@plt>
lea    -0x250(%rbp),%rax
mov    %rax,-0x278(%rbp)
lea    -0x250(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x278(%rbp),%rdi
mov    %rax,%rdx
mov    -0x10(%rbp),%rcx
mov    $0x1,%esi
call   1090 <fwrite@plt>
mov    %rax,-0x228(%rbp)
mov    -0x228(%rbp),%rax
mov    %rax,-0x270(%rbp)
lea    -0x250(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x270(%rbp),%rax
cmp    %rcx,%rax
jae    1305 <update_localdate+0x185>
jmp    131a <update_localdate+0x19a>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x0,-0x4(%rbp)
jmp    132a <update_localdate+0x1aa>
mov    -0x10(%rbp),%rdi
call   1030 <fclose@plt>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x280,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<update_localdate>:
push   %rbp
push   %r14
push   %rbx
sub    $0x230,%rsp
xor    %eax,%eax
call   10a0 <gethome@plt>
mov    $0x1,%ebp
test   %rax,%rax
je     126a <update_localdate+0xea>
mov    %rax,%rbx
movq   $0x0,0x8(%rsp)
mov    %rax,%rdi
call   1040 <strlen@plt>
lea    0x30(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x200,%edx
mov    %rbx,%rcx
mov    %eax,%r8d
call   1050 <sstrncat@plt>
test   %rax,%rax
jne    126a <update_localdate+0xea>
lea    0xe22(%rip),%rcx
lea    0x30(%rsp),%rdi
lea    0x8(%rsp),%rsi
mov    $0x200,%edx
mov    $0xc,%r8d
call   1050 <sstrncat@plt>
test   %rax,%rax
jne    126a <update_localdate+0xea>
lea    0xe09(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1070 <fopen@plt>
test   %rax,%rax
je     126a <update_localdate+0xea>
mov    %rax,%r14
xor    %edi,%edi
call   1060 <time@plt>
lea    0xdeb(%rip),%rsi
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
mov    %eax,%edx
call   1080 <sprintf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %rax,%rdx
mov    %r14,%rcx
call   1090 <fwrite@plt>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1040 <strlen@plt>
xor    %ebp,%ebp
cmp    %rax,%rbx
setb   %bpl
mov    %r14,%rdi
call   1030 <fclose@plt>
mov    %ebp,%eax
add    $0x230,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"/.tldrc/date\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%ld\00"
define i32 @update_localdate() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca [512 x i8]
%6 = alloca i64
%7 = alloca [32 x i8]
%8 = alloca i32
%9 = call i8* (...) @gethome()
store i8* %9, i8** %4
%10 = load i8*, i8** %4
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %13
12: ; preds = %0
store i32 1, i32* %1
br label %54
13: ; preds = %0
store i64 0, i64* %3
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%15 = load i8*, i8** %4
%16 = load i8*, i8** %4
%17 = call i64 @strlen(i8* %16)
%18 = trunc i64 %17 to i32
%19 = call i64 @sstrncat(i8* %14, i64* %3, i32 512, i8* %15, i32 %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %13
store i32 1, i32* %1
br label %54
22: ; preds = %13
%23 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%24 = call i64 @sstrncat(i8* %23, i64* %3, i32 512, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %27
26: ; preds = %22
store i32 1, i32* %1
br label %54
27: ; preds = %22
%28 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%29 = call i32* @fopen(i8* %28, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %29, i32** %2
%30 = load i32*, i32** %2
%31 = icmp ne i32* %30, null
br i1 %31, label %33, label %32
32: ; preds = %27
store i32 1, i32* %1
br label %54
33: ; preds = %27
%34 = call i32 @time(i32* null)
store i32 %34, i32* %8
%35 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%36 = load i32, i32* %8
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %36)
%38 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%39 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%40 = call i64 @strlen(i8* %39)
%41 = load i32*, i32** %2
%42 = call i64 @fwrite(i8* %38, i32 1, i64 %40, i32* %41)
store i64 %42, i64* %6
%43 = load i64, i64* %6
%44 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%45 = call i64 @strlen(i8* %44)
%46 = icmp ult i64 %43, %45
br i1 %46, label %47, label %48
47: ; preds = %33
br label %51
48: ; preds = %33
%49 = load i32*, i32** %2
%50 = call i32 @fclose(i32* %49)
store i32 0, i32* %1
br label %54
51: ; preds = %47
%52 = load i32*, i32** %2
%53 = call i32 @fclose(i32* %52)
store i32 1, i32* %1
br label %54
54: ; preds = %51, %48, %32, %26, %21, %12
%55 = load i32, i32* %1
ret i32 %55
}

LLVM IR (-O2):
@.str = [13 x i8] c"/.tldrc/date\00"
@.str.1 = [2 x i8] c"w\00"
@.str.2 = [4 x i8] c"%ld\00"
define i32 @update_localdate() {
%1 = alloca i64
%2 = alloca [512 x i8]
%3 = alloca [32 x i8]
%4 = bitcast i64* %1 to i8*
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = tail call i8* (...) @gethome()
%8 = icmp eq i8* %7, null
br i1 %8, label %29, label %9
9: ; preds = %0
store i64 0, i64* %1
%10 = tail call i64 @strlen(i8* %7)
%11 = trunc i64 %10 to i32
%12 = call i64 @sstrncat(i8* %5, i64* %1, i32 512, i8* %7, i32 %11)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %29
14: ; preds = %9
%15 = call i64 @sstrncat(i8* %5, i64* %1, i32 512, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0), i32 12)
%16 = icmp eq i64 %15, 0
br i1 %16, label %17, label %29
17: ; preds = %14
%18 = call i32* @fopen(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %29, label %20
20: ; preds = %17
%21 = call i32 @time(i32* null)
%22 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %21)
%23 = call i64 @strlen(i8* %6)
%24 = call i64 @fwrite(i8* %6, i32 1, i64 %23, i32* %18)
%25 = call i64 @strlen(i8* %6)
%26 = icmp ult i64 %24, %25
%27 = call i32 @fclose(i32* %18)
%28 = zext i1 %26 to i32
br label %29
29: ; preds = %20, %17, %14, %9, %0
%30 = phi i32 [ 1, %0 ], [ 1, %9 ], [ 1, %14 ], [ 1, %17 ], [ %28, %20 ]
ret i32 %30
}