#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 scalar_t__ ctoken;

/* Variables and functions */
int /*<<< orphan*/ fuerror(char *, char *);
int /*<<< orphan*/ isprint(int);
int /*<<< orphan*/ werror(char *, char *);
char *basename(char *string) {
  static char retval[256];
  char *last_dot, *last_start;
  register char *store;
  register char *fetch;
  register int ctoken;
  last_dot = (char *)0;
  last_start = string;
  for (fetch = string;; fetch++) {
    switch (ctoken = *fetch & 0377) {
    case '.':
      last_dot = fetch;
      break;
    case '/':
      last_start = fetch + 1;
      break;
    case 0:
      goto out;
    }
    if (!(((ctoken) & ~0x7f) == 0) || !isprint(ctoken)) {
      werror("non-ascii characters in argument %s", string);
    }
  }
out:
  if (!*last_start)
    fuerror("empty filename \"%s\"", string);
  for (fetch = last_start, store = retval;
       *fetch && fetch != last_dot && store < &retval[sizeof retval - 1];
       fetch++, store++) {
    *store = *fetch;
  }
  *store = 0;
  return retval;
}


x86 assembly (clang, -O0):
<basename>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movzbl (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    %eax,-0x2c(%rbp)
test   %eax,%eax
je     11ae <basename+0x7e>
jmp    116e <basename+0x3e>
mov    -0x30(%rbp),%eax
sub    $0x2e,%eax
je     1190 <basename+0x60>
jmp    117f <basename+0x4f>
mov    -0x30(%rbp),%eax
sub    $0x2f,%eax
je     119d <basename+0x6d>
jmp    11b3 <basename+0x83>
mov    -0x28(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    11b3 <basename+0x83>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    11b3 <basename+0x83>
jmp    11f9 <basename+0xc9>
mov    -0x2c(%rbp),%eax
and    $0xffffff80,%eax
cmp    $0x0,%eax
jne    11d3 <basename+0xa3>
mov    -0x2c(%rbp),%edi
call   1040 <isprint@plt>
cmp    $0x0,%eax
jne    11e3 <basename+0xb3>
mov    -0x8(%rbp),%rsi
lea    0xe22(%rip),%rdi
call   1030 <werror@plt>
jmp    11e8 <basename+0xb8>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1154 <basename+0x24>
mov    -0x18(%rbp),%rax
cmpb   $0x0,(%rax)
jne    1216 <basename+0xe6>
mov    -0x8(%rbp),%rsi
lea    0xe13(%rip),%rdi
call   1050 <fuerror@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
lea    0x2e2b(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x31(%rbp)
je     1268 <basename+0x138>
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x31(%rbp)
je     1268 <basename+0x138>
lea    0x2df8(%rip),%rax
add    $0xff,%rax
cmp    %rax,-0x20(%rbp)
setb   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1278 <basename+0x148>
jmp    12a1 <basename+0x171>
mov    -0x28(%rbp),%rax
mov    (%rax),%cl
mov    -0x20(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    1229 <basename+0xf9>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
lea    0x2da1(%rip),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<basename>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rdi,%r12
xor    %r15d,%r15d
lea    0xeba(%rip),%r14
mov    %rdi,%r13
mov    %rdi,%rbx
jmp    115f <basename+0x2f>
xchg   %ax,%ax
mov    %r14,%rdi
mov    %r12,%rsi
call   1030 <werror@plt>
add    $0x1,%rbx
movzbl (%rbx),%edi
cmp    $0x2e,%edi
je     1180 <basename+0x50>
cmp    $0x2f,%dil
je     1190 <basename+0x60>
test   %dil,%dil
je     119f <basename+0x6f>
test   $0x80,%dil
jne    1150 <basename+0x20>
jmp    1194 <basename+0x64>
nopw   0x0(%rax,%rax,1)
mov    %rbx,%r15
jmp    1194 <basename+0x64>
cs nopw 0x0(%rax,%rax,1)
nop
lea    0x1(%rbx),%r13
call   1040 <isprint@plt>
test   %eax,%eax
jne    115b <basename+0x2b>
jmp    1150 <basename+0x20>
mov    0x0(%r13),%cl
test   %cl,%cl
jne    11be <basename+0x8e>
lea    0xe76(%rip),%rdi
mov    %r12,%rsi
call   1050 <fuerror@plt>
mov    0x0(%r13),%cl
test   %cl,%cl
je     11ef <basename+0xbf>
lea    0x2e8b(%rip),%rax
lea    0x2f83(%rip),%rdx
nopl   0x0(%rax)
cmp    %r15,%r13
je     11f6 <basename+0xc6>
cmp    %rdx,%rax
jae    11f6 <basename+0xc6>
mov    %cl,(%rax)
add    $0x1,%rax
movzbl 0x1(%r13),%ecx
add    $0x1,%r13
test   %cl,%cl
jne    11d0 <basename+0xa0>
jmp    11f6 <basename+0xc6>
lea    0x2e5a(%rip),%rax
movb   $0x0,(%rax)
lea    0x2e50(%rip),%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@basename.retval = internal global [256 x i8] zeroinitializer
@.str = [36 x i8] c"non-ascii characters in argument %s\00"
@.str.1 = [20 x i8] c"empty filename \22%s\22\00"
define i8* @basename(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %2
store i8* null, i8** %3
%8 = load i8*, i8** %2
store i8* %8, i8** %4
%9 = load i8*, i8** %2
store i8* %9, i8** %6
br label %10
10: ; preds = %33, %1
%11 = load i8*, i8** %6
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = and i32 %13, 255
store i32 %14, i32* %7
switch i32 %14, label %21 [
i32 46, label %15
i32 47, label %17
i32 0, label %20
]
15: ; preds = %10
%16 = load i8*, i8** %6
store i8* %16, i8** %3
br label %21
17: ; preds = %10
%18 = load i8*, i8** %6
%19 = getelementptr inbounds i8, i8* %18, i64 1
store i8* %19, i8** %4
br label %21
20: ; preds = %10
br label %36
21: ; preds = %10, %17, %15
%22 = load i32, i32* %7
%23 = and i32 %22, -128
%24 = icmp eq i32 %23, 0
br i1 %24, label %25, label %29
25: ; preds = %21
%26 = load i32, i32* %7
%27 = call i32 @isprint(i32 %26)
%28 = icmp ne i32 %27, 0
br i1 %28, label %32, label %29
29: ; preds = %25, %21
%30 = load i8*, i8** %2
%31 = call i32 @werror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %30)
br label %32
32: ; preds = %29, %25
br label %33
33: ; preds = %32
%34 = load i8*, i8** %6
%35 = getelementptr inbounds i8, i8* %34, i32 1
store i8* %35, i8** %6
br label %10
36: ; preds = %20
%37 = load i8*, i8** %4
%38 = load i8, i8* %37
%39 = icmp ne i8 %38, 0
br i1 %39, label %43, label %40
40: ; preds = %36
%41 = load i8*, i8** %2
%42 = call i32 @fuerror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %41)
br label %43
43: ; preds = %40, %36
%44 = load i8*, i8** %4
store i8* %44, i8** %6
store i8* getelementptr inbounds ([256 x i8], [256 x i8]* @basename.retval, i64 0, i64 0), i8** %5
br label %45
45: ; preds = %63, %43
%46 = load i8*, i8** %6
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %57
50: ; preds = %45
%51 = load i8*, i8** %6
%52 = load i8*, i8** %3
%53 = icmp ne i8* %51, %52
br i1 %53, label %54, label %57
54: ; preds = %50
%55 = load i8*, i8** %5
%56 = icmp ult i8* %55, getelementptr inbounds ([256 x i8], [256 x i8]* @basename.retval, i64 0, i64 255)
br label %57
57: ; preds = %54, %50, %45
%58 = phi i1 [ false, %50 ], [ false, %45 ], [ %56, %54 ]
br i1 %58, label %59, label %68
59: ; preds = %57
%60 = load i8*, i8** %6
%61 = load i8, i8* %60
%62 = load i8*, i8** %5
store i8 %61, i8* %62
br label %63
63: ; preds = %59
%64 = load i8*, i8** %6
%65 = getelementptr inbounds i8, i8* %64, i32 1
store i8* %65, i8** %6
%66 = load i8*, i8** %5
%67 = getelementptr inbounds i8, i8* %66, i32 1
store i8* %67, i8** %5
br label %45
68: ; preds = %57
%69 = load i8*, i8** %5
store i8 0, i8* %69
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @basename.retval, i64 0, i64 0)
}

LLVM IR (-O2):
@basename.retval = internal global [256 x i8] zeroinitializer
@.str = [36 x i8] c"non-ascii characters in argument %s\00"
@.str.1 = [20 x i8] c"empty filename \22%s\22\00"
define i8* @basename(i8* %0) {
br label %2
2: ; preds = %22, %1
%3 = phi i8* [ %0, %1 ], [ %24, %22 ]
%4 = phi i8* [ %0, %1 ], [ %25, %22 ]
%5 = phi i8* [ null, %1 ], [ %23, %22 ]
%6 = load i8, i8* %4
%7 = zext i8 %6 to i32
switch i8 %6, label %10 [
i8 46, label %13
i8 47, label %8
i8 0, label %26
]
8: ; preds = %2
%9 = getelementptr inbounds i8, i8* %4, i64 1
br label %13
10: ; preds = %2
%11 = and i32 %7, 128
%12 = icmp eq i32 %11, 0
br i1 %12, label %13, label %18
13: ; preds = %2, %8, %10
%14 = phi i8* [ %5, %10 ], [ %5, %8 ], [ %4, %2 ]
%15 = phi i8* [ %3, %10 ], [ %9, %8 ], [ %3, %2 ]
%16 = tail call i32 @isprint(i32 %7)
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %13, %10
%19 = phi i8* [ %14, %13 ], [ %5, %10 ]
%20 = phi i8* [ %15, %13 ], [ %3, %10 ]
%21 = tail call i32 @werror(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0)
br label %22
22: ; preds = %13, %18
%23 = phi i8* [ %14, %13 ], [ %19, %18 ]
%24 = phi i8* [ %15, %13 ], [ %20, %18 ]
%25 = getelementptr inbounds i8, i8* %4, i64 1
br label %2
26: ; preds = %2
%27 = load i8, i8* %3
%28 = icmp eq i8 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = tail call i32 @fuerror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%31 = load i8, i8* %3
%32 = icmp eq i8 %31, 0
br i1 %32, label %47, label %33
33: ; preds = %26, %29
%34 = phi i8 [ %27, %26 ], [ %31, %29 ]
br label %35
35: ; preds = %33, %42
%36 = phi i8 [ %45, %42 ], [ %34, %33 ]
%37 = phi i8* [ %43, %42 ], [ %3, %33 ]
%38 = phi i8* [ %44, %42 ], [ getelementptr inbounds ([256 x i8], [256 x i8]* @basename.retval, i64 0, i64 0), %33 ]
%39 = icmp ne i8* %37, %5
%40 = icmp ult i8* %38, getelementptr inbounds ([256 x i8], [256 x i8]* @basename.retval, i64 0, i64 255)
%41 = select i1 %39, i1 %40, i1 false
br i1 %41, label %42, label %47
42: ; preds = %35
store i8 %36, i8* %38
%43 = getelementptr inbounds i8, i8* %37, i64 1
%44 = getelementptr inbounds i8, i8* %38, i64 1
%45 = load i8, i8* %43
%46 = icmp eq i8 %45, 0
br i1 %46, label %47, label %35
47: ; preds = %35, %42, %29
%48 = phi i8* [ getelementptr inbounds ([256 x i8], [256 x i8]* @basename.retval, i64 0, i64 0), %29 ], [ %44, %42 ], [ %38, %35 ]
store i8 0, i8* %48
ret i8* getelementptr inbounds ([256 x i8], [256 x i8]* @basename.retval, i64 0, i64 0)
}