#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 ENOENT;
int ENOMEM;
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ path_prepend(char **, char *, unsigned int);
int sscanf(char *, char *, unsigned int *);
char *strdup(char const *);
int vdev_sysfs_get_sysname(char const *, char **, size_t *);
int handle_bcma(char const *parent, char **path, char **new_parent) {
  unsigned int core = 0;
  int rc = 0;
  char *sysname = NULL;
  size_t sysname_len = 0;
  rc = vdev_sysfs_get_sysname(parent, &sysname, &sysname_len);
  if (rc != 0) {
    return rc;
  }
  rc = sscanf(sysname, "bcma%*u:%u", &core);
  free(sysname);
  if (rc != 1) {
    return -ENOENT;
  }
  path_prepend(path, "bcma-%u", core);
  *new_parent = strdup(parent);
  if (*new_parent == NULL) {
    return -ENOMEM;
  }
  return 0;
}


x86 assembly (clang, -O0):
<handle_bcma>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x30(%rbp),%rsi
lea    -0x38(%rbp),%rdx
call   1050 <vdev_sysfs_get_sysname@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0x0,-0x28(%rbp)
je     11ab <handle_bcma+0x5b>
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <handle_bcma+0xe6>
mov    -0x30(%rbp),%rdi
lea    0xe4a(%rip),%rsi
lea    -0x24(%rbp),%rdx
call   1060 <sscanf@plt>
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
call   1040 <free@plt>
cmpl   $0x1,-0x28(%rbp)
je     11e8 <handle_bcma+0x98>
xor    %eax,%eax
mov    0x2df2(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <handle_bcma+0xe6>
mov    -0x18(%rbp),%rdi
mov    -0x24(%rbp),%edx
lea    0xe15(%rip),%rsi
call   1030 <path_prepend@plt>
mov    -0x10(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
jne    122f <handle_bcma+0xdf>
xor    %eax,%eax
mov    0x2dbb(%rip),%rcx
sub    (%rcx),%eax
mov    %eax,-0x4(%rbp)
jmp    1236 <handle_bcma+0xe6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_bcma>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %rdx,%r14
mov    %rsi,%r15
mov    %rdi,%rbx
movl   $0x0,0x4(%rsp)
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
lea    0x8(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   1050 <vdev_sysfs_get_sysname@plt>
test   %eax,%eax
jne    11f3 <handle_bcma+0xa3>
mov    0x8(%rsp),%rdi
lea    0xe64(%rip),%rsi
lea    0x4(%rsp),%rdx
call   1060 <sscanf@plt>
mov    %eax,%ebp
mov    0x8(%rsp),%rdi
call   1040 <free@plt>
cmp    $0x1,%ebp
jne    11e8 <handle_bcma+0x98>
mov    0x4(%rsp),%edx
lea    0xe49(%rip),%rsi
mov    %r15,%rdi
call   1030 <path_prepend@plt>
mov    %rbx,%rdi
call   1070 <strdup@plt>
mov    %rax,%rcx
mov    %rax,(%r14)
xor    %eax,%eax
test   %rcx,%rcx
jne    11f3 <handle_bcma+0xa3>
mov    0x2dfa(%rip),%rcx
jmp    11ef <handle_bcma+0x9f>
mov    0x2de1(%rip),%rcx
xor    %eax,%eax
sub    (%rcx),%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"bcma%*u:%u\00"
@ENOENT = global i32 0
@.str.1 = [8 x i8] c"bcma-%u\00"
@ENOMEM = global i32 0
define i32 @handle_bcma(i8* %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i64
store i8* %0, i8** %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
store i32 0, i32* %8
store i32 0, i32* %9
store i8* null, i8** %10
store i64 0, i64* %11
%12 = load i8*, i8** %5
%13 = call i32 @vdev_sysfs_get_sysname(i8* %12, i8** %10, i64* %11)
store i32 %13, i32* %9
%14 = load i32, i32* %9
%15 = icmp ne i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %3
%17 = load i32, i32* %9
store i32 %17, i32* %4
br label %42
18: ; preds = %3
%19 = load i8*, i8** %10
%20 = call i32 @sscanf(i8* %19, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32* %8)
store i32 %20, i32* %9
%21 = load i8*, i8** %10
%22 = call i32 @free(i8* %21)
%23 = load i32, i32* %9
%24 = icmp ne i32 %23, 1
br i1 %24, label %25, label %28
25: ; preds = %18
%26 = load i32, i32* @ENOENT
%27 = sub nsw i32 0, %26
store i32 %27, i32* %4
br label %42
28: ; preds = %18
%29 = load i8**, i8*** %6
%30 = load i32, i32* %8
%31 = call i32 @path_prepend(i8** %29, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %30)
%32 = load i8*, i8** %5
%33 = call i8* @strdup(i8* %32)
%34 = load i8**, i8*** %7
store i8* %33, i8** %34
%35 = load i8**, i8*** %7
%36 = load i8*, i8** %35
%37 = icmp eq i8* %36, null
br i1 %37, label %38, label %41
38: ; preds = %28
%39 = load i32, i32* @ENOMEM
%40 = sub nsw i32 0, %39
store i32 %40, i32* %4
br label %42
41: ; preds = %28
store i32 0, i32* %4
br label %42
42: ; preds = %41, %38, %25, %16
%43 = load i32, i32* %4
ret i32 %43
}

LLVM IR (-O2):
@.str = [11 x i8] c"bcma%*u:%u\00"
@ENOENT = global i32 0
@.str.1 = [8 x i8] c"bcma-%u\00"
@ENOMEM = global i32 0
define i32 @handle_bcma(i8* %0, i8** %1, i8** nocapture writeonly %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = bitcast i32* %4 to i8*
store i32 0, i32* %4
%8 = bitcast i8** %5 to i8*
store i8* null, i8** %5
%9 = bitcast i64* %6 to i8*
store i64 0, i64* %6
%10 = call i32 @vdev_sysfs_get_sysname(i8* %0, i8** %5, i64* %6)
%11 = icmp eq i32 %10, 0
br i1 %11, label %12, label %29
12: ; preds = %3
%13 = load i8*, i8** %5
%14 = call i32 @sscanf(i8* %13, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i32* %4)
%15 = load i8*, i8** %5
%16 = call i32 @free(i8* %15)
%17 = icmp eq i32 %14, 1
br i1 %17, label %21, label %18
18: ; preds = %12
%19 = load i32, i32* @ENOENT
%20 = sub nsw i32 0, %19
br label %29
21: ; preds = %12
%22 = load i32, i32* %4
%23 = call i32 @path_prepend(i8** %1, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = call i8* @strdup(i8* %0)
store i8* %24, i8** %2
%25 = icmp eq i8* %24, null
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i32, i32* @ENOMEM
%28 = sub nsw i32 0, %27
br label %29
29: ; preds = %21, %3, %26, %18
%30 = phi i32 [ %20, %18 ], [ %28, %26 ], [ %10, %3 ], [ 0, %21 ]
ret i32 %30
}