#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 */

/* Variables and functions */
int PATH_MAX;
scalar_t__ chdir(char const *);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ *getcwd(char *, int);
scalar_t__ safe_malloc(char *, int, scalar_t__);
int /*<<< orphan*/ stderr;
char *strcpy(char *, char *);
char *strerror(int /*<<< orphan*/);
scalar_t__ strlen(char *);
char *normalise_dir(const char *dir) {
  char startdir[PATH_MAX];
  char srcdir[PATH_MAX];
  if (getcwd(startdir, PATH_MAX) == NULL) {
    fprintf(stderr, "Couldn't get start directory!\n");
    exit(1);
  }
  if (chdir(dir) < 0) {
    fprintf(stderr, "Couldn't cd to %s : %s!\n", dir, strerror(errno));
    exit(1);
  }
  if (getcwd(srcdir, PATH_MAX) == NULL) {
    fprintf(stderr, "Couldn't read absolute src directory!\n");
    exit(1);
  }
  if (chdir(startdir) < 0) {
    fprintf(stderr, "Couldn't cd back to starting directory %s!\n", startdir);
    exit(1);
  }
  return strcpy(
      (char *)safe_malloc(
          "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
          "2022-01-22/02-19-57/repos/rc0/spill/refs/heads/master/spill.c",
          137, 1 + strlen(srcdir)),
      (srcdir));
}


