#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*/ ENOMEM;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ getwd(char *);
char *malloc(int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *getcwd(char *buf, int n) {
  char tmp[90];
  getwd(tmp);
  if (strlen(tmp) + 1 > n) {
    errno = ERANGE;
    return 0;
  }
  if (buf == 0) {
    buf = malloc(n);
    if (buf == 0) {
      errno = ENOMEM;
      return 0;
    }
  }
  strcpy(buf, tmp);
  return buf;
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <getwd@plt>
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cmp    -0x14(%rbp),%eax
jle    118c <getcwd+0x4c>
mov    0x2e74(%rip),%rax
mov    (%rax),%ecx
mov    0x2e53(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11e7 <getcwd+0xa7>
cmpq   $0x0,-0x10(%rbp)
jne    11d2 <getcwd+0x92>
mov    -0x14(%rbp),%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cd <getcwd+0x8d>
mov    0x2e23(%rip),%rax
mov    (%rax),%ecx
mov    0x2e12(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    11e7 <getcwd+0xa7>
jmp    11d2 <getcwd+0x92>
mov    -0x10(%rbp),%rdi
lea    -0x70(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %rbp
push   %r14
push   %rbx
sub    $0x60,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1050 <getwd@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
cmp    %r14d,%eax
jge    1188 <getcwd+0x48>
test   %rbx,%rbx
jne    117b <getcwd+0x3b>
mov    %r14d,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11a8 <getcwd+0x68>
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1030 <strcpy@plt>
jmp    119c <getcwd+0x5c>
mov    0x2e59(%rip),%rax
mov    (%rax),%eax
mov    0x2e38(%rip),%rcx
mov    %eax,(%rcx)
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x60,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    0x2e29(%rip),%rax
jmp    118f <getcwd+0x4f>

LLVM IR (-O0):
@ERANGE = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i8* @getcwd(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca [90 x i8]
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = getelementptr inbounds [90 x i8], [90 x i8]* %6, i64 0, i64 0
%8 = call i32 @getwd(i8* %7)
%9 = getelementptr inbounds [90 x i8], [90 x i8]* %6, i64 0, i64 0
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 1
%12 = load i32, i32* %5
%13 = icmp sgt i32 %11, %12
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* @ERANGE
store i32 %15, i32* @errno
store i8* null, i8** %3
br label %32
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %27
19: ; preds = %16
%20 = load i32, i32* %5
%21 = call i8* @malloc(i32 %20)
store i8* %21, i8** %4
%22 = load i8*, i8** %4
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %26
24: ; preds = %19
%25 = load i32, i32* @ENOMEM
store i32 %25, i32* @errno
store i8* null, i8** %3
br label %32
26: ; preds = %19
br label %27
27: ; preds = %26, %16
%28 = load i8*, i8** %4
%29 = getelementptr inbounds [90 x i8], [90 x i8]* %6, i64 0, i64 0
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i8*, i8** %4
store i8* %31, i8** %3
br label %32
32: ; preds = %27, %24, %14
%33 = load i8*, i8** %3
ret i8* %33
}

LLVM IR (-O2):
@ERANGE = global i32 0
@errno = global i32 0
@ENOMEM = global i32 0
define i8* @getcwd(i8* %0, i32 %1) {
%3 = alloca [90 x i8]
%4 = getelementptr inbounds [90 x i8], [90 x i8]* %3, i64 0, i64 0
%5 = call i32 @getwd(i8* %4)
%6 = call i32 @strlen(i8* %4)
%7 = icmp slt i32 %6, %1
br i1 %7, label %10, label %8
8: ; preds = %2
%9 = load i32, i32* @ERANGE
store i32 %9, i32* @errno
br label %20
10: ; preds = %2
%11 = icmp eq i8* %0, null
br i1 %11, label %12, label %17
12: ; preds = %10
%13 = call i8* @malloc(i32 %1)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = load i32, i32* @ENOMEM
store i32 %16, i32* @errno
br label %20
17: ; preds = %12, %10
%18 = phi i8* [ %13, %12 ], [ %0, %10 ]
%19 = call i32 @strcpy(i8* %18, i8* %4)
br label %20
20: ; preds = %17, %15, %8
%21 = phi i8* [ null, %8 ], [ null, %15 ], [ %18, %17 ]
ret i8* %21
}