#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 PATH_MAX;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
char *getcwd(char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
char *xgetcwd(void) {
  char *buf = (char *)malloc(sizeof(char) * (PATH_MAX + 1));
  if (buf == NULL) {
    fprintf(stderr, "malloc failed\n");
    exit(1);
  }
  buf = getcwd(buf, PATH_MAX + 1);
  if (!buf) {
    perror("xgetcwd");
    exit(1);
  }
  return buf;
}


x86 assembly (clang, -O0):
<xgetcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e91(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    119f <xgetcwd+0x4f>
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe70(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    0x2e46(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
call   1030 <getcwd@plt>
mov    %rax,-0x8(%rbp)
cmpq   $0x0,-0x8(%rbp)
jne    11d9 <xgetcwd+0x89>
lea    0xe45(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%rax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<xgetcwd>:
push   %rbx
mov    0x2e98(%rip),%rbx
mov    (%rbx),%edi
add    $0x1,%edi
call   1050 <malloc@plt>
test   %rax,%rax
je     117b <xgetcwd+0x2b>
mov    (%rbx),%esi
add    $0x1,%esi
mov    %rax,%rdi
call   1030 <getcwd@plt>
test   %rax,%rax
je     119a <xgetcwd+0x4a>
pop    %rbx
ret
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
lea    0xe75(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
lea    0xe6e(%rip),%rdi
call   1060 <perror@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"malloc failed\0A\00"
@.str.1 = [8 x i8] c"xgetcwd\00"
define i8* @xgetcwd() {
%1 = alloca i8*
%2 = load i32, i32* @PATH_MAX
%3 = add nsw i32 %2, 1
%4 = sext i32 %3 to i64
%5 = mul i64 1, %4
%6 = trunc i64 %5 to i32
%7 = call i64 @malloc(i32 %6)
%8 = inttoptr i64 %7 to i8*
store i8* %8, i8** %1
%9 = load i8*, i8** %1
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %15
11: ; preds = %0
%12 = load i32, i32* @stderr
%13 = call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %0
%16 = load i8*, i8** %1
%17 = load i32, i32* @PATH_MAX
%18 = add nsw i32 %17, 1
%19 = call i8* @getcwd(i8* %16, i32 %18)
store i8* %19, i8** %1
%20 = load i8*, i8** %1
%21 = icmp ne i8* %20, null
br i1 %21, label %25, label %22
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%24 = call i32 @exit(i32 1)
unreachable
25: ; preds = %15
%26 = load i8*, i8** %1
ret i8* %26
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@stderr = global i32 0
@.str = [15 x i8] c"malloc failed\0A\00"
@.str.1 = [8 x i8] c"xgetcwd\00"
define i8* @xgetcwd() {
%1 = load i32, i32* @PATH_MAX
%2 = add nsw i32 %1, 1
%3 = tail call i64 @malloc(i32 %2)
%4 = icmp eq i64 %3, 0
br i1 %4, label %5, label %9
5: ; preds = %0
%6 = load i32, i32* @stderr
%7 = tail call i32 @fprintf(i32 %6, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
%8 = tail call i32 @exit(i32 1)
unreachable
9: ; preds = %0
%10 = inttoptr i64 %3 to i8*
%11 = load i32, i32* @PATH_MAX
%12 = add nsw i32 %11, 1
%13 = tail call i8* @getcwd(i8* %10, i32 %12)
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %18
15: ; preds = %9
%16 = tail call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%17 = tail call i32 @exit(i32 1)
unreachable
18: ; preds = %9
ret i8* %13
}