#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;
struct tm {
  int dummy;
};
struct passwd {
  char *pw_name;
};
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ ENOENT;
int /*<<< orphan*/ err_msg(char *);
scalar_t__ errno;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *, int const,
                           char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
int /*<<< orphan*/ geteuid();
struct passwd *getpwuid(int /*<<< orphan*/);
struct tm *localtime(int /*<<< orphan*/ *);
scalar_t__ malloc(scalar_t__);
int mkdir(char *, int);
int /*<<< orphan*/ *opendir(char *);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int /*<<< orphan*/ strftime(char *, int, char *, struct tm *);
scalar_t__ strlen(char const *);
int /*<<< orphan*/ time(int /*<<< orphan*/ *);
void save_score(const int time_taken) {
  time_t tm = time(NULL);
  struct tm *today = localtime(&tm);
  char tmpbuffer[129];
  today = localtime(&tm);
  char appdata_dir[4096];
  const char *score_filename = "4digits.4digits.scores";
  strcpy(appdata_dir, getenv("HOME"));
  strcat(appdata_dir, "/.4digits/");
  char *scorefile =
      (char *)malloc(strlen(appdata_dir) + strlen(score_filename) + 1);
  if (!scorefile)
    err_msg("Memory allocation error.\n");
  strcpy(scorefile, appdata_dir);
  strcat(scorefile, score_filename);
  FILE *sfp = fopen(scorefile, "a+");
  if (!sfp) {
    if (errno == ENOENT) {
      DIR *dp = opendir(appdata_dir);
      if (!dp)
        if (errno == ENOENT) {
          int ret = mkdir(appdata_dir, 0700);
          if (ret == -1)
            err_msg("Cannot open score file.\n");
          sfp = fopen(scorefile, "a+");
        }
    } else
      err_msg("Cannot open score file.\n");
  }
  strftime(tmpbuffer, 128, "%a %b %d %H:%M:%S %Y", today);
  struct passwd *pwd;
  pwd = getpwuid(geteuid());
  fprintf(sfp, "%s %ds %s\n", pwd->pw_name, time_taken, tmpbuffer);
  free(scorefile);
}


