#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*/ buf;

/* Variables and functions */
int /*<<< orphan*/ MAXPATHLEN;
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
void cmd_getcwd(int argc, char *argv[]) {
  char buf[MAXPATHLEN + 1];
  if (getcwd(buf, sizeof(buf)) == NULL)
    perror("getcwd");
  else
    printf("%s\n", buf);
}


x86 assembly (clang, -O0):
<cmd_getcwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2ea2(%rip),%rax
mov    (%rax),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x28(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x20(%rbp)
mov    %eax,%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
jne    1190 <cmd_getcwd+0x60>
lea    0xe7a(%rip),%rdi
call   1050 <perror@plt>
jmp    11a0 <cmd_getcwd+0x70>
mov    -0x28(%rbp),%rsi
lea    0xe6c(%rip),%rdi
call   1030 <printf@plt>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cmd_getcwd>:
push   %rbp
mov    %rsp,%rbp
push   %rbx
push   %rax
mov    0x2eab(%rip),%rax
mov    (%rax),%esi
add    $0x1,%esi
mov    %rsp,%rbx
lea    0xf(%rsi),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1040 <getcwd@plt>
test   %rax,%rax
je     1176 <cmd_getcwd+0x46>
lea    0xea0(%rip),%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
lea    -0x8(%rbp),%rsp
pop    %rbx
pop    %rbp
ret
lea    0xe83(%rip),%rdi
call   1050 <perror@plt>
jmp    116f <cmd_getcwd+0x3f>

LLVM IR (-O0):
@MAXPATHLEN = global i32 0
@.str = [7 x i8] c"getcwd\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define void @cmd_getcwd(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i8*
%6 = alloca i64
store i32 %0, i32* %3
store i8** %1, i8*** %4
%7 = load i32, i32* @MAXPATHLEN
%8 = add nsw i32 %7, 1
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %5
%11 = alloca i8, i64 %9
store i64 %9, i64* %6
%12 = trunc i64 %9 to i32
%13 = call i32* @getcwd(i8* %11, i32 %12)
%14 = icmp eq i32* %13, null
br i1 %14, label %15, label %17
15: ; preds = %2
%16 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %19
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %11)
br label %19
19: ; preds = %17, %15
%20 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@MAXPATHLEN = global i32 0
@.str = [7 x i8] c"getcwd\00"
@.str.1 = [4 x i8] c"%s\0A\00"
define void @cmd_getcwd(i32 %0, i8** nocapture readnone %1) {
%3 = load i32, i32* @MAXPATHLEN
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32* @getcwd(i8* %6, i32 %4)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %13
11: ; preds = %2
%12 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %6)
br label %13
13: ; preds = %11, %9
ret void
}