#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 ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int errno;
int readlink(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
int cm_link_get_target(char *path, char **target) {
  char buf[256];
  ssize_t l;
  errno = 0;
  l = readlink(path, buf, sizeof(buf) - 1);
  if (l == -1) {
    return -errno;
  }
  buf[l] = '\0';
  strncpy(*target, buf, sizeof(buf));
  return 0;
}


x86 assembly (clang, -O0):
<cm_link_get_target>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    0x2ea6(%rip),%rax
movl   $0x0,(%rax)
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x124(%rbp)
cmpl   $0xffffffff,-0x124(%rbp)
jne    117b <cm_link_get_target+0x5b>
xor    %eax,%eax
mov    0x2e6f(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    11a9 <cm_link_get_target+0x89>
movslq -0x124(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
mov    -0x18(%rbp),%rax
mov    (%rax),%rdi
lea    -0x120(%rbp),%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<cm_link_get_target>:
push   %r14
push   %rbx
sub    $0x108,%rsp
mov    %rsi,%rbx
mov    0x2eac(%rip),%r14
movl   $0x0,(%r14)
mov    %rsp,%rsi
mov    $0xff,%edx
call   1040 <readlink@plt>
cmp    $0xffffffff,%eax
je     1167 <cm_link_get_target+0x47>
cltq
movb   $0x0,(%rsp,%rax,1)
mov    (%rbx),%rdi
mov    %rsp,%rsi
mov    $0x100,%edx
call   1030 <strncpy@plt>
xor    %eax,%eax
jmp    116c <cm_link_get_target+0x4c>
xor    %eax,%eax
sub    (%r14),%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@errno = global i32 0
define i32 @cm_link_get_target(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca [256 x i8]
%7 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
store i32 0, i32* @errno
%8 = load i8*, i8** %4
%9 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%10 = call i32 @readlink(i8* %8, i8* %9, i32 255)
store i32 %10, i32* %7
%11 = load i32, i32* %7
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @errno
%15 = sub nsw i32 0, %14
store i32 %15, i32* %3
br label %24
16: ; preds = %2
%17 = load i32, i32* %7
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %18
store i8 0, i8* %19
%20 = load i8**, i8*** %5
%21 = load i8*, i8** %20
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%23 = call i32 @strncpy(i8* %21, i8* %22, i32 256)
store i32 0, i32* %3
br label %24
24: ; preds = %16, %13
%25 = load i32, i32* %3
ret i32 %25
}

LLVM IR (-O2):
@errno = global i32 0
define i32 @cm_link_get_target(i8* nocapture %0, i8** nocapture %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
store i32 0, i32* @errno
%5 = call i32 @readlink(i8* %0, i8* %4, i32 255)
%6 = icmp eq i32 %5, -1
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = load i32, i32* @errno
%9 = sub nsw i32 0, %8
br label %15
10: ; preds = %2
%11 = sext i32 %5 to i64
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %11
store i8 0, i8* %12
%13 = load i8*, i8** %1
%14 = call i32 @strncpy(i8* %13, i8* %4, i32 256)
br label %15
15: ; preds = %10, %7
%16 = phi i32 [ %9, %7 ], [ 0, %10 ]
ret i32 %16
}