#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 */
scalar_t__ ENOMEM;
scalar_t__ ERANGE;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
char *getcwd(char *, size_t);
char *malloc(size_t);
int /*<<< orphan*/ memcpy(char *, char *, size_t);
char *realloc(char *, size_t);
int strlen(char *);
char *sgsXPC_GetCurrentDirectory() {
  char stack_buf[4096];
  char *buf;
  size_t cur_size = 4096, max_size = 4096 * 10;
  buf = getcwd(NULL, 0);
  if (buf)
    return buf;
  errno = 0;
  buf = stack_buf;
  for (;;) {
    if (getcwd(buf, cur_size)) {
      if (buf == stack_buf) {
        size_t len = strlen(buf) + 1;
        buf = (char *)malloc(len);
        memcpy(buf, stack_buf, len);
      }
      return buf;
    }
    if (errno != ERANGE || cur_size >= max_size) {
      if (buf != stack_buf)
        free(buf);
      return NULL;
    }
    cur_size *= 2;
    if (buf == stack_buf)
      buf = malloc(cur_size);
    else
      buf = realloc(buf, cur_size);
    if (!buf) {
      errno = ENOMEM;
      return NULL;
    }
  }
  return NULL;
}


x86 assembly (clang, -O0):
<sgsXPC_GetCurrentDirectory>:
push   %rbp
mov    %rsp,%rbp
sub    $0x1030,%rsp
movq   $0x1000,-0x1020(%rbp)
movq   $0xa000,-0x1028(%rbp)
xor    %eax,%eax
mov    %eax,%esi
mov    %rsi,%rdi
call   1050 <getcwd@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
je     11b2 <sgsXPC_GetCurrentDirectory+0x52>
mov    -0x1018(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1344 <sgsXPC_GetCurrentDirectory+0x1e4>
mov    0x2e17(%rip),%rax
movq   $0x0,(%rax)
lea    -0x1010(%rbp),%rax
mov    %rax,-0x1018(%rbp)
mov    -0x1018(%rbp),%rdi
mov    -0x1020(%rbp),%rsi
call   1050 <getcwd@plt>
cmp    $0x0,%rax
je     1257 <sgsXPC_GetCurrentDirectory+0xf7>
mov    -0x1018(%rbp),%rax
lea    -0x1010(%rbp),%rcx
cmp    %rcx,%rax
jne    1247 <sgsXPC_GetCurrentDirectory+0xe7>
mov    -0x1018(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x1030(%rbp)
mov    -0x1030(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x1018(%rbp)
mov    -0x1018(%rbp),%rdi
lea    -0x1010(%rbp),%rsi
mov    -0x1030(%rbp),%rdx
call   1060 <memcpy@plt>
mov    -0x1018(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1344 <sgsXPC_GetCurrentDirectory+0x1e4>
mov    0x2d72(%rip),%rax
mov    (%rax),%rax
mov    0x2d80(%rip),%rcx
cmp    (%rcx),%rax
jne    1285 <sgsXPC_GetCurrentDirectory+0x125>
mov    -0x1020(%rbp),%rax
cmp    -0x1028(%rbp),%rax
jb     12b5 <sgsXPC_GetCurrentDirectory+0x155>
mov    -0x1018(%rbp),%rax
lea    -0x1010(%rbp),%rcx
cmp    %rcx,%rax
je     12a8 <sgsXPC_GetCurrentDirectory+0x148>
mov    -0x1018(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    1344 <sgsXPC_GetCurrentDirectory+0x1e4>
mov    -0x1020(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x1020(%rbp)
mov    -0x1018(%rbp),%rax
lea    -0x1010(%rbp),%rcx
cmp    %rcx,%rax
jne    12f6 <sgsXPC_GetCurrentDirectory+0x196>
mov    -0x1020(%rbp),%rdi
call   1070 <malloc@plt>
mov    %rax,-0x1018(%rbp)
jmp    1310 <sgsXPC_GetCurrentDirectory+0x1b0>
mov    -0x1018(%rbp),%rdi
mov    -0x1020(%rbp),%rsi
call   1080 <realloc@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jne    133f <sgsXPC_GetCurrentDirectory+0x1df>
mov    0x2cb3(%rip),%rax
mov    (%rax),%rcx
mov    0x2ca1(%rip),%rax
mov    %rcx,(%rax)
movq   $0x0,-0x8(%rbp)
jmp    1344 <sgsXPC_GetCurrentDirectory+0x1e4>
jmp    11ce <sgsXPC_GetCurrentDirectory+0x6e>
mov    -0x8(%rbp),%rax
add    $0x1030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sgsXPC_GetCurrentDirectory>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x1000,%rsp
xor    %edi,%edi
xor    %esi,%esi
call   1050 <getcwd@plt>
mov    %rax,%r14
test   %rax,%rax
jne    124d <sgsXPC_GetCurrentDirectory+0xed>
mov    %rsp,%r12
mov    0x2e41(%rip),%r15
movq   $0x0,(%r15)
mov    $0x1000,%ebx
mov    0x2e46(%rip),%r13
mov    %r12,%r14
jmp    11c3 <sgsXPC_GetCurrentDirectory+0x63>
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <realloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     122b <sgsXPC_GetCurrentDirectory+0xcb>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1050 <getcwd@plt>
test   %rax,%rax
jne    11f7 <sgsXPC_GetCurrentDirectory+0x97>
cmp    $0x9fff,%rbx
ja     123a <sgsXPC_GetCurrentDirectory+0xda>
mov    0x0(%r13),%rax
cmp    %rax,(%r15)
jne    123a <sgsXPC_GetCurrentDirectory+0xda>
add    %rbx,%rbx
cmp    %r12,%r14
jne    11b0 <sgsXPC_GetCurrentDirectory+0x50>
mov    %rbx,%rdi
call   1070 <malloc@plt>
jmp    11bb <sgsXPC_GetCurrentDirectory+0x5b>
mov    %rsp,%rax
cmp    %rax,%r14
jne    124d <sgsXPC_GetCurrentDirectory+0xed>
mov    %rsp,%r15
mov    %r15,%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
movslq %eax,%rbx
mov    %rbx,%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %r15,%rsi
mov    %rbx,%rdx
call   1060 <memcpy@plt>
jmp    124d <sgsXPC_GetCurrentDirectory+0xed>
mov    0x2da6(%rip),%rax
mov    (%rax),%rax
mov    %rax,(%r15)
jmp    124a <sgsXPC_GetCurrentDirectory+0xea>
mov    %rsp,%rax
cmp    %rax,%r14
je     124a <sgsXPC_GetCurrentDirectory+0xea>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x1000,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
@ENOMEM = global i64 0
define i8* @sgsXPC_GetCurrentDirectory() {
%1 = alloca i8*
%2 = alloca [4096 x i8]
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
store i64 4096, i64* %4
store i64 40960, i64* %5
%7 = call i8* @getcwd(i8* null, i64 0)
store i8* %7, i8** %3
%8 = load i8*, i8** %3
%9 = icmp ne i8* %8, null
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = load i8*, i8** %3
store i8* %11, i8** %1
br label %71
12: ; preds = %0
store i64 0, i64* @errno
%13 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
store i8* %13, i8** %3
br label %14
14: ; preds = %70, %12
%15 = load i8*, i8** %3
%16 = load i64, i64* %4
%17 = call i8* @getcwd(i8* %15, i64 %16)
%18 = icmp ne i8* %17, null
br i1 %18, label %19, label %36
19: ; preds = %14
%20 = load i8*, i8** %3
%21 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%22 = icmp eq i8* %20, %21
br i1 %22, label %23, label %34
23: ; preds = %19
%24 = load i8*, i8** %3
%25 = call i32 @strlen(i8* %24)
%26 = add nsw i32 %25, 1
%27 = sext i32 %26 to i64
store i64 %27, i64* %6
%28 = load i64, i64* %6
%29 = call i8* @malloc(i64 %28)
store i8* %29, i8** %3
%30 = load i8*, i8** %3
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%32 = load i64, i64* %6
%33 = call i32 @memcpy(i8* %30, i8* %31, i64 %32)
br label %34
34: ; preds = %23, %19
%35 = load i8*, i8** %3
store i8* %35, i8** %1
br label %71
36: ; preds = %14
%37 = load i64, i64* @errno
%38 = load i64, i64* @ERANGE
%39 = icmp ne i64 %37, %38
br i1 %39, label %44, label %40
40: ; preds = %36
%41 = load i64, i64* %4
%42 = load i64, i64* %5
%43 = icmp uge i64 %41, %42
br i1 %43, label %44, label %52
44: ; preds = %40, %36
%45 = load i8*, i8** %3
%46 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%47 = icmp ne i8* %45, %46
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i8*, i8** %3
%50 = call i32 @free(i8* %49)
br label %51
51: ; preds = %48, %44
store i8* null, i8** %1
br label %71
52: ; preds = %40
%53 = load i64, i64* %4
%54 = mul i64 %53, 2
store i64 %54, i64* %4
%55 = load i8*, i8** %3
%56 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%57 = icmp eq i8* %55, %56
br i1 %57, label %58, label %61
58: ; preds = %52
%59 = load i64, i64* %4
%60 = call i8* @malloc(i64 %59)
store i8* %60, i8** %3
br label %65
61: ; preds = %52
%62 = load i8*, i8** %3
%63 = load i64, i64* %4
%64 = call i8* @realloc(i8* %62, i64 %63)
store i8* %64, i8** %3
br label %65
65: ; preds = %61, %58
%66 = load i8*, i8** %3
%67 = icmp ne i8* %66, null
br i1 %67, label %70, label %68
68: ; preds = %65
%69 = load i64, i64* @ENOMEM
store i64 %69, i64* @errno
store i8* null, i8** %1
br label %71
70: ; preds = %65
br label %14
71: ; preds = %68, %51, %34, %10
%72 = load i8*, i8** %1
ret i8* %72
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
@ENOMEM = global i64 0
define i8* @sgsXPC_GetCurrentDirectory() {
%1 = alloca [4096 x i8]
%2 = getelementptr inbounds [4096 x i8], [4096 x i8]* %1, i64 0, i64 0
%3 = tail call i8* @getcwd(i8* null, i64 0)
%4 = icmp eq i8* %3, null
br i1 %4, label %5, label %41
5: ; preds = %0
store i64 0, i64* @errno
br label %6
6: ; preds = %36, %5
%7 = phi i64 [ 4096, %5 ], [ %30, %36 ]
%8 = phi i8* [ %2, %5 ], [ %37, %36 ]
%9 = call i8* @getcwd(i8* %8, i64 %7)
%10 = icmp eq i8* %9, null
br i1 %10, label %19, label %11
11: ; preds = %6
%12 = icmp eq i8* %8, %2
br i1 %12, label %13, label %41
13: ; preds = %11
%14 = call i32 @strlen(i8* %2)
%15 = add nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = call i8* @malloc(i64 %16)
%18 = call i32 @memcpy(i8* %17, i8* %2, i64 %16)
br label %41
19: ; preds = %6
%20 = load i64, i64* @errno
%21 = load i64, i64* @ERANGE
%22 = icmp ne i64 %20, %21
%23 = icmp ugt i64 %7, 40959
%24 = or i1 %23, %22
br i1 %24, label %25, label %29
25: ; preds = %19
%26 = icmp eq i8* %8, %2
br i1 %26, label %41, label %27
27: ; preds = %25
%28 = call i32 @free(i8* %8)
br label %41
29: ; preds = %19
%30 = shl nuw nsw i64 %7, 1
%31 = icmp eq i8* %8, %2
br i1 %31, label %32, label %34
32: ; preds = %29
%33 = call i8* @malloc(i64 %30)
br label %36
34: ; preds = %29
%35 = call i8* @realloc(i8* %8, i64 %30)
br label %36
36: ; preds = %34, %32
%37 = phi i8* [ %33, %32 ], [ %35, %34 ]
%38 = icmp eq i8* %37, null
br i1 %38, label %39, label %6
39: ; preds = %36
%40 = load i64, i64* @ENOMEM
store i64 %40, i64* @errno
br label %41
41: ; preds = %25, %27, %11, %13, %0, %39
%42 = phi i8* [ null, %39 ], [ %3, %0 ], [ %17, %13 ], [ %8, %11 ], [ null, %27 ], [ null, %25 ]
ret i8* %42
}