#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 */
size_t readlink(char *, char *, int);
scalar_t__ strncmp(char *, char *, int);
char *strrchr(char *, char);
int /*<<< orphan*/ udev_log_null(struct udev *, char *, char *, ...);
int /*<<< orphan*/ util_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;
  len = readlink(syspath, link_target, sizeof(link_target));
  if (len <= 0 || len == (ssize_t)sizeof(link_target))
    return -1;
  link_target[len] = '\0';
  udev_log_null(udev, "path link '%s' points to '%s'\n", syspath, link_target);
  for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
    ;
  udev_log_null(udev, "base '%s', tail '%s', back %i\n", syspath,
                &link_target[back * 3], back);
  for (i = 0; i <= back; i++) {
    base = strrchr(syspath, '/');
    if (base == NULL)
      return -1;
    base[0] = '\0';
  }
  udev_log_null(udev, "after moving back '%s'\n", syspath);
  util_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)
mov    -0x18(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    %rax,-0x428(%rbp)
cmpq   $0x0,-0x428(%rbp)
jbe    11a2 <util_resolve_sys_link+0x52>
cmpq   $0x400,-0x428(%rbp)
jne    11ae <util_resolve_sys_link+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <util_resolve_sys_link+0x1d1>
mov    -0x428(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x420(%rbp),%rcx
lea    0xe2d(%rip),%rsi
mov    $0x0,%al
call   1050 <udev_log_null@plt>
movl   $0x0,-0x430(%rbp)
imul   $0x3,-0x430(%rbp),%eax
cltq
lea    -0x420(%rbp),%rdi
add    %rax,%rdi
lea    0xe21(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    122b <util_resolve_sys_link+0xdb>
jmp    1217 <util_resolve_sys_link+0xc7>
mov    -0x430(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x430(%rbp)
jmp    11e4 <util_resolve_sys_link+0x94>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
imul   $0x3,-0x430(%rbp),%eax
cltq
lea    -0x420(%rbp),%rcx
add    %rax,%rcx
mov    -0x430(%rbp),%r8d
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1050 <udev_log_null@plt>
movl   $0x0,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
cmp    -0x430(%rbp),%eax
jg     12c4 <util_resolve_sys_link+0x174>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    12a6 <util_resolve_sys_link+0x156>
movl   $0xffffffff,-0x4(%rbp)
jmp    1321 <util_resolve_sys_link+0x1d1>
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    1265 <util_resolve_sys_link+0x115>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd6f(%rip),%rsi
mov    $0x0,%al
call   1050 <udev_log_null@plt>
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    0xd11(%rip),%rdx
xor    %eax,%eax
mov    %eax,%r8d
call   1070 <util_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   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rdx,%r15
mov    %rsi,%r12
mov    %rdi,%r13
mov    %rsp,%rbx
mov    %rsi,%rdi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    $0xffffffff,%r14d
test   $0xfffffffffffffbff,%rax
je     125b <util_resolve_sys_link+0x10b>
movb   $0x0,(%rsp,%rax,1)
lea    0xe66(%rip),%rsi
xor    %r14d,%r14d
mov    %r13,%rdi
mov    %r12,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <udev_log_null@plt>
lea    0xe6b(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rbx,%rdi
mov    %rbp,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
add    $0x1,%r14d
add    $0x3,%rbx
test   %rax,%rax
je     11c0 <util_resolve_sys_link+0x70>
lea    -0x1(%r14),%r8d
add    $0xfffffffffffffffd,%rbx
lea    0xe37(%rip),%rsi
mov    %r13,%rdi
mov    %r12,%rdx
mov    %rbx,%rcx
xor    %eax,%eax
call   1050 <udev_log_null@plt>
nopl   0x0(%rax)
mov    %r12,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1255 <util_resolve_sys_link+0x105>
mov    %rax,%rbp
movb   $0x0,(%rax)
add    $0xffffffff,%r14d
jne    1200 <util_resolve_sys_link+0xb0>
lea    0xe1d(%rip),%rsi
xor    %r14d,%r14d
mov    %r13,%rdi
mov    %r12,%rdx
xor    %eax,%eax
call   1050 <udev_log_null@plt>
add    %r12,%r15
sub    %rbp,%r15
lea    0xddf(%rip),%rdx
mov    %rbp,%rdi
mov    %r15,%rsi
mov    %rbx,%rcx
xor    %r8d,%r8d
call   1070 <util_strscpyl@plt>
jmp    125b <util_resolve_sys_link+0x10b>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.udev = type { i32 }
@.str = [31 x i8] c"path link '%s' points to '%s'\0A\00"
@.str.1 = [4 x i8] c"../\00"
@.str.2 = [31 x i8] c"base '%s', tail '%s', back %i\0A\00"
@.str.3 = [24 x i8] c"after moving back '%s'\0A\00"
@.str.4 = [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
%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 %82
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
%25 = load %struct.udev*, %struct.udev** %5
%26 = load i8*, i8** %6
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%28 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %25, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %26, i8* %27)
store i32 0, i32* %11
br label %29
29: ; preds = %37, %22
%30 = load i32, i32* %11
%31 = mul nsw i32 %30, 3
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %32
%34 = call i64 @strncmp(i8* %33, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%35 = icmp eq i64 %34, 0
br i1 %35, label %36, label %40
36: ; preds = %29
br label %37
37: ; preds = %36
%38 = load i32, i32* %11
%39 = add nsw i32 %38, 1
store i32 %39, i32* %11
br label %29
40: ; preds = %29
%41 = load %struct.udev*, %struct.udev** %5
%42 = load i8*, i8** %6
%43 = load i32, i32* %11
%44 = mul nsw i32 %43, 3
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %45
%47 = load i32, i32* %11
%48 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %41, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %42, i8* %46, i32 %47)
store i32 0, i32* %10
br label %49
49: ; preds = %62, %40
%50 = load i32, i32* %10
%51 = load i32, i32* %11
%52 = icmp sle i32 %50, %51
br i1 %52, label %53, label %65
53: ; preds = %49
%54 = load i8*, i8** %6
%55 = call i8* @strrchr(i8* %54, i8 signext 47)
store i8* %55, i8** %12
%56 = load i8*, i8** %12
%57 = icmp eq i8* %56, null
br i1 %57, label %58, label %59
58: ; preds = %53
store i32 -1, i32* %4
br label %82
59: ; preds = %53
%60 = load i8*, i8** %12
%61 = getelementptr inbounds i8, i8* %60, i64 0
store i8 0, i8* %61
br label %62
62: ; preds = %59
%63 = load i32, i32* %10
%64 = add nsw i32 %63, 1
store i32 %64, i32* %10
br label %49
65: ; preds = %49
%66 = load %struct.udev*, %struct.udev** %5
%67 = load i8*, i8** %6
%68 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %66, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %67)
%69 = load i8*, i8** %12
%70 = load i64, i64* %7
%71 = load i8*, i8** %12
%72 = load i8*, i8** %6
%73 = ptrtoint i8* %71 to i64
%74 = ptrtoint i8* %72 to i64
%75 = sub i64 %73, %74
%76 = sub i64 %70, %75
%77 = load i32, i32* %11
%78 = mul nsw i32 %77, 3
%79 = sext i32 %78 to i64
%80 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %79
%81 = call i32 @util_strscpyl(i8* %69, i64 %76, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* %80, i32* null)
store i32 0, i32* %4
br label %82
82: ; preds = %65, %58, %21
%83 = load i32, i32* %4
ret i32 %83
}

LLVM IR (-O2):
%struct.udev = type { i32 }
@.str = [31 x i8] c"path link '%s' points to '%s'\0A\00"
@.str.1 = [4 x i8] c"../\00"
@.str.2 = [31 x i8] c"base '%s', tail '%s', back %i\0A\00"
@.str.3 = [24 x i8] c"after moving back '%s'\0A\00"
@.str.4 = [2 x i8] c"/\00"
define i32 @util_resolve_sys_link(%struct.udev* %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
%11 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %1, i8* %5)
br label %12
12: ; preds = %12, %9
%13 = phi i64 [ %18, %12 ], [ 0, %9 ]
%14 = mul nuw nsw i64 %13, 3
%15 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %14
%16 = call i64 @strncmp(i8* %15, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%17 = icmp eq i64 %16, 0
%18 = add nuw i64 %13, 1
br i1 %17, label %12, label %19
19: ; preds = %12
%20 = trunc i64 %13 to i32
%21 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %1, i8* %15, i32 %20)
br label %22
22: ; preds = %19, %26
%23 = phi i32 [ 0, %19 ], [ %27, %26 ]
%24 = call i8* @strrchr(i8* dereferenceable(1) %1, i8 signext 47)
%25 = icmp eq i8* %24, null
br i1 %25, label %36, label %26
26: ; preds = %22
store i8 0, i8* %24
%27 = add nuw i32 %23, 1
%28 = icmp eq i32 %23, %20
br i1 %28, label %29, label %22
29: ; preds = %26
%30 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %0, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %1)
%31 = ptrtoint i8* %24 to i64
%32 = ptrtoint i8* %1 to i64
%33 = add i64 %32, %2
%34 = sub i64 %33, %31
%35 = call i32 @util_strscpyl(i8* %24, i64 %34, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i8* %15, i32* null)
br label %36
36: ; preds = %22, %3, %29
%37 = phi i32 [ 0, %29 ], [ -1, %3 ], [ -1, %22 ]
ret i32 %37
}