#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__ ERANGE;
scalar_t__ errno;
int /*<<< orphan*/ free(char *);
scalar_t__ getcwd(char *, size_t);
char *malloc(size_t);
char *realloc(char *, size_t);
char *my_getcmd(void) {
  char *buf, *tmp;
  size_t size = 1024;
  buf = malloc(size);
  if (!buf) {
    return NULL;
  }
  for (;;) {
    errno = 0;
    if (getcwd(buf, size)) {
      return buf;
    }
    if (errno != ERANGE) {
      break;
    }
    size *= 2;
    tmp = realloc(buf, size);
    if (!tmp) {
      break;
    }
    buf = tmp;
  }
  free(buf);
  return NULL;
}


x86 assembly (clang, -O0):
<my_getcmd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
movq   $0x400,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    1175 <my_getcmd+0x35>
movq   $0x0,-0x8(%rbp)
jmp    1216 <my_getcmd+0xd6>
jmp    117a <my_getcmd+0x3a>
mov    0x2e57(%rip),%rax
movq   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
je     11ac <my_getcmd+0x6c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1216 <my_getcmd+0xd6>
mov    0x2e25(%rip),%rax
mov    (%rax),%rax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%rax
je     11cb <my_getcmd+0x8b>
jmp    1205 <my_getcmd+0xc5>
mov    -0x20(%rbp),%rax
shl    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1060 <realloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f8 <my_getcmd+0xb8>
jmp    1205 <my_getcmd+0xc5>
mov    -0x18(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    117a <my_getcmd+0x3a>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<my_getcmd>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    $0x400,%ebx
mov    $0x400,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     11ae <my_getcmd+0x6e>
mov    0x2e75(%rip),%r15
mov    0x2e7e(%rip),%r12
nopw   0x0(%rax,%rax,1)
mov    %rax,%r14
movq   $0x0,(%r15)
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <getcwd@plt>
test   %rax,%rax
jne    11b1 <my_getcmd+0x71>
mov    (%r15),%rax
cmp    (%r12),%rax
jne    11a6 <my_getcmd+0x66>
add    %rbx,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1060 <realloc@plt>
test   %rax,%rax
jne    1170 <my_getcmd+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@errno = global i64 0
@ERANGE = global i64 0
define i8* @my_getcmd() {
%1 = alloca i8*
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
store i64 1024, i64* %4
%5 = load i64, i64* %4
%6 = call i8* @malloc(i64 %5)
store i8* %6, i8** %2
%7 = load i8*, i8** %2
%8 = icmp ne i8* %7, null
br i1 %8, label %10, label %9
9: ; preds = %0
store i8* null, i8** %1
br label %37
10: ; preds = %0
br label %11
11: ; preds = %32, %10
store i64 0, i64* @errno
%12 = load i8*, i8** %2
%13 = load i64, i64* %4
%14 = call i64 @getcwd(i8* %12, i64 %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %11
%17 = load i8*, i8** %2
store i8* %17, i8** %1
br label %37
18: ; preds = %11
%19 = load i64, i64* @errno
%20 = load i64, i64* @ERANGE
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %23
22: ; preds = %18
br label %34
23: ; preds = %18
%24 = load i64, i64* %4
%25 = mul i64 %24, 2
store i64 %25, i64* %4
%26 = load i8*, i8** %2
%27 = load i64, i64* %4
%28 = call i8* @realloc(i8* %26, i64 %27)
store i8* %28, i8** %3
%29 = load i8*, i8** %3
%30 = icmp ne i8* %29, null
br i1 %30, label %32, label %31
31: ; preds = %23
br label %34
32: ; preds = %23
%33 = load i8*, i8** %3
store i8* %33, i8** %2
br label %11
34: ; preds = %31, %22
%35 = load i8*, i8** %2
%36 = call i32 @free(i8* %35)
store i8* null, i8** %1
br label %37
37: ; preds = %34, %16, %9
%38 = load i8*, i8** %1
ret i8* %38
}

LLVM IR (-O2):
@errno = global i64 0
@ERANGE = global i64 0
define i8* @my_getcmd() {
%1 = tail call dereferenceable_or_null(1024) i8* @malloc(i64 1024)
%2 = icmp eq i8* %1, null
br i1 %2, label %18, label %3
3: ; preds = %0, %12
%4 = phi i8* [ %14, %12 ], [ %1, %0 ]
%5 = phi i64 [ %13, %12 ], [ 1024, %0 ]
store i64 0, i64* @errno
%6 = tail call i64 @getcwd(i8* %4, i64 %5)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %18
8: ; preds = %3
%9 = load i64, i64* @errno
%10 = load i64, i64* @ERANGE
%11 = icmp eq i64 %9, %10
br i1 %11, label %12, label %16
12: ; preds = %8
%13 = shl i64 %5, 1
%14 = tail call i8* @realloc(i8* %4, i64 %13)
%15 = icmp eq i8* %14, null
br i1 %15, label %16, label %3
16: ; preds = %12, %8
%17 = tail call i32 @free(i8* %4)
br label %18
18: ; preds = %3, %0, %16
%19 = phi i8* [ null, %16 ], [ null, %0 ], [ %4, %3 ]
ret i8* %19
}