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

/* Variables and functions */
int readlink(char *, char *, int);
int /*<<< orphan*/ strncat(char *, char *, size_t);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
int sysfs_resolve_link(char *devpath, size_t size) {
  char link_path[512];
  char link_target[512];
  int len;
  int i;
  int back;
  strncpy(link_path, "/sys", sizeof(link_path));
  strncat(link_path, devpath, sizeof(link_path));
  len = readlink(link_path, link_target, sizeof(link_target));
  if (len <= 0)
    return -1;
  link_target[len] = '\0';
  do {
  } while (0);
  for (back = 0; strncmp(&link_target[back * 3], "../", 3) == 0; back++)
    ;
  do {
  } while (0);
  for (i = 0; i <= back; i++) {
    char *pos = strrchr(devpath, '/');
    if (pos == NULL)
      return -1;
    pos[0] = '\0';
  }
  do {
  } while (0);
  strncat(devpath, "/", size);
  strncat(devpath, &link_target[back * 3], size);
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_resolve_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x220(%rbp),%rdi
lea    0xe8f(%rip),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
lea    -0x220(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x200,%edx
call   1070 <strncat@plt>
lea    -0x220(%rbp),%rdi
lea    -0x420(%rbp),%rsi
mov    $0x200,%edx
call   1050 <readlink@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0x0,-0x424(%rbp)
jg     11c7 <sysfs_resolve_link+0x77>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e4 <sysfs_resolve_link+0x194>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
jmp    11db <sysfs_resolve_link+0x8b>
movl   $0x0,-0x42c(%rbp)
imul   $0x3,-0x42c(%rbp),%eax
cltq
lea    -0x420(%rbp),%rdi
add    %rax,%rdi
lea    0xe06(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    122c <sysfs_resolve_link+0xdc>
jmp    1218 <sysfs_resolve_link+0xc8>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    11e5 <sysfs_resolve_link+0x95>
jmp    1231 <sysfs_resolve_link+0xe1>
jmp    1236 <sysfs_resolve_link+0xe6>
movl   $0x0,-0x428(%rbp)
mov    -0x428(%rbp),%eax
cmp    -0x42c(%rbp),%eax
jg     129f <sysfs_resolve_link+0x14f>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    1281 <sysfs_resolve_link+0x131>
movl   $0xffffffff,-0x4(%rbp)
jmp    12e4 <sysfs_resolve_link+0x194>
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
jmp    1240 <sysfs_resolve_link+0xf0>
jmp    12a4 <sysfs_resolve_link+0x154>
jmp    12a9 <sysfs_resolve_link+0x159>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xd4f(%rip),%rsi
call   1070 <strncat@plt>
mov    -0x10(%rbp),%rdi
imul   $0x3,-0x42c(%rbp),%eax
cltq
lea    -0x420(%rbp),%rsi
add    %rax,%rsi
mov    -0x18(%rbp),%rdx
call   1070 <strncat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sysfs_resolve_link>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r14
mov    %rdi,%r15
lea    0xe92(%rip),%rsi
lea    0x200(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
mov    $0x200,%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1070 <strncat@plt>
mov    %rsp,%rbx
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1050 <readlink@plt>
mov    $0xffffffff,%r12d
test   %eax,%eax
jle    121d <sysfs_resolve_link+0xcd>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
xor    %ebp,%ebp
lea    0xe46(%rip),%r13
nop
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
add    $0x1,%ebp
add    $0x3,%rbx
test   %rax,%rax
je     11c0 <sysfs_resolve_link+0x70>
add    $0xfffffffffffffffd,%rbx
mov    %r15,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     121d <sysfs_resolve_link+0xcd>
movb   $0x0,(%rax)
add    $0xffffffff,%ebp
jne    11e0 <sysfs_resolve_link+0x90>
lea    0xe06(%rip),%rsi
mov    %r15,%rdi
mov    %r14,%rdx
call   1070 <strncat@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1070 <strncat@plt>
xor    %r12d,%r12d
mov    %r12d,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"/sys\00"
@.str.1 = [4 x i8] c"../\00"
@.str.2 = [2 x i8] c"/\00"
define i32 @sysfs_resolve_link(i8* %0, i64 %1) {
%3 = alloca [512 x i8]
%4 = alloca [512 x i8]
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%7 = call i32 @strncpy(i8* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i32 512)
%8 = call i32 @strncat(i8* %5, i8* %0, i64 512)
%9 = call i32 @readlink(i8* %5, i8* %6, i32 512)
%10 = icmp slt i32 %9, 1
br i1 %10, label %33, label %11
11: ; preds = %2
%12 = zext i32 %9 to i64
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %12
store i8 0, i8* %13
br label %14
14: ; preds = %14, %11
%15 = phi i64 [ %20, %14 ], [ 0, %11 ]
%16 = mul nuw nsw i64 %15, 3
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %16
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%19 = icmp eq i64 %18, 0
%20 = add nuw i64 %15, 1
br i1 %19, label %14, label %21
21: ; preds = %14
%22 = trunc i64 %15 to i32
br label %23
23: ; preds = %21, %27
%24 = phi i32 [ 0, %21 ], [ %28, %27 ]
%25 = call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%26 = icmp eq i8* %25, null
br i1 %26, label %33, label %27
27: ; preds = %23
store i8 0, i8* %25
%28 = add nuw i32 %24, 1
%29 = icmp eq i32 %24, %22
br i1 %29, label %30, label %23
30: ; preds = %27
%31 = call i32 @strncat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i64 %1)
%32 = call i32 @strncat(i8* %0, i8* %17, i64 %1)
br label %33
33: ; preds = %23, %2, %30
%34 = phi i32 [ 0, %30 ], [ -1, %2 ], [ -1, %23 ]
ret i32 %34
}