#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*/ *__getcwd(char *, int);
int /*<<< orphan*/ __set_errno(int /*<<< orphan*/);
int /*<<< orphan*/ __strerror_r(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ errno;
char *strcpy(char *, char *);
char *getwd(char *buf) {
  char tmpbuf[1024];
  if (buf == NULL) {
    __set_errno(EINVAL);
    return NULL;
  }
  if (__getcwd(tmpbuf, 1024) == NULL) {
    __strerror_r(errno, buf, 1024);
    return NULL;
  }
  return strcpy(buf, tmpbuf);
}


x86 assembly (clang, -O0):
<getwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <getwd+0x35>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <getwd+0x88>
lea    -0x410(%rbp),%rdi
mov    $0x400,%esi
call   1060 <__getcwd@plt>
cmp    $0x0,%rax
jne    11b4 <getwd+0x74>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
mov    $0x400,%edx
call   1050 <__strerror_r@plt>
movq   $0x0,-0x8(%rbp)
jmp    11c8 <getwd+0x88>
mov    -0x10(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1040 <strcpy@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getwd>:
push   %rbx
sub    $0x400,%rsp
test   %rdi,%rdi
je     116f <getwd+0x2f>
mov    %rdi,%rbx
mov    %rsp,%rdi
mov    $0x400,%esi
call   1060 <__getcwd@plt>
test   %rax,%rax
je     117f <getwd+0x3f>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1040 <strcpy@plt>
jmp    1197 <getwd+0x57>
mov    0x2e62(%rip),%rax
mov    (%rax),%edi
call   1030 <__set_errno@plt>
jmp    1195 <getwd+0x55>
mov    0x2e5a(%rip),%rax
mov    (%rax),%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <__strerror_r@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x400,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i8* @getwd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca [1024 x i8]
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp eq i8* %5, null
br i1 %6, label %7, label %10
7: ; preds = %1
%8 = load i32, i32* @EINVAL
%9 = call i32 @__set_errno(i32 %8)
store i8* null, i8** %2
br label %22
10: ; preds = %1
%11 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%12 = call i32* @__getcwd(i8* %11, i32 1024)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %18
14: ; preds = %10
%15 = load i32, i32* @errno
%16 = load i8*, i8** %3
%17 = call i32 @__strerror_r(i32 %15, i8* %16, i32 1024)
store i8* null, i8** %2
br label %22
18: ; preds = %10
%19 = load i8*, i8** %3
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%21 = call i8* @strcpy(i8* %19, i8* %20)
store i8* %21, i8** %2
br label %22
22: ; preds = %18, %14, %7
%23 = load i8*, i8** %2
ret i8* %23
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i8* @getwd(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = icmp eq i8* %0, null
br i1 %4, label %5, label %8
5: ; preds = %1
%6 = load i32, i32* @EINVAL
%7 = tail call i32 @__set_errno(i32 %6)
br label %16
8: ; preds = %1
%9 = call i32* @__getcwd(i8* %3, i32 1024)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %8
%12 = load i32, i32* @errno
%13 = call i32 @__strerror_r(i32 %12, i8* %0, i32 1024)
br label %16
14: ; preds = %8
%15 = call i8* @strcpy(i8* %0, i8* %3)
br label %16
16: ; preds = %14, %11, %5
%17 = phi i8* [ null, %5 ], [ null, %11 ], [ %0, %14 ]
ret i8* %17
}