x86 assembly (clang, -O0):
<save_score>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10e0,%rsp
mov    %edi,-0x4(%rbp)
xor    %eax,%eax
mov    %eax,%edi
call   10d0 <time@plt>
mov    %eax,-0x8(%rbp)
lea    -0x8(%rbp),%rdi
call   1050 <localtime@plt>
mov    %rax,-0x10(%rbp)
lea    -0x8(%rbp),%rdi
call   1050 <localtime@plt>
mov    %rax,-0x10(%rbp)
lea    0xdc5(%rip),%rax
mov    %rax,-0x10a8(%rbp)
lea    -0x10a0(%rbp),%rax
mov    %rax,-0x10e0(%rbp)
lea    0xdc0(%rip),%rdi
call   1030 <getenv@plt>
mov    -0x10e0(%rbp),%rdi
mov    %rax,%rsi
call   1060 <strcpy@plt>
lea    -0x10a0(%rbp),%rdi
lea    0xda3(%rip),%rsi
call   1120 <strcat@plt>
lea    -0x10a0(%rbp),%rdi
call   10a0 <strlen@plt>
mov    %rax,-0x10d8(%rbp)
mov    -0x10a8(%rbp),%rdi
call   10a0 <strlen@plt>
mov    -0x10d8(%rbp),%rdi
add    %rax,%rdi
add    $0x1,%rdi
call   10e0 <malloc@plt>
mov    %rax,-0x10b0(%rbp)
cmpq   $0x0,-0x10b0(%rbp)
jne    12d1 <save_score+0xd1>
lea    0xd5b(%rip),%rdi
call   1110 <err_msg@plt>
mov    -0x10b0(%rbp),%rdi
lea    -0x10a0(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x10b0(%rbp),%rdi
mov    -0x10a8(%rbp),%rsi
call   1120 <strcat@plt>
mov    -0x10b0(%rbp),%rdi
lea    0xd3c(%rip),%rsi
call   1100 <fopen@plt>
mov    %rax,-0x10b8(%rbp)
cmpq   $0x0,-0x10b8(%rbp)
jne    13d9 <save_score+0x1d9>
mov    0x2cba(%rip),%rax
mov    (%rax),%rax
mov    0x2ca0(%rip),%rcx
cmp    (%rcx),%rax
jne    13c8 <save_score+0x1c8>
lea    -0x10a0(%rbp),%rdi
call   1080 <opendir@plt>
mov    %rax,-0x10c0(%rbp)
cmpq   $0x0,-0x10c0(%rbp)
jne    13c3 <save_score+0x1c3>
mov    0x2c7f(%rip),%rax
mov    (%rax),%rax
mov    0x2c65(%rip),%rcx
cmp    (%rcx),%rax
jne    13be <save_score+0x1be>
lea    -0x10a0(%rbp),%rdi
mov    $0x1c0,%esi
call   1070 <mkdir@plt>
mov    %eax,-0x10c4(%rbp)
cmpl   $0xffffffff,-0x10c4(%rbp)
jne    13a4 <save_score+0x1a4>
lea    0xca5(%rip),%rdi
call   1110 <err_msg@plt>
mov    -0x10b0(%rbp),%rdi
lea    0xc8f(%rip),%rsi
call   1100 <fopen@plt>
mov    %rax,-0x10b8(%rbp)
jmp    13c3 <save_score+0x1c3>
jmp    13d4 <save_score+0x1d4>
lea    0xc75(%rip),%rdi
call   1110 <err_msg@plt>
jmp    13d9 <save_score+0x1d9>
lea    -0xa0(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x80,%esi
lea    0xc6d(%rip),%rdx
call   10f0 <strftime@plt>
mov    $0x0,%al
call   10b0 <geteuid@plt>
mov    %eax,%edi
call   1090 <getpwuid@plt>
mov    %rax,-0x10d0(%rbp)
mov    -0x10b8(%rbp),%rdi
mov    -0x10d0(%rbp),%rax
mov    (%rax),%rdx
mov    -0x4(%rbp),%ecx
lea    -0xa0(%rbp),%r8
lea    0xc46(%rip),%rsi
call   10c0 <fprintf@plt>
mov    -0x10b0(%rbp),%rdi
call   1040 <free@plt>
add    $0x10e0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<save_score>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10a0,%rsp
mov    %edi,%r14d
xor    %edi,%edi
call   10d0 <time@plt>
mov    %eax,0xc(%rsp)
lea    0xc(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <localtime@plt>
mov    %rbx,%rdi
call   1050 <localtime@plt>
mov    %rax,%r15
lea    0xddb(%rip),%rdi
call   1030 <getenv@plt>
lea    0xa0(%rsp),%rbx
mov    %rbx,%rdi
mov    %rax,%rsi
call   1060 <strcpy@plt>
lea    0xdc1(%rip),%rsi
mov    %rbx,%rdi
call   1120 <strcat@plt>
mov    %rbx,%rdi
call   10a0 <strlen@plt>
lea    0x17(%rax),%rdi
call   10e0 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    1288 <save_score+0x88>
lea    0xda4(%rip),%rdi
call   1110 <err_msg@plt>
lea    0xa0(%rsp),%rsi
mov    %rbp,%rdi
call   1060 <strcpy@plt>
lea    0xd61(%rip),%rsi
mov    %rbp,%rdi
call   1120 <strcat@plt>
lea    0xd93(%rip),%rsi
mov    %rbp,%rdi
call   1100 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1335 <save_score+0x135>
mov    0x2d1b(%rip),%r12
mov    (%r12),%rax
mov    0x2d00(%rip),%rbx
cmp    (%rbx),%rax
jne    1327 <save_score+0x127>
lea    0xa0(%rsp),%rdi
call   1080 <opendir@plt>
test   %rax,%rax
jne    1333 <save_score+0x133>
mov    (%r12),%rax
cmp    (%rbx),%rax
jne    1333 <save_score+0x133>
lea    0xa0(%rsp),%rdi
mov    $0x1c0,%esi
call   1070 <mkdir@plt>
cmp    $0xffffffff,%eax
jne    1313 <save_score+0x113>
lea    0xd36(%rip),%rdi
call   1110 <err_msg@plt>
lea    0xd27(%rip),%rsi
mov    %rbp,%rdi
call   1100 <fopen@plt>
mov    %rax,%rbx
jmp    1335 <save_score+0x135>
lea    0xd16(%rip),%rdi
call   1110 <err_msg@plt>
xor    %ebx,%ebx
lea    0xd21(%rip),%rdx
lea    0x10(%rsp),%r12
mov    %r12,%rdi
mov    $0x80,%esi
mov    %r15,%rcx
call   10f0 <strftime@plt>
xor    %eax,%eax
call   10b0 <geteuid@plt>
mov    %eax,%edi
call   1090 <getpwuid@plt>
mov    (%rax),%rdx
lea    0xd09(%rip),%rsi
mov    %rbx,%rdi
mov    %r14d,%ecx
mov    %r12,%r8
call   10c0 <fprintf@plt>
mov    %rbp,%rdi
call   1040 <free@plt>
add    $0x10a0,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.tm = type { i32 }
%struct.passwd = type { i8* }
@.str = [23 x i8] c"4digits.4digits.scores\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [11 x i8] c"/.4digits/\00"
@.str.3 = [26 x i8] c"Memory allocation error.\0A\00"
@.str.4 = [3 x i8] c"a+\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.5 = [25 x i8] c"Cannot open score file.\0A\00"
@.str.6 = [21 x i8] c"%a %b %d %H:%M:%S %Y\00"
@.str.7 = [11 x i8] c"%s %ds %s\0A\00"
define void @save_score(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.tm*
%5 = alloca [129 x i8]
%6 = alloca [4096 x i8]
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32*
%10 = alloca i32*
%11 = alloca i32
%12 = alloca %struct.passwd*
store i32 %0, i32* %2
%13 = call i32 @time(i32* null)
store i32 %13, i32* %3
%14 = call %struct.tm* @localtime(i32* %3)
store %struct.tm* %14, %struct.tm** %4
%15 = call %struct.tm* @localtime(i32* %3)
store %struct.tm* %15, %struct.tm** %4
store i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8** %7
%16 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%17 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%18 = call i32 @strcpy(i8* %16, i8* %17)
%19 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%20 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%22 = call i64 @strlen(i8* %21)
%23 = load i8*, i8** %7
%24 = call i64 @strlen(i8* %23)
%25 = add nsw i64 %22, %24
%26 = add nsw i64 %25, 1
%27 = call i64 @malloc(i64 %26)
%28 = inttoptr i64 %27 to i8*
store i8* %28, i8** %8
%29 = load i8*, i8** %8
%30 = icmp ne i8* %29, null
br i1 %30, label %33, label %31
31: ; preds = %1
%32 = call i32 @err_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %33
33: ; preds = %31, %1
%34 = load i8*, i8** %8
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%36 = call i32 @strcpy(i8* %34, i8* %35)
%37 = load i8*, i8** %8
%38 = load i8*, i8** %7
%39 = call i32 @strcat(i8* %37, i8* %38)
%40 = load i8*, i8** %8
%41 = call i32* @fopen(i8* %40, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %41, i32** %9
%42 = load i32*, i32** %9
%43 = icmp ne i32* %42, null
br i1 %43, label %72, label %44
44: ; preds = %33
%45 = load i64, i64* @errno
%46 = load i64, i64* @ENOENT
%47 = icmp eq i64 %45, %46
br i1 %47, label %48, label %69
48: ; preds = %44
%49 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%50 = call i32* @opendir(i8* %49)
store i32* %50, i32** %10
%51 = load i32*, i32** %10
%52 = icmp ne i32* %51, null
br i1 %52, label %68, label %53
53: ; preds = %48
%54 = load i64, i64* @errno
%55 = load i64, i64* @ENOENT
%56 = icmp eq i64 %54, %55
br i1 %56, label %57, label %67
57: ; preds = %53
%58 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%59 = call i32 @mkdir(i8* %58, i32 448)
store i32 %59, i32* %11
%60 = load i32, i32* %11
%61 = icmp eq i32 %60, -1
br i1 %61, label %62, label %64
62: ; preds = %57
%63 = call i32 @err_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %64
64: ; preds = %62, %57
%65 = load i8*, i8** %8
%66 = call i32* @fopen(i8* %65, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
store i32* %66, i32** %9
br label %67
67: ; preds = %64, %53
br label %68
68: ; preds = %67, %48
br label %71
69: ; preds = %44
%70 = call i32 @err_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %71
71: ; preds = %69, %68
br label %72
72: ; preds = %71, %33
%73 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%74 = load %struct.tm*, %struct.tm** %4
%75 = call i32 @strftime(i8* %73, i32 128, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), %struct.tm* %74)
%76 = call i32 (...) @geteuid()
%77 = call %struct.passwd* @getpwuid(i32 %76)
store %struct.passwd* %77, %struct.passwd** %12
%78 = load i32*, i32** %9
%79 = load %struct.passwd*, %struct.passwd** %12
%80 = getelementptr inbounds %struct.passwd, %struct.passwd* %79, i32 0, i32 0
%81 = load i8*, i8** %80
%82 = load i32, i32* %2
%83 = getelementptr inbounds [129 x i8], [129 x i8]* %5, i64 0, i64 0
%84 = call i32 @fprintf(i32* %78, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* %81, i32 %82, i8* %83)
%85 = load i8*, i8** %8
%86 = call i32 @free(i8* %85)
ret void
}

LLVM IR (-O2):
%struct.tm = type { i32 }
%struct.passwd = type { i8* }
@.str = [23 x i8] c"4digits.4digits.scores\00"
@.str.1 = [5 x i8] c"HOME\00"
@.str.2 = [11 x i8] c"/.4digits/\00"
@.str.3 = [26 x i8] c"Memory allocation error.\0A\00"
@.str.4 = [3 x i8] c"a+\00"
@errno = global i64 0
@ENOENT = global i64 0
@.str.5 = [25 x i8] c"Cannot open score file.\0A\00"
@.str.6 = [21 x i8] c"%a %b %d %H:%M:%S %Y\00"
@.str.7 = [11 x i8] c"%s %ds %s\0A\00"
define void @save_score(i32 %0) {
%2 = alloca i32
%3 = alloca [129 x i8]
%4 = alloca [4096 x i8]
%5 = bitcast i32* %2 to i8*
%6 = tail call i32 @time(i32* null)
store i32 %6, i32* %2
%7 = call %struct.tm* @localtime(i32* %2)
%8 = getelementptr inbounds [129 x i8], [129 x i8]* %3, i64 0, i64 0
%9 = call %struct.tm* @localtime(i32* %2)
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%11 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 @strcpy(i8* %10, i8* %11)
%13 = call i32 @strcat(i8* %10, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%14 = call i64 @strlen(i8* %10)
%15 = add nsw i64 %14, 23
%16 = call i64 @malloc(i64 %15)
%17 = inttoptr i64 %16 to i8*
%18 = icmp eq i64 %16, 0
br i1 %18, label %19, label %21
19: ; preds = %1
%20 = call i32 @err_msg(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %21
21: ; preds = %19, %1
%22 = call i32 @strcpy(i8* %17, i8* %10)
%23 = call i32 @strcat(i8* %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%24 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %46
26: ; preds = %21
%27 = load i64, i64* @errno
%28 = load i64, i64* @ENOENT
%29 = icmp eq i64 %27, %28
br i1 %29, label %30, label %44
30: ; preds = %26
%31 = call i32* @opendir(i8* %10)
%32 = icmp eq i32* %31, null
br i1 %32, label %33, label %46
33: ; preds = %30
%34 = load i64, i64* @errno
%35 = load i64, i64* @ENOENT
%36 = icmp eq i64 %34, %35
br i1 %36, label %37, label %46
37: ; preds = %33
%38 = call i32 @mkdir(i8* %10, i32 448)
%39 = icmp eq i32 %38, -1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = call i32 @err_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %42
42: ; preds = %40, %37
%43 = call i32* @fopen(i8* %17, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
br label %46
44: ; preds = %26
%45 = call i32 @err_msg(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %46
46: ; preds = %30, %42, %33, %44, %21
%47 = phi i32* [ %24, %21 ], [ null, %44 ], [ null, %30 ], [ %43, %42 ], [ null, %33 ]
%48 = call i32 @strftime(i8* %8, i32 128, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.6, i64 0, i64 0), %struct.tm* %9)
%49 = call i32 (...) @geteuid()
%50 = call %struct.passwd* @getpwuid(i32 %49)
%51 = getelementptr inbounds %struct.passwd, %struct.passwd* %50, i64 0, i32 0
%52 = load i8*, i8** %51
%53 = call i32 @fprintf(i32* %47, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.7, i64 0, i64 0), i8* %52, i32 %0, i8* %8)
%54 = call i32 @free(i8* %17)
ret void
}