#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 atoi(char const *);
scalar_t__ isdigit(char const);
int strlen(char const *);
int unit_of_devname(const char *devname) {
  int len;
  if (!devname || !*devname)
    return -1;
  len = strlen(devname);
  while (len > 0 && isdigit(devname[len - 1]))
    len--;
  return atoi(devname + len);
}


x86 assembly (clang, -O0):
<unit_of_devname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
je     1154 <unit_of_devname+0x24>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1160 <unit_of_devname+0x30>
movl   $0xffffffff,-0x4(%rbp)
jmp    11cc <unit_of_devname+0x9c>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,-0x14(%rbp)
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x15(%rbp)
jle    119b <unit_of_devname+0x6b>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1030 <isdigit@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    11ab <unit_of_devname+0x7b>
jmp    11b9 <unit_of_devname+0x89>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    116c <unit_of_devname+0x3c>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
call   1050 <atoi@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unit_of_devname>:
push   %rbp
push   %r14
push   %rbx
test   %rdi,%rdi
je     118f <unit_of_devname+0x5f>
mov    %rdi,%r14
cmpb   $0x0,(%rdi)
je     118f <unit_of_devname+0x5f>
mov    %r14,%rdi
call   1040 <strlen@plt>
mov    %eax,%ebx
xor    %ebp,%ebp
test   %eax,%eax
cmovs  %eax,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
test   %ebx,%ebx
jle    117d <unit_of_devname+0x4d>
movsbl -0x1(%r14,%rbx,1),%edi
add    $0xffffffffffffffff,%rbx
call   1030 <isdigit@plt>
test   %rax,%rax
jne    1160 <unit_of_devname+0x30>
add    $0x1,%ebx
mov    %ebx,%ebp
movslq %ebp,%rax
add    %rax,%r14
mov    %r14,%rdi
pop    %rbx
pop    %r14
pop    %rbp
jmp    1050 <atoi@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @unit_of_devname(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i32
store i8* %0, i8** %3
%5 = load i8*, i8** %3
%6 = icmp ne i8* %5, null
br i1 %6, label %7, label %11
7: ; preds = %1
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = icmp ne i8 %9, 0
br i1 %10, label %12, label %11
11: ; preds = %7, %1
store i32 -1, i32* %2
br label %38
12: ; preds = %7
%13 = load i8*, i8** %3
%14 = call i32 @strlen(i8* %13)
store i32 %14, i32* %4
br label %15
15: ; preds = %29, %12
%16 = load i32, i32* %4
%17 = icmp sgt i32 %16, 0
br i1 %17, label %18, label %27
18: ; preds = %15
%19 = load i8*, i8** %3
%20 = load i32, i32* %4
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
%24 = load i8, i8* %23
%25 = call i64 @isdigit(i8 signext %24)
%26 = icmp ne i64 %25, 0
br label %27
27: ; preds = %18, %15
%28 = phi i1 [ false, %15 ], [ %26, %18 ]
br i1 %28, label %29, label %32
29: ; preds = %27
%30 = load i32, i32* %4
%31 = add nsw i32 %30, -1
store i32 %31, i32* %4
br label %15
32: ; preds = %27
%33 = load i8*, i8** %3
%34 = load i32, i32* %4
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %33, i64 %35
%37 = call i32 @atoi(i8* %36)
store i32 %37, i32* %2
br label %38
38: ; preds = %32, %11
%39 = load i32, i32* %2
ret i32 %39
}

LLVM IR (-O2):
define i32 @unit_of_devname(i8* %0) {
%2 = icmp eq i8* %0, null
br i1 %2, label %27, label %3
3: ; preds = %1
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 0
br i1 %5, label %27, label %6
6: ; preds = %3
%7 = tail call i32 @strlen(i8* %0)
%8 = zext i32 %7 to i64
%9 = call i32 @llvm.smin.i32(i32 %7, i32 0)
br label %10
10: ; preds = %14, %6
%11 = phi i64 [ %15, %14 ], [ %8, %6 ]
%12 = trunc i64 %11 to i32
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %22
14: ; preds = %10
%15 = add nsw i64 %11, -1
%16 = getelementptr inbounds i8, i8* %0, i64 %15
%17 = load i8, i8* %16
%18 = tail call i64 @isdigit(i8 signext %17)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %10
20: ; preds = %14
%21 = trunc i64 %11 to i32
br label %22
22: ; preds = %10, %20
%23 = phi i32 [ %21, %20 ], [ %9, %10 ]
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
%26 = tail call i32 @atoi(i8* %25)
br label %27
27: ; preds = %1, %3, %22
%28 = phi i32 [ %26, %22 ], [ -1, %3 ], [ -1, %1 ]
ret i32 %28
}