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

/* Variables and functions */
int EINVAL;
size_t readlink(char *, char *, int);
scalar_t__ startswith(char *, char *);
char *strrchr(char *, char);
int /*<<< orphan*/ strscpyl(char *, size_t, char *, char *,
                            int /*<<< orphan*/ *);
int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size) {
  char link_target[1024];
  ssize_t len;
  int i;
  int back;
  char *base = NULL;
  len = readlink(syspath, link_target, sizeof(link_target));
  if (len <= 0 || len == (ssize_t)sizeof(link_target))
    return -1;
  link_target[len] = '\0';
  for (back = 0; startswith(&link_target[back * 3], "../"); back++)
    ;
  for (i = 0; i <= back; i++) {
    base = strrchr(syspath, '/');
    if (base == NULL)
      return -EINVAL;
    base[0] = '\0';
  }
  strscpyl(base, size - (base - syspath), "/", &link_target[back * 3], NULL);
  return 0;
}


x86 assembly (clang, -O0):
<util_resolve_sys_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movq   $0x0,-0x438(%rbp)
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jbe    119d <util_resolve_sys_link+0x5d>
cmpq   $0x400,-0x428(%rbp)
jne    11a9 <util_resolve_sys_link+0x69>
movl   $0xffffffff,-0x4(%rbp)
jmp    12bb <util_resolve_sys_link+0x17b>
mov    -0x428(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
movl   $0x0,-0x430(%rbp)
imul   $0x3,-0x430(%rbp),%eax
cltq
lea    -0x420(%rbp),%rdi
add    %rax,%rdi
lea    0xe24(%rip),%rsi
call   1050 <startswith@plt>
cmp    $0x0,%rax
je     1204 <util_resolve_sys_link+0xc4>
jmp    11f0 <util_resolve_sys_link+0xb0>
mov    -0x430(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x430(%rbp)
jmp    11c2 <util_resolve_sys_link+0x82>
movl   $0x0,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
cmp    -0x430(%rbp),%eax
jg     1274 <util_resolve_sys_link+0x134>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    1256 <util_resolve_sys_link+0x116>
xor    %eax,%eax
mov    0x2d94(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    12bb <util_resolve_sys_link+0x17b>
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    120e <util_resolve_sys_link+0xce>
mov    -0x438(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x438(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
sub    %rax,%rsi
imul   $0x3,-0x430(%rbp),%eax
cltq
lea    -0x420(%rbp),%rcx
add    %rax,%rcx
lea    0xd58(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1060 <strscpyl@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<util_resolve_sys_link>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rdx,%r15
mov    %rsi,%r14
mov    %rsp,%rbx
mov    %rsi,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1030 <readlink@plt>
test   $0xfffffffffffffbff,%rax
je     11dd <util_resolve_sys_link+0x9d>
movb   $0x0,(%rsp,%rax,1)
xor    %ebp,%ebp
lea    0xe83(%rip),%r12
nopl   (%rax)
mov    %rbx,%rdi
mov    %r12,%rsi
call   1050 <startswith@plt>
add    $0x1,%ebp
add    $0x3,%rbx
test   %rax,%rax
jne    1180 <util_resolve_sys_link+0x40>
add    $0xfffffffffffffffd,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
test   %rax,%rax
je     11e5 <util_resolve_sys_link+0xa5>
movb   $0x0,(%rax)
add    $0xffffffff,%ebp
jne    11a0 <util_resolve_sys_link+0x60>
add    %r15,%r14
sub    %rax,%r14
lea    0xe3b(%rip),%rdx
xor    %r15d,%r15d
mov    %rax,%rdi
mov    %r14,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1060 <strscpyl@plt>
jmp    11f2 <util_resolve_sys_link+0xb2>
mov    $0xffffffff,%r15d
jmp    11f2 <util_resolve_sys_link+0xb2>
mov    0x2df4(%rip),%rax
xor    %r15d,%r15d
sub    (%rax),%r15d
mov    %r15d,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@.str = [4 x i8] c"../\00"
@EINVAL = global i32 0
@.str.1 = [2 x i8] c"/\00"
define i32 @util_resolve_sys_link(%struct.udev* %0, i8* %1, i64 %2) {
%4 = alloca i32
%5 = alloca %struct.udev*
%6 = alloca i8*
%7 = alloca i64
%8 = alloca [1024 x i8]
%9 = alloca i64
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8*
store %struct.udev* %0, %struct.udev** %5
store i8* %1, i8** %6
store i64 %2, i64* %7
store i8* null, i8** %12
%13 = load i8*, i8** %6
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%15 = call i64 @readlink(i8* %13, i8* %14, i32 1024)
store i64 %15, i64* %9
%16 = load i64, i64* %9
%17 = icmp ule i64 %16, 0
br i1 %17, label %21, label %18
18: ; preds = %3
%19 = load i64, i64* %9
%20 = icmp eq i64 %19, 1024
br i1 %20, label %21, label %22
21: ; preds = %18, %3
store i32 -1, i32* %4
br label %69
22: ; preds = %18
%23 = load i64, i64* %9
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %23
store i8 0, i8* %24
store i32 0, i32* %11
br label %25
25: ; preds = %33, %22
%26 = load i32, i32* %11
%27 = mul nsw i32 %26, 3
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %28
%30 = call i64 @startswith(i8* %29, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %36
32: ; preds = %25
br label %33
33: ; preds = %32
%34 = load i32, i32* %11
%35 = add nsw i32 %34, 1
store i32 %35, i32* %11
br label %25
36: ; preds = %25
store i32 0, i32* %10
br label %37
37: ; preds = %52, %36
%38 = load i32, i32* %10
%39 = load i32, i32* %11
%40 = icmp sle i32 %38, %39
br i1 %40, label %41, label %55
41: ; preds = %37
%42 = load i8*, i8** %6
%43 = call i8* @strrchr(i8* %42, i8 signext 47)
store i8* %43, i8** %12
%44 = load i8*, i8** %12
%45 = icmp eq i8* %44, null
br i1 %45, label %46, label %49
46: ; preds = %41
%47 = load i32, i32* @EINVAL
%48 = sub nsw i32 0, %47
store i32 %48, i32* %4
br label %69
49: ; preds = %41
%50 = load i8*, i8** %12
%51 = getelementptr inbounds i8, i8* %50, i64 0
store i8 0, i8* %51
br label %52
52: ; preds = %49
%53 = load i32, i32* %10
%54 = add nsw i32 %53, 1
store i32 %54, i32* %10
br label %37
55: ; preds = %37
%56 = load i8*, i8** %12
%57 = load i64, i64* %7
%58 = load i8*, i8** %12
%59 = load i8*, i8** %6
%60 = ptrtoint i8* %58 to i64
%61 = ptrtoint i8* %59 to i64
%62 = sub i64 %60, %61
%63 = sub i64 %57, %62
%64 = load i32, i32* %11
%65 = mul nsw i32 %64, 3
%66 = sext i32 %65 to i64
%67 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %66
%68 = call i32 @strscpyl(i8* %56, i64 %63, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %67, i32* null)
store i32 0, i32* %4
br label %69
69: ; preds = %55, %46, %21
%70 = load i32, i32* %4
ret i32 %70
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@.str = [4 x i8] c"../\00"
@EINVAL = global i32 0
@.str.1 = [2 x i8] c"/\00"
define i32 @util_resolve_sys_link(%struct.udev* nocapture readnone %0, i8* %1, i64 %2) {
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%6 = call i64 @readlink(i8* %1, i8* %5, i32 1024)
%7 = and i64 %6, -1025
%8 = icmp eq i64 %7, 0
br i1 %8, label %36, label %9
9: ; preds = %3
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %6
store i8 0, i8* %10
br label %11
11: ; preds = %11, %9
%12 = phi i64 [ %17, %11 ], [ 0, %9 ]
%13 = mul nuw nsw i64 %12, 3
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %13
%15 = call i64 @startswith(i8* %14, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%16 = icmp eq i64 %15, 0
%17 = add nuw i64 %12, 1
br i1 %16, label %18, label %11
18: ; preds = %11
%19 = trunc i64 %12 to i32
br label %20
20: ; preds = %18, %27
%21 = phi i32 [ 0, %18 ], [ %28, %27 ]
%22 = call i8* @strrchr(i8* dereferenceable(1) %1, i8 signext 47)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = load i32, i32* @EINVAL
%26 = sub nsw i32 0, %25
br label %36
27: ; preds = %20
store i8 0, i8* %22
%28 = add nuw i32 %21, 1
%29 = icmp eq i32 %21, %19
br i1 %29, label %30, label %20
30: ; preds = %27
%31 = ptrtoint i8* %22 to i64
%32 = ptrtoint i8* %1 to i64
%33 = add i64 %32, %2
%34 = sub i64 %33, %31
%35 = call i32 @strscpyl(i8* %22, i64 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* %14, i32* null)
br label %36
36: ; preds = %3, %30, %24
%37 = phi i32 [ %26, %24 ], [ 0, %30 ], [ -1, %3 ]
ret i32 %37
}