#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__ chdir(char const *);
int /*<<< orphan*/ free(char *);
char *getcwd(int /*<<< orphan*/ *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char const *);
int /*<<< orphan*/ printf(char *, char *);
int doit(const char *path) {
  char *buf;
  if (chdir(path) != 0) {
    perror(path);
    return -1;
  }
  buf = getcwd(NULL, 0);
  if (!buf) {
    perror(path);
    return -1;
  }
  (void)printf("%s\n", buf);
  free(buf);
  return 0;
}


x86 assembly (clang, -O0):
<doit>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <chdir@plt>
cmp    $0x0,%rax
je     1184 <doit+0x34>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <doit+0x83>
xor    %eax,%eax
mov    %eax,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11b3 <doit+0x63>
mov    -0x10(%rbp),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11d3 <doit+0x83>
mov    -0x18(%rbp),%rsi
lea    0xe42(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doit>:
push   %r15
push   %r14
push   %rbx
mov    %rdi,%r15
call   1040 <chdir@plt>
test   %rax,%rax
jne    1196 <doit+0x46>
xor    %r14d,%r14d
xor    %edi,%edi
xor    %esi,%esi
call   1060 <getcwd@plt>
test   %rax,%rax
je     1196 <doit+0x46>
mov    %rax,%rbx
lea    0xe83(%rip),%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    %r14d,%eax
pop    %rbx
pop    %r14
pop    %r15
ret
mov    %r15,%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%r14d
jmp    118d <doit+0x3d>

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

LLVM IR (-O2):
@.str = [4 x i8] c"%s\0A\00"
define i32 @doit(i8* %0) {
%2 = tail call i64 @chdir(i8* %0)
%3 = icmp eq i64 %2, 0
br i1 %3, label %6, label %4
4: ; preds = %1
%5 = tail call i32 @perror(i8* %0)
br label %14
6: ; preds = %1
%7 = tail call i8* @getcwd(i32* null, i32 0)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = tail call i32 @perror(i8* %0)
br label %14
11: ; preds = %6
%12 = tail call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* %7)
%13 = tail call i32 @free(i8* %7)
br label %14
14: ; preds = %11, %9, %4
%15 = phi i32 [ -1, %4 ], [ 0, %11 ], [ -1, %9 ]
ret i32 %15
}