#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*/ *getwd(char *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *getcwd(char *buf, size_t size) {
  static char mybuf[256];
  if ((getwd(mybuf)) == NULL) {
    return NULL;
  }
  if (buf == NULL)
    return mybuf;
  strncpy(buf, mybuf, size - 1);
  mybuf[size - 1] = 0;
  return buf;
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    0x2f09(%rip),%rdi
call   1040 <getwd@plt>
cmp    $0x0,%rax
jne    1153 <getcwd+0x33>
movq   $0x0,-0x8(%rbp)
jmp    11a1 <getcwd+0x81>
cmpq   $0x0,-0x10(%rbp)
jne    116e <getcwd+0x4e>
lea    0x2edb(%rip),%rax
mov    %rax,-0x8(%rbp)
jmp    11a1 <getcwd+0x81>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
sub    $0x1,%rdx
lea    0x2ebf(%rip),%rsi
call   1030 <strncpy@plt>
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
lea    0x2eab(%rip),%rax
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   %r15
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
lea    0x2f0e(%rip),%rbx
mov    %rbx,%rdi
call   1040 <getwd@plt>
test   %rax,%rax
je     1165 <getcwd+0x45>
test   %r15,%r15
je     1167 <getcwd+0x47>
lea    -0x1(%r14),%rdx
lea    0x2ef1(%rip),%rbx
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
mov    %r15,%rbx
jmp    1167 <getcwd+0x47>
xor    %ebx,%ebx
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@getcwd.mybuf = internal global [256 x i8] zeroinitializer
define i8* @getcwd(i8* %0, i64 %1) {
%3 = tail call i32* @getwd(i8* getelementptr inbounds ([256 x i8], [256 x i8]* @getcwd.mybuf, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %11, label %5
5: ; preds = %2
%6 = icmp eq i8* %0, null
br i1 %6, label %11, label %7
7: ; preds = %5
%8 = add i64 %1, -1
%9 = tail call i32 @strncpy(i8* %0, i8* getelementptr inbounds ([256 x i8], [256 x i8]* @getcwd.mybuf, i64 0, i64 0), i64 %8)
%10 = getelementptr inbounds [256 x i8], [256 x i8]* @getcwd.mybuf, i64 0, i64 %8
store i8 0, i8* %10
br label %11
11: ; preds = %5, %2, %7
%12 = phi i8* [ %0, %7 ], [ null, %2 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @getcwd.mybuf, i64 0, i64 0), %5 ]
ret i8* %12
}