#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 *);
char *dirname(char *);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ realpath(char *, char *);
char *strdup(char *);
char *working_dir(char *script_path) {
  char resolved_path[PATH_MAX];
  if (!realpath(script_path, resolved_path)) {
    perror("realpath");
    exit(1);
  }
  char *dir = dirname(strdup(resolved_path));
  if (chdir(dir) < 0) {
    perror("chdir");
    exit(1);
  }
  return strdup(resolved_path);
}


x86 assembly (clang, -O0):
<working_dir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x28(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <realpath@plt>
cmp    $0x0,%eax
jne    11c0 <working_dir+0x60>
lea    0xe4f(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1080 <strdup@plt>
mov    %rax,%rdi
call   1040 <dirname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <chdir@plt>
cmp    $0x0,%rax
jge    11fe <working_dir+0x9e>
lea    0xe1a(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
call   1080 <strdup@plt>
mov    -0x10(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<working_dir>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rsi
call   1050 <realpath@plt>
test   %eax,%eax
je     11b8 <working_dir+0x58>
mov    %rbx,%rdi
call   1080 <strdup@plt>
mov    %rax,%rdi
call   1040 <dirname@plt>
mov    %rax,%rdi
call   1030 <chdir@plt>
test   %rax,%rax
js     11c1 <working_dir+0x61>
mov    %rbx,%rdi
call   1080 <strdup@plt>
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe41(%rip),%rdi
jmp    11c8 <working_dir+0x68>
lea    0xe41(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [9 x i8] c"realpath\00"
@.str.1 = [6 x i8] c"chdir\00"
define i8* @working_dir(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
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 i8*, i8** %2
%11 = call i32 @realpath(i8* %10, i8* %9)
%12 = icmp ne i32 %11, 0
br i1 %12, label %16, label %13
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %1
%17 = call i8* @strdup(i8* %9)
%18 = call i8* @dirname(i8* %17)
store i8* %18, i8** %5
%19 = load i8*, i8** %5
%20 = call i64 @chdir(i8* %19)
%21 = icmp slt i64 %20, 0
br i1 %21, label %22, label %25
22: ; preds = %16
%23 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %16
%26 = call i8* @strdup(i8* %9)
%27 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [9 x i8] c"realpath\00"
@.str.1 = [6 x i8] c"chdir\00"
define noalias i8* @working_dir(i8* nocapture %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32 @realpath(i8* %0, i8* %4)
%6 = icmp eq i32 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %1
%11 = call i8* @strdup(i8* %4)
%12 = call i8* @dirname(i8* %11)
%13 = call i64 @chdir(i8* %12)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %18
15: ; preds = %10
%16 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
%17 = call i32 @exit(i32 1)
unreachable
18: ; preds = %10
%19 = call i8* @strdup(i8* %4)
ret i8* %19
}