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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
char *getcwd(char *, int);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
size_t strlen(char *);
char *strrchr(char *, char);
void logfile(char *bin) {
  FILE *logfile;
  char *rc;
  size_t length;
  char *work_dir;
  char buffer[512 + 1];
  char dir[512 + 1];
  work_dir = getcwd(buffer, sizeof(buffer));
  if (work_dir == NULL) {
    puts("Unable to fetch directory path");
    exit(EXIT_FAILURE);
  }
  rc = strrchr(work_dir, '/');
  length = strlen(rc);
  memcpy(dir, rc + 1, length);
  snprintf(buffer, sizeof(buffer), "../%s_sums", dir);
  logfile = fopen(buffer, "a");
  if (logfile == NULL) {
    printf("Unable to create log file");
  }
  fprintf(logfile, "%s\n", bin);
  fclose(logfile);
}


x86 assembly (clang, -O0):
<logfile>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
lea    -0x230(%rbp),%rdi
mov    $0x201,%esi
call   1090 <getcwd@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11f9 <logfile+0x49>
lea    0xe1a(%rip),%rdi
call   1030 <puts@plt>
mov    0x2dee(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    $0x2f,%esi
call   1080 <strrchr@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x20(%rbp)
lea    -0x440(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x1,%rsi
mov    -0x20(%rbp),%rdx
call   10b0 <memcpy@plt>
lea    -0x230(%rbp),%rdi
lea    -0x440(%rbp),%rcx
mov    $0x201,%esi
lea    0xdd5(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x230(%rbp),%rdi
lea    0xdcd(%rip),%rsi
call   10c0 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    127d <logfile+0xcd>
lea    0xdb4(%rip),%rdi
call   1060 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdba(%rip),%rsi
call   10a0 <fprintf@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<logfile>:
push   %r15
push   %r14
push   %rbx
sub    $0x420,%rsp
mov    %rdi,%r14
mov    %rsp,%rdi
mov    $0x201,%esi
call   1090 <getcwd@plt>
test   %rax,%rax
je     126b <logfile+0xbb>
mov    %rax,%rdi
mov    $0x2f,%esi
call   1080 <strrchr@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <strlen@plt>
add    $0x1,%rbx
lea    0x210(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rax,%rdx
call   10b0 <memcpy@plt>
lea    0xe11(%rip),%rdx
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    $0x201,%esi
mov    %r15,%rcx
call   1070 <snprintf@plt>
lea    0xe02(%rip),%rsi
mov    %rbx,%rdi
call   10c0 <fopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1244 <logfile+0x94>
lea    0xded(%rip),%rdi
call   1060 <printf@plt>
lea    0xdfb(%rip),%rsi
mov    %rbx,%rdi
mov    %r14,%rdx
call   10a0 <fprintf@plt>
mov    %rbx,%rdi
call   1040 <fclose@plt>
add    $0x420,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xd8e(%rip),%rdi
call   1030 <puts@plt>
mov    0x2d62(%rip),%rax
mov    (%rax),%edi
call   10d0 <exit@plt>

LLVM IR (-O0):
@.str = [31 x i8] c"Unable to fetch directory path\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"../%s_sums\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [26 x i8] c"Unable to create log file\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @logfile(i8* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca [513 x i8]
%8 = alloca [513 x i8]
store i8* %0, i8** %2
%9 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%10 = call i8* @getcwd(i8* %9, i32 513)
store i8* %10, i8** %6
%11 = load i8*, i8** %6
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = load i8*, i8** %6
%19 = call i8* @strrchr(i8* %18, i8 signext 47)
store i8* %19, i8** %4
%20 = load i8*, i8** %4
%21 = call i64 @strlen(i8* %20)
store i64 %21, i64* %5
%22 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%23 = load i8*, i8** %4
%24 = getelementptr inbounds i8, i8* %23, i64 1
%25 = load i64, i64* %5
%26 = call i32 @memcpy(i8* %22, i8* %24, i64 %25)
%27 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%28 = getelementptr inbounds [513 x i8], [513 x i8]* %8, i64 0, i64 0
%29 = call i32 @snprintf(i8* %27, i32 513, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %28)
%30 = getelementptr inbounds [513 x i8], [513 x i8]* %7, i64 0, i64 0
%31 = call i32* @fopen(i8* %30, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %31, i32** %3
%32 = load i32*, i32** %3
%33 = icmp eq i32* %32, null
br i1 %33, label %34, label %36
34: ; preds = %17
%35 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %36
36: ; preds = %34, %17
%37 = load i32*, i32** %3
%38 = load i8*, i8** %2
%39 = call i32 @fprintf(i32* %37, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %38)
%40 = load i32*, i32** %3
%41 = call i32 @fclose(i32* %40)
ret void
}

LLVM IR (-O2):
@.str = [31 x i8] c"Unable to fetch directory path\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [11 x i8] c"../%s_sums\00"
@.str.2 = [2 x i8] c"a\00"
@.str.3 = [26 x i8] c"Unable to create log file\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @logfile(i8* %0) {
%2 = alloca [513 x i8]
%3 = alloca [513 x i8]
%4 = getelementptr inbounds [513 x i8], [513 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [513 x i8], [513 x i8]* %3, i64 0, i64 0
%6 = call i8* @getcwd(i8* %4, i32 513)
%7 = icmp eq i8* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %1
%13 = call i8* @strrchr(i8* %6, i8 signext 47)
%14 = call i64 @strlen(i8* dereferenceable(1) %13)
%15 = getelementptr inbounds i8, i8* %13, i64 1
%16 = call i32 @memcpy(i8* %5, i8* %15, i64 %14)
%17 = call i32 @snprintf(i8* %4, i32 513, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0), i8* %5)
%18 = call i32* @fopen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32* %18, null
br i1 %19, label %20, label %22
20: ; preds = %12
%21 = call i32 @printf(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
br label %22
22: ; preds = %20, %12
%23 = call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %0)
%24 = call i32 @fclose(i32* %18)
ret void
}