#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 __getcwd(char *, size_t);
int /*<<< orphan*/ errno;
char *getcwd(char *buf, size_t buflen) {
  int r;
  if (buflen < 1) {
    errno = EINVAL;
    return NULL;
  }
  r = __getcwd(buf, buflen - 1);
  if (r < 0) {
    return NULL;
  }
  buf[r] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
cmpq   $0x1,-0x18(%rbp)
jae    114a <getcwd+0x3a>
mov    0x2ea6(%rip),%rax
mov    (%rax),%ecx
mov    0x2ea5(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1189 <getcwd+0x79>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    $0x1,%rsi
call   1030 <__getcwd@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1175 <getcwd+0x65>
movq   $0x0,-0x8(%rbp)
jmp    1189 <getcwd+0x79>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %rbx
test   %rsi,%rsi
je     112e <getcwd+0x1e>
mov    %rdi,%rbx
add    $0xffffffffffffffff,%rsi
call   1030 <__getcwd@plt>
test   %eax,%eax
js     1140 <getcwd+0x30>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
jmp    1142 <getcwd+0x32>
mov    0x2ea3(%rip),%rax
mov    (%rax),%eax
mov    0x2ea2(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i8* %0, i8** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = icmp ult i64 %7, 1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = load i32, i32* @EINVAL
store i32 %10, i32* @errno
store i8* null, i8** %3
br label %25
11: ; preds = %2
%12 = load i8*, i8** %4
%13 = load i64, i64* %5
%14 = sub i64 %13, 1
%15 = call i32 @__getcwd(i8* %12, i64 %14)
store i32 %15, i32* %6
%16 = load i32, i32* %6
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %11
store i8* null, i8** %3
br label %25
19: ; preds = %11
%20 = load i8*, i8** %4
%21 = load i32, i32* %6
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %20, i64 %22
store i8 0, i8* %23
%24 = load i8*, i8** %4
store i8* %24, i8** %3
br label %25
25: ; preds = %19, %18, %9
%26 = load i8*, i8** %3
ret i8* %26
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i8* @getcwd(i8* %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i32, i32* @EINVAL
store i32 %5, i32* @errno
br label %13
6: ; preds = %2
%7 = add i64 %1, -1
%8 = tail call i32 @__getcwd(i8* %0, i64 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %13, label %10
10: ; preds = %6
%11 = zext i32 %8 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %6, %10, %4
%14 = phi i8* [ null, %4 ], [ %0, %10 ], [ null, %6 ]
ret i8* %14
}