#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_ino;
};
struct dirent {
  scalar_t__ d_ino;
  char *d_name;
};
typedef scalar_t__ ino_t;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
int /*<<< orphan*/ chdir(char *);
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int /*<<< orphan*/ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
size_t strlen(char *);
char *pwd() {
  char lpwd[500], lpwd2[500], ret[500];
  DIR *curdir;
  int aaa = 0;
  ino_t myinod, thisinod;
  struct dirent *direntp;
  struct stat info;
  while (1) {
    stat(".", &info);
    myinod = info.st_ino;
    stat("..", &info);
    thisinod = info.st_ino;
    if (myinod == thisinod) {
      if (lpwd[strlen(lpwd) - 1] != '/') {
        lpwd[strlen(lpwd) - 1] = 0;
      }
      ret[0] = '/';
      ret[1] = 0;
      strcat(ret, lpwd);
      chdir(ret);
      return ret;
    }
    chdir("..");
    curdir = opendir(".");
    while ((direntp = readdir(curdir)) != NULL) {
      if (direntp->d_ino == myinod) {
        strcpy(lpwd2, direntp->d_name);
        lpwd2[strlen(direntp->d_name)] = 0;
        strcat(lpwd2, "/");
        strcat(lpwd2, lpwd);
        strcpy(lpwd, lpwd2);
      }
    }
    closedir(curdir);
  }
}


