#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 /*<<< orphan*/ EXIT_FAILURE;
int PATH_MAX;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, char const *);
int /*<<< orphan*/ *getcwd(char *, int);
int /*<<< orphan*/ perror(char *);
int readlink(char *, char *, int);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char const *);
void check_cwd(const char *expected) {
  char path[PATH_MAX];
  int status;
  if (getcwd(path, PATH_MAX) == NULL) {
    perror("getcwd");
    exit(EXIT_FAILURE);
  }
  if (strcmp(path, expected) != 0) {
    fprintf(stderr, "getcwd: %s != %s\n", path, expected);
    exit(EXIT_FAILURE);
  }
  status = readlink("/proc/self/cwd", path, PATH_MAX - 1);
  if (status < 0) {
    perror("readlink");
    exit(EXIT_FAILURE);
  }
  path[status] = '\0';
  if (strcmp(path, expected) != 0) {
    fprintf(stderr, "readlink /proc/self/cwd: %s != %s\n", path, expected);
    exit(EXIT_FAILURE);
  }
}


x86 assembly (clang, -O0):
<check_cwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    0x2e7d(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%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,-0x18(%rbp)
mov    0x2e51(%rip),%rax
mov    (%rax),%esi
call   1040 <getcwd@plt>
cmp    $0x0,%rax
jne    11ca <check_cwd+0x6a>
lea    0xe49(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e0d(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     120c <check_cwd+0xac>
mov    -0x28(%rbp),%rdx
mov    0x2dec(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rcx
lea    0xe0e(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rsi
mov    0x2dd9(%rip),%rax
mov    (%rax),%edx
sub    $0x1,%edx
lea    0xdf6(%rip),%rdi
call   1030 <readlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    124f <check_cwd+0xef>
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
mov    0x2d88(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x28(%rbp),%rdi
movslq -0x1c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    -0x8(%rbp),%rsi
call   1050 <strcmp@plt>
cmp    $0x0,%rax
je     1299 <check_cwd+0x139>
mov    -0x28(%rbp),%rdx
mov    0x2d5f(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rcx
lea    0xdab(%rip),%rsi
call   1060 <fprintf@plt>
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_cwd>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    0x2e7c(%rip),%r15
mov    (%r15),%esi
mov    %rsp,%rbx
mov    %rsi,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
call   1040 <getcwd@plt>
test   %rax,%rax
je     11e2 <check_cwd+0x82>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    11eb <check_cwd+0x8b>
mov    (%r15),%edx
add    $0xffffffff,%edx
lea    0xe64(%rip),%rdi
mov    %rbx,%rsi
call   1030 <readlink@plt>
test   %eax,%eax
js     11fd <check_cwd+0x9d>
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <strcmp@plt>
test   %rax,%rax
jne    120b <check_cwd+0xab>
lea    -0x18(%rbp),%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe17(%rip),%rdi
jmp    1204 <check_cwd+0xa4>
mov    0x2de6(%rip),%rax
mov    (%rax),%edi
lea    0xe0c(%rip),%rsi
jmp    121b <check_cwd+0xbb>
lea    0xe24(%rip),%rdi
call   1070 <perror@plt>
jmp    1226 <check_cwd+0xc6>
mov    0x2dc6(%rip),%rax
mov    (%rax),%edi
lea    0xe16(%rip),%rsi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1060 <fprintf@plt>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1080 <exit@plt>

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [7 x i8] c"getcwd\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"getcwd: %s != %s\0A\00"
@.str.2 = [15 x i8] c"/proc/self/cwd\00"
@.str.3 = [9 x i8] c"readlink\00"
@.str.4 = [35 x i8] c"readlink /proc/self/cwd: %s != %s\0A\00"
define void @check_cwd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca i32
store i8* %0, i8** %2
%6 = load i32, i32* @PATH_MAX
%7 = zext i32 %6 to i64
%8 = call i8* @llvm.stacksave()
store i8* %8, i8** %3
%9 = alloca i8, i64 %7
store i64 %7, i64* %4
%10 = load i32, i32* @PATH_MAX
%11 = call i32* @getcwd(i8* %9, i32 %10)
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %17
13: ; preds = %1
%14 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = load i32, i32* @EXIT_FAILURE
%16 = call i32 @exit(i32 %15)
unreachable
17: ; preds = %1
%18 = load i8*, i8** %2
%19 = call i64 @strcmp(i8* %9, i8* %18)
%20 = icmp ne i64 %19, 0
br i1 %20, label %21, label %27
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %2
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %9, i8* %23)
%25 = load i32, i32* @EXIT_FAILURE
%26 = call i32 @exit(i32 %25)
unreachable
27: ; preds = %17
%28 = load i32, i32* @PATH_MAX
%29 = sub nsw i32 %28, 1
%30 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %9, i32 %29)
store i32 %30, i32* %5
%31 = load i32, i32* %5
%32 = icmp slt i32 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %27
%34 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%35 = load i32, i32* @EXIT_FAILURE
%36 = call i32 @exit(i32 %35)
unreachable
37: ; preds = %27
%38 = load i32, i32* %5
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %9, i64 %39
store i8 0, i8* %40
%41 = load i8*, i8** %2
%42 = call i64 @strcmp(i8* %9, i8* %41)
%43 = icmp ne i64 %42, 0
br i1 %43, label %44, label %50
44: ; preds = %37
%45 = load i32, i32* @stderr
%46 = load i8*, i8** %2
%47 = call i32 @fprintf(i32 %45, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %9, i8* %46)
%48 = load i32, i32* @EXIT_FAILURE
%49 = call i32 @exit(i32 %48)
unreachable
50: ; preds = %37
%51 = load i8*, i8** %3
ret void
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [7 x i8] c"getcwd\00"
@EXIT_FAILURE = global i32 0
@stderr = global i32 0
@.str.1 = [18 x i8] c"getcwd: %s != %s\0A\00"
@.str.2 = [15 x i8] c"/proc/self/cwd\00"
@.str.3 = [9 x i8] c"readlink\00"
@.str.4 = [35 x i8] c"readlink /proc/self/cwd: %s != %s\0A\00"
define void @check_cwd(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = call i32* @getcwd(i8* %4, i32 %2)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
%10 = call i32 @exit(i32 %9)
unreachable
11: ; preds = %1
%12 = call i64 @strcmp(i8* %4, i8* %0)
%13 = icmp eq i64 %12, 0
br i1 %13, label %19, label %14
14: ; preds = %11
%15 = load i32, i32* @stderr
%16 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %4, i8* %0)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; preds = %11
%20 = load i32, i32* @PATH_MAX
%21 = add nsw i32 %20, -1
%22 = call i32 @readlink(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0), i8* %4, i32 %21)
%23 = icmp slt i32 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%26 = load i32, i32* @EXIT_FAILURE
%27 = call i32 @exit(i32 %26)
unreachable
28: ; preds = %19
%29 = zext i32 %22 to i64
%30 = getelementptr inbounds i8, i8* %4, i64 %29
store i8 0, i8* %30
%31 = call i64 @strcmp(i8* %4, i8* %0)
%32 = icmp eq i64 %31, 0
br i1 %32, label %38, label %33
33: ; preds = %28
%34 = load i32, i32* @stderr
%35 = call i32 @fprintf(i32 %34, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.4, i64 0, i64 0), i8* %4, i8* %0)
%36 = load i32, i32* @EXIT_FAILURE
%37 = call i32 @exit(i32 %36)
unreachable
38: ; preds = %28
ret void
}