#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 {
  int /*<<< orphan*/ st_ino;
  int /*<<< orphan*/ st_dev;
};

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
char *malloc(int);
char *recurser(char *, int, int /*<<< orphan*/, int /*<<< orphan*/);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
char *getcwd(char *buf, int size) {
  struct stat st;
  if (size == 0) {
    __set_errno(EINVAL);
    return NULL;
  }
  if (size < 3) {
    __set_errno(ERANGE);
    return NULL;
  }
  if (buf == NULL) {
    buf = malloc(size);
    if (buf == NULL)
      return NULL;
  }
  strcpy(buf, ".");
  if (stat("/", &st) < 0) {
    return NULL;
  }
  return recurser(buf, size, st.st_dev, st.st_ino);
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jne    1184 <getcwd+0x34>
mov    0x2e68(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    122a <getcwd+0xda>
cmpl   $0x3,-0x14(%rbp)
jge    11a9 <getcwd+0x59>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    122a <getcwd+0xda>
cmpq   $0x0,-0x10(%rbp)
jne    11dd <getcwd+0x8d>
mov    -0x14(%rbp),%edi
call   1070 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11d8 <getcwd+0x88>
movq   $0x0,-0x8(%rbp)
jmp    122a <getcwd+0xda>
jmp    11dd <getcwd+0x8d>
mov    -0x10(%rbp),%rdi
lea    0xe18(%rip),%rsi
call   1040 <strcpy@plt>
lea    0xe0e(%rip),%rdi
lea    -0x20(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    1214 <getcwd+0xc4>
movq   $0x0,-0x8(%rbp)
jmp    122a <getcwd+0xda>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x1c(%rbp),%edx
mov    -0x20(%rbp),%ecx
call   1050 <recurser@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %r14
push   %rbx
push   %rax
test   %esi,%esi
je     1168 <getcwd+0x18>
mov    %esi,%ebx
cmp    $0x2,%esi
jg     1180 <getcwd+0x30>
mov    0x2e82(%rip),%rax
jmp    116f <getcwd+0x1f>
mov    0x2e69(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %rdi,%r14
test   %rdi,%rdi
jne    1197 <getcwd+0x47>
mov    %ebx,%edi
call   1070 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     1176 <getcwd+0x26>
lea    0xe62(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
lea    0xe55(%rip),%rdi
mov    %rsp,%rsi
call   1060 <stat@plt>
test   %rax,%rax
js     1176 <getcwd+0x26>
mov    (%rsp),%ecx
mov    0x4(%rsp),%edx
mov    %r14,%rdi
mov    %ebx,%esi
call   1050 <recurser@plt>
jmp    1178 <getcwd+0x28>

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

LLVM IR (-O2):
%struct.stat = type { i32, i32 }
@EINVAL = global i32 0
@ERANGE = global i32 0
@.str = [2 x i8] c".\00"
@.str.1 = [2 x i8] c"/\00"
define i8* @getcwd(i8* %0, i32 %1) {
%3 = alloca %struct.stat
%4 = bitcast %struct.stat* %3 to i8*
%5 = icmp eq i32 %1, 0
br i1 %5, label %6, label %9
6: ; preds = %2
%7 = load i32, i32* @EINVAL
%8 = tail call i32 @__set_errno(i32 %7)
br label %30
9: ; preds = %2
%10 = icmp slt i32 %1, 3
br i1 %10, label %11, label %14
11: ; preds = %9
%12 = load i32, i32* @ERANGE
%13 = tail call i32 @__set_errno(i32 %12)
br label %30
14: ; preds = %9
%15 = icmp eq i8* %0, null
br i1 %15, label %16, label %19
16: ; preds = %14
%17 = tail call i8* @malloc(i32 %1)
%18 = icmp eq i8* %17, null
br i1 %18, label %30, label %19
19: ; preds = %16, %14
%20 = phi i8* [ %17, %16 ], [ %0, %14 ]
%21 = tail call i32 @strcpy(i8* %20, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = call i64 @stat(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %struct.stat* %3)
%23 = icmp slt i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %19
%25 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 1
%26 = load i32, i32* %25
%27 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%28 = load i32, i32* %27
%29 = call i8* @recurser(i8* %20, i32 %1, i32 %26, i32 %28)
br label %30
30: ; preds = %19, %16, %24, %11, %6
%31 = phi i8* [ null, %6 ], [ null, %11 ], [ %29, %24 ], [ null, %16 ], [ null, %19 ]
ret i8* %31
}