#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 */
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
char *mbasename(char *s, char *res, int reslen) {
  char *p;
  if (strncmp(s, "/dev/", 5) == 0) {
    strncpy(res, s + 5, reslen - 1);
    res[reslen - 1] = 0;
    for (p = res; *p; p++)
      if (*p == '/')
        *p = '_';
  } else {
    if ((p = strrchr(s, '/')) == NULL)
      p = s;
    else
      p++;
    strncpy(res, p, reslen - 1);
    res[reslen - 1] = 0;
  }
  return res;
}


x86 assembly (clang, -O0):
<mbasename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xeb2(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
jne    11d1 <mbasename+0xa1>
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rsi
add    $0x5,%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     11cc <mbasename+0x9c>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
jne    11b6 <mbasename+0x86>
mov    -0x20(%rbp),%rax
movb   $0x5f,(%rax)
jmp    11bb <mbasename+0x8b>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1192 <mbasename+0x62>
jmp    122a <mbasename+0xfa>
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    11fa <mbasename+0xca>
mov    -0x8(%rbp),%rax
mov    %rax,-0x20(%rbp)
jmp    1206 <mbasename+0xd6>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    $0x1,%edx
call   1030 <strncpy@plt>
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<mbasename>:
push   %rbp
push   %r14
push   %rbx
mov    %edx,%ebp
mov    %rsi,%r14
mov    %rdi,%rbx
lea    0xebd(%rip),%rsi
mov    $0x5,%edx
call   1040 <strncmp@plt>
test   %rax,%rax
je     1181 <mbasename+0x51>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1050 <strrchr@plt>
lea    0x1(%rax),%rsi
test   %rax,%rax
cmove  %rbx,%rsi
movslq %ebp,%rbx
lea    -0x1(%rbx),%rdx
mov    %r14,%rdi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%rbx,1)
jmp    11c3 <mbasename+0x93>
add    $0x5,%rbx
movslq %ebp,%rbp
lea    -0x1(%rbp),%rdx
mov    %r14,%rdi
mov    %rbx,%rsi
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%r14,%rbp,1)
mov    %r14,%rax
jmp    11b7 <mbasename+0x87>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
movb   $0x5f,(%rax)
add    $0x1,%rax
movzbl (%rax),%ecx
cmp    $0x2f,%cl
je     11b0 <mbasename+0x80>
test   %cl,%cl
jne    11b3 <mbasename+0x83>
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [6 x i8] c"/dev/\00"
define i8* @mbasename(i8* %0, i8* returned %1, i32 %2) {
%4 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i32 5)
%5 = icmp eq i64 %4, 0
br i1 %5, label %6, label %18
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %0, i64 5
%8 = add nsw i32 %2, -1
%9 = tail call i32 @strncpy(i8* %1, i8* %7, i32 %8)
%10 = sext i32 %8 to i64
%11 = getelementptr inbounds i8, i8* %1, i64 %10
store i8 0, i8* %11
br label %12
12: ; preds = %16, %6
%13 = phi i8* [ %1, %6 ], [ %17, %16 ]
%14 = load i8, i8* %13
switch i8 %14, label %16 [
i8 0, label %27
i8 47, label %15
]
15: ; preds = %12
store i8 95, i8* %13
br label %16
16: ; preds = %12, %15
%17 = getelementptr inbounds i8, i8* %13, i64 1
br label %12
18: ; preds = %3
%19 = tail call i8* @strrchr(i8* dereferenceable(1) %0, i8 signext 47)
%20 = icmp eq i8* %19, null
%21 = getelementptr inbounds i8, i8* %19, i64 1
%22 = select i1 %20, i8* %0, i8* %21
%23 = add nsw i32 %2, -1
%24 = tail call i32 @strncpy(i8* %1, i8* %22, i32 %23)
%25 = sext i32 %23 to i64
%26 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 0, i8* %26
br label %27
27: ; preds = %12, %18
ret i8* %1
}