#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 scalar_t__ pid_t;

/* Variables and functions */
int MAXPATHLEN;
int /*<<< orphan*/ execl(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ getcwd(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stderr;
int strlen(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
int main() {
  pid_t pid;
  pid = fork();
  if (pid < 0) {
    fprintf(stderr, "could not fork");
    exit(-1);
  } else if (pid == 0) {
    char current_path[MAXPATHLEN];
    char *relative_executable_path = "MacOs/Punakea";
    char *not_needed_string = "Resources";
    getcwd(current_path, MAXPATHLEN);
    int base_path_length = strlen(current_path) - strlen(not_needed_string);
    char final_path[base_path_length + strlen(relative_executable_path)];
    int i;
    for (i = 0; i < base_path_length; i++)
      final_path[i] = current_path[i];
    int j;
    for (j = 0; j < strlen(relative_executable_path); j++) {
      i++;
      final_path[i] = relative_executable_path[j];
    }
    printf("path: %s\n", final_path);
    execl(final_path, NULL);
    printf("Punakea started\n");
  } else {
    wait(NULL);
    printf("done\n");
    exit(0);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11c4 <main+0x44>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
lea    0xe4b(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%edi
call   1070 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1329 <main+0x1a9>
mov    0x2e12(%rip),%rax
mov    (%rax),%ecx
mov    %rsp,%rdx
mov    %rdx,-0x18(%rbp)
mov    %rcx,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rdi
sub    %rdx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rcx,-0x20(%rbp)
lea    0xe0d(%rip),%rcx
mov    %rcx,-0x28(%rbp)
lea    0xe10(%rip),%rcx
mov    %rcx,-0x30(%rbp)
mov    (%rax),%esi
call   1050 <getcwd@plt>
mov    -0x60(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x58(%rbp)
mov    -0x30(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x58(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x34(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x54(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x54(%rbp),%eax
add    %ecx,%eax
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x44(%rbp)
mov    -0x44(%rbp),%eax
cmp    -0x34(%rbp),%eax
jge    12a0 <main+0x120>
mov    -0x50(%rbp),%rax
mov    -0x60(%rbp),%rcx
movslq -0x44(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x44(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x44(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44(%rbp)
jmp    1270 <main+0xf0>
movl   $0x0,-0x48(%rbp)
mov    -0x48(%rbp),%eax
mov    %eax,-0x64(%rbp)
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x64(%rbp),%eax
cmp    %ecx,%eax
jge    12f0 <main+0x170>
mov    -0x50(%rbp),%rax
mov    -0x44(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x44(%rbp)
mov    -0x28(%rbp),%rcx
movslq -0x48(%rbp),%rdx
mov    (%rcx,%rdx,1),%dl
movslq -0x44(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x48(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x48(%rbp)
jmp    12a7 <main+0x127>
mov    -0x50(%rbp),%rsi
lea    0xd2c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1080 <execl@plt>
lea    0xd1b(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
jmp    1347 <main+0x1c7>
xor    %eax,%eax
mov    %eax,%edi
call   1090 <wait@plt>
lea    0xd09(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %edi,%edi
call   1070 <exit@plt>
jmp    134c <main+0x1cc>
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
js     12c4 <main+0x124>
jne    12e3 <main+0x143>
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r13
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
mov    %r13,%rdi
call   1060 <getcwd@plt>
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %eax,%r15d
lea    0xe22(%rip),%rdi
call   1040 <strlen@plt>
mov    %eax,%r12d
mov    %r15d,%ebx
sub    %eax,%ebx
lea    0xe00(%rip),%rdi
call   1040 <strlen@plt>
add    %ebx,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%r14
sub    %rax,%r14
mov    %r14,%rsp
test   %ebx,%ebx
jle    1248 <main+0xa8>
not    %r12d
add    %r12d,%r15d
add    $0x1,%r15
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r15,%rdx
call   1080 <memcpy@plt>
mov    %r15d,%ebx
jmp    124a <main+0xaa>
xor    %ebx,%ebx
lea    0xdbe(%rip),%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    128c <main+0xec>
lea    (%rbx,%r14,1),%r12
add    $0x1,%r12
xor    %ebx,%ebx
nopw   0x0(%rax,%rax,1)
movzbl (%rbx,%r15,1),%eax
mov    %al,(%r12,%rbx,1)
add    $0x1,%rbx
mov    %r15,%rdi
call   1040 <strlen@plt>
cltq
cmp    %rax,%rbx
jl     1270 <main+0xd0>
lea    0xd94(%rip),%rdi
mov    %r14,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %r14,%rdi
xor    %esi,%esi
call   10a0 <execl@plt>
lea    0xd88(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2d0d(%rip),%rax
mov    (%rax),%edi
lea    0xd2c(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0xffffffff,%edi
call   1090 <exit@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
lea    0xd40(%rip),%rdi
call   1030 <puts@plt>
xor    %edi,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [15 x i8] c"could not fork\00"
@MAXPATHLEN = global i32 0
@.str.1 = [14 x i8] c"MacOs/Punakea\00"
@.str.2 = [10 x i8] c"Resources\00"
@.str.3 = [10 x i8] c"path: %s\0A\00"
@.str.4 = [17 x i8] c"Punakea started\0A\00"
@.str.5 = [6 x i8] c"done\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i64
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %1
%11 = call i64 (...) @fork()
store i64 %11, i64* %2
%12 = load i64, i64* %2
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %18
14: ; preds = %0
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%17 = call i32 @exit(i32 -1)
unreachable
18: ; preds = %0
%19 = load i64, i64* %2
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %78
21: ; preds = %18
%22 = load i32, i32* @MAXPATHLEN
%23 = zext i32 %22 to i64
%24 = call i8* @llvm.stacksave()
store i8* %24, i8** %3
%25 = alloca i8, i64 %23
store i64 %23, i64* %4
store i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8** %5
store i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8** %6
%26 = load i32, i32* @MAXPATHLEN
%27 = call i32 @getcwd(i8* %25, i32 %26)
%28 = call i32 @strlen(i8* %25)
%29 = load i8*, i8** %6
%30 = call i32 @strlen(i8* %29)
%31 = sub nsw i32 %28, %30
store i32 %31, i32* %7
%32 = load i32, i32* %7
%33 = load i8*, i8** %5
%34 = call i32 @strlen(i8* %33)
%35 = add nsw i32 %32, %34
%36 = zext i32 %35 to i64
%37 = alloca i8, i64 %36
store i64 %36, i64* %8
store i32 0, i32* %9
br label %38
38: ; preds = %50, %21
%39 = load i32, i32* %9
%40 = load i32, i32* %7
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %53
42: ; preds = %38
%43 = load i32, i32* %9
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %25, i64 %44
%46 = load i8, i8* %45
%47 = load i32, i32* %9
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %37, i64 %48
store i8 %46, i8* %49
br label %50
50: ; preds = %42
%51 = load i32, i32* %9
%52 = add nsw i32 %51, 1
store i32 %52, i32* %9
br label %38
53: ; preds = %38
store i32 0, i32* %10
br label %54
54: ; preds = %70, %53
%55 = load i32, i32* %10
%56 = load i8*, i8** %5
%57 = call i32 @strlen(i8* %56)
%58 = icmp slt i32 %55, %57
br i1 %58, label %59, label %73
59: ; preds = %54
%60 = load i32, i32* %9
%61 = add nsw i32 %60, 1
store i32 %61, i32* %9
%62 = load i8*, i8** %5
%63 = load i32, i32* %10
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
%66 = load i8, i8* %65
%67 = load i32, i32* %9
%68 = sext i32 %67 to i64
%69 = getelementptr inbounds i8, i8* %37, i64 %68
store i8 %66, i8* %69
br label %70
70: ; preds = %59
%71 = load i32, i32* %10
%72 = add nsw i32 %71, 1
store i32 %72, i32* %10
br label %54
73: ; preds = %54
%74 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %37)
%75 = call i32 @execl(i8* %37, i32* null)
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.4, i64 0, i64 0))
%77 = load i8*, i8** %3
br label %82
78: ; preds = %18
%79 = call i32 @wait(i32* null)
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%81 = call i32 @exit(i32 0)
unreachable
82: ; preds = %73
br label %83
83: ; preds = %82
%84 = load i32, i32* %1
ret i32 %84
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [15 x i8] c"could not fork\00"
@MAXPATHLEN = global i32 0
@.str.1 = [14 x i8] c"MacOs/Punakea\00"
@.str.2 = [10 x i8] c"Resources\00"
@.str.3 = [10 x i8] c"path: %s\0A\00"
@str = [5 x i8] c"done\00"
@str.6 = [16 x i8] c"Punakea started\00"
define i32 @main() {
%1 = tail call i64 (...) @fork()
%2 = icmp slt i64 %1, 0
br i1 %2, label %3, label %7
3: ; preds = %0
%4 = load i32, i32* @stderr
%5 = tail call i32 @fprintf(i32 %4, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%6 = tail call i32 @exit(i32 -1)
unreachable
7: ; preds = %0
%8 = icmp eq i64 %1, 0
br i1 %8, label %9, label %47
9: ; preds = %7
%10 = load i32, i32* @MAXPATHLEN
%11 = zext i32 %10 to i64
%12 = alloca i8, i64 %11
%13 = call i32 @getcwd(i8* %12, i32 %10)
%14 = call i32 @strlen(i8* %12)
%15 = call i32 @strlen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0))
%16 = sub i32 %14, %15
%17 = call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%18 = add nsw i32 %16, %17
%19 = zext i32 %18 to i64
%20 = alloca i8, i64 %19
%21 = icmp sgt i32 %16, 0
br i1 %21, label %22, label %28
22: ; preds = %9
%23 = xor i32 %15, -1
%24 = add i32 %14, %23
%25 = zext i32 %24 to i64
%26 = add nuw nsw i64 %25, 1
%27 = and i64 %26, 4294967295
br label %28
28: ; preds = %22, %9
%29 = phi i64 [ 0, %9 ], [ %27, %22 ]
%30 = call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp sgt i32 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %28, %32
%33 = phi i64 [ %35, %32 ], [ %29, %28 ]
%34 = phi i64 [ %39, %32 ], [ 0, %28 ]
%35 = add nuw nsw i64 %33, 1
%36 = getelementptr inbounds [14 x i8], [14 x i8]* @.str.1, i64 0, i64 %34
%37 = load i8, i8* %36
%38 = getelementptr inbounds i8, i8* %20, i64 %35
store i8 %37, i8* %38
%39 = add nuw nsw i64 %34, 1
%40 = call i32 @strlen(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0))
%41 = sext i32 %40 to i64
%42 = icmp slt i64 %39, %41
br i1 %42, label %32, label %43
43: ; preds = %32, %28
%44 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i8* %20)
%45 = call i32 @execl(i8* %20, i32* null)
%46 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @str.6, i64 0, i64 0))
ret i32 0
47: ; preds = %7
%48 = tail call i32 @wait(i32* null)
%49 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%50 = tail call i32 @exit(i32 0)
unreachable
}