#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 /*<<< orphan*/ EXIT_SUCCESS;
int PATH_MAX;
int /*<<< orphan*/ SYS_getcwd;
int /*<<< orphan*/ SYS_readlink;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ stderr;
int strlen(char *);
int syscall(int /*<<< orphan*/, char *, ...);
int main(void) {
  char path[PATH_MAX];
  int status;
  status = syscall(SYS_readlink, "/proc/self/cwd", path, PATH_MAX);
  if (status < 0) {
    perror("readlink()");
    exit(EXIT_FAILURE);
  }
  path[status] = '\0';
  if (status != strlen(path)) {
    fprintf(stderr, "readlink() returned the wrong size %d != %z.\n", status,
            strlen(path));
    exit(EXIT_FAILURE);
  }
  status = syscall(SYS_getcwd, path, PATH_MAX);
  if (status < 0) {
    perror("getcwd()");
    exit(EXIT_FAILURE);
  }
  if (status != strlen(path) + 1) {
    fprintf(stderr, "getcwd() returned the wrong size %d != %z.\n", status,
            strlen(path));
    exit(EXIT_FAILURE);
  }
  exit(EXIT_SUCCESS);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e8a(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdx
sub    %rcx,%rdx
mov    %rdx,-0x28(%rbp)
mov    %rdx,%rsp
mov    %rax,-0x18(%rbp)
mov    0x2e3e(%rip),%rax
mov    (%rax),%edi
mov    0x2e55(%rip),%rax
mov    (%rax),%ecx
lea    0xe5c(%rip),%rsi
mov    $0x0,%al
call   1050 <syscall@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d2 <main+0x82>
lea    0xe50(%rip),%rdi
call   1060 <perror@plt>
mov    0x2df5(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
movslq -0x1c(%rbp),%rax
movb   $0x0,(%rdi,%rax,1)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x2c(%rbp)
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x2c(%rbp),%eax
cmp    %ecx,%eax
je     1233 <main+0xe3>
mov    -0x28(%rbp),%rdi
mov    0x2dc7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x34(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
call   1030 <strlen@plt>
mov    -0x34(%rbp),%edi
mov    -0x30(%rbp),%edx
mov    %eax,%ecx
lea    0xdfa(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rsi
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
mov    0x2da9(%rip),%rax
mov    (%rax),%edx
mov    $0x0,%al
call   1050 <syscall@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1277 <main+0x127>
lea    0xde4(%rip),%rdi
call   1060 <perror@plt>
mov    0x2d50(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    -0x28(%rbp),%rdi
mov    -0x1c(%rbp),%eax
mov    %eax,-0x38(%rbp)
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x38(%rbp),%eax
add    $0x1,%ecx
cmp    %ecx,%eax
je     12d3 <main+0x183>
mov    -0x28(%rbp),%rdi
mov    0x2d27(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x3c(%rbp)
call   1030 <strlen@plt>
mov    -0x40(%rbp),%edi
mov    -0x3c(%rbp),%edx
mov    %eax,%ecx
lea    0xd91(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2cf4(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2cde(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    0x2e8f(%rip),%r15
mov    (%r15),%ecx
mov    %rsp,%rbx
mov    %rcx,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    0x2e51(%rip),%rax
mov    (%rax),%edi
lea    0xe78(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1050 <syscall@plt>
test   %eax,%eax
jns    11a7 <main+0x57>
lea    0xe72(%rip),%rdi
call   1060 <perror@plt>
jmp    1242 <main+0xf2>
mov    %eax,%r14d
mov    %eax,%eax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    %eax,%r14d
jne    11e0 <main+0x90>
mov    0x2e14(%rip),%rax
mov    (%rax),%edi
mov    (%r15),%edx
mov    %rbx,%rsi
xor    %eax,%eax
call   1050 <syscall@plt>
test   %eax,%eax
jns    11fb <main+0xab>
lea    0xe6a(%rip),%rdi
jmp    119d <main+0x4d>
mov    0x2de1(%rip),%rax
mov    (%rax),%r15d
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0xe21(%rip),%rsi
jmp    1235 <main+0xe5>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cmp    %eax,%r14d
jne    121c <main+0xcc>
mov    0x2da3(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
mov    0x2da5(%rip),%rax
mov    (%rax),%r15d
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0xe1c(%rip),%rsi
mov    %r15d,%edi
mov    %r14d,%edx
mov    %eax,%ecx
call   1040 <fprintf@plt>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

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

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