#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 /*<<< orphan*/ *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
char *strpbrk(char const *, char *);
char *osys_path_chdir(char *buffer, size_t bufsize, const char *old_path,
                      const char *new_dirname) {
  const char *path, *ptr;
  size_t dirlen;
  path = old_path;
  for (;;) {
    ptr = strpbrk(path, "/");
    if (ptr == NULL)
      break;
    path = ptr + 1;
  }
  dirlen = strlen(new_dirname);
  if (dirlen + strlen(path) + 2 >= bufsize)
    return NULL;
  if (dirlen > 0) {
    strcpy(buffer, new_dirname);
    {
      if (strchr("/", buffer[dirlen - 1]) == NULL)
        buffer[dirlen++] = '/';
    }
  }
  strcpy(buffer + dirlen, path);
  return buffer;
}


x86 assembly (clang, -O0):
<osys_path_chdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xe95(%rip),%rsi
call   1060 <strpbrk@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jne    1184 <osys_path_chdir+0x44>
jmp    1195 <osys_path_chdir+0x55>
mov    -0x38(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1160 <osys_path_chdir+0x20>
mov    -0x28(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x40(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
mov    -0x48(%rbp),%rax
add    %rcx,%rax
add    $0x2,%rax
cmp    -0x18(%rbp),%rax
jb     11d8 <osys_path_chdir+0x98>
movq   $0x0,-0x8(%rbp)
jmp    124b <osys_path_chdir+0x10b>
cmpq   $0x0,-0x40(%rbp)
jbe    1232 <osys_path_chdir+0xf2>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    $0x1,%rcx
lea    0xdfd(%rip),%rdi
movsbl (%rax,%rcx,1),%esi
call   1050 <strchr@plt>
cmp    $0x0,%rax
jne    122d <osys_path_chdir+0xed>
mov    -0x10(%rbp),%rax
mov    -0x40(%rbp),%rcx
mov    %rcx,%rdx
add    $0x1,%rdx
mov    %rdx,-0x40(%rbp)
movb   $0x2f,(%rax,%rcx,1)
jmp    1232 <osys_path_chdir+0xf2>
mov    -0x10(%rbp),%rdi
add    -0x40(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<osys_path_chdir>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rcx,%r12
mov    %rsi,%r13
mov    %rdi,%r14
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rdx,%rbx
mov    %rdx,%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
lea    0x1(%rax),%rdx
test   %rax,%rax
jne    1150 <osys_path_chdir+0x20>
mov    %r12,%rdi
call   1040 <strlen@plt>
mov    %rax,%r15
mov    %rbx,%rdi
call   1040 <strlen@plt>
add    %r15,%rax
add    $0x2,%rax
cmp    %r13,%rax
jae    11ba <osys_path_chdir+0x8a>
test   %r15,%r15
je     11bf <osys_path_chdir+0x8f>
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <strcpy@plt>
movsbl -0x1(%r15,%r14,1),%esi
lea    0xe5b(%rip),%rdi
call   1050 <strchr@plt>
test   %rax,%rax
jne    11c2 <osys_path_chdir+0x92>
movb   $0x2f,(%r14,%r15,1)
add    $0x1,%r15
jmp    11c2 <osys_path_chdir+0x92>
xor    %r14d,%r14d
jmp    11d0 <osys_path_chdir+0xa0>
xor    %r15d,%r15d
add    %r14,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
define i8* @osys_path_chdir(i8* %0, i64 %1, i8* %2, i8* %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i8*
%12 = alloca i64
store i8* %0, i8** %6
store i64 %1, i64* %7
store i8* %2, i8** %8
store i8* %3, i8** %9
%13 = load i8*, i8** %8
store i8* %13, i8** %10
br label %14
14: ; preds = %20, %4
%15 = load i8*, i8** %10
%16 = call i8* @strpbrk(i8* %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %16, i8** %11
%17 = load i8*, i8** %11
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %14
br label %23
20: ; preds = %14
%21 = load i8*, i8** %11
%22 = getelementptr inbounds i8, i8* %21, i64 1
store i8* %22, i8** %10
br label %14
23: ; preds = %19
%24 = load i8*, i8** %9
%25 = call i64 @strlen(i8* %24)
store i64 %25, i64* %12
%26 = load i64, i64* %12
%27 = load i8*, i8** %10
%28 = call i64 @strlen(i8* %27)
%29 = add i64 %26, %28
%30 = add i64 %29, 2
%31 = load i64, i64* %7
%32 = icmp uge i64 %30, %31
br i1 %32, label %33, label %34
33: ; preds = %23
store i8* null, i8** %5
br label %61
34: ; preds = %23
%35 = load i64, i64* %12
%36 = icmp ugt i64 %35, 0
br i1 %36, label %37, label %54
37: ; preds = %34
%38 = load i8*, i8** %6
%39 = load i8*, i8** %9
%40 = call i32 @strcpy(i8* %38, i8* %39)
%41 = load i8*, i8** %6
%42 = load i64, i64* %12
%43 = sub i64 %42, 1
%44 = getelementptr inbounds i8, i8* %41, i64 %43
%45 = load i8, i8* %44
%46 = call i32* @strchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8 signext %45)
%47 = icmp eq i32* %46, null
br i1 %47, label %48, label %53
48: ; preds = %37
%49 = load i8*, i8** %6
%50 = load i64, i64* %12
%51 = add i64 %50, 1
store i64 %51, i64* %12
%52 = getelementptr inbounds i8, i8* %49, i64 %50
store i8 47, i8* %52
br label %53
53: ; preds = %48, %37
br label %54
54: ; preds = %53, %34
%55 = load i8*, i8** %6
%56 = load i64, i64* %12
%57 = getelementptr inbounds i8, i8* %55, i64 %56
%58 = load i8*, i8** %10
%59 = call i32 @strcpy(i8* %57, i8* %58)
%60 = load i8*, i8** %6
store i8* %60, i8** %5
br label %61
61: ; preds = %54, %33
%62 = load i8*, i8** %5
ret i8* %62
}

LLVM IR (-O2):
@.str = [2 x i8] c"/\00"
define i8* @osys_path_chdir(i8* %0, i64 %1, i8* %2, i8* %3) {
br label %5
5: ; preds = %5, %4
%6 = phi i8* [ %2, %4 ], [ %9, %5 ]
%7 = tail call i8* bitcast (i32* (i8*, i8)* @strchr to i8* (i8*, i32)*)(i8* %6, i32 47)
%8 = icmp eq i8* %7, null
%9 = getelementptr inbounds i8, i8* %7, i64 1
br i1 %8, label %10, label %5
10: ; preds = %5
%11 = tail call i64 @strlen(i8* dereferenceable(1) %3)
%12 = tail call i64 @strlen(i8* dereferenceable(1) %6)
%13 = add i64 %11, 2
%14 = add i64 %13, %12
%15 = icmp ult i64 %14, %1
br i1 %15, label %16, label %32
16: ; preds = %10
%17 = icmp eq i64 %11, 0
br i1 %17, label %28, label %18
18: ; preds = %16
%19 = tail call i32 @strcpy(i8* %0, i8* %3)
%20 = add i64 %11, -1
%21 = getelementptr inbounds i8, i8* %0, i64 %20
%22 = load i8, i8* %21
%23 = tail call i32* @strchr(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8 signext %22)
%24 = icmp eq i32* %23, null
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = add i64 %11, 1
%27 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 47, i8* %27
br label %28
28: ; preds = %18, %25, %16
%29 = phi i64 [ %26, %25 ], [ %11, %18 ], [ 0, %16 ]
%30 = getelementptr inbounds i8, i8* %0, i64 %29
%31 = tail call i32 @strcpy(i8* %30, i8* %6)
br label %32
32: ; preds = %10, %28
%33 = phi i8* [ %0, %28 ], [ null, %10 ]
ret i8* %33
}