#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;
struct udev {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int readlink(char *, char *, int);
char *strrchr(char *, char);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char const *);
int util_strscpy(char *, size_t, char const *);
int /*<<< orphan*/ util_strscpyl(char *, int, char const *, char *,
                                 char const *, int /*<<< orphan*/ *);
ssize_t get_sys_link(struct udev *udev, const char *slink, const char *syspath,
                     char *value, size_t size) {
  char path[1024];
  char target[1024];
  ssize_t len;
  const char *pos;
  util_strscpyl(path, sizeof(path), syspath, "/", slink, NULL);
  len = readlink(path, target, sizeof(target));
  if (len <= 0 || len == (ssize_t)sizeof(target))
    return -1;
  target[len] = '\0';
  pos = strrchr(target, '/');
  if (pos == NULL)
    return -1;
  pos = &pos[1];
  udev_log_null(udev, "resolved link to: '%s'\n", pos);
  return util_strscpy(value, size, pos);
}


x86 assembly (clang, -O0):
<get_sys_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x840,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
lea    -0x430(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    -0x18(%rbp),%r8
mov    $0x400,%esi
lea    0xe76(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r9d
call   1070 <util_strscpyl@plt>
lea    -0x430(%rbp),%rdi
lea    -0x830(%rbp),%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    %eax,-0x834(%rbp)
cmpl   $0x0,-0x834(%rbp)
jle    11cf <get_sys_link+0x7f>
cmpl   $0x400,-0x834(%rbp)
jne    11db <get_sys_link+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <get_sys_link+0x10c>
movslq -0x834(%rbp),%rax
movb   $0x0,-0x830(%rbp,%rax,1)
lea    -0x830(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x840(%rbp)
cmpq   $0x0,-0x840(%rbp)
jne    121c <get_sys_link+0xcc>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <get_sys_link+0x10c>
mov    -0x840(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x840(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x840(%rbp),%rdx
lea    0xdc2(%rip),%rsi
call   1050 <udev_log_null@plt>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x840(%rbp),%rdx
call   1030 <util_strscpy@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sys_link>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x800,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rsi,%r8
mov    %rdi,%r12
lea    0xe8e(%rip),%rcx
lea    0x400(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x400,%esi
xor    %r9d,%r9d
call   1070 <util_strscpyl@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jle    11eb <get_sys_link+0x9b>
cmp    $0x400,%eax
je     11eb <get_sys_link+0x9b>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11eb <get_sys_link+0x9b>
mov    %rax,%rbx
add    $0x1,%rbx
lea    0xe32(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
call   1050 <udev_log_null@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    %rbx,%rdx
call   1030 <util_strscpy@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@.str = [2 x i8] c"/\00"
@.str.1 = [24 x i8] c"resolved link to: '%s'\0A\00"
define i32 @get_sys_link(%struct.udev* %0, i8* %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca i32
%7 = alloca %struct.udev*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i8*
%11 = alloca i64
%12 = alloca [1024 x i8]
%13 = alloca [1024 x i8]
%14 = alloca i32
%15 = alloca i8*
store %struct.udev* %0, %struct.udev** %7
store i8* %1, i8** %8
store i8* %2, i8** %9
store i8* %3, i8** %10
store i64 %4, i64* %11
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%17 = load i8*, i8** %9
%18 = load i8*, i8** %8
%19 = call i32 @util_strscpyl(i8* %16, i32 1024, i8* %17, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %18, i32* null)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%22 = call i32 @readlink(i8* %20, i8* %21, i32 1024)
store i32 %22, i32* %14
%23 = load i32, i32* %14
%24 = icmp sle i32 %23, 0
br i1 %24, label %28, label %25
25: ; preds = %5
%26 = load i32, i32* %14
%27 = icmp eq i32 %26, 1024
br i1 %27, label %28, label %29
28: ; preds = %25, %5
store i32 -1, i32* %6
br label %48
29: ; preds = %25
%30 = load i32, i32* %14
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 %31
store i8 0, i8* %32
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %13, i64 0, i64 0
%34 = call i8* @strrchr(i8* %33, i8 signext 47)
store i8* %34, i8** %15
%35 = load i8*, i8** %15
%36 = icmp eq i8* %35, null
br i1 %36, label %37, label %38
37: ; preds = %29
store i32 -1, i32* %6
br label %48
38: ; preds = %29
%39 = load i8*, i8** %15
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8* %40, i8** %15
%41 = load %struct.udev*, %struct.udev** %7
%42 = load i8*, i8** %15
%43 = call i32 @udev_log_null(%struct.udev* %41, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %42)
%44 = load i8*, i8** %10
%45 = load i64, i64* %11
%46 = load i8*, i8** %15
%47 = call i32 @util_strscpy(i8* %44, i64 %45, i8* %46)
store i32 %47, i32* %6
br label %48
48: ; preds = %38, %37, %28
%49 = load i32, i32* %6
ret i32 %49
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@.str = [2 x i8] c"/\00"
@.str.1 = [24 x i8] c"resolved link to: '%s'\0A\00"
define i32 @get_sys_link(%struct.udev* %0, i8* %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%10 = call i32 @util_strscpyl(i8* %8, i32 1024, i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* %1, i32* null)
%11 = call i32 @readlink(i8* %8, i8* %9, i32 1024)
%12 = icmp slt i32 %11, 1
%13 = icmp eq i32 %11, 1024
%14 = or i1 %12, %13
br i1 %14, label %24, label %15
15: ; preds = %5
%16 = zext i32 %11 to i64
%17 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %16
store i8 0, i8* %17
%18 = call i8* @strrchr(i8* %9, i8 signext 47)
%19 = icmp eq i8* %18, null
br i1 %19, label %24, label %20
20: ; preds = %15
%21 = getelementptr inbounds i8, i8* %18, i64 1
%22 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %21)
%23 = call i32 @util_strscpy(i8* %3, i64 %4, i8* %21)
br label %24
24: ; preds = %15, %5, %20
%25 = phi i32 [ %23, %20 ], [ -1, %5 ], [ -1, %15 ]
ret i32 %25
}