#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 /*<<< orphan*/ link_target;

/* Variables and functions */
int 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_strlcat(char *, char *, size_t);
int util_resolve_sys_link(struct udev *udev, char *syspath, size_t size) {
  char link_target[1024];
  int len;
  int i;
  int back;
  len = readlink(syspath, link_target, sizeof(link_target));
  if (len <= 0)
    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++) {
    char *pos = strrchr(syspath, '/');
    if (pos == NULL)
      return -1;
    pos[0] = '\0';
  }
  udev_log_null(udev, "after moving back '%s'\n", syspath);
  util_strlcat(syspath, "/", size);
  util_strlcat(syspath, &link_target[back * 3], size);
  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    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jg     119b <util_resolve_sys_link+0x4b>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <util_resolve_sys_link+0x1b2>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    -0x420(%rbp),%rcx
lea    0xe40(%rip),%rsi
mov    $0x0,%al
call   1050 <udev_log_null@plt>
movl   $0x0,-0x42c(%rbp)
imul   $0x3,-0x42c(%rbp),%eax
cltq
lea    -0x420(%rbp),%rdi
add    %rax,%rdi
lea    0xe34(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    1218 <util_resolve_sys_link+0xc8>
jmp    1204 <util_resolve_sys_link+0xb4>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    11d1 <util_resolve_sys_link+0x81>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
imul   $0x3,-0x42c(%rbp),%eax
cltq
lea    -0x420(%rbp),%rcx
add    %rax,%rcx
mov    -0x42c(%rbp),%r8d
lea    0xde2(%rip),%rsi
mov    $0x0,%al
call   1050 <udev_log_null@plt>
movl   $0x0,-0x428(%rbp)
mov    -0x428(%rbp),%eax
cmp    -0x42c(%rbp),%eax
jg     12b1 <util_resolve_sys_link+0x161>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    1293 <util_resolve_sys_link+0x143>
movl   $0xffffffff,-0x4(%rbp)
jmp    1302 <util_resolve_sys_link+0x1b2>
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
jmp    1252 <util_resolve_sys_link+0x102>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd82(%rip),%rsi
mov    $0x0,%al
call   1050 <udev_log_null@plt>
mov    -0x18(%rbp),%rdi
mov    -0x20(%rbp),%rdx
lea    0xd4b(%rip),%rsi
call   1070 <util_strlcat@plt>
mov    -0x18(%rbp),%rdi
imul   $0x3,-0x42c(%rbp),%eax
cltq
lea    -0x420(%rbp),%rsi
add    %rax,%rsi
mov    -0x20(%rbp),%rdx
call   1070 <util_strlcat@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    $0x418,%rsp
mov    %rdx,%rbx
mov    %rsi,%r13
mov    %rdi,%r12
lea    0x10(%rsp),%rbp
mov    %rsi,%rdi
mov    %rbp,%rsi
mov    $0x400,%edx
call   1040 <readlink@plt>
mov    $0xffffffff,%r15d
test   %eax,%eax
jle    1257 <util_resolve_sys_link+0x107>
mov    %rbx,0x8(%rsp)
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0xe60(%rip),%rsi
xor    %r14d,%r14d
mov    %r12,%rdi
mov    %r13,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1050 <udev_log_null@plt>
lea    0xe65(%rip),%rbx
nopw   0x0(%rax,%rax,1)
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
add    $0x1,%r14d
add    $0x3,%rbp
test   %rax,%rax
je     11c0 <util_resolve_sys_link+0x70>
lea    -0x1(%r14),%r8d
add    $0xfffffffffffffffd,%rbp
lea    0xe37(%rip),%rsi
mov    %r12,%rdi
mov    %r13,%rdx
mov    %rbp,%rcx
xor    %eax,%eax
call   1050 <udev_log_null@plt>
nopl   0x0(%rax)
mov    %r13,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     1257 <util_resolve_sys_link+0x107>
movb   $0x0,(%rax)
add    $0xffffffff,%r14d
jne    1200 <util_resolve_sys_link+0xb0>
lea    0xe20(%rip),%rsi
xor    %r15d,%r15d
mov    %r12,%rdi
mov    %r13,%rdx
xor    %eax,%eax
call   1050 <udev_log_null@plt>
lea    0xde8(%rip),%rsi
mov    %r13,%rdi
mov    0x8(%rsp),%rbx
mov    %rbx,%rdx
call   1070 <util_strlcat@plt>
mov    %r13,%rdi
mov    %rbp,%rsi
mov    %rbx,%rdx
call   1070 <util_strlcat@plt>
mov    %r15d,%eax
add    $0x418,%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 i32
%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 i32 @readlink(i8* %13, i8* %14, i32 1024)
store i32 %15, i32* %9
%16 = load i32, i32* %9
%17 = icmp sle i32 %16, 0
br i1 %17, label %18, label %19
18: ; preds = %3
store i32 -1, i32* %4
br label %77
19: ; preds = %3
%20 = load i32, i32* %9
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %21
store i8 0, i8* %22
%23 = load %struct.udev*, %struct.udev** %5
%24 = load i8*, i8** %6
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 0
%26 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %23, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %24, i8* %25)
store i32 0, i32* %11
br label %27
27: ; preds = %35, %19
%28 = load i32, i32* %11
%29 = mul nsw i32 %28, 3
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %30
%32 = call i64 @strncmp(i8* %31, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %38
34: ; preds = %27
br label %35
35: ; preds = %34
%36 = load i32, i32* %11
%37 = add nsw i32 %36, 1
store i32 %37, i32* %11
br label %27
38: ; preds = %27
%39 = load %struct.udev*, %struct.udev** %5
%40 = load i8*, i8** %6
%41 = load i32, i32* %11
%42 = mul nsw i32 %41, 3
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %43
%45 = load i32, i32* %11
%46 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %39, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.2, i64 0, i64 0), i8* %40, i8* %44, i32 %45)
store i32 0, i32* %10
br label %47
47: ; preds = %60, %38
%48 = load i32, i32* %10
%49 = load i32, i32* %11
%50 = icmp sle i32 %48, %49
br i1 %50, label %51, label %63
51: ; preds = %47
%52 = load i8*, i8** %6
%53 = call i8* @strrchr(i8* %52, i8 signext 47)
store i8* %53, i8** %12
%54 = load i8*, i8** %12
%55 = icmp eq i8* %54, null
br i1 %55, label %56, label %57
56: ; preds = %51
store i32 -1, i32* %4
br label %77
57: ; preds = %51
%58 = load i8*, i8** %12
%59 = getelementptr inbounds i8, i8* %58, i64 0
store i8 0, i8* %59
br label %60
60: ; preds = %57
%61 = load i32, i32* %10
%62 = add nsw i32 %61, 1
store i32 %62, i32* %10
br label %47
63: ; preds = %47
%64 = load %struct.udev*, %struct.udev** %5
%65 = load i8*, i8** %6
%66 = call i32 (%struct.udev*, i8*, i8*, ...) @udev_log_null(%struct.udev* %64, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0), i8* %65)
%67 = load i8*, i8** %6
%68 = load i64, i64* %7
%69 = call i32 @util_strlcat(i8* %67, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i64 %68)
%70 = load i8*, i8** %6
%71 = load i32, i32* %11
%72 = mul nsw i32 %71, 3
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %8, i64 0, i64 %73
%75 = load i64, i64* %7
%76 = call i32 @util_strlcat(i8* %70, i8* %74, i64 %75)
store i32 0, i32* %4
br label %77
77: ; preds = %63, %56, %18
%78 = load i32, i32* %4
ret i32 %78
}

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 i32 @readlink(i8* %1, i8* %5, i32 1024)
%7 = icmp slt i32 %6, 1
br i1 %7, label %33, label %8
8: ; preds = %3
%9 = zext i32 %6 to i64
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %9
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, %8
%13 = phi i64 [ %18, %12 ], [ 0, %8 ]
%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 %33, 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 = call i32 @util_strlcat(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0), i64 %2)
%32 = call i32 @util_strlcat(i8* %1, i8* %15, i64 %2)
br label %33
33: ; preds = %22, %3, %29
%34 = phi i32 [ 0, %29 ], [ -1, %3 ], [ -1, %22 ]
ret i32 %34
}