#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) {
  struct stat st;
  dev_t this_dev;
  ino_t this_ino;
  if (stat(path_buf, &st) < 0) {
    if (errno != EFAULT)
      goto oops;
    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) {
      goto oops;
    }
    strcpy(path_buf, "/");
    return path_buf;
  }
  if (strlen(path_buf) + 4 > path_size) {
    goto oops;
  }
  strcat(path_buf, "/..");
  if (recurser(path_buf, path_size, root_dev, root_ino) == 0)
    return 0;
  return search_dir(this_dev, this_ino, path_buf, path_size);
oops:
  __set_errno(ERANGE);
  return 0;
}


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    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    11ca <recurser+0x5a>
mov    0x2e2b(%rip),%rax
mov    (%rax),%rax
mov    0x2e49(%rip),%rcx
cmp    (%rcx),%rax
je     11bd <recurser+0x4d>
jmp    1294 <recurser+0x124>
movq   $0x0,-0x8(%rbp)
jmp    12aa <recurser+0x13a>
mov    -0x38(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x30(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rax
cmp    -0x20(%rbp),%rax
jne    1222 <recurser+0xb2>
mov    -0x48(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    1222 <recurser+0xb2>
cmpl   $0x2,-0x14(%rbp)
jge    1205 <recurser+0x95>
jmp    1294 <recurser+0x124>
mov    -0x10(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12aa <recurser+0x13a>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x4,%eax
cmp    -0x14(%rbp),%eax
jle    123c <recurser+0xcc>
jmp    1294 <recurser+0x124>
mov    -0x10(%rbp),%rdi
lea    0xdbb(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
call   1050 <recurser@plt>
cmp    $0x0,%rax
jne    1277 <recurser+0x107>
movq   $0x0,-0x8(%rbp)
jmp    12aa <recurser+0x13a>
mov    -0x40(%rbp),%rdi
mov    -0x48(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
call   1080 <search_dir@plt>
mov    %rax,-0x8(%rbp)
jmp    12aa <recurser+0x13a>
mov    0x2d45(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-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    %rcx,%r14
mov    %rdx,%rbp
mov    %esi,%r15d
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
js     11c4 <recurser+0x54>
mov    0x8(%rsp),%r12
mov    0x10(%rsp),%r13
cmp    %rbp,%r12
jne    11dc <recurser+0x6c>
cmp    %r14,%r13
jne    11dc <recurser+0x6c>
cmp    $0x2,%r15d
jl     11ec <recurser+0x7c>
lea    0xe46(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
jmp    11fc <recurser+0x8c>
mov    0x2e05(%rip),%rax
mov    (%rax),%rax
mov    0x2e23(%rip),%rcx
cmp    (%rcx),%rax
jne    11ec <recurser+0x7c>
jmp    11fa <recurser+0x8a>
mov    %rbx,%rdi
call   1060 <strlen@plt>
add    $0x4,%eax
cmp    %r15d,%eax
jle    120e <recurser+0x9e>
mov    0x2ded(%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    0xded(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r15d,%esi
mov    %rbp,%rdx
mov    %r14,%rcx
call   1050 <recurser@plt>
test   %rax,%rax
je     11fa <recurser+0x8a>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r15d,%ecx
call   1080 <search_dir@plt>
mov    %rax,%rbx
jmp    11fc <recurser+0x8c>

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

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