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

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int PATH_MAX;
int /*<<< orphan*/ closedir(int /*<<< orphan*/ *);
int /*<<< orphan*/ errno;
struct dirent *getdirent(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ *opendir(char *);
scalar_t__ prepend(char *, char *, size_t);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
char *getcwd(char *buf, size_t size) {
  ino_t cino;
  dev_t cdev;
  ino_t rino;
  dev_t rdev;
  DIR *dirp;
  struct stat st;
  struct dirent *dp;
  char curdir[PATH_MAX];
  if (size == 0) {
    errno = EINVAL;
    return (NULL);
  }
  buf[0] = '\0';
  strcpy(curdir, ".");
  if (stat("/", &st) < 0)
    goto error0;
  rino = st.st_ino;
  rdev = st.st_dev;
  while (1) {
    if (stat(curdir, &st) < 0)
      goto error0;
    cino = st.st_ino;
    cdev = st.st_dev;
    if ((cino == rino) && (cdev == rdev))
      break;
    strcat(curdir, "/..");
    if ((dirp = opendir(curdir)) == NULL)
      goto error0;
    dp = getdirent(dirp, cino);
    if (prepend(buf, dp->d_name, size))
      goto error1;
    closedir(dirp);
  }
  if (buf[0] == '\0') {
    if (prepend(buf, "", size))
      goto error0;
  }
  return (buf);
error1:
  closedir(dirp);
error0:
  return (NULL);
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x60(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x78(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x68(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e0 <getcwd+0x70>
mov    0x2e0f(%rip),%rax
mov    (%rax),%ecx
mov    0x2e0e(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    1343 <getcwd+0x1d3>
mov    -0x78(%rbp),%rdi
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
lea    0xe12(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xe02(%rip),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    1216 <getcwd+0xa6>
jmp    1334 <getcwd+0x1c4>
mov    -0x48(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x38(%rbp)
jmp    1229 <getcwd+0xb9>
mov    -0x78(%rbp),%rdi
lea    -0x50(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    1245 <getcwd+0xd5>
jmp    1334 <getcwd+0x1c4>
mov    -0x48(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x2c(%rbp),%eax
jne    1272 <getcwd+0x102>
mov    -0x28(%rbp),%rax
cmp    -0x38(%rbp),%rax
jne    1272 <getcwd+0x102>
jmp    12df <getcwd+0x16f>
mov    -0x78(%rbp),%rdi
lea    0xd85(%rip),%rsi
call   1090 <strcat@plt>
mov    -0x78(%rbp),%rdi
call   1050 <opendir@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
jne    129e <getcwd+0x12e>
jmp    1334 <getcwd+0x1c4>
mov    -0x40(%rbp),%rdi
mov    -0x1c(%rbp),%esi
call   1070 <getdirent@plt>
mov    %rax,-0x58(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x58(%rbp),%rax
mov    (%rax),%rsi
mov    -0x18(%rbp),%rdx
call   1030 <prepend@plt>
cmp    $0x0,%rax
je     12d1 <getcwd+0x161>
jmp    132b <getcwd+0x1bb>
mov    -0x40(%rbp),%rdi
call   1060 <closedir@plt>
jmp    1224 <getcwd+0xb4>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1317 <getcwd+0x1a7>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd07(%rip),%rsi
call   1030 <prepend@plt>
cmp    $0x0,%rax
je     1312 <getcwd+0x1a2>
jmp    1334 <getcwd+0x1c4>
jmp    1317 <getcwd+0x1a7>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x6c(%rbp)
jmp    1343 <getcwd+0x1d3>
mov    -0x40(%rbp),%rdi
call   1060 <closedir@plt>
movq   $0x0,-0x8(%rbp)
movl   $0x1,-0x6c(%rbp)
mov    -0x60(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rsp,-0x58(%rbp)
mov    0x2e64(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%r14
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
mov    %rsi,-0x48(%rbp)
test   %rsi,%rsi
je     1260 <getcwd+0xf0>
mov    %rdi,%r15
movb   $0x0,(%rdi)
lea    0xe4b(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
lea    0xe38(%rip),%rdi
lea    -0x40(%rbp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     1272 <getcwd+0x102>
mov    -0x38(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x40(%rbp),%rax
mov    %rax,-0x50(%rbp)
lea    -0x40(%rbp),%rsi
mov    %r14,%rdi
nop
call   1080 <stat@plt>
test   %rax,%rax
js     1272 <getcwd+0x102>
mov    -0x38(%rbp),%r13d
cmp    -0x2c(%rbp),%r13d
jne    120e <getcwd+0x9e>
mov    -0x50(%rbp),%rax
cmp    %rax,-0x40(%rbp)
je     128b <getcwd+0x11b>
mov    %r14,%rdi
lea    0xdea(%rip),%rsi
call   1090 <strcat@plt>
mov    %r14,%rdi
call   1050 <opendir@plt>
test   %rax,%rax
je     1272 <getcwd+0x102>
mov    %rax,%r12
mov    %rax,%rdi
mov    %r13d,%esi
call   1070 <getdirent@plt>
mov    (%rax),%rsi
mov    %r15,%rdi
mov    -0x48(%rbp),%rdx
call   1030 <prepend@plt>
mov    %rax,%rbx
mov    %r12,%rdi
call   1060 <closedir@plt>
test   %rbx,%rbx
jne    1272 <getcwd+0x102>
mov    %r14,%rdi
lea    -0x40(%rbp),%rsi
jmp    11f0 <getcwd+0x80>
mov    0x2d69(%rip),%rax
mov    (%rax),%eax
mov    0x2d68(%rip),%rcx
mov    %eax,(%rcx)
xor    %r15d,%r15d
mov    -0x58(%rbp),%rsp
mov    %r15,%rax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
cmpb   $0x0,(%r15)
jne    1275 <getcwd+0x105>
lea    0xd6d(%rip),%rsi
mov    %r15,%rdi
mov    -0x48(%rbp),%rdx
call   1030 <prepend@plt>
test   %rax,%rax
jne    1272 <getcwd+0x102>
jmp    1275 <getcwd+0x105>

LLVM IR (-O0):
%struct.stat = type { i64, i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [4 x i8] c"/..\00"
@.str.3 = [1 x i8] zeroinitializer
define i8* @getcwd(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32*
%11 = alloca %struct.stat
%12 = alloca %struct.dirent*
%13 = alloca i8*
%14 = alloca i64
%15 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%16 = load i32, i32* @PATH_MAX
%17 = zext i32 %16 to i64
%18 = call i8* @llvm.stacksave()
store i8* %18, i8** %13
%19 = alloca i8, i64 %17
store i64 %17, i64* %14
%20 = load i64, i64* %5
%21 = icmp eq i64 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = load i32, i32* @EINVAL
store i32 %23, i32* @errno
store i8* null, i8** %3
store i32 1, i32* %15
br label %93
24: ; preds = %2
%25 = load i8*, i8** %4
%26 = getelementptr inbounds i8, i8* %25, i64 0
store i8 0, i8* %26
%27 = call i32 @strcpy(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%28 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %11)
%29 = icmp slt i64 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %24
br label %92
31: ; preds = %24
%32 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 1
%33 = load i32, i32* %32
store i32 %33, i32* %8
%34 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%35 = load i64, i64* %34
store i64 %35, i64* %9
br label %36
36: ; preds = %71, %31
br label %37
37: ; preds = %36
%38 = call i64 @stat(i8* %19, %struct.stat* %11)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %41
40: ; preds = %37
br label %92
41: ; preds = %37
%42 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 1
%43 = load i32, i32* %42
store i32 %43, i32* %6
%44 = getelementptr inbounds %struct.stat, %struct.stat* %11, i32 0, i32 0
%45 = load i64, i64* %44
store i64 %45, i64* %7
%46 = load i32, i32* %6
%47 = load i32, i32* %8
%48 = icmp eq i32 %46, %47
br i1 %48, label %49, label %54
49: ; preds = %41
%50 = load i64, i64* %7
%51 = load i64, i64* %9
%52 = icmp eq i64 %50, %51
br i1 %52, label %53, label %54
53: ; preds = %49
br label %74
54: ; preds = %49, %41
%55 = call i32 @strcat(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%56 = call i32* @opendir(i8* %19)
store i32* %56, i32** %10
%57 = icmp eq i32* %56, null
br i1 %57, label %58, label %59
58: ; preds = %54
br label %92
59: ; preds = %54
%60 = load i32*, i32** %10
%61 = load i32, i32* %6
%62 = call %struct.dirent* @getdirent(i32* %60, i32 %61)
store %struct.dirent* %62, %struct.dirent** %12
%63 = load i8*, i8** %4
%64 = load %struct.dirent*, %struct.dirent** %12
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = load i64, i64* %5
%68 = call i64 @prepend(i8* %63, i8* %66, i64 %67)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %71
70: ; preds = %59
br label %89
71: ; preds = %59
%72 = load i32*, i32** %10
%73 = call i32 @closedir(i32* %72)
br label %36
74: ; preds = %53
%75 = load i8*, i8** %4
%76 = getelementptr inbounds i8, i8* %75, i64 0
%77 = load i8, i8* %76
%78 = sext i8 %77 to i32
%79 = icmp eq i32 %78, 0
br i1 %79, label %80, label %87
80: ; preds = %74
%81 = load i8*, i8** %4
%82 = load i64, i64* %5
%83 = call i64 @prepend(i8* %81, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i64 %82)
%84 = icmp ne i64 %83, 0
br i1 %84, label %85, label %86
85: ; preds = %80
br label %92
86: ; preds = %80
br label %87
87: ; preds = %86, %74
%88 = load i8*, i8** %4
store i8* %88, i8** %3
store i32 1, i32* %15
br label %93
89: ; preds = %70
%90 = load i32*, i32** %10
%91 = call i32 @closedir(i32* %90)
br label %92
92: ; preds = %89, %85, %58, %40, %30
store i8* null, i8** %3
store i32 1, i32* %15
br label %93
93: ; preds = %92, %87, %22
%94 = load i8*, i8** %13
%95 = load i8*, i8** %3
ret i8* %95
}

LLVM IR (-O2):
%struct.stat = type { i64, i32 }
%struct.dirent = type { i8* }
@PATH_MAX = global i32 0
@EINVAL = global i32 0
@errno = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
@.str.2 = [4 x i8] c"/..\00"
@.str.3 = [1 x i8] zeroinitializer
define i8* @getcwd(i8* %0, i64 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = load i32, i32* @PATH_MAX
%6 = zext i32 %5 to i64
%7 = tail call i8* @llvm.stacksave()
%8 = alloca i8, i64 %6
%9 = icmp eq i64 %1, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
br label %50
12: ; preds = %2
store i8 0, i8* %0
%13 = call i32 @strcpy(i8* %8, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%15 = icmp slt i64 %14, 0
br i1 %15, label %49, label %16
16: ; preds = %12
%17 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%18 = load i32, i32* %17
%19 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%20 = load i64, i64* %19
%21 = call i64 @stat(i8* %8, %struct.stat* %3)
%22 = icmp slt i64 %21, 0
br i1 %22, label %49, label %23
23: ; preds = %16, %40
%24 = load i32, i32* %17
%25 = icmp eq i32 %24, %18
%26 = load i64, i64* %19
%27 = icmp eq i64 %26, %20
%28 = select i1 %25, i1 %27, i1 false
br i1 %28, label %43, label %29
29: ; preds = %23
%30 = call i32 @strcat(i8* %8, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0))
%31 = call i32* @opendir(i8* %8)
%32 = icmp eq i32* %31, null
br i1 %32, label %49, label %33
33: ; preds = %29
%34 = call %struct.dirent* @getdirent(i32* %31, i32 %24)
%35 = getelementptr inbounds %struct.dirent, %struct.dirent* %34, i64 0, i32 0
%36 = load i8*, i8** %35
%37 = call i64 @prepend(i8* %0, i8* %36, i64 %1)
%38 = icmp eq i64 %37, 0
%39 = call i32 @closedir(i32* %31)
br i1 %38, label %40, label %49
40: ; preds = %33
%41 = call i64 @stat(i8* %8, %struct.stat* %3)
%42 = icmp slt i64 %41, 0
br i1 %42, label %49, label %23
43: ; preds = %23
%44 = load i8, i8* %0
%45 = icmp eq i8 %44, 0
br i1 %45, label %46, label %50
46: ; preds = %43
%47 = call i64 @prepend(i8* %0, i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str.3, i64 0, i64 0), i64 %1)
%48 = icmp eq i64 %47, 0
br i1 %48, label %50, label %49
49: ; preds = %40, %29, %33, %16, %46, %12
br label %50
50: ; preds = %43, %46, %49, %10
%51 = phi i8* [ null, %10 ], [ null, %49 ], [ %0, %46 ], [ %0, %43 ]
ret i8* %51
}