char *partname(char *dev, int pno, int lth) {
  static char bufp[80];
  char *p;
  int w, wp;
  w = strlen(dev);
  p = "";
  if (isdigit(dev[w - 1]))
    p = "p";
  if (strcmp(dev + w - 4, "disc") == 0) {
    w -= 4;
    p = "part";
  }
  if ((strncmp(dev, "/dev/disk/by-id", strlen("/dev/disk/by-id")) == 0) ||
      strncmp(dev, "/dev/disk/by-path", strlen("/dev/disk/by-path")) == 0) {
    p = "-part";
  }
  wp = strlen(p);
  if (lth) {
    snprintf(bufp, sizeof(bufp), "%*.*s%s%-2u", lth - wp - 2, w, dev, p, pno);
  } else {
    snprintf(bufp, sizeof(bufp), "%.*s%s%-2u", w, dev, p, pno);
  }
  return bufp;
}


x86 assembly (clang, -O0):
<partname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x1c(%rbp)
lea    0xe91(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%edi
call   1040 <isdigit@plt>
cmp    $0x0,%eax
je     11a3 <partname+0x53>
lea    0xe61(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xe49(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%eax
jne    11db <partname+0x8b>
mov    -0x1c(%rbp),%eax
sub    $0x4,%eax
mov    %eax,-0x1c(%rbp)
lea    0xe53(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe21(%rip),%rsi
mov    $0xf,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
je     1217 <partname+0xc7>
mov    -0x8(%rbp),%rdi
lea    0xe13(%rip),%rsi
mov    $0x11,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1222 <partname+0xd2>
lea    0xe0b(%rip),%rax
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x10(%rbp)
je     1277 <partname+0x127>
mov    -0x10(%rbp),%ecx
sub    -0x20(%rbp),%ecx
sub    $0x2,%ecx
mov    -0x1c(%rbp),%r8d
mov    -0x8(%rbp),%r9
mov    -0x18(%rbp),%r10
mov    -0xc(%rbp),%eax
lea    0x2e09(%rip),%rdi
mov    $0x50,%esi
lea    0xdcc(%rip),%rdx
mov    %r10,(%rsp)
mov    %eax,0x8(%rsp)
mov    $0x0,%al
call   1060 <snprintf@plt>
jmp    12a2 <partname+0x152>
mov    -0x1c(%rbp),%ecx
mov    -0x8(%rbp),%r8
mov    -0x18(%rbp),%r9
mov    -0xc(%rbp),%eax
lea    0x2dd4(%rip),%rdi
mov    $0x50,%esi
lea    0xda3(%rip),%rdx
mov    %eax,(%rsp)
mov    $0x0,%al
call   1060 <snprintf@plt>
lea    0x2db7(%rip),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<partname>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%r12d
mov    %rsi,(%rsp)
mov    %rdi,%rbx
call   1040 <strlen@plt>
mov    %rax,%rbp
movslq %ebp,%r14
lea    (%rbx,%r14,1),%rdi
add    $0xfffffffffffffffc,%rdi
lea    0xe93(%rip),%rsi
call   1060 <strcmp@plt>
mov    %eax,%r13d
lea    -0x4(%r14),%r15d
test   %eax,%eax
cmovne %r14d,%r15d
lea    0xe7f(%rip),%rsi
mov    $0xf,%edx
mov    %rbx,%rdi
call   1030 <strncmp@plt>
test   %eax,%eax
je     1231 <partname+0xf1>
shl    $0x20,%rbp
movabs $0xffffffff00000000,%rax
ff ff ff
add    %rbp,%rax
sar    $0x20,%rax
movsbl (%rbx,%rax,1),%eax
add    $0xffffffd0,%eax
cmp    $0xa,%eax
lea    0xe3d(%rip),%rax
lea    0xe3c(%rip),%rcx
cmovb  %rax,%rcx
test   %r13d,%r13d
lea    0xe52(%rip),%rbp
cmovne %rcx,%rbp
lea    0xe34(%rip),%rsi
mov    $0x11,%edx
mov    %rbx,%rdi
call   1030 <strncmp@plt>
test   %eax,%eax
je     1231 <partname+0xf1>
test   %r12d,%r12d
je     123d <partname+0xfd>
mov    %rbp,%rdi
call   1040 <strlen@plt>
sub    %eax,%r12d
add    $0xfffffffe,%r12d
lea    0x2e41(%rip),%rdi
lea    0xe19(%rip),%rdx
mov    $0x50,%esi
mov    %r12d,%ecx
mov    %r15d,%r8d
mov    %rbx,%r9
xor    %eax,%eax
push   (%rsp)
push   %rbp
call   1050 <snprintf@plt>
jmp    1268 <partname+0x128>
lea    0xdf1(%rip),%rbp
test   %r12d,%r12d
jne    11f9 <partname+0xb9>
sub    $0x8,%rsp
lea    0x2e08(%rip),%rdi
lea    0xdec(%rip),%rdx
mov    $0x50,%esi
mov    %r15d,%ecx
mov    %rbx,%r8
mov    %rbp,%r9
xor    %eax,%eax
push   0x8(%rsp)
call   1050 <snprintf@plt>
add    $0x10,%rsp
lea    0x2ddd(%rip),%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@partname.bufp = internal global [80 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"p\00"
@.str.2 = [5 x i8] c"disc\00"
@.str.3 = [5 x i8] c"part\00"
@.str.4 = [16 x i8] c"/dev/disk/by-id\00"
@.str.5 = [18 x i8] c"/dev/disk/by-path\00"
@.str.6 = [6 x i8] c"-part\00"
@.str.7 = [12 x i8] c"%*.*s%s%-2u\00"
@.str.8 = [11 x i8] c"%.*s%s%-2u\00"
define i8* @partname(i8* %0, i32 %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32 %2, i32* %6
%10 = load i8*, i8** %4
%11 = call i64 @strlen(i8* %10)
%12 = trunc i64 %11 to i32
store i32 %12, i32* %8
store i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), i8** %7
%13 = load i8*, i8** %4
%14 = load i32, i32* %8
%15 = sub nsw i32 %14, 1
%16 = sext i32 %15 to i64
%17 = getelementptr inbounds i8, i8* %13, i64 %16
%18 = load i8, i8* %17
%19 = sext i8 %18 to i32
%20 = call i32 @isdigit(i32 %19)
%21 = icmp ne i32 %20, 0
br i1 %21, label %22, label %23
22: ; preds = %3
store i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8** %7
br label %23
23: ; preds = %22, %3
%24 = load i8*, i8** %4
%25 = load i32, i32* %8
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = getelementptr inbounds i8, i8* %27, i64 -4
%29 = call i32 @strcmp(i8* %28, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%30 = icmp eq i32 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %23
%32 = load i32, i32* %8
%33 = sub nsw i32 %32, 4
store i32 %33, i32* %8
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8** %7
br label %34
34: ; preds = %31, %23
%35 = load i8*, i8** %4
%36 = call i32 @strncmp(i8* %35, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 15)
%37 = icmp eq i32 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %34
%39 = load i8*, i8** %4
%40 = call i32 @strncmp(i8* %39, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i64 17)
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %43
42: ; preds = %38, %34
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), i8** %7
br label %43
43: ; preds = %42, %38
%44 = load i8*, i8** %7
%45 = call i64 @strlen(i8* %44)
%46 = trunc i64 %45 to i32
store i32 %46, i32* %9
%47 = load i32, i32* %6
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %59
49: ; preds = %43
%50 = load i32, i32* %6
%51 = load i32, i32* %9
%52 = sub nsw i32 %50, %51
%53 = sub nsw i32 %52, 2
%54 = load i32, i32* %8
%55 = load i8*, i8** %4
%56 = load i8*, i8** %7
%57 = load i32, i32* %5
%58 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @partname.bufp, i64 0, i64 0), i64 80, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %53, i32 %54, i8* %55, i8* %56, i32 %57)
br label %65
59: ; preds = %43
%60 = load i32, i32* %8
%61 = load i8*, i8** %4
%62 = load i8*, i8** %7
%63 = load i32, i32* %5
%64 = call i32 (i8*, i64, i8*, ...) @snprintf(i8* getelementptr inbounds ([80 x i8], [80 x i8]* @partname.bufp, i64 0, i64 0), i64 80, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i32 %60, i8* %61, i8* %62, i32 %63)
br label %65
65: ; preds = %59, %49
ret i8* getelementptr inbounds ([80 x i8], [80 x i8]* @partname.bufp, i64 0, i64 0)
}

LLVM IR (-O2):
@partname.bufp = internal global [80 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
@.str.1 = [2 x i8] c"p\00"
@.str.2 = [5 x i8] c"disc\00"
@.str.3 = [5 x i8] c"part\00"
@.str.4 = [16 x i8] c"/dev/disk/by-id\00"
@.str.5 = [18 x i8] c"/dev/disk/by-path\00"
@.str.6 = [6 x i8] c"-part\00"
@.str.7 = [12 x i8] c"%*.*s%s%-2u\00"
@.str.8 = [11 x i8] c"%.*s%s%-2u\00"
define i8* @partname(i8* %0, i32 %1, i32 %2) {
%4 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%5 = trunc i64 %4 to i32
%6 = shl i64 %4, 32
%7 = ashr exact i64 %6, 32
%8 = getelementptr inbounds i8, i8* %0, i64 %7
%9 = getelementptr inbounds i8, i8* %8, i64 -4
%10 = tail call i32 @strcmp(i8* %9, i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%11 = icmp eq i32 %10, 0
%12 = add nsw i32 %5, -4
%13 = select i1 %11, i32 %12, i32 %5
%14 = tail call i32 @strncmp(i8* dereferenceable(1) %0, i8* dereferenceable(16) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i64 15)
%15 = icmp eq i32 %14, 0
br i1 %15, label %28, label %16
16: ; preds = %3
%17 = add i64 %6, -4294967296
%18 = ashr exact i64 %17, 32
%19 = getelementptr inbounds i8, i8* %0, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = add nsw i32 %21, -48
%23 = icmp ult i32 %22, 10
%24 = select i1 %23, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0)
%25 = select i1 %11, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i8* %24
%26 = tail call i32 @strncmp(i8* dereferenceable(1) %0, i8* dereferenceable(18) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.5, i64 0, i64 0), i64 17)
%27 = icmp eq i32 %26, 0
br i1 %27, label %28, label %29
28: ; preds = %16, %3
br label %29
29: ; preds = %28, %16
%30 = phi i8* [ getelementptr inbounds ([6 x i8], [6 x i8]* @.str.6, i64 0, i64 0), %28 ], [ %25, %16 ]
%31 = icmp eq i32 %2, 0
br i1 %31, label %38, label %32
32: ; preds = %29
%33 = tail call i64 @strlen(i8* dereferenceable(1) %30)
%34 = trunc i64 %33 to i32
%35 = add i32 %2, -2
%36 = sub i32 %35, %34
%37 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @partname.bufp, i64 0, i64 0), i64 80, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.7, i64 0, i64 0), i32 %36, i32 %13, i8* %0, i8* %30, i32 %1)
br label %40
38: ; preds = %29
%39 = tail call i32 (i8*, i64, i8*, ...) @snprintf(i8* dereferenceable(1) getelementptr inbounds ([80 x i8], [80 x i8]* @partname.bufp, i64 0, i64 0), i64 80, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.8, i64 0, i64 0), i32 %13, i8* %0, i8* %30, i32 %1)
br label %40
40: ; preds = %38, %32
ret i8* getelementptr inbounds ([80 x i8], [80 x i8]* @partname.bufp, i64 0, i64 0)
}