#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 */
struct stat {
  scalar_t__ st_dev;
  scalar_t__ st_ino;
};
typedef scalar_t__ ino_t;
typedef scalar_t__ dev_t;

/* Variables and functions */
scalar_t__ EFAULT;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
scalar_t__ errno;
char *search_dir(scalar_t__, scalar_t__, char *, int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *recurser(char *path_buf, int path_size, dev_t root_dev, ino_t root_ino,
               int depth) {
  struct stat st;
  dev_t this_dev;
  ino_t this_ino;
  if (stat(path_buf, &st) < 0) {
    if (errno != EFAULT) {
      __set_errno(ERANGE);
    }
    return 0;
  }
  this_dev = st.st_dev;
  this_ino = st.st_ino;
  if (this_dev == root_dev && this_ino == root_ino) {
    if (path_size < 2) {
      __set_errno(ERANGE);
      return 0;
    }
    strcpy(path_buf, "/");
    return path_buf;
  }
  if (strlen(path_buf) + 4 > path_size) {
    __set_errno(ERANGE);
    return 0;
  }
  strcat(path_buf, "/..");
  if (recurser(path_buf, path_size, root_dev, root_ino, ++depth) == 0)
    return 0;
  return search_dir(this_dev, this_ino, path_buf, path_size);
}


x86 assembly (clang, -O0):
<recurser>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8d,-0x2c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    11d7 <recurser+0x67>
mov    0x2e27(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
je     11ca <recurser+0x5a>
mov    0x2e1d(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <recurser+0x164>
mov    -0x40(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    -0x48(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1245 <recurser+0xd5>
mov    -0x50(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    1245 <recurser+0xd5>
cmpl   $0x2,-0x14(%rbp)
jge    1228 <recurser+0xb8>
mov    0x2dcc(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <recurser+0x164>
mov    -0x10(%rbp),%rdi
lea    0xdcd(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12d4 <recurser+0x164>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x4,%eax
cmp    -0x14(%rbp),%eax
jle    1275 <recurser+0x105>
mov    0x2d7f(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <recurser+0x164>
mov    -0x10(%rbp),%rdi
lea    0xd82(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    -0x2c(%rbp),%r8d
add    $0x1,%r8d
mov    %r8d,-0x2c(%rbp)
call   1050 <recurser@plt>
cmp    $0x0,%rax
jne    12bc <recurser+0x14c>
movq   $0x0,-0x8(%rbp)
jmp    12d4 <recurser+0x164>
mov    -0x48(%rbp),%rdi
mov    -0x50(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1080 <search_dir@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<recurser>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %r8d,%r14d
mov    %rcx,%r15
mov    %rdx,%rbp
mov    %esi,%r12d
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
js     11c7 <recurser+0x57>
mov    0x8(%rsp),%r13
mov    0x10(%rsp),%rax
cmp    %rbp,%r13
jne    11df <recurser+0x6f>
cmp    %r15,%rax
jne    11df <recurser+0x6f>
cmp    $0x1,%r12d
jle    11f3 <recurser+0x83>
lea    0xe43(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
jmp    1203 <recurser+0x93>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2e20(%rip),%rcx
cmp    (%rcx),%rax
jne    11f3 <recurser+0x83>
jmp    1201 <recurser+0x91>
mov    %rax,(%rsp)
mov    %rbx,%rdi
call   1060 <strlen@plt>
add    $0x4,%eax
cmp    %r12d,%eax
jle    1215 <recurser+0xa5>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde6(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
add    $0x1,%r14d
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %rbp,%rdx
mov    %r15,%rcx
mov    %r14d,%r8d
call   1050 <recurser@plt>
test   %rax,%rax
je     1201 <recurser+0x91>
mov    %r13,%rdi
mov    (%rsp),%rsi
mov    %rbx,%rdx
mov    %r12d,%ecx
call   1080 <search_dir@plt>
mov    %rax,%rbx
jmp    1203 <recurser+0x93>

LLVM IR (-O0):
%struct.stat = type { i64, i64 }
@errno = global i64 0
@EFAULT = global i64 0
@ERANGE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [4 x i8] c"/..\00"
define i8* @recurser(i8* %0, i32 %1, i64 %2, i64 %3, i32 %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i64
%11 = alloca i32
%12 = alloca %struct.stat
%13 = alloca i64
%14 = alloca i64
store i8* %0, i8** %7
store i32 %1, i32* %8
store i64 %2, i64* %9
store i64 %3, i64* %10
store i32 %4, i32* %11
%15 = load i8*, i8** %7
%16 = call i64 @stat(i8* %15, %struct.stat* %12)
%17 = icmp slt i64 %16, 0
br i1 %17, label %18, label %26
18: ; preds = %5
%19 = load i64, i64* @errno
%20 = load i64, i64* @EFAULT
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %25
22: ; preds = %18
%23 = load i32, i32* @ERANGE
%24 = call i32 @__set_errno(i32 %23)
br label %25
25: ; preds = %22, %18
store i8* null, i8** %6
br label %75
26: ; preds = %5
%27 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 0
%28 = load i64, i64* %27
store i64 %28, i64* %13
%29 = getelementptr inbounds %struct.stat, %struct.stat* %12, i32 0, i32 1
%30 = load i64, i64* %29
store i64 %30, i64* %14
%31 = load i64, i64* %13
%32 = load i64, i64* %9
%33 = icmp eq i64 %31, %32
br i1 %33, label %34, label %48
34: ; preds = %26
%35 = load i64, i64* %14
%36 = load i64, i64* %10
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %48
38: ; preds = %34
%39 = load i32, i32* %8
%40 = icmp slt i32 %39, 2
br i1 %40, label %41, label %44
41: ; preds = %38
%42 = load i32, i32* @ERANGE
%43 = call i32 @__set_errno(i32 %42)
store i8* null, i8** %6
br label %75
44: ; preds = %38
%45 = load i8*, i8** %7
%46 = call i32 @strcpy(i8* %45, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%47 = load i8*, i8** %7
store i8* %47, i8** %6
br label %75
48: ; preds = %34, %26
%49 = load i8*, i8** %7
%50 = call i32 @strlen(i8* %49)
%51 = add nsw i32 %50, 4
%52 = load i32, i32* %8
%53 = icmp sgt i32 %51, %52
br i1 %53, label %54, label %57
54: ; preds = %48
%55 = load i32, i32* @ERANGE
%56 = call i32 @__set_errno(i32 %55)
store i8* null, i8** %6
br label %75
57: ; preds = %48
%58 = load i8*, i8** %7
%59 = call i32 @strcat(i8* %58, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%60 = load i8*, i8** %7
%61 = load i32, i32* %8
%62 = load i64, i64* %9
%63 = load i64, i64* %10
%64 = load i32, i32* %11
%65 = add nsw i32 %64, 1
store i32 %65, i32* %11
%66 = call i8* @recurser(i8* %60, i32 %61, i64 %62, i64 %63, i32 %65)
%67 = icmp eq i8* %66, null
br i1 %67, label %68, label %69
68: ; preds = %57
store i8* null, i8** %6
br label %75
69: ; preds = %57
%70 = load i64, i64* %13
%71 = load i64, i64* %14
%72 = load i8*, i8** %7
%73 = load i32, i32* %8
%74 = call i8* @search_dir(i64 %70, i64 %71, i8* %72, i32 %73)
store i8* %74, i8** %6
br label %75
75: ; preds = %69, %68, %54, %44, %41, %25
%76 = load i8*, i8** %6
ret i8* %76
}

LLVM IR (-O2):
%struct.stat = type { i64, i64 }
@errno = global i64 0
@EFAULT = global i64 0
@ERANGE = global i32 0
@.str = [2 x i8] c"/\00"
@.str.1 = [4 x i8] c"/..\00"
define i8* @recurser(i8* %0, i32 %1, i64 %2, i64 %3, i32 %4) {
%6 = alloca %struct.stat
%7 = bitcast %struct.stat* %6 to i8*
%8 = call i64 @stat(i8* %0, %struct.stat* %6)
%9 = icmp slt i64 %8, 0
br i1 %9, label %10, label %17
10: ; preds = %5
%11 = load i64, i64* @errno
%12 = load i64, i64* @EFAULT
%13 = icmp eq i64 %11, %12
br i1 %13, label %46, label %14
14: ; preds = %10
%15 = load i32, i32* @ERANGE
%16 = call i32 @__set_errno(i32 %15)
br label %46
17: ; preds = %5
%18 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 0
%19 = load i64, i64* %18
%20 = getelementptr inbounds %struct.stat, %struct.stat* %6, i64 0, i32 1
%21 = load i64, i64* %20
%22 = icmp eq i64 %19, %2
%23 = icmp eq i64 %21, %3
%24 = select i1 %22, i1 %23, i1 false
br i1 %24, label %25, label %32
25: ; preds = %17
%26 = icmp slt i32 %1, 2
br i1 %26, label %27, label %30
27: ; preds = %25
%28 = load i32, i32* @ERANGE
%29 = call i32 @__set_errno(i32 %28)
br label %46
30: ; preds = %25
%31 = call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %46
32: ; preds = %17
%33 = call i32 @strlen(i8* %0)
%34 = add nsw i32 %33, 4
%35 = icmp sgt i32 %34, %1
br i1 %35, label %36, label %39
36: ; preds = %32
%37 = load i32, i32* @ERANGE
%38 = call i32 @__set_errno(i32 %37)
br label %46
39: ; preds = %32
%40 = call i32 @strcat(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%41 = add nsw i32 %4, 1
%42 = call i8* @recurser(i8* %0, i32 %1, i64 %2, i64 %3, i32 %41)
%43 = icmp eq i8* %42, null
br i1 %43, label %46, label %44
44: ; preds = %39
%45 = call i8* @search_dir(i64 %19, i64 %21, i8* %0, i32 %1)
br label %46
46: ; preds = %39, %10, %14, %44, %36, %30, %27
%47 = phi i8* [ null, %27 ], [ %0, %30 ], [ null, %36 ], [ %45, %44 ], [ null, %14 ], [ null, %10 ], [ null, %39 ]
ret i8* %47
}