#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 */
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 /*<<< orphan*/ util_strlcat(char *, char const *, int);
int util_strlcpy(char *, char const *, size_t);
ssize_t get_sys_link(struct udev *udev, const char *slink, const char *syspath,
                     char *value, size_t size) {
  char path[1024];
  ssize_t len;
  const char *pos;
  util_strlcpy(path, syspath, sizeof(path));
  util_strlcat(path, "/", sizeof(path));
  util_strlcat(path, slink, sizeof(path));
  len = readlink(path, path, sizeof(path));
  if (len < 0 || len >= (ssize_t)sizeof(path))
    return -1;
  path[len] = '\0';
  pos = strrchr(path, '/');
  if (pos == NULL)
    return -1;
  pos = &pos[1];
  udev_log_null(udev, "resolved link to: '%s'\n", pos);
  return util_strlcpy(value, pos, size);
}


x86 assembly (clang, -O0):
<get_sys_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%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),%rsi
mov    $0x400,%edx
call   1060 <util_strlcpy@plt>
lea    -0x430(%rbp),%rdi
lea    0xe6e(%rip),%rsi
mov    $0x400,%edx
call   1070 <util_strlcat@plt>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    $0x400,%edx
call   1070 <util_strlcat@plt>
lea    -0x430(%rbp),%rdi
lea    -0x430(%rbp),%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %eax,-0x434(%rbp)
cmpl   $0x0,-0x434(%rbp)
jl     11ec <get_sys_link+0x9c>
cmpl   $0x400,-0x434(%rbp)
jl     11f8 <get_sys_link+0xa8>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <get_sys_link+0x129>
movslq -0x434(%rbp),%rax
movb   $0x0,-0x430(%rbp,%rax,1)
lea    -0x430(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
jne    1239 <get_sys_link+0xe9>
movl   $0xffffffff,-0x4(%rbp)
jmp    1279 <get_sys_link+0x129>
mov    -0x440(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x440(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x440(%rbp),%rdx
lea    0xda5(%rip),%rsi
call   1040 <udev_log_null@plt>
mov    -0x28(%rbp),%rdi
mov    -0x440(%rbp),%rsi
mov    -0x30(%rbp),%rdx
call   1060 <util_strlcpy@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_sys_link>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %r8,%r14
mov    %rcx,%r15
mov    %rdx,%rax
mov    %rsi,%rbx
mov    %rdi,%r12
mov    %rsp,%rbp
mov    $0x400,%edx
mov    %rbp,%rdi
mov    %rax,%rsi
call   1060 <util_strlcpy@plt>
lea    0xe78(%rip),%rsi
mov    %rbp,%rdi
mov    $0x400,%edx
call   1070 <util_strlcat@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1070 <util_strlcat@plt>
mov    %rbp,%rdi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    $0xffffffff,%ebp
cmp    $0x3ff,%eax
ja     1202 <get_sys_link+0xb2>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
test   %rax,%rax
je     1202 <get_sys_link+0xb2>
mov    %rax,%rbx
add    $0x1,%rbx
lea    0xe1b(%rip),%rsi
mov    %r12,%rdi
mov    %rbx,%rdx
call   1040 <udev_log_null@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <util_strlcpy@plt>
mov    %eax,%ebp
mov    %ebp,%eax
add    $0x400,%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 i32
%14 = 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
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%16 = load i8*, i8** %9
%17 = call i32 @util_strlcpy(i8* %15, i8* %16, i64 1024)
%18 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%19 = call i32 @util_strlcat(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1024)
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%21 = load i8*, i8** %8
%22 = call i32 @util_strlcat(i8* %20, i8* %21, i32 1024)
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%25 = call i32 @readlink(i8* %23, i8* %24, i32 1024)
store i32 %25, i32* %13
%26 = load i32, i32* %13
%27 = icmp slt i32 %26, 0
br i1 %27, label %31, label %28
28: ; preds = %5
%29 = load i32, i32* %13
%30 = icmp sge i32 %29, 1024
br i1 %30, label %31, label %32
31: ; preds = %28, %5
store i32 -1, i32* %6
br label %51
32: ; preds = %28
%33 = load i32, i32* %13
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 %34
store i8 0, i8* %35
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %12, i64 0, i64 0
%37 = call i8* @strrchr(i8* %36, i8 signext 47)
store i8* %37, i8** %14
%38 = load i8*, i8** %14
%39 = icmp eq i8* %38, null
br i1 %39, label %40, label %41
40: ; preds = %32
store i32 -1, i32* %6
br label %51
41: ; preds = %32
%42 = load i8*, i8** %14
%43 = getelementptr inbounds i8, i8* %42, i64 1
store i8* %43, i8** %14
%44 = load %struct.udev*, %struct.udev** %7
%45 = load i8*, i8** %14
%46 = call i32 @udev_log_null(%struct.udev* %44, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %45)
%47 = load i8*, i8** %10
%48 = load i8*, i8** %14
%49 = load i64, i64* %11
%50 = call i32 @util_strlcpy(i8* %47, i8* %48, i64 %49)
store i32 %50, i32* %6
br label %51
51: ; preds = %41, %40, %31
%52 = load i32, i32* %6
ret i32 %52
}

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 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%8 = call i32 @util_strlcpy(i8* %7, i8* %2, i64 1024)
%9 = call i32 @util_strlcat(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i32 1024)
%10 = call i32 @util_strlcat(i8* %7, i8* %1, i32 1024)
%11 = call i32 @readlink(i8* %7, i8* %7, i32 1024)
%12 = icmp ugt i32 %11, 1023
br i1 %12, label %22, label %13
13: ; preds = %5
%14 = zext i32 %11 to i64
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 %14
store i8 0, i8* %15
%16 = call i8* @strrchr(i8* %7, i8 signext 47)
%17 = icmp eq i8* %16, null
br i1 %17, label %22, label %18
18: ; preds = %13
%19 = getelementptr inbounds i8, i8* %16, i64 1
%20 = call i32 @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i8* %19)
%21 = call i32 @util_strlcpy(i8* %3, i8* %19, i64 %4)
br label %22
22: ; preds = %13, %5, %18
%23 = phi i32 [ %21, %18 ], [ -1, %5 ], [ -1, %13 ]
ret i32 %23
}