#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*/ _PC_PATH_MAX;
scalar_t__ calloc(long, int);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ *getcwd(char *, long);
long pathconf(char *, int /*<<< orphan*/);
char *iop_getcwd(void) {
  long maxpath = 0;
  char *fullpath = NULL;
  if ((maxpath = pathconf(".", _PC_PATH_MAX)) != -1) {
    fullpath = (char *)calloc(maxpath, sizeof(char));
    if (fullpath != NULL && getcwd(fullpath, maxpath) == NULL) {
      free(fullpath);
      fullpath = NULL;
    }
  }
  return fullpath;
}


x86 assembly (clang, -O0):
<iop_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movq   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    0x2e81(%rip),%rax
mov    (%rax),%esi
lea    0xe98(%rip),%rdi
call   1040 <pathconf@plt>
mov    %rax,-0x8(%rbp)
cmp    $0xffffffffffffffff,%rax
je     11c5 <iop_getcwd+0x85>
mov    -0x8(%rbp),%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     11c0 <iop_getcwd+0x80>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
jne    11c0 <iop_getcwd+0x80>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x10(%rbp)
jmp    11c5 <iop_getcwd+0x85>
mov    -0x10(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<iop_getcwd>:
push   %r14
push   %rbx
push   %rax
mov    0x2e95(%rip),%rax
mov    (%rax),%esi
lea    0xeac(%rip),%rdi
call   1040 <pathconf@plt>
cmp    $0xffffffffffffffff,%rax
je     118f <iop_getcwd+0x4f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
call   1060 <calloc@plt>
test   %rax,%rax
je     118f <iop_getcwd+0x4f>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1050 <getcwd@plt>
test   %rax,%rax
jne    1192 <iop_getcwd+0x52>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@_PC_PATH_MAX = global i32 0
define i8* @iop_getcwd() {
%1 = alloca i64
%2 = alloca i8*
store i64 0, i64* %1
store i8* null, i8** %2
%3 = load i32, i32* @_PC_PATH_MAX
%4 = call i64 @pathconf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %3)
store i64 %4, i64* %1
%5 = icmp ne i64 %4, -1
br i1 %5, label %6, label %21
6: ; preds = %0
%7 = load i64, i64* %1
%8 = call i64 @calloc(i64 %7, i32 1)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %2
%10 = load i8*, i8** %2
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %20
12: ; preds = %6
%13 = load i8*, i8** %2
%14 = load i64, i64* %1
%15 = call i32* @getcwd(i8* %13, i64 %14)
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %12
%18 = load i8*, i8** %2
%19 = call i32 @free(i8* %18)
store i8* null, i8** %2
br label %20
20: ; preds = %17, %12, %6
br label %21
21: ; preds = %20, %0
%22 = load i8*, i8** %2
ret i8* %22
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@_PC_PATH_MAX = global i32 0
define i8* @iop_getcwd() {
%1 = load i32, i32* @_PC_PATH_MAX
%2 = tail call i64 @pathconf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 %1)
%3 = icmp eq i64 %2, -1
br i1 %3, label %13, label %4
4: ; preds = %0
%5 = tail call i64 @calloc(i64 %2, i32 1)
%6 = inttoptr i64 %5 to i8*
%7 = icmp eq i64 %5, 0
br i1 %7, label %13, label %8
8: ; preds = %4
%9 = tail call i32* @getcwd(i8* %6, i64 %2)
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %13
11: ; preds = %8
%12 = tail call i32 @free(i8* %6)
br label %13
13: ; preds = %4, %8, %11, %0
%14 = phi i8* [ null, %11 ], [ %6, %8 ], [ null, %4 ], [ null, %0 ]
ret i8* %14
}