#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 strscpy(char *, size_t, char const *);
int /*<<< orphan*/ strscpyl(char *, int, char const *, char *, char const *,
                            int /*<<< orphan*/ *);
ssize_t util_get_sys_core_link_value(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;
  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];
  return strscpy(value, size, pos);
}


x86 assembly (clang, -O0):
<util_get_sys_core_link_value>:
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    0xe86(%rip),%rcx
xor    %eax,%eax
mov    %eax,%r9d
call   1050 <strscpyl@plt>
lea    -0x430(%rbp),%rdi
lea    -0x830(%rbp),%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,-0x834(%rbp)
cmpl   $0x0,-0x834(%rbp)
jle    11bf <util_get_sys_core_link_value+0x7f>
cmpl   $0x400,-0x834(%rbp)
jne    11cb <util_get_sys_core_link_value+0x8b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <util_get_sys_core_link_value+0xf5>
movslq -0x834(%rbp),%rax
movb   $0x0,-0x830(%rbp,%rax,1)
lea    -0x830(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x840(%rbp)
cmpq   $0x0,-0x840(%rbp)
jne    120c <util_get_sys_core_link_value+0xcc>
movl   $0xffffffff,-0x4(%rbp)
jmp    1235 <util_get_sys_core_link_value+0xf5>
mov    -0x840(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x840(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rsi
mov    -0x840(%rbp),%rdx
call   1060 <strscpy@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x840,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_get_sys_core_link_value>:
push   %rbp
push   %r14
push   %rbx
sub    $0x800,%rsp
mov    %r8,%r14
mov    %rcx,%rbx
mov    %rsi,%r8
lea    0xea5(%rip),%rcx
lea    0x400(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x400,%esi
xor    %r9d,%r9d
call   1050 <strscpyl@plt>
mov    %rsp,%rsi
mov    %rbp,%rdi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
jle    11bf <util_get_sys_core_link_value+0x7f>
cmp    $0x400,%eax
je     11bf <util_get_sys_core_link_value+0x7f>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     11bf <util_get_sys_core_link_value+0x7f>
add    $0x1,%rax
mov    %rbx,%rdi
mov    %r14,%rsi
mov    %rax,%rdx
call   1060 <strscpy@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x800,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@.str = [2 x i8] c"/\00"
define i32 @util_get_sys_core_link_value(%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 @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 %45
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 %45
38: ; preds = %29
%39 = load i8*, i8** %15
%40 = getelementptr inbounds i8, i8* %39, i64 1
store i8* %40, i8** %15
%41 = load i8*, i8** %10
%42 = load i64, i64* %11
%43 = load i8*, i8** %15
%44 = call i32 @strscpy(i8* %41, i64 %42, i8* %43)
store i32 %44, i32* %6
br label %45
45: ; preds = %38, %37, %28
%46 = load i32, i32* %6
ret i32 %46
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@.str = [2 x i8] c"/\00"
define i32 @util_get_sys_core_link_value(%struct.udev* nocapture readnone %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 @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 %23, 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 %23, label %20
20: ; preds = %15
%21 = getelementptr inbounds i8, i8* %18, i64 1
%22 = call i32 @strscpy(i8* %3, i64 %4, i8* %21)
br label %23
23: ; preds = %15, %5, %20
%24 = phi i32 [ %22, %20 ], [ -1, %5 ], [ -1, %15 ]
ret i32 %24
}