#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 */
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
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)
    return 0;
  this_dev = st.st_dev;
  this_ino = st.st_ino;
  if (this_dev == root_dev && this_ino == root_ino) {
    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) == 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    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    11ab <recurser+0x3b>
movq   $0x0,-0x8(%rbp)
jmp    1277 <recurser+0x107>
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    11f4 <recurser+0x84>
mov    -0x48(%rbp),%rax
cmp    -0x28(%rbp),%rax
jne    11f4 <recurser+0x84>
mov    -0x10(%rbp),%rdi
lea    0xe1e(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1277 <recurser+0x107>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x4,%eax
cmp    -0x14(%rbp),%eax
jle    1224 <recurser+0xb4>
mov    0x2dd8(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    1277 <recurser+0x107>
mov    -0x10(%rbp),%rdi
lea    0xdd3(%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    125f <recurser+0xef>
movq   $0x0,-0x8(%rbp)
jmp    1277 <recurser+0x107>
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)
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,%r15
mov    %rdx,%rbp
mov    %esi,%r14d
mov    %rdi,%rbx
lea    0x8(%rsp),%rsi
call   1070 <stat@plt>
test   %rax,%rax
js     121d <recurser+0xad>
mov    0x8(%rsp),%r12
mov    0x10(%rsp),%r13
cmp    %rbp,%r12
jne    11c2 <recurser+0x52>
cmp    %r15,%r13
jne    11c2 <recurser+0x52>
lea    0xe48(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
jmp    121f <recurser+0xaf>
mov    %rbx,%rdi
call   1060 <strlen@plt>
add    $0x4,%eax
cmp    %r14d,%eax
jle    11e2 <recurser+0x72>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    121d <recurser+0xad>
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   1090 <strcat@plt>
mov    %rbx,%rdi
mov    %r14d,%esi
mov    %rbp,%rdx
mov    %r15,%rcx
call   1050 <recurser@plt>
test   %rax,%rax
je     121d <recurser+0xad>
mov    %r12,%rdi
mov    %r13,%rsi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1080 <search_dir@plt>
mov    %rax,%rbx
jmp    121f <recurser+0xaf>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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