#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*/ OPENSSL_free(char *);
char *OPENSSL_strdup(char const *);
int chdir(char *);
char *strpbrk(char *, char *);
int /*<<< orphan*/ test_note(char *, char *);
int change_path(const char *file) {
  char *s = OPENSSL_strdup(file);
  char *p = s;
  char *last = NULL;
  int ret;
  if (s == NULL)
    return -1;
  while ((p = strpbrk(p, "/")) != NULL) {
    last = p++;
  }
  if (last == NULL)
    return 0;
  last[0] = 0;
  test_note("changing path to %s", s);
  ret = chdir(s);
  OPENSSL_free(s);
  return ret;
}


x86 assembly (clang, -O0):
<change_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <OPENSSL_strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1190 <change_path+0x40>
movl   $0xffffffff,-0x4(%rbp)
jmp    1214 <change_path+0xc4>
jmp    1195 <change_path+0x45>
mov    -0x20(%rbp),%rdi
lea    0xe60(%rip),%rsi
call   1050 <strpbrk@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11cb <change_path+0x7b>
mov    -0x20(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,-0x28(%rbp)
jmp    1195 <change_path+0x45>
cmpq   $0x0,-0x28(%rbp)
jne    11e2 <change_path+0x92>
movl   $0x0,-0x4(%rbp)
jmp    1214 <change_path+0xc4>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x18(%rbp),%rsi
lea    0xe0e(%rip),%rdi
call   1060 <test_note@plt>
mov    -0x18(%rbp),%rdi
call   1030 <chdir@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x18(%rbp),%rdi
call   1070 <OPENSSL_free@plt>
mov    -0x2c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<change_path>:
push   %rbp
push   %rbx
push   %rax
call   1050 <OPENSSL_strdup@plt>
test   %rax,%rax
je     11b4 <change_path+0x64>
mov    %rax,%rbx
xor    %eax,%eax
mov    %rbx,%rdi
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rax,%rbp
mov    $0x2f,%esi
call   1040 <strchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
jne    1170 <change_path+0x20>
test   %rbp,%rbp
je     11bb <change_path+0x6b>
movb   $0x0,0x0(%rbp)
lea    0xe6a(%rip),%rdi
mov    %rbx,%rsi
call   1060 <test_note@plt>
mov    %rbx,%rdi
call   1030 <chdir@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1070 <OPENSSL_free@plt>
mov    %ebp,%eax
jmp    11bd <change_path+0x6d>
mov    $0xffffffff,%eax
jmp    11bd <change_path+0x6d>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"/\00"
@.str.1 = [20 x i8] c"changing path to %s\00"
define i32 @change_path(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i8*, i8** %3
%9 = call i8* @OPENSSL_strdup(i8* %8)
store i8* %9, i8** %4
%10 = load i8*, i8** %4
store i8* %10, i8** %5
store i8* null, i8** %6
%11 = load i8*, i8** %4
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %36
14: ; preds = %1
br label %15
15: ; preds = %19, %14
%16 = load i8*, i8** %5
%17 = call i8* @strpbrk(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %5
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %22
19: ; preds = %15
%20 = load i8*, i8** %5
%21 = getelementptr inbounds i8, i8* %20, i32 1
store i8* %21, i8** %5
store i8* %20, i8** %6
br label %15
22: ; preds = %15
%23 = load i8*, i8** %6
%24 = icmp eq i8* %23, null
br i1 %24, label %25, label %26
25: ; preds = %22
store i32 0, i32* %2
br label %36
26: ; preds = %22
%27 = load i8*, i8** %6
%28 = getelementptr inbounds i8, i8* %27, i64 0
store i8 0, i8* %28
%29 = load i8*, i8** %4
%30 = call i32 @test_note(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %29)
%31 = load i8*, i8** %4
%32 = call i32 @chdir(i8* %31)
store i32 %32, i32* %7
%33 = load i8*, i8** %4
%34 = call i32 @OPENSSL_free(i8* %33)
%35 = load i32, i32* %7
store i32 %35, i32* %2
br label %36
36: ; preds = %26, %25, %13
%37 = load i32, i32* %2
ret i32 %37
}

LLVM IR (-O2):
@.str.1 = [20 x i8] c"changing path to %s\00"
define i32 @change_path(i8* %0) {
%2 = tail call i8* @OPENSSL_strdup(i8* %0)
%3 = icmp eq i8* %2, null
br i1 %3, label %16, label %4
4: ; preds = %1, %4
%5 = phi i8* [ %9, %4 ], [ %2, %1 ]
%6 = phi i8* [ %7, %4 ], [ null, %1 ]
%7 = tail call i8* @strchr(i8* dereferenceable(1) %5, i32 47)
%8 = icmp eq i8* %7, null
%9 = getelementptr inbounds i8, i8* %7, i64 1
br i1 %8, label %10, label %4
10: ; preds = %4
%11 = icmp eq i8* %6, null
br i1 %11, label %16, label %12
12: ; preds = %10
store i8 0, i8* %6
%13 = tail call i32 @test_note(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%14 = tail call i32 @chdir(i8* %2)
%15 = tail call i32 @OPENSSL_free(i8* %2)
br label %16
16: ; preds = %10, %1, %12
%17 = phi i32 [ %14, %12 ], [ -1, %1 ], [ 0, %10 ]
ret i32 %17
}