x86 assembly (clang, -O0):
<pwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x630,%rsp
movl   $0x0,-0x60c(%rbp)
lea    0xe65(%rip),%rdi
lea    -0x630(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x630(%rbp),%rax
mov    %rax,-0x618(%rbp)
lea    0xe43(%rip),%rdi
lea    -0x630(%rbp),%rsi
call   1080 <stat@plt>
mov    -0x630(%rbp),%rax
mov    %rax,-0x620(%rbp)
mov    -0x618(%rbp),%rax
cmp    -0x620(%rbp),%rax
jne    1261 <pwd+0xe1>
lea    -0x200(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%rax
movsbl -0x200(%rbp,%rax,1),%eax
ff
cmp    $0x2f,%eax
je     1224 <pwd+0xa4>
lea    -0x200(%rbp),%rdi
call   1050 <strlen@plt>
sub    $0x1,%rax
movb   $0x0,-0x200(%rbp,%rax,1)
movb   $0x2f,-0x600(%rbp)
movb   $0x0,-0x5ff(%rbp)
lea    -0x600(%rbp),%rdi
lea    -0x200(%rbp),%rsi
call   10a0 <strcat@plt>
lea    -0x600(%rbp),%rdi
call   1060 <chdir@plt>
lea    -0x600(%rbp),%rax
add    $0x630,%rsp
pop    %rbp
ret
lea    0xd98(%rip),%rdi
call   1060 <chdir@plt>
lea    0xd8d(%rip),%rdi
call   1040 <opendir@plt>
mov    %rax,-0x608(%rbp)
mov    -0x608(%rbp),%rdi
call   1090 <readdir@plt>
mov    %rax,-0x628(%rbp)
cmp    $0x0,%rax
je     1321 <pwd+0x1a1>
mov    -0x628(%rbp),%rax
mov    (%rax),%rax
cmp    -0x618(%rbp),%rax
jne    131c <pwd+0x19c>
lea    -0x400(%rbp),%rdi
mov    -0x628(%rbp),%rax
mov    0x8(%rax),%rsi
call   1030 <strcpy@plt>
mov    -0x628(%rbp),%rax
mov    0x8(%rax),%rdi
call   1050 <strlen@plt>
movb   $0x0,-0x400(%rbp,%rax,1)
lea    -0x400(%rbp),%rdi
lea    0xd12(%rip),%rsi
call   10a0 <strcat@plt>
lea    -0x400(%rbp),%rdi
lea    -0x200(%rbp),%rsi
call   10a0 <strcat@plt>
lea    -0x200(%rbp),%rdi
lea    -0x400(%rbp),%rsi
call   1030 <strcpy@plt>
jmp    1280 <pwd+0x100>
mov    -0x608(%rbp),%rdi
call   1070 <closedir@plt>
jmp    1195 <pwd+0x15>

x86 assembly (clang, -O2):
<pwd>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x608,%rsp
lea    0xe69(%rip),%rdi
lea    0x8(%rsp),%rbx
mov    %rbx,%rsi
call   1080 <stat@plt>
mov    0x8(%rsp),%r12
lea    0xe4f(%rip),%rdi
mov    %rbx,%rsi
call   1080 <stat@plt>
cmp    0x8(%rsp),%r12
jne    1215 <pwd+0x95>
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
call   1050 <strlen@plt>
cmpb   $0x2f,-0x1(%rax,%rbx,1)
je     11d9 <pwd+0x59>
movb   $0x0,0xf(%rsp,%rax,1)
movw   $0x2f,0x210(%rsp)
lea    0x210(%rsp),%rbx
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
call   10a0 <strcat@plt>
mov    %rbx,%rdi
call   1060 <chdir@plt>
mov    %rbx,%rax
add    $0x608,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xde4(%rip),%r14
lea    0xdde(%rip),%r15
lea    0x410(%rsp),%rbx
lea    0x10(%rsp),%r13
jmp    127d <pwd+0xfd>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %r14,%rdi
call   1070 <closedir@plt>
mov    %rbp,%r15
mov    %rbp,%rdi
lea    0x8(%rsp),%rbp
mov    %rbp,%rsi
call   1080 <stat@plt>
mov    0x8(%rsp),%r12
lea    0xd99(%rip),%r14
mov    %r14,%rdi
mov    %rbp,%rsi
call   1080 <stat@plt>
cmp    0x8(%rsp),%r12
je     11c0 <pwd+0x40>
mov    %r14,%rdi
call   1060 <chdir@plt>
mov    %r15,%rbp
mov    %r15,%rdi
call   1040 <opendir@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1090 <readdir@plt>
test   %rax,%rax
je     1240 <pwd+0xc0>
mov    %rax,%r15
jmp    12c0 <pwd+0x140>
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14,%rdi
call   1090 <readdir@plt>
mov    %rax,%r15
test   %rax,%rax
je     1240 <pwd+0xc0>
cmp    %r12,(%r15)
jne    12b0 <pwd+0x130>
mov    0x8(%r15),%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
mov    0x8(%r15),%rdi
call   1050 <strlen@plt>
movb   $0x0,0x410(%rsp,%rax,1)
mov    %rbx,%rdi
lea    0xd17(%rip),%rsi
call   10a0 <strcat@plt>
mov    %rbx,%rdi
mov    %r13,%rsi
call   10a0 <strcat@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
jmp    12b0 <pwd+0x130>

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

LLVM IR (-O2):
%struct.stat = type { i64 }
%struct.dirent = type { i64, i8* }
@.str = [2 x i8] c".\00"
@.str.1 = [3 x i8] c"..\00"
@.str.2 = [2 x i8] c"/\00"
define i8* @pwd() {
%1 = alloca [500 x i8]
%2 = alloca [500 x i8]
%3 = alloca [500 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 0
%6 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 0
%7 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 0
%8 = bitcast %struct.stat* %4 to i8*
%9 = call i32 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.stat* %4)
%10 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%11 = load i64, i64* %10
%12 = call i32 @stat(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %4)
%13 = load i64, i64* %10
%14 = icmp eq i64 %11, %13
br i1 %14, label %15, label %26
15: ; preds = %50, %0
%16 = call i64 @strlen(i8* %5)
%17 = add i64 %16, -1
%18 = getelementptr inbounds [500 x i8], [500 x i8]* %1, i64 0, i64 %17
%19 = load i8, i8* %18
%20 = icmp eq i8 %19, 47
br i1 %20, label %22, label %21
21: ; preds = %15
store i8 0, i8* %18
br label %22
22: ; preds = %21, %15
store i8 47, i8* %7
%23 = getelementptr inbounds [500 x i8], [500 x i8]* %3, i64 0, i64 1
store i8 0, i8* %23
%24 = call i32 @strcat(i8* %7, i8* %5)
%25 = call i32 @chdir(i8* %7)
ret i8* %7
26: ; preds = %0, %50
%27 = phi i64 [ %53, %50 ], [ %11, %0 ]
%28 = call i32 @chdir(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
%29 = call i32* @opendir(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%30 = call %struct.dirent* @readdir(i32* %29)
%31 = icmp eq %struct.dirent* %30, null
br i1 %31, label %50, label %32
32: ; preds = %26, %47
%33 = phi %struct.dirent* [ %48, %47 ], [ %30, %26 ]
%34 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i64 0, i32 0
%35 = load i64, i64* %34
%36 = icmp eq i64 %35, %27
br i1 %36, label %37, label %47
37: ; preds = %32
%38 = getelementptr inbounds %struct.dirent, %struct.dirent* %33, i64 0, i32 1
%39 = load i8*, i8** %38
%40 = call i32 @strcpy(i8* %6, i8* %39)
%41 = load i8*, i8** %38
%42 = call i64 @strlen(i8* dereferenceable(1) %41)
%43 = getelementptr inbounds [500 x i8], [500 x i8]* %2, i64 0, i64 %42
store i8 0, i8* %43
%44 = call i32 @strcat(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%45 = call i32 @strcat(i8* %6, i8* %5)
%46 = call i32 @strcpy(i8* %5, i8* %6)
br label %47
47: ; preds = %37, %32
%48 = call %struct.dirent* @readdir(i32* %29)
%49 = icmp eq %struct.dirent* %48, null
br i1 %49, label %50, label %32
50: ; preds = %47, %26
%51 = call i32 @closedir(i32* %29)
%52 = call i32 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), %struct.stat* %4)
%53 = load i64, i64* %10
%54 = call i32 @stat(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %4)
%55 = load i64, i64* %10
%56 = icmp eq i64 %53, %55
br i1 %56, label %15, label %26
}