#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*/ *);
scalar_t__ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
char *getcwd(char *buf, int size) {
  FILE *fp;
  char *p;
  if (size <= 0) {
    errno = EINVAL;
    return NULL;
  }
  if ((fp = popen("/bin/pwd", "r")) == NULL)
    return NULL;
  if (fgets(buf, size, fp) == NULL || 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    124e <getcwd+0x11e>
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    124e <getcwd+0x11e>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11c3 <getcwd+0x93>
mov    -0x20(%rbp),%rdi
call   1030 <pclose@plt>
cmp    $0x0,%rax
je     11e2 <getcwd+0xb2>
mov    0x2e16(%rip),%rax
mov    (%rax),%ecx
mov    0x2dfd(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    124e <getcwd+0x11e>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
je     123f <getcwd+0x10f>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
jne    1229 <getcwd+0xf9>
mov    0x2dd7(%rip),%rax
mov    (%rax),%ecx
mov    0x2db6(%rip),%rax
mov    %ecx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    124e <getcwd+0x11e>
jmp    122e <getcwd+0xfe>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11ea <getcwd+0xba>
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    1180 <getcwd+0x50>
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xebc(%rip),%rdi
lea    0xebe(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     1192 <getcwd+0x62>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     1177 <getcwd+0x47>
mov    %rbx,%rdi
call   1030 <pclose@plt>
test   %rax,%rax
je     119d <getcwd+0x6d>
mov    0x2e62(%rip),%rax
jmp    1187 <getcwd+0x57>
mov    0x2e41(%rip),%rax
mov    (%rax),%eax
mov    0x2e40(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret
mov    %r14,%rax
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    1195 <getcwd+0x65>
mov    0x2e2a(%rip),%rax
jmp    1187 <getcwd+0x57>

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*
store i8* %0, i8** %4
store i32 %1, i32* %5
%8 = load i32, i32* %5
%9 = icmp sle i32 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %2
%11 = load i32, i32* @EINVAL
store i32 %11, i32* @errno
store i8* null, i8** %3
br label %49
12: ; preds = %2
%13 = 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* %13, i32** %6
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %16
15: ; preds = %12
store i8* null, i8** %3
br label %49
16: ; preds = %12
%17 = load i8*, i8** %4
%18 = load i32, i32* %5
%19 = load i32*, i32** %6
%20 = call i32* @fgets(i8* %17, i32 %18, i32* %19)
%21 = icmp eq i32* %20, null
br i1 %21, label %26, label %22
22: ; preds = %16
%23 = load i32*, i32** %6
%24 = call i64 @pclose(i32* %23)
%25 = icmp ne i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22, %16
%27 = load i32, i32* @EACCES
store i32 %27, i32* @errno
store i8* null, i8** %3
br label %49
28: ; preds = %22
%29 = load i8*, i8** %4
store i8* %29, i8** %7
br label %30
30: ; preds = %43, %28
%31 = load i8*, i8** %7
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 10
br i1 %34, label %35, label %46
35: ; preds = %30
%36 = load i8*, i8** %7
%37 = load i8, i8* %36
%38 = sext i8 %37 to i32
%39 = icmp eq i32 %38, 0
br i1 %39, label %40, label %42
40: ; preds = %35
%41 = load i32, i32* @ERANGE
store i32 %41, i32* @errno
store i8* null, i8** %3
br label %49
42: ; preds = %35
br label %43
43: ; preds = %42
%44 = load i8*, i8** %7
%45 = getelementptr inbounds i8, i8* %44, i32 1
store i8* %45, i8** %7
br label %30
46: ; preds = %30
%47 = load i8*, i8** %7
store i8 0, i8* %47
%48 = load i8*, i8** %4
store i8* %48, i8** %3
br label %49
49: ; preds = %46, %40, %26, %15, %10
%50 = load i8*, i8** %3
ret i8* %50
}

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 i64 @pclose(i32* %7)
%14 = icmp eq i64 %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
}