#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 size_t ssize_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
scalar_t__ EINVAL;
int /*<<< orphan*/ LOG_DEBUG;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ isdigit(char);
size_t readlink(char *, char *, int);
int /*<<< orphan*/ stderr;
char *strdup(char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
char *strrchr(char *, char);
int /*<<< orphan*/ write_log(int /*<<< orphan*/, char *, char *, char *);
char *disk_name(char *path) {
  ssize_t len;
  char buf[256];
  char *s;
  if (*path != '/')
    return path;
  if ((len = readlink(path, buf, sizeof(buf) - 1)) <= 0) {
    if (errno != EINVAL)
      return path;
    strncpy(buf, path, sizeof(buf) - 1);
    buf[sizeof(buf) - 1] = '\0';
    len = strlen(buf);
  }
  buf[len] = '\0';
  for (s = buf + strlen(buf) - 1; s >= buf && isdigit(*s); s--)
    *s = '\0';
  if ((s = strrchr(buf, '/')) != NULL)
    s++;
  else
    s = buf;
  if ((s = strdup(s)) == NULL) {
    fprintf(stderr, "out of memory");
    exit(2);
  }
  write_log(LOG_DEBUG, "using %s for %s", s, path);
  return (s);
}


x86 assembly (clang, -O0):
<disk_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x2f,%eax
je     11bc <disk_name+0x2c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    138e <disk_name+0x1fe>
mov    -0x10(%rbp),%rdi
lea    -0x120(%rbp),%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
ja     1231 <disk_name+0xa1>
mov    0x2df2(%rip),%rax
mov    (%rax),%rax
mov    0x2de0(%rip),%rcx
cmp    (%rcx),%rax
je     1206 <disk_name+0x76>
mov    -0x10(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    138e <disk_name+0x1fe>
lea    -0x120(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x21(%rbp)
lea    -0x120(%rbp),%rdi
call   1060 <strlen@plt>
cltq
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,-0x120(%rbp,%rax,1)
lea    -0x120(%rbp),%rax
mov    %rax,-0x130(%rbp)
lea    -0x120(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x130(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rcx
lea    -0x120(%rbp),%rdx
xor    %eax,%eax
cmp    %rdx,%rcx
mov    %al,-0x131(%rbp)
jb     12ac <disk_name+0x11c>
mov    -0x128(%rbp),%rax
movsbl (%rax),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x131(%rbp)
mov    -0x131(%rbp),%al
test   $0x1,%al
jne    12bf <disk_name+0x12f>
jmp    12e0 <disk_name+0x150>
mov    -0x128(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x128(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x128(%rbp)
jmp    1271 <disk_name+0xe1>
lea    -0x120(%rbp),%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
je     1319 <disk_name+0x189>
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
jmp    1327 <disk_name+0x197>
lea    -0x120(%rbp),%rax
mov    %rax,-0x128(%rbp)
mov    -0x128(%rbp),%rdi
call   10b0 <strdup@plt>
mov    %rax,-0x128(%rbp)
cmp    $0x0,%rax
jne    1363 <disk_name+0x1d3>
mov    0x2c95(%rip),%rax
mov    (%rax),%edi
lea    0xcac(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    0x2c5e(%rip),%rax
mov    (%rax),%edi
mov    -0x128(%rbp),%rdx
mov    -0x10(%rbp),%rcx
lea    0xc90(%rip),%rsi
call   1090 <write_log@plt>
mov    -0x128(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<disk_name>:
push   %r15
push   %r14
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r15
cmpb   $0x2f,(%rdi)
jne    127c <disk_name+0xec>
mov    %rsp,%r14
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
test   %rax,%rax
jne    11ff <disk_name+0x6f>
mov    0x2e11(%rip),%rax
mov    (%rax),%rax
mov    0x2dff(%rip),%rcx
cmp    (%rcx),%rax
jne    127c <disk_name+0xec>
mov    %rsp,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    $0xff,%edx
call   1030 <strncpy@plt>
movb   $0x0,0xff(%rsp)
mov    %rbx,%rdi
call   1060 <strlen@plt>
cltq
movb   $0x0,(%rsp,%rax,1)
mov    %r14,%rdi
call   1060 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rbx
add    $0xffffffffffffffff,%rbx
cmp    %r14,%rbx
jb     1239 <disk_name+0xa9>
nopw   0x0(%rax,%rax,1)
movsbl (%rbx),%edi
call   1040 <isdigit@plt>
test   %rax,%rax
je     1239 <disk_name+0xa9>
movb   $0x0,(%rbx)
add    $0xffffffffffffffff,%rbx
cmp    %r14,%rbx
jae    1220 <disk_name+0x90>
mov    %r14,%rdi
mov    $0x2f,%esi
call   1070 <strrchr@plt>
lea    0x1(%rax),%rdi
test   %rax,%rax
cmove  %r14,%rdi
call   10b0 <strdup@plt>
test   %rax,%rax
je     128c <disk_name+0xfc>
mov    %rax,%rbx
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
mov    %rbx,%rdx
mov    %r15,%rcx
call   1090 <write_log@plt>
mov    %rbx,%r15
mov    %r15,%rax
add    $0x100,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2d4d(%rip),%rax
mov    (%rax),%edi
lea    0xd64(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
@.str = [14 x i8] c"out of memory\00"
@LOG_DEBUG = global i32 0
@.str.1 = [16 x i8] c"using %s for %s\00"
define i8* @disk_name(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i64
%5 = alloca [256 x i8]
%6 = alloca i8*
store i8* %0, i8** %3
%7 = load i8*, i8** %3
%8 = load i8, i8* %7
%9 = sext i8 %8 to i32
%10 = icmp ne i32 %9, 47
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = load i8*, i8** %3
store i8* %12, i8** %2
br label %80
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%16 = call i64 @readlink(i8* %14, i8* %15, i32 255)
store i64 %16, i64* %4
%17 = icmp ule i64 %16, 0
br i1 %17, label %18, label %32
18: ; preds = %13
%19 = load i64, i64* @errno
%20 = load i64, i64* @EINVAL
%21 = icmp ne i64 %19, %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = load i8*, i8** %3
store i8* %23, i8** %2
br label %80
24: ; preds = %18
%25 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%26 = load i8*, i8** %3
%27 = call i32 @strncpy(i8* %25, i8* %26, i32 255)
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 255
store i8 0, i8* %28
%29 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = sext i32 %30 to i64
store i64 %31, i64* %4
br label %32
32: ; preds = %24, %13
%33 = load i64, i64* %4
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 %33
store i8 0, i8* %34
%35 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%36 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%37 = call i32 @strlen(i8* %36)
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %35, i64 %38
%40 = getelementptr inbounds i8, i8* %39, i64 -1
store i8* %40, i8** %6
br label %41
41: ; preds = %54, %32
%42 = load i8*, i8** %6
%43 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%44 = icmp uge i8* %42, %43
br i1 %44, label %45, label %50
45: ; preds = %41
%46 = load i8*, i8** %6
%47 = load i8, i8* %46
%48 = call i64 @isdigit(i8 signext %47)
%49 = icmp ne i64 %48, 0
br label %50
50: ; preds = %45, %41
%51 = phi i1 [ false, %41 ], [ %49, %45 ]
br i1 %51, label %52, label %57
52: ; preds = %50
%53 = load i8*, i8** %6
store i8 0, i8* %53
br label %54
54: ; preds = %52
%55 = load i8*, i8** %6
%56 = getelementptr inbounds i8, i8* %55, i32 -1
store i8* %56, i8** %6
br label %41
57: ; preds = %50
%58 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%59 = call i8* @strrchr(i8* %58, i8 signext 47)
store i8* %59, i8** %6
%60 = icmp ne i8* %59, null
br i1 %60, label %61, label %64
61: ; preds = %57
%62 = load i8*, i8** %6
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %6
br label %66
64: ; preds = %57
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
store i8* %65, i8** %6
br label %66
66: ; preds = %64, %61
%67 = load i8*, i8** %6
%68 = call i8* @strdup(i8* %67)
store i8* %68, i8** %6
%69 = icmp eq i8* %68, null
br i1 %69, label %70, label %74
70: ; preds = %66
%71 = load i32, i32* @stderr
%72 = call i32 @fprintf(i32 %71, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%73 = call i32 @exit(i32 2)
unreachable
74: ; preds = %66
%75 = load i32, i32* @LOG_DEBUG
%76 = load i8*, i8** %6
%77 = load i8*, i8** %3
%78 = call i32 @write_log(i32 %75, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %76, i8* %77)
%79 = load i8*, i8** %6
store i8* %79, i8** %2
br label %80
80: ; preds = %74, %22, %11
%81 = load i8*, i8** %2
ret i8* %81
}

LLVM IR (-O2):
@errno = global i64 0
@EINVAL = global i64 0
@stderr = global i32 0
@.str = [14 x i8] c"out of memory\00"
@LOG_DEBUG = global i32 0
@.str.1 = [16 x i8] c"using %s for %s\00"
define i8* @disk_name(i8* %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = load i8, i8* %0
%5 = icmp eq i8 %4, 47
br i1 %5, label %6, label %48
6: ; preds = %1
%7 = call i64 @readlink(i8* %0, i8* %3, i32 255)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %18
9: ; preds = %6
%10 = load i64, i64* @errno
%11 = load i64, i64* @EINVAL
%12 = icmp eq i64 %10, %11
br i1 %12, label %13, label %48
13: ; preds = %9
%14 = call i32 @strncpy(i8* %3, i8* %0, i32 255)
%15 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 255
store i8 0, i8* %15
%16 = call i32 @strlen(i8* %3)
%17 = sext i32 %16 to i64
br label %18
18: ; preds = %13, %6
%19 = phi i64 [ %17, %13 ], [ %7, %6 ]
%20 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %19
store i8 0, i8* %20
%21 = call i32 @strlen(i8* %3)
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 %22
%24 = getelementptr inbounds i8, i8* %23, i64 -1
%25 = icmp ult i8* %24, %3
br i1 %25, label %34, label %26
26: ; preds = %18, %31
%27 = phi i8* [ %32, %31 ], [ %24, %18 ]
%28 = load i8, i8* %27
%29 = call i64 @isdigit(i8 signext %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %26
store i8 0, i8* %27
%32 = getelementptr inbounds i8, i8* %27, i64 -1
%33 = icmp ult i8* %32, %3
br i1 %33, label %34, label %26
34: ; preds = %26, %31, %18
%35 = call i8* @strrchr(i8* %3, i8 signext 47)
%36 = icmp eq i8* %35, null
%37 = getelementptr inbounds i8, i8* %35, i64 1
%38 = select i1 %36, i8* %3, i8* %37
%39 = call i8* @strdup(i8* %38)
%40 = icmp eq i8* %39, null
br i1 %40, label %41, label %45
41: ; preds = %34
%42 = load i32, i32* @stderr
%43 = call i32 @fprintf(i32 %42, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
%44 = call i32 @exit(i32 2)
unreachable
45: ; preds = %34
%46 = load i32, i32* @LOG_DEBUG
%47 = call i32 @write_log(i32 %46, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* %39, i8* %0)
br label %48
48: ; preds = %9, %1, %45
%49 = phi i8* [ %39, %45 ], [ %0, %1 ], [ %0, %9 ]
ret i8* %49
}