x86 assembly (clang, -O0):
<normalise_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e5d(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x10(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x30(%rbp)
mov    %rdi,%rsp
mov    %rcx,-0x18(%rbp)
mov    (%rax),%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    0x2e13(%rip),%rax
mov    (%rax),%esi
call   1070 <getcwd@plt>
cmp    $0x0,%rax
jne    120f <normalise_dir+0x8f>
mov    0x2de3(%rip),%rax
mov    (%rax),%edi
lea    0xe02(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x8(%rbp),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
jge    1266 <normalise_dir+0xe6>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    0x2d93(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%edi
mov    -0x38(%rbp),%rdx
mov    %rax,%rcx
lea    0xdca(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    0x2d7f(%rip),%rax
mov    (%rax),%esi
call   1070 <getcwd@plt>
cmp    $0x0,%rax
jne    12a3 <normalise_dir+0x123>
mov    0x2d4f(%rip),%rax
mov    (%rax),%edi
lea    0xda6(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x30(%rbp),%rdi
call   1060 <chdir@plt>
cmp    $0x0,%rax
jge    12db <normalise_dir+0x15b>
mov    -0x30(%rbp),%rdx
mov    0x2d17(%rip),%rax
mov    (%rax),%edi
lea    0xd95(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdx
add    $0x1,%rdx
lea    0xd99(%rip),%rdi
mov    $0x89,%esi
call   1040 <safe_malloc@plt>
mov    -0x28(%rbp),%rsi
mov    %rax,%rdi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<normalise_dir>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
mov    0x2e5b(%rip),%rbx
mov    (%rbx),%esi
mov    %rsp,%r14
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %rsp,%r12
sub    %rax,%r12
mov    %r12,%rsp
mov    %r14,%rdi
call   1070 <getcwd@plt>
test   %rax,%rax
je     122d <normalise_dir+0xad>
mov    %r15,%rdi
call   1060 <chdir@plt>
test   %rax,%rax
js     123f <normalise_dir+0xbf>
mov    (%rbx),%esi
mov    %r12,%rdi
call   1070 <getcwd@plt>
test   %rax,%rax
je     1276 <normalise_dir+0xf6>
mov    %r14,%rdi
call   1060 <chdir@plt>
test   %rax,%rax
js     1297 <normalise_dir+0x117>
mov    %r12,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
lea    0xe86(%rip),%rdi
mov    $0x89,%esi
call   1040 <safe_malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2da4(%rip),%rax
mov    (%rax),%edi
lea    0xdc3(%rip),%rsi
jmp    1286 <normalise_dir+0x106>
mov    0x2d92(%rip),%rax
mov    (%rax),%ebx
mov    0x2d81(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
lea    0xdc2(%rip),%rsi
mov    %ebx,%edi
mov    %r15,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d5b(%rip),%rax
mov    (%rax),%edi
lea    0xdb2(%rip),%rsi
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>
mov    0x2d3a(%rip),%rax
mov    (%rax),%edi
lea    0xdb8(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Couldn't get start directory!\0A\00"
@.str.1 = [25 x i8] c"Couldn't cd to %s : %s!\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"Couldn't read absolute src directory!\0A\00"
@.str.3 = [44 x i8] c"Couldn't cd back to starting directory %s!\0A\00"
@.str.4 = [126 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/rc0/spill/refs/heads/master/spill.c\00"
define i8* @normalise_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
store i8* %0, i8** %2
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %3
%9 = alloca i8, i64 %7
store i64 %7, i64* %4
%10 = load i32, i32* @PATH_MAX
%11 = zext i32 %10 to i64
%12 = alloca i8, i64 %11
store i64 %11, i64* %5
%13 = load i32, i32* @PATH_MAX
%14 = call i32* @getcwd(i8* %9, i32 %13)
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %20
16: ; preds = %1
%17 = load i32, i32* @stderr
%18 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %1
%21 = load i8*, i8** %2
%22 = call i64 @chdir(i8* %21)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %20
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %2
%27 = load i32, i32* @errno
%28 = call i8* @strerror(i32 %27)
%29 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %26, i8* %28)
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
%32 = load i32, i32* @PATH_MAX
%33 = call i32* @getcwd(i8* %12, i32 %32)
%34 = icmp eq i32* %33, null
br i1 %34, label %35, label %39
35: ; preds = %31
%36 = load i32, i32* @stderr
%37 = call i32 (i32, i8*, ...) @fprintf(i32 %36, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%38 = call i32 @exit(i32 1)
unreachable
39: ; preds = %31
%40 = call i64 @chdir(i8* %9)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %46
42: ; preds = %39
%43 = load i32, i32* @stderr
%44 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %9)
%45 = call i32 @exit(i32 1)
unreachable
46: ; preds = %39
%47 = call i64 @strlen(i8* %12)
%48 = add nsw i64 1, %47
%49 = call i64 @safe_malloc(i8* getelementptr inbounds ([126 x i8], [126 x i8]* @.str.4, i64 0, i64 0), i32 137, i64 %48)
%50 = inttoptr i64 %49 to i8*
%51 = call i8* @strcpy(i8* %50, i8* %12)
%52 = load i8*, i8** %3
ret i8* %51
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"Couldn't get start directory!\0A\00"
@.str.1 = [25 x i8] c"Couldn't cd to %s : %s!\0A\00"
@errno = global i32 0
@.str.2 = [39 x i8] c"Couldn't read absolute src directory!\0A\00"
@.str.3 = [44 x i8] c"Couldn't cd back to starting directory %s!\0A\00"
@.str.4 = [126 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/rc0/spill/refs/heads/master/spill.c\00"
define i8* @normalise_dir(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = alloca i8, i64 %3
%6 = call i32* @getcwd(i8* %4, i32 %2)
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %12
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
%11 = call i32 @exit(i32 1)
unreachable
12: ; preds = %1
%13 = call i64 @chdir(i8* %0)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %12
%16 = load i32, i32* @stderr
%17 = load i32, i32* @errno
%18 = call i8* @strerror(i32 %17)
%19 = call i32 (i32, i8*, ...) @fprintf(i32 %16, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i8* %0, i8* %18)
%20 = call i32 @exit(i32 1)
unreachable
21: ; preds = %12
%22 = load i32, i32* @PATH_MAX
%23 = call i32* @getcwd(i8* %5, i32 %22)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* @stderr
%27 = call i32 (i32, i8*, ...) @fprintf(i32 %26, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.2, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %21
%30 = call i64 @chdir(i8* %4)
%31 = icmp slt i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %29
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.3, i64 0, i64 0), i8* %4)
%35 = call i32 @exit(i32 1)
unreachable
36: ; preds = %29
%37 = call i64 @strlen(i8* %5)
%38 = add nsw i64 %37, 1
%39 = call i64 @safe_malloc(i8* getelementptr inbounds ([126 x i8], [126 x i8]* @.str.4, i64 0, i64 0), i32 137, i64 %38)
%40 = inttoptr i64 %39 to i8*
%41 = call i8* @strcpy(i8* dereferenceable(1) %40, i8* %5)
ret i8* %40
}