#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 */
scalar_t__ _chdir(char *);
int errno;
int recover(char *p) {
  int e = errno, slash;
  char *p0;
  while (*p != 0) {
    p0 = ++p;
    do
      p++;
    while (*p != 0 && *p != '/');
    slash = *p;
    *p = 0;
    if (_chdir(p0) < 0)
      return -1;
    *p = slash;
  }
  errno = e;
  return 0;
}


x86 assembly (clang, -O0):
<recover>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2ebd(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     11c4 <recover+0xb4>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x21(%rbp)
je     1179 <recover+0x69>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
setne  %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    1148 <recover+0x38>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <_chdir@plt>
cmp    $0x0,%rax
jge    11b4 <recover+0xa4>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d7 <recover+0xc7>
mov    -0x18(%rbp),%eax
mov    %al,%cl
mov    -0x10(%rbp),%rax
mov    %cl,(%rax)
jmp    1128 <recover+0x18>
mov    -0x14(%rbp),%ecx
mov    0x2e12(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recover>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2ec2(%rip),%r14
mov    (%r14),%ebp
cmpb   $0x0,(%rdi)
je     1169 <recover+0x59>
mov    %rdi,%rbx
nopl   0x0(%rax)
mov    %rbx,%rdi
add    $0x1,%rdi
mov    %rdi,%rbx
nopw   0x0(%rax,%rax,1)
movzbl 0x1(%rbx),%r15d
add    $0x1,%rbx
cmp    $0x2f,%r15b
je     1154 <recover+0x44>
test   %r15b,%r15b
jne    1140 <recover+0x30>
movb   $0x0,(%rbx)
call   1030 <_chdir@plt>
test   %rax,%rax
js     1170 <recover+0x60>
mov    %r15b,(%rbx)
test   %r15b,%r15b
jne    1130 <recover+0x20>
mov    %ebp,(%r14)
xor    %eax,%eax
jmp    1175 <recover+0x65>
mov    $0xffffffff,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @recover(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i32, i32* @errno
store i32 %7, i32* %4
br label %8
8: ; preds = %40, %1
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp ne i32 %11, 0
br i1 %12, label %13, label %44
13: ; preds = %8
%14 = load i8*, i8** %3
%15 = getelementptr inbounds i8, i8* %14, i32 1
store i8* %15, i8** %3
store i8* %15, i8** %6
br label %16
16: ; preds = %29, %13
%17 = load i8*, i8** %3
%18 = getelementptr inbounds i8, i8* %17, i32 1
store i8* %18, i8** %3
br label %19
19: ; preds = %16
%20 = load i8*, i8** %3
%21 = load i8, i8* %20
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %29
24: ; preds = %19
%25 = load i8*, i8** %3
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp ne i32 %27, 47
br label %29
29: ; preds = %24, %19
%30 = phi i1 [ false, %19 ], [ %28, %24 ]
br i1 %30, label %16, label %31
31: ; preds = %29
%32 = load i8*, i8** %3
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
store i32 %34, i32* %5
%35 = load i8*, i8** %3
store i8 0, i8* %35
%36 = load i8*, i8** %6
%37 = call i64 @_chdir(i8* %36)
%38 = icmp slt i64 %37, 0
br i1 %38, label %39, label %40
39: ; preds = %31
store i32 -1, i32* %2
br label %46
40: ; preds = %31
%41 = load i32, i32* %5
%42 = trunc i32 %41 to i8
%43 = load i8*, i8** %3
store i8 %42, i8* %43
br label %8
44: ; preds = %8
%45 = load i32, i32* %4
store i32 %45, i32* @errno
store i32 0, i32* %2
br label %46
46: ; preds = %44, %39
%47 = load i32, i32* %2
ret i32 %47
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @recover(i8* %0) {
%2 = load i32, i32* @errno
%3 = load i8, i8* %0
%4 = icmp eq i8 %3, 0
br i1 %4, label %17, label %5
5: ; preds = %1, %15
%6 = phi i8* [ %10, %15 ], [ %0, %1 ]
%7 = getelementptr inbounds i8, i8* %6, i64 1
br label %8
8: ; preds = %8, %5
%9 = phi i8* [ %7, %5 ], [ %10, %8 ]
%10 = getelementptr inbounds i8, i8* %9, i64 1
%11 = load i8, i8* %10
switch i8 %11, label %8 [
i8 0, label %12
i8 47, label %12
]
12: ; preds = %8, %8
store i8 0, i8* %10
%13 = tail call i64 @_chdir(i8* %7)
%14 = icmp slt i64 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %12
store i8 %11, i8* %10
%16 = icmp eq i8 %11, 0
br i1 %16, label %17, label %5
17: ; preds = %15, %1
store i32 %2, i32* @errno
br label %18
18: ; preds = %12, %17
%19 = phi i32 [ 0, %17 ], [ -1, %12 ]
ret i32 %19
}