#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 part;
typedef int minor;
typedef int dev_t;
typedef int atadev;

/* Variables and functions */
char *strrchr(char const *, char);
char const *strstr(char const *, char *);
int strtol(char const *, char **, int);
dev_t bdev_by_name(const char *name) {
  int minor;
  int atadev, part = 0;
  char *tmp = strrchr(name, '/');
  if (tmp) {
    name = tmp + 1;
  }
  if (strstr(name, "ata") != name) {
    return 0;
  }
  name += 3;
  atadev = strtol(name, &tmp, 10);
  if (tmp == name) {
    return 0;
  }
  name = tmp;
  if (*name++ == 'p') {
    part = strtol(name, &tmp, 10) + 1;
    if (tmp == name) {
      return 0;
    }
  }
  minor = ((((atadev)&0xf) << 4) | ((part)&0xf));
  return ((((1) & 0xff) << 8) | ((minor)&0xff));
}


x86 assembly (clang, -O0):
<bdev_by_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1030 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     116c <bdev_by_name+0x3c>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe89(%rip),%rsi
call   1050 <strstr@plt>
cmp    -0x10(%rbp),%rax
je     1192 <bdev_by_name+0x62>
movl   $0x0,-0x4(%rbp)
jmp    124b <bdev_by_name+0x11b>
mov    -0x10(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
mov    %eax,-0x18(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    11cd <bdev_by_name+0x9d>
movl   $0x0,-0x4(%rbp)
jmp    124b <bdev_by_name+0x11b>
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x10(%rbp)
movsbl (%rax),%eax
cmp    $0x70,%eax
jne    1227 <bdev_by_name+0xf7>
mov    -0x10(%rbp),%rdi
lea    -0x28(%rbp),%rsi
mov    $0xa,%edx
call   1040 <strtol@plt>
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jne    1222 <bdev_by_name+0xf2>
movl   $0x0,-0x4(%rbp)
jmp    124b <bdev_by_name+0x11b>
jmp    1227 <bdev_by_name+0xf7>
mov    -0x18(%rbp),%eax
and    $0xf,%eax
shl    $0x4,%eax
mov    -0x1c(%rbp),%ecx
and    $0xf,%ecx
or     %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x14(%rbp),%eax
and    $0xff,%eax
or     $0x100,%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bdev_by_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbp
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,(%rsp)
lea    0x1(%rax),%rbx
test   %rax,%rax
cmove  %rbp,%rbx
lea    0xea6(%rip),%rsi
mov    $0x3,%edx
mov    %rbx,%rdi
call   1030 <strncmp@plt>
xor    %ebp,%ebp
test   %eax,%eax
jne    11ca <bdev_by_name+0x9a>
add    $0x3,%rbx
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
mov    (%rsp),%r15
cmp    %rbx,%r15
je     11ca <bdev_by_name+0x9a>
mov    %eax,%r14d
xor    %eax,%eax
cmpb   $0x70,(%r15)
jne    11b2 <bdev_by_name+0x82>
add    $0x1,%r15
mov    %rsp,%rsi
mov    %r15,%rdi
mov    $0xa,%edx
call   1050 <strtol@plt>
cmp    %r15,(%rsp)
je     11c8 <bdev_by_name+0x98>
add    $0x1,%eax
shl    $0x4,%r14d
movzbl %r14b,%ecx
and    $0xf,%eax
lea    (%rcx,%rax,1),%ebp
add    $0x100,%ebp
jmp    11ca <bdev_by_name+0x9a>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [4 x i8] c"ata\00"
define i32 @bdev_by_name(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %3
store i32 0, i32* %6
%8 = load i8*, i8** %3
%9 = call i8* @strrchr(i8* %8, i8 signext 47)
store i8* %9, i8** %7
%10 = load i8*, i8** %7
%11 = icmp ne i8* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i8*, i8** %7
%14 = getelementptr inbounds i8, i8* %13, i64 1
store i8* %14, i8** %3
br label %15
15: ; preds = %12, %1
%16 = load i8*, i8** %3
%17 = call i8* @strstr(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%18 = load i8*, i8** %3
%19 = icmp ne i8* %17, %18
br i1 %19, label %20, label %21
20: ; preds = %15
store i32 0, i32* %2
br label %56
21: ; preds = %15
%22 = load i8*, i8** %3
%23 = getelementptr inbounds i8, i8* %22, i64 3
store i8* %23, i8** %3
%24 = load i8*, i8** %3
%25 = call i32 @strtol(i8* %24, i8** %7, i32 10)
store i32 %25, i32* %5
%26 = load i8*, i8** %7
%27 = load i8*, i8** %3
%28 = icmp eq i8* %26, %27
br i1 %28, label %29, label %30
29: ; preds = %21
store i32 0, i32* %2
br label %56
30: ; preds = %21
%31 = load i8*, i8** %7
store i8* %31, i8** %3
%32 = load i8*, i8** %3
%33 = getelementptr inbounds i8, i8* %32, i32 1
store i8* %33, i8** %3
%34 = load i8, i8* %32
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 112
br i1 %36, label %37, label %46
37: ; preds = %30
%38 = load i8*, i8** %3
%39 = call i32 @strtol(i8* %38, i8** %7, i32 10)
%40 = add nsw i32 %39, 1
store i32 %40, i32* %6
%41 = load i8*, i8** %7
%42 = load i8*, i8** %3
%43 = icmp eq i8* %41, %42
br i1 %43, label %44, label %45
44: ; preds = %37
store i32 0, i32* %2
br label %56
45: ; preds = %37
br label %46
46: ; preds = %45, %30
%47 = load i32, i32* %5
%48 = and i32 %47, 15
%49 = shl i32 %48, 4
%50 = load i32, i32* %6
%51 = and i32 %50, 15
%52 = or i32 %49, %51
store i32 %52, i32* %4
%53 = load i32, i32* %4
%54 = and i32 %53, 255
%55 = or i32 256, %54
store i32 %55, i32* %2
br label %56
56: ; preds = %46, %44, %29, %20
%57 = load i32, i32* %2
ret i32 %57
}

LLVM IR (-O2):
@.str = [4 x i8] c"ata\00"
define i32 @bdev_by_name(i8* %0) {
%2 = alloca i8*
%3 = bitcast i8** %2 to i8*
%4 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
store i8* %4, i8** %2
%5 = icmp eq i8* %4, null
%6 = getelementptr inbounds i8, i8* %4, i64 1
%7 = select i1 %5, i8* %0, i8* %6
%8 = tail call i32 @strncmp(i8* dereferenceable(1) %7, i8* dereferenceable(4) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i64 3)
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %31
10: ; preds = %1
%11 = getelementptr inbounds i8, i8* %7, i64 3
%12 = call i32 @strtol(i8* %11, i8** %2, i32 10)
%13 = load i8*, i8** %2
%14 = icmp eq i8* %13, %11
br i1 %14, label %31, label %15
15: ; preds = %10
%16 = getelementptr inbounds i8, i8* %13, i64 1
%17 = load i8, i8* %13
%18 = icmp eq i8 %17, 112
br i1 %18, label %19, label %24
19: ; preds = %15
%20 = call i32 @strtol(i8* %16, i8** %2, i32 10)
%21 = add nsw i32 %20, 1
%22 = load i8*, i8** %2
%23 = icmp eq i8* %22, %16
br i1 %23, label %31, label %24
24: ; preds = %19, %15
%25 = phi i32 [ %21, %19 ], [ 0, %15 ]
%26 = shl i32 %12, 4
%27 = and i32 %26, 240
%28 = and i32 %25, 15
%29 = or i32 %27, %28
%30 = or i32 %29, 256
br label %31
31: ; preds = %19, %10, %1, %24
%32 = phi i32 [ %30, %24 ], [ 0, %1 ], [ 0, %10 ], [ 0, %19 ]
ret i32 %32
}