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

/* Variables and functions */
int readlink(char *, char *, int);
int strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
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)),
   (link_path)[(sizeof(link_path)) - 1] = '\0');
  strncat(link_path, devpath, (sizeof(link_path)) - strlen(link_path) - 1);
  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)-strlen(devpath) - 1);
  strncat(devpath, &link_target[back * 3], (size)-strlen(devpath) - 1);
  return 0;
}


x86 assembly (clang, -O0):
<sysfs_resolve_link>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
lea    -0x220(%rbp),%rdi
lea    0xe7f(%rip),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x220(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x440(%rbp)
lea    -0x220(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x448(%rbp),%rdi
mov    -0x440(%rbp),%rsi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1080 <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     120f <sysfs_resolve_link+0xaf>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a1 <sysfs_resolve_link+0x241>
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
jmp    1223 <sysfs_resolve_link+0xc3>
movl   $0x0,-0x42c(%rbp)
imul   $0x3,-0x42c(%rbp),%eax
cltq
lea    -0x420(%rbp),%rdi
add    %rax,%rdi
lea    0xdbe(%rip),%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    1274 <sysfs_resolve_link+0x114>
jmp    1260 <sysfs_resolve_link+0x100>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    122d <sysfs_resolve_link+0xcd>
jmp    1279 <sysfs_resolve_link+0x119>
jmp    127e <sysfs_resolve_link+0x11e>
movl   $0x0,-0x428(%rbp)
mov    -0x428(%rbp),%eax
cmp    -0x42c(%rbp),%eax
jg     12e7 <sysfs_resolve_link+0x187>
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x438(%rbp)
cmpq   $0x0,-0x438(%rbp)
jne    12c9 <sysfs_resolve_link+0x169>
movl   $0xffffffff,-0x4(%rbp)
jmp    13a1 <sysfs_resolve_link+0x241>
mov    -0x438(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
jmp    1288 <sysfs_resolve_link+0x128>
jmp    12ec <sysfs_resolve_link+0x18c>
jmp    12f1 <sysfs_resolve_link+0x191>
mov    -0x10(%rbp),%rax
mov    %rax,-0x470(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x468(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x470(%rbp),%rdi
mov    %eax,%ecx
mov    -0x468(%rbp),%rax
movslq %ecx,%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
lea    0xcd4(%rip),%rsi
call   1080 <strncat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x460(%rbp)
imul   $0x3,-0x42c(%rbp),%eax
movslq %eax,%rcx
lea    -0x420(%rbp),%rax
add    %rcx,%rax
mov    %rax,-0x458(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x450(%rbp)
mov    -0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0x460(%rbp),%rdi
mov    -0x458(%rbp),%rsi
mov    %eax,%ecx
mov    -0x450(%rbp),%rax
movslq %ecx,%rcx
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
call   1080 <strncat@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x470,%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    0xe82(%rip),%rsi
lea    0x200(%rsp),%rbp
mov    %rbp,%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x3ff(%rsp)
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
mov    %rbp,%rdi
mov    %r15,%rsi
call   1080 <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    1265 <sysfs_resolve_link+0x105>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
xor    %ebp,%ebp
lea    0xe20(%rip),%r13
cs nopw 0x0(%rax,%rax,1)
nop
mov    %rbx,%rdi
mov    %r13,%rsi
mov    $0x3,%edx
call   1040 <strncmp@plt>
add    $0x1,%ebp
add    $0x3,%rbx
test   %rax,%rax
je     11f0 <sysfs_resolve_link+0x90>
add    $0xfffffffffffffffd,%rbx
mov    %r15,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
test   %rax,%rax
je     1265 <sysfs_resolve_link+0x105>
movb   $0x0,(%rax)
add    $0xffffffff,%ebp
jne    1210 <sysfs_resolve_link+0xb0>
mov    %r15,%rdi
call   1060 <strlen@plt>
not    %eax
lea    (%rax,%r14,1),%edx
lea    0xdc8(%rip),%rsi
mov    %r15,%rdi
call   1080 <strncat@plt>
mov    %r15,%rdi
call   1060 <strlen@plt>
not    %eax
add    %eax,%r14d
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1080 <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 511
store i8 0, i8* %14
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%16 = load i8*, i8** %4
%17 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%18 = call i32 @strlen(i8* %17)
%19 = sext i32 %18 to i64
%20 = sub i64 512, %19
%21 = sub i64 %20, 1
%22 = trunc i64 %21 to i32
%23 = call i32 @strncat(i8* %15, i8* %16, i32 %22)
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%25 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 0
%26 = call i32 @readlink(i8* %24, i8* %25, i32 512)
store i32 %26, i32* %8
%27 = load i32, i32* %8
%28 = icmp sle i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %2
store i32 -1, i32* %3
br label %91
30: ; preds = %2
%31 = load i32, i32* %8
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 %32
store i8 0, i8* %33
br label %34
34: ; preds = %30
br label %35
35: ; preds = %34
store i32 0, i32* %10
br label %36
36: ; preds = %44, %35
%37 = load i32, i32* %10
%38 = mul nsw i32 %37, 3
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 %39
%41 = call i64 @strncmp(i8* %40, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%42 = icmp eq i64 %41, 0
br i1 %42, label %43, label %47
43: ; preds = %36
br label %44
44: ; preds = %43
%45 = load i32, i32* %10
%46 = add nsw i32 %45, 1
store i32 %46, i32* %10
br label %36
47: ; preds = %36
br label %48
48: ; preds = %47
br label %49
49: ; preds = %48
store i32 0, i32* %9
br label %50
50: ; preds = %63, %49
%51 = load i32, i32* %9
%52 = load i32, i32* %10
%53 = icmp sle i32 %51, %52
br i1 %53, label %54, label %66
54: ; preds = %50
%55 = load i8*, i8** %4
%56 = call i8* @strrchr(i8* %55, i8 signext 47)
store i8* %56, i8** %11
%57 = load i8*, i8** %11
%58 = icmp eq i8* %57, null
br i1 %58, label %59, label %60
59: ; preds = %54
store i32 -1, i32* %3
br label %91
60: ; preds = %54
%61 = load i8*, i8** %11
%62 = getelementptr inbounds i8, i8* %61, i64 0
store i8 0, i8* %62
br label %63
63: ; preds = %60
%64 = load i32, i32* %9
%65 = add nsw i32 %64, 1
store i32 %65, i32* %9
br label %50
66: ; preds = %50
br label %67
67: ; preds = %66
br label %68
68: ; preds = %67
%69 = load i8*, i8** %4
%70 = load i64, i64* %5
%71 = load i8*, i8** %4
%72 = call i32 @strlen(i8* %71)
%73 = sext i32 %72 to i64
%74 = sub i64 %70, %73
%75 = sub i64 %74, 1
%76 = trunc i64 %75 to i32
%77 = call i32 @strncat(i8* %69, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %76)
%78 = load i8*, i8** %4
%79 = load i32, i32* %10
%80 = mul nsw i32 %79, 3
%81 = sext i32 %80 to i64
%82 = getelementptr inbounds [512 x i8], [512 x i8]* %7, i64 0, i64 %81
%83 = load i64, i64* %5
%84 = load i8*, i8** %4
%85 = call i32 @strlen(i8* %84)
%86 = sext i32 %85 to i64
%87 = sub i64 %83, %86
%88 = sub i64 %87, 1
%89 = trunc i64 %88 to i32
%90 = call i32 @strncat(i8* %78, i8* %82, i32 %89)
store i32 0, i32* %3
br label %91
91: ; preds = %68, %59, %29
%92 = load i32, i32* %3
ret i32 %92
}

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 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 511
store i8 0, i8* %8
%9 = call i32 @strlen(i8* %5)
%10 = sub i32 511, %9
%11 = call i32 @strncat(i8* %5, i8* %0, i32 %10)
%12 = call i32 @readlink(i8* %5, i8* %6, i32 512)
%13 = icmp slt i32 %12, 1
br i1 %13, label %43, label %14
14: ; preds = %2
%15 = zext i32 %12 to i64
%16 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %15
store i8 0, i8* %16
br label %17
17: ; preds = %17, %14
%18 = phi i64 [ %23, %17 ], [ 0, %14 ]
%19 = mul nuw nsw i64 %18, 3
%20 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 %19
%21 = call i64 @strncmp(i8* %20, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%22 = icmp eq i64 %21, 0
%23 = add nuw i64 %18, 1
br i1 %22, label %17, label %24
24: ; preds = %17
%25 = trunc i64 %18 to i32
br label %26
26: ; preds = %24, %30
%27 = phi i32 [ 0, %24 ], [ %31, %30 ]
%28 = call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%29 = icmp eq i8* %28, null
br i1 %29, label %43, label %30
30: ; preds = %26
store i8 0, i8* %28
%31 = add nuw i32 %27, 1
%32 = icmp eq i32 %27, %25
br i1 %32, label %33, label %26
33: ; preds = %30
%34 = call i32 @strlen(i8* %0)
%35 = xor i32 %34, -1
%36 = trunc i64 %1 to i32
%37 = add i32 %35, %36
%38 = call i32 @strncat(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 %37)
%39 = call i32 @strlen(i8* %0)
%40 = xor i32 %39, -1
%41 = add i32 %40, %36
%42 = call i32 @strncat(i8* %0, i8* %20, i32 %41)
br label %43
43: ; preds = %26, %2, %33
%44 = phi i32 [ 0, %33 ], [ -1, %2 ], [ -1, %26 ]
ret i32 %44
}