#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 */
char *calloc(int, int);
char *dirname(char *);
int /*<<< orphan*/ free(char *);
char *getenv(char *);
char *realpath(char const *, int /*<<< orphan*/ *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *, char *);
char *strdup(char *);
int strlen(char *);
int add_pwd_to_path(const char *file) {
  int ret, len;
  char *abspath, *curdir, *path, *oldpath;
  oldpath = getenv("PATH");
  abspath = realpath(file, NULL);
  if (!abspath)
    goto error;
  curdir = strdup(abspath);
  if (!curdir)
    goto error;
  curdir = dirname(curdir);
  len = strlen(curdir) + strlen(oldpath) + 2;
  path = calloc(sizeof(char), len);
  if (!path)
    goto error;
  sprintf(path, "%s:%s", curdir, oldpath);
  setenv("PATH", path, 1);
  ret = 0;
done:
  free(path);
  free(curdir);
  free(abspath);
  return ret;
error:
  ret = -1;
  goto done;
}


x86 assembly (clang, -O0):
<add_pwd_to_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe5d(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x30(%rbp)
mov    -0x8(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
call   1090 <realpath@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11cd <add_pwd_to_path+0x3d>
jmp    1292 <add_pwd_to_path+0x102>
mov    -0x18(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11ea <add_pwd_to_path+0x5a>
jmp    1292 <add_pwd_to_path+0x102>
mov    -0x20(%rbp),%rdi
call   1070 <dirname@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x34(%rbp),%eax
add    %ecx,%eax
add    $0x2,%eax
mov    %eax,-0x10(%rbp)
mov    -0x10(%rbp),%esi
mov    $0x1,%edi
call   1080 <calloc@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    123a <add_pwd_to_path+0xaa>
jmp    1292 <add_pwd_to_path+0x102>
mov    -0x28(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x30(%rbp),%rcx
lea    0xdb8(%rip),%rsi
call   10a0 <sprintf@plt>
mov    -0x28(%rbp),%rsi
lea    0xda3(%rip),%rdi
mov    $0x1,%edx
call   1050 <setenv@plt>
movl   $0x0,-0xc(%rbp)
mov    -0x28(%rbp),%rdi
call   1040 <free@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x18(%rbp),%rdi
call   1040 <free@plt>
mov    -0xc(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret
movl   $0xffffffff,-0xc(%rbp)
jmp    126e <add_pwd_to_path+0xde>

x86 assembly (clang, -O2):
<add_pwd_to_path>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%rbp
lea    0xe5e(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
xor    %ebx,%ebx
mov    %rbp,%rdi
xor    %esi,%esi
call   1090 <realpath@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   10b0 <strdup@plt>
mov    $0xffffffff,%r12d
test   %rax,%rax
je     1234 <add_pwd_to_path+0xa4>
mov    %rax,%rdi
call   1070 <dirname@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1060 <strlen@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1060 <strlen@plt>
lea    (%rax,%rbp,1),%esi
add    $0x2,%esi
mov    $0x1,%edi
call   1080 <calloc@plt>
xor    %edi,%edi
test   %rax,%rax
je     1236 <add_pwd_to_path+0xa6>
mov    %rax,%rbp
lea    0xdfb(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
mov    %r15,%rcx
call   10a0 <sprintf@plt>
lea    0xde1(%rip),%rdi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <setenv@plt>
mov    %rbp,%rdi
xor    %r12d,%r12d
jmp    1236 <add_pwd_to_path+0xa6>
xor    %edi,%edi
call   1040 <free@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
mov    %r14,%rdi
call   1040 <free@plt>
mov    %r12d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [6 x i8] c"%s:%s\00"
define i32 @add_pwd_to_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i8*
store i8* %0, i8** %2
%9 = call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i8* %9, i8** %8
%10 = load i8*, i8** %2
%11 = call i8* @realpath(i8* %10, i32* null)
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %1
br label %50
15: ; preds = %1
%16 = load i8*, i8** %5
%17 = call i8* @strdup(i8* %16)
store i8* %17, i8** %6
%18 = load i8*, i8** %6
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %15
br label %50
21: ; preds = %15
%22 = load i8*, i8** %6
%23 = call i8* @dirname(i8* %22)
store i8* %23, i8** %6
%24 = load i8*, i8** %6
%25 = call i32 @strlen(i8* %24)
%26 = load i8*, i8** %8
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %25, %27
%29 = add nsw i32 %28, 2
store i32 %29, i32* %4
%30 = load i32, i32* %4
%31 = call i8* @calloc(i32 1, i32 %30)
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = icmp ne i8* %32, null
br i1 %33, label %35, label %34
34: ; preds = %21
br label %50
35: ; preds = %21
%36 = load i8*, i8** %7
%37 = load i8*, i8** %6
%38 = load i8*, i8** %8
%39 = call i32 @sprintf(i8* %36, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %37, i8* %38)
%40 = load i8*, i8** %7
%41 = call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %40, i32 1)
store i32 0, i32* %3
br label %42
42: ; preds = %50, %35
%43 = load i8*, i8** %7
%44 = call i32 @free(i8* %43)
%45 = load i8*, i8** %6
%46 = call i32 @free(i8* %45)
%47 = load i8*, i8** %5
%48 = call i32 @free(i8* %47)
%49 = load i32, i32* %3
ret i32 %49
50: ; preds = %34, %20, %14
store i32 -1, i32* %3
br label %42
}

LLVM IR (-O2):
@.str = [5 x i8] c"PATH\00"
@.str.1 = [6 x i8] c"%s:%s\00"
define i32 @add_pwd_to_path(i8* nocapture %0) {
%2 = tail call i8* @getenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%3 = tail call i8* @realpath(i8* %0, i32* null)
%4 = icmp ne i8* %3, null
tail call void @llvm.assume(i1 %4)
%5 = tail call i8* @strdup(i8* %3)
%6 = icmp eq i8* %5, null
br i1 %6, label %18, label %7
7: ; preds = %1
%8 = tail call i8* @dirname(i8* %5)
%9 = tail call i32 @strlen(i8* %8)
%10 = tail call i32 @strlen(i8* %2)
%11 = add i32 %9, 2
%12 = add i32 %11, %10
%13 = tail call i8* @calloc(i32 1, i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %18, label %15
15: ; preds = %7
%16 = tail call i32 @sprintf(i8* %13, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %2)
%17 = tail call i32 @setenv(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %13, i32 1)
br label %18
18: ; preds = %1, %7, %15
%19 = phi i8* [ %8, %15 ], [ %8, %7 ], [ null, %1 ]
%20 = phi i8* [ %13, %15 ], [ null, %7 ], [ null, %1 ]
%21 = phi i32 [ 0, %15 ], [ -1, %7 ], [ -1, %1 ]
%22 = tail call i32 @free(i8* %20)
%23 = tail call i32 @free(i8* %19)
%24 = tail call i32 @free(i8* %3)
ret i32 %21
}