#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 */

/* Variables and functions */
int readlink(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
unsigned int strlen(char *);
char *strrchr(char *, char);
int extract_busid(char *name, char *busid) {
  int count;
  unsigned int namelen;
  char linkbuffer[256];
  char *start, *end;
  namelen = strlen(name);
  if ((256 - namelen) < sizeof("device"))
    return 0;
  end = name + namelen;
  strcpy(end, "device");
  count = readlink(name, linkbuffer, 256 - 1);
  if (count < 0)
    return -1;
  linkbuffer[count] = 0;
  start = strrchr(linkbuffer, '/');
  if (!start)
    return -1;
  start++;
  strcpy(busid, start);
  return 0;
}


x86 assembly (clang, -O0):
<extract_busid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x130,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20(%rbp)
mov    $0x100,%eax
sub    -0x20(%rbp),%eax
mov    %eax,%eax
cmp    $0x7,%rax
jae    117f <extract_busid+0x3f>
movl   $0x0,-0x4(%rbp)
jmp    1238 <extract_busid+0xf8>
mov    -0x10(%rbp),%rax
mov    -0x20(%rbp),%ecx
add    %rcx,%rax
mov    %rax,-0x130(%rbp)
mov    -0x130(%rbp),%rdi
lea    0xe62(%rip),%rsi
call   1030 <strcpy@plt>
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1040 <readlink@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11d1 <extract_busid+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <extract_busid+0xf8>
movslq -0x1c(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x128(%rbp)
cmpq   $0x0,-0x128(%rbp)
jne    120f <extract_busid+0xcf>
movl   $0xffffffff,-0x4(%rbp)
jmp    1238 <extract_busid+0xf8>
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x128(%rbp),%rsi
call   1030 <strcpy@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x130,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<extract_busid>:
push   %rbp
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    -0xfa(%rax),%ecx
xor    %ebp,%ebp
cmp    $0x7,%ecx
jb     11b6 <extract_busid+0x76>
mov    %eax,%edi
add    %rbx,%rdi
lea    0xe91(%rip),%rsi
call   1030 <strcpy@plt>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xff,%edx
call   1040 <readlink@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     11b6 <extract_busid+0x76>
mov    %eax,%eax
movb   $0x0,(%rsp,%rax,1)
mov    %rsp,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11b6 <extract_busid+0x76>
add    $0x1,%rax
mov    %r14,%rdi
mov    %rax,%rsi
call   1030 <strcpy@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [7 x i8] c"device\00"
define i32 @extract_busid(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca [256 x i8]
%9 = alloca i8*
%10 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = sub i32 256, %13
%15 = zext i32 %14 to i64
%16 = icmp ult i64 %15, 7
br i1 %16, label %17, label %18
17: ; preds = %2
store i32 0, i32* %3
br label %46
18: ; preds = %2
%19 = load i8*, i8** %4
%20 = load i32, i32* %7
%21 = zext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
store i8* %22, i8** %10
%23 = load i8*, i8** %10
%24 = call i32 @strcpy(i8* %23, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%25 = load i8*, i8** %4
%26 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%27 = call i32 @readlink(i8* %25, i8* %26, i32 255)
store i32 %27, i32* %6
%28 = load i32, i32* %6
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %31
30: ; preds = %18
store i32 -1, i32* %3
br label %46
31: ; preds = %18
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 %33
store i8 0, i8* %34
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%36 = call i8* @strrchr(i8* %35, i8 signext 47)
store i8* %36, i8** %9
%37 = load i8*, i8** %9
%38 = icmp ne i8* %37, null
br i1 %38, label %40, label %39
39: ; preds = %31
store i32 -1, i32* %3
br label %46
40: ; preds = %31
%41 = load i8*, i8** %9
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %9
%43 = load i8*, i8** %5
%44 = load i8*, i8** %9
%45 = call i32 @strcpy(i8* %43, i8* %44)
store i32 0, i32* %3
br label %46
46: ; preds = %40, %39, %30, %17
%47 = load i32, i32* %3
ret i32 %47
}

LLVM IR (-O2):
@.str = [7 x i8] c"device\00"
define i32 @extract_busid(i8* %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%5 = tail call i32 @strlen(i8* %0)
%6 = add i32 %5, -250
%7 = icmp ult i32 %6, 7
br i1 %7, label %22, label %8
8: ; preds = %2
%9 = zext i32 %5 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = tail call i32 @strcpy(i8* %10, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%12 = call i32 @readlink(i8* %0, i8* %4, i32 255)
%13 = icmp slt i32 %12, 0
br i1 %13, label %22, label %14
14: ; preds = %8
%15 = zext i32 %12 to i64
%16 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %15
store i8 0, i8* %16
%17 = call i8* @strrchr(i8* %4, i8 signext 47)
%18 = icmp eq i8* %17, null
br i1 %18, label %22, label %19
19: ; preds = %14
%20 = getelementptr inbounds i8, i8* %17, i64 1
%21 = call i32 @strcpy(i8* %1, i8* %20)
br label %22
22: ; preds = %14, %8, %2, %19
%23 = phi i32 [ 0, %19 ], [ 0, %2 ], [ -1, %8 ], [ -1, %14 ]
ret i32 %23
}