#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 */

/* Variables and functions */
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ SET_ERRNO(int /*<<< orphan*/);
int /*<<< orphan*/ assert(int /*<<< orphan*/);
char *getenv(char *);
int /*<<< orphan*/ setenv(char *, char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char const *);
int strlen(char const *);
char *getcwd(char *buff, size_t size) {
  const char *dir;
  if ((buff == NULL) || (size == 0)) {
    SET_ERRNO(EINVAL);
    return NULL;
  }
  dir = getenv("PWD");
  if (dir == NULL) {
    setenv("PWD", "/", 0);
    dir = "/";
  }
  assert(dir != NULL);
  if (strlen(dir) + 1 >= size) {
    SET_ERRNO(ERANGE);
    return NULL;
  }
  strcpy(buff, dir);
  return buff;
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1186 <getcwd+0x26>
cmpq   $0x0,-0x18(%rbp)
jne    11a1 <getcwd+0x41>
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
call   1040 <SET_ERRNO@plt>
movq   $0x0,-0x8(%rbp)
jmp    1236 <getcwd+0xd6>
lea    0xe58(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    11dc <getcwd+0x7c>
lea    0xe3d(%rip),%rdi
lea    0xe3a(%rip),%rsi
xor    %edx,%edx
call   1060 <setenv@plt>
lea    0xe2c(%rip),%rax
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cltq
cmp    -0x18(%rbp),%rax
jb     1221 <getcwd+0xc1>
mov    0x2ddb(%rip),%rax
mov    (%rax),%edi
call   1040 <SET_ERRNO@plt>
movq   $0x0,-0x8(%rbp)
jmp    1236 <getcwd+0xd6>
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %r15
push   %r14
push   %rbx
test   %rdi,%rdi
je     11ca <getcwd+0x6a>
mov    %rsi,%r14
test   %rsi,%rsi
je     11ca <getcwd+0x6a>
mov    %rdi,%rbx
lea    0xe84(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,%r15
test   %rax,%rax
jne    11a1 <getcwd+0x41>
lea    0xe70(%rip),%rdi
lea    0xe6d(%rip),%r15
mov    %r15,%rsi
xor    %edx,%edx
call   1060 <setenv@plt>
mov    $0x1,%edi
call   1080 <assert@plt>
mov    %r15,%rdi
call   1070 <strlen@plt>
add    $0x1,%eax
cltq
cmp    %r14,%rax
jae    11d3 <getcwd+0x73>
mov    %rbx,%rdi
mov    %r15,%rsi
call   1050 <strcpy@plt>
jmp    11e3 <getcwd+0x83>
mov    0x2e07(%rip),%rax
jmp    11da <getcwd+0x7a>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
call   1040 <SET_ERRNO@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@EINVAL = global i32 0
@.str = [4 x i8] c"PWD\00"
@.str.1 = [2 x i8] c"/\00"
@ERANGE = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = icmp eq i8* %0, null
%4 = icmp eq i64 %1, 0
%5 = or i1 %3, %4
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 %26
9: ; preds = %2
%10 = tail call i8* @getenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq i8* %10, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @setenv(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %14
14: ; preds = %12, %9
%15 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %12 ], [ %10, %9 ]
%16 = tail call i32 @assert(i32 1)
%17 = tail call i32 @strlen(i8* %15)
%18 = add nsw i32 %17, 1
%19 = sext i32 %18 to i64
%20 = icmp ult i64 %19, %1
br i1 %20, label %24, label %21
21: ; preds = %14
%22 = load i32, i32* @ERANGE
%23 = tail call i32 @SET_ERRNO(i32 %22)
br label %26
24: ; preds = %14
%25 = tail call i32 @strcpy(i8* %0, i8* %15)
br label %26
26: ; preds = %24, %21, %6
%27 = phi i8* [ null, %6 ], [ null, %21 ], [ %0, %24 ]
ret i8* %27
}