#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 EINVAL;
int /*<<< orphan*/ __set_errno(int);
int errno;
char *malloc(unsigned long);
int /*<<< orphan*/ printf(char *);
char *recurser(char *, unsigned long, int /*<<< orphan*/, int /*<<< orphan*/,
               int);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int __syscall_getcwd(char *buf, unsigned long size) {
  int len;
  char *cwd;
  struct stat st;
  int olderrno, depth = 0;
  if (buf == NULL) {
    printf("<fixme> libc-getcwd: warning allocating memory for buffer\n");
    buf = malloc(size);
  }
  if (stat("/", &st) < 0) {
    return EINVAL;
  }
  strcpy(buf, ".");
  olderrno = errno;
  len = -1;
  cwd = recurser(buf, size, st.st_dev, st.st_ino, depth);
  if (cwd) {
    len = strlen(buf);
    __set_errno(olderrno);
  }
  return len;
}


x86 assembly (clang, -O0):
<__syscall_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x38(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11ab <__syscall_getcwd+0x3b>
lea    0xe67(%rip),%rdi
call   1070 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1090 <malloc@plt>
mov    %rax,-0x10(%rbp)
lea    0xe89(%rip),%rdi
lea    -0x30(%rbp),%rsi
call   1080 <stat@plt>
cmp    $0x0,%rax
jge    11d6 <__syscall_getcwd+0x66>
mov    0x2e0c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    1239 <__syscall_getcwd+0xc9>
mov    -0x10(%rbp),%rdi
lea    0xe5c(%rip),%rsi
call   1040 <strcpy@plt>
mov    0x2df3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x2c(%rbp),%edx
mov    -0x30(%rbp),%ecx
mov    -0x38(%rbp),%r8d
call   1050 <recurser@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     1233 <__syscall_getcwd+0xc3>
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x34(%rbp),%edi
call   1030 <__set_errno@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<__syscall_getcwd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
test   %rdi,%rdi
jne    119a <__syscall_getcwd+0x2a>
lea    0xe7a(%rip),%rdi
call   1050 <puts@plt>
mov    %r14,%rdi
call   1090 <malloc@plt>
mov    %rax,%rbx
lea    0xe5f(%rip),%rdi
lea    0x8(%rsp),%rsi
call   1080 <stat@plt>
test   %rax,%rax
js     11f6 <__syscall_getcwd+0x86>
lea    0xe4b(%rip),%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
mov    0x2e1a(%rip),%rax
mov    (%rax),%ebp
mov    0x8(%rsp),%ecx
mov    0xc(%rsp),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
xor    %r8d,%r8d
call   1060 <recurser@plt>
test   %rax,%rax
je     1201 <__syscall_getcwd+0x91>
mov    %rbx,%rdi
call   1070 <strlen@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1030 <__set_errno@plt>
jmp    1206 <__syscall_getcwd+0x96>
mov    0x2ddb(%rip),%rax
mov    (%rax),%ebx
jmp    1206 <__syscall_getcwd+0x96>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

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

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