#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ EACCES;
int /*<<< orphan*/ EINVAL;
int /*<<< orphan*/ ERANGE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
char *getcwd(char *buf, int size) {
  FILE *fp;
  char *p;
  int sts;
  if (size <= 0) {
    errno = EINVAL;
    return NULL;
  }
  if ((fp = popen("/bin/pwd", "r")) == NULL)
    return NULL;
  if (fgets(buf, size, fp) == NULL || (sts = pclose(fp)) != 0) {
    errno = EACCES;
    return NULL;
  }
  for (p = buf; *p != '\n'; p++) {
    if (*p == '\0') {
      errno = ERANGE;
      return NULL;
    }
  }
  *p = '\0';
  return buf;
}


x86 assembly (clang, -O0):
<getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     1168 <getcwd+0x38>
mov    0x2e78(%rip),%rax
mov    (%rax),%ecx
mov    0x2e77(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1250 <getcwd+0x120>
lea    0xe91(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1196 <getcwd+0x66>
movq   $0x0,-0x8(%rbp)
jmp    1250 <getcwd+0x120>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11c5 <getcwd+0x95>
mov    -0x20(%rbp),%rdi
call   1030 <pclose@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
je     11e4 <getcwd+0xb4>
mov    0x2e14(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfb(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1250 <getcwd+0x120>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     1241 <getcwd+0x111>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    122b <getcwd+0xfb>
mov    0x2dd5(%rip),%rax
mov    (%rax),%ecx
mov    0x2db4(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1250 <getcwd+0x120>
jmp    1230 <getcwd+0x100>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11ec <getcwd+0xbc>
mov    -0x28(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getcwd>:
push   %rbp
push   %r14
push   %rbx
test   %esi,%esi
jle    117f <getcwd+0x4f>
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xebc(%rip),%rdi
lea    0xebe(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     1191 <getcwd+0x61>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1176 <getcwd+0x46>
mov    %rbx,%rdi
call   1030 <pclose@plt>
test   %eax,%eax
je     119c <getcwd+0x6c>
mov    0x2e63(%rip),%rax
jmp    1186 <getcwd+0x56>
mov    0x2e42(%rip),%rax
mov    (%rax),%eax
mov    0x2e41(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14,%rax
nop
movzbl (%rax),%ecx
cmp    $0xa,%cl
je     11b2 <getcwd+0x82>
test   %cl,%cl
je     11b7 <getcwd+0x87>
add    $0x1,%rax
jmp    11a0 <getcwd+0x70>
movb   $0x0,(%rax)
jmp    1194 <getcwd+0x64>
mov    0x2e2a(%rip),%rax
jmp    1186 <getcwd+0x56>

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"/bin/pwd\00"
@.str.1 = [2 x i8] c"r\00"
@EACCES = global i32 0
@ERANGE = global i32 0
define i8* @getcwd(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%9 = load i32, i32* %5
%10 = icmp sle i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = load i32, i32* @EINVAL
store i32 %12, i32* @errno
store i8* null, i8** %3
br label %50
13: ; preds = %2
%14 = call i32* @popen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %14, i32** %6
%15 = icmp eq i32* %14, null
br i1 %15, label %16, label %17
16: ; preds = %13
store i8* null, i8** %3
br label %50
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = load i32, i32* %5
%20 = load i32*, i32** %6
%21 = call i32* @fgets(i8* %18, i32 %19, i32* %20)
%22 = icmp eq i32* %21, null
br i1 %22, label %27, label %23
23: ; preds = %17
%24 = load i32*, i32** %6
%25 = call i32 @pclose(i32* %24)
store i32 %25, i32* %8
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %29
27: ; preds = %23, %17
%28 = load i32, i32* @EACCES
store i32 %28, i32* @errno
store i8* null, i8** %3
br label %50
29: ; preds = %23
%30 = load i8*, i8** %4
store i8* %30, i8** %7
br label %31
31: ; preds = %44, %29
%32 = load i8*, i8** %7
%33 = load i8, i8* %32
%34 = sext i8 %33 to i32
%35 = icmp ne i32 %34, 10
br i1 %35, label %36, label %47
36: ; preds = %31
%37 = load i8*, i8** %7
%38 = load i8, i8* %37
%39 = sext i8 %38 to i32
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = load i32, i32* @ERANGE
store i32 %42, i32* @errno
store i8* null, i8** %3
br label %50
43: ; preds = %36
br label %44
44: ; preds = %43
%45 = load i8*, i8** %7
%46 = getelementptr inbounds i8, i8* %45, i32 1
store i8* %46, i8** %7
br label %31
47: ; preds = %31
%48 = load i8*, i8** %7
store i8 0, i8* %48
%49 = load i8*, i8** %4
store i8* %49, i8** %3
br label %50
50: ; preds = %47, %41, %27, %16, %11
%51 = load i8*, i8** %3
ret i8* %51
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
@.str = [9 x i8] c"/bin/pwd\00"
@.str.1 = [2 x i8] c"r\00"
@EACCES = global i32 0
@ERANGE = global i32 0
define i8* @getcwd(i8* %0, i32 %1) {
%3 = icmp slt i32 %1, 1
br i1 %3, label %4, label %6
4: ; preds = %2
%5 = load i32, i32* @EINVAL
store i32 %5, i32* @errno
br label %25
6: ; preds = %2
%7 = tail call i32* @popen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %25, label %9
9: ; preds = %6
%10 = tail call i32* @fgets(i8* %0, i32 %1, i32* %7)
%11 = icmp eq i32* %10, null
br i1 %11, label %15, label %12
12: ; preds = %9
%13 = tail call i32 @pclose(i32* %7)
%14 = icmp eq i32 %13, 0
br i1 %14, label %17, label %15
15: ; preds = %12, %9
%16 = load i32, i32* @EACCES
store i32 %16, i32* @errno
br label %25
17: ; preds = %12, %22
%18 = phi i8* [ %23, %22 ], [ %0, %12 ]
%19 = load i8, i8* %18
switch i8 %19, label %22 [
i8 10, label %24
i8 0, label %20
]
20: ; preds = %17
%21 = load i32, i32* @ERANGE
store i32 %21, i32* @errno
br label %25
22: ; preds = %17
%23 = getelementptr inbounds i8, i8* %18, i64 1
br label %17
24: ; preds = %17
store i8 0, i8* %18
br label %25
25: ; preds = %6, %24, %20, %15, %4
%26 = phi i8* [ null, %4 ], [ null, %15 ], [ null, %20 ], [ %0, %24 ], [ null, %6 ]
ret i8* %26
}