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

/* Variables and functions */
scalar_t__ EINVAL;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int readlink(char *, char *, int);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  char target_path[256];
  char *link_path = argv[1];
  int len = readlink(link_path, target_path, sizeof(target_path));
  if (len == -1) {
    if (errno == EINVAL)
      fprintf(stderr, "%s is not a symbolic link\n", link_path);
    else
      perror("readlink");
    return 1;
  } else {
    target_path[len] = '\0';
    printf("%s\n", target_path);
    return 0;
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x118(%rbp)
mov    -0x118(%rbp),%rdi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1030 <readlink@plt>
mov    %eax,-0x11c(%rbp)
cmpl   $0xffffffff,-0x11c(%rbp)
jne    11e6 <main+0xa6>
mov    0x2e3e(%rip),%rax
mov    (%rax),%rax
mov    0x2e2c(%rip),%rcx
cmp    (%rcx),%rax
jne    11ce <main+0x8e>
mov    0x2e2c(%rip),%rax
mov    (%rax),%edi
mov    -0x118(%rbp),%rdx
lea    0xe3c(%rip),%rsi
call   1050 <fprintf@plt>
jmp    11da <main+0x9a>
lea    0xe46(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    120f <main+0xcf>
movslq -0x11c(%rbp),%rax
movb   $0x0,-0x110(%rbp,%rax,1)
lea    -0x110(%rbp),%rsi
lea    0xe21(%rip),%rdi
call   1040 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbx
sub    $0x100,%rsp
mov    0x8(%rsi),%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x100,%edx
call   1030 <readlink@plt>
cmp    $0xffffffff,%eax
je     117a <main+0x3a>
cltq
movb   $0x0,(%rsp,%rax,1)
lea    0xeb6(%rip),%rdi
mov    %rsp,%rsi
call   1040 <printf@plt>
xor    %eax,%eax
jmp    11ad <main+0x6d>
mov    0x2e57(%rip),%rax
mov    (%rax),%rax
mov    0x2e45(%rip),%rcx
cmp    (%rcx),%rax
jne    11b6 <main+0x76>
mov    0x2e49(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
mov    %rbx,%rdx
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x100,%rsp
pop    %rbx
ret
lea    0xe5e(%rip),%rdi
call   1060 <perror@plt>
jmp    11a8 <main+0x68>

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"%s is not a symbolic link\0A\00"
@.str.1 = [9 x i8] c"readlink\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i8*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i8**, i8*** %5
%10 = getelementptr inbounds i8*, i8** %9, i64 1
%11 = load i8*, i8** %10
store i8* %11, i8** %7
%12 = load i8*, i8** %7
%13 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%14 = call i32 @readlink(i8* %12, i8* %13, i32 256)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %28
17: ; preds = %2
%18 = load i64, i64* @errno
%19 = load i64, i64* @EINVAL
%20 = icmp eq i64 %18, %19
br i1 %20, label %21, label %25
21: ; preds = %17
%22 = load i32, i32* @stderr
%23 = load i8*, i8** %7
%24 = call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %27
25: ; preds = %17
%26 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %27
27: ; preds = %25, %21
store i32 1, i32* %3
br label %34
28: ; preds = %2
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %30
store i8 0, i8* %31
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%33 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %32)
store i32 0, i32* %3
br label %34
34: ; preds = %28, %27
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
@.str = [27 x i8] c"%s is not a symbolic link\0A\00"
@.str.1 = [9 x i8] c"readlink\00"
@.str.2 = [4 x i8] c"%s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = getelementptr inbounds i8*, i8** %1, i64 1
%6 = load i8*, i8** %5
%7 = call i32 @readlink(i8* %6, i8* %4, i32 256)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %18
9: ; preds = %2
%10 = load i64, i64* @errno
%11 = load i64, i64* @EINVAL
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %16
13: ; preds = %9
%14 = load i32, i32* @stderr
%15 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %22
16: ; preds = %9
%17 = tail call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %22
18: ; preds = %2
%19 = sext i32 %7 to i64
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8* %4)
br label %22
22: ; preds = %13, %16, %18
%23 = phi i32 [ 0, %18 ], [ 1, %16 ], [ 1, %13 ]
ret i32 %23
}