#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 __syscall_getcwd(char *, size_t);
scalar_t__ __unlikely(int);
int /*<<< orphan*/ errno;
char *getcwd(char *buf, size_t size) {
  if (__unlikely(!size)) {
    errno = EINVAL;
    buf = 0;
  } else {
    int tmp;
    if ((tmp = __syscall_getcwd(buf, size - 1)) < 0)
      return 0;
    buf[tmp] = 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   $0x0,-0x18(%rbp)
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1040 <__unlikely@plt>
cmp    $0x0,%rax
je     116d <getcwd+0x4d>
mov    0x2e83(%rip),%rax
mov    (%rax),%ecx
mov    0x2e82(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x10(%rbp)
jmp    11a3 <getcwd+0x83>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
sub    $0x1,%rsi
call   1030 <__syscall_getcwd@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0x0,%eax
jge    1197 <getcwd+0x77>
movq   $0x0,-0x8(%rbp)
jmp    11ab <getcwd+0x8b>
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   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%r14
xor    %edi,%edi
test   %rsi,%rsi
sete   %dil
call   1040 <__unlikely@plt>
test   %rax,%rax
je     1154 <getcwd+0x34>
mov    0x2e94(%rip),%rax
mov    (%rax),%eax
mov    0x2e93(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
jmp    116e <getcwd+0x4e>
add    $0xffffffffffffffff,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <__syscall_getcwd@plt>
test   %eax,%eax
js     114f <getcwd+0x2f>
mov    %eax,%eax
movb   $0x0,(%r14,%rax,1)
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
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 ne i64 %7, 0
%9 = xor i1 %8, true
%10 = zext i1 %9 to i32
%11 = call i64 @__unlikely(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %13, label %15
13: ; preds = %2
%14 = load i32, i32* @EINVAL
store i32 %14, i32* @errno
store i8* null, i8** %4
br label %27
15: ; preds = %2
%16 = load i8*, i8** %4
%17 = load i64, i64* %5
%18 = sub i64 %17, 1
%19 = call i32 @__syscall_getcwd(i8* %16, i64 %18)
store i32 %19, i32* %6
%20 = icmp slt i32 %19, 0
br i1 %20, label %21, label %22
21: ; preds = %15
store i8* null, i8** %3
br label %29
22: ; preds = %15
%23 = load i8*, i8** %4
%24 = load i32, i32* %6
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds i8, i8* %23, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %22, %13
%28 = load i8*, i8** %4
store i8* %28, i8** %3
br label %29
29: ; preds = %27, %21
%30 = load i8*, i8** %3
ret i8* %30
}

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