#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 /*<<< orphan*/ strcat(char *, char *);
scalar_t__ strcmp(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
char *strrchr(char *, char);
unsigned char so_stdname(char *stdname, char *descname, int buflen) {
  char *ptr;
  if ((descname == 0) || (stdname == 0) ||
      (((int)strlen(descname)) >= buflen - 6))
    return (0);
  strcpy(stdname, descname);
  if ((ptr = strrchr(descname, '/')) == 0)
    ptr = descname;
  else
    ptr++;
  stdname[(int)(ptr - descname)] = 0;
  if (strncmp(ptr, "lib", 3))
    strcat(stdname, "lib");
  strcat(stdname, ptr);
  if (strcmp(stdname + strlen(stdname) - 3, ".so"))
    strcat(stdname, ".so");
  return (1);
}


x86 assembly (clang, -O0):
<so_stdname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11a0 <so_stdname+0x40>
cmpq   $0x0,-0x10(%rbp)
je     11a0 <so_stdname+0x40>
mov    -0x18(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x1c(%rbp),%ecx
sub    $0x6,%ecx
cmp    %ecx,%eax
jl     11a9 <so_stdname+0x49>
movb   $0x0,-0x1(%rbp)
jmp    1285 <so_stdname+0x125>
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
jne    11df <so_stdname+0x7f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
jmp    11eb <so_stdname+0x8b>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x18(%rbp),%rdx
sub    %rdx,%rcx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rdi
lea    0xdf4(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
je     1230 <so_stdname+0xd0>
mov    -0x10(%rbp),%rdi
lea    0xdd5(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x10(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x30(%rbp),%rdi
cltq
add    %rax,%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xda2(%rip),%rsi
call   1070 <strcmp@plt>
cmp    $0x0,%rax
je     1281 <so_stdname+0x121>
mov    -0x10(%rbp),%rdi
lea    0xd88(%rip),%rsi
call   1080 <strcat@plt>
movb   $0x1,-0x1(%rbp)
movzbl -0x1(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<so_stdname>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
xor    %r15d,%r15d
test   %rdi,%rdi
je     1233 <so_stdname+0xd3>
mov    %rsi,%rbx
test   %rsi,%rsi
je     1233 <so_stdname+0xd3>
mov    %edx,%ebp
mov    %rdi,%r14
mov    %rbx,%rdi
call   1050 <strlen@plt>
add    $0xfffffffa,%ebp
cmp    %ebp,%eax
jge    1230 <so_stdname+0xd0>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
lea    0x1(%rax),%r15
test   %rax,%rax
cmove  %rbx,%r15
mov    %r15d,%eax
sub    %ebx,%eax
cltq
movb   $0x0,(%r14,%rax,1)
lea    0xe33(%rip),%rsi
mov    %r15,%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
test   %rax,%rax
je     11ee <so_stdname+0x8e>
lea    0xe1a(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcat@plt>
mov    %r14,%rdi
mov    %r15,%rsi
call   1080 <strcat@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
cltq
lea    (%r14,%rax,1),%rdi
add    $0xfffffffffffffffd,%rdi
lea    0xdf2(%rip),%rsi
call   1070 <strcmp@plt>
mov    $0x1,%r15b
test   %rax,%rax
je     1233 <so_stdname+0xd3>
lea    0xdde(%rip),%rsi
mov    %r14,%rdi
call   1080 <strcat@plt>
jmp    1233 <so_stdname+0xd3>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"lib\00"
@.str.1 = [4 x i8] c".so\00"
define zeroext i8 @so_stdname(i8* %0, i8* %1, i32 %2) {
%4 = icmp eq i8* %1, null
%5 = icmp eq i8* %0, null
%6 = or i1 %5, %4
br i1 %6, label %37, label %7
7: ; preds = %3
%8 = tail call i32 @strlen(i8* %1)
%9 = add nsw i32 %2, -6
%10 = icmp slt i32 %8, %9
br i1 %10, label %11, label %37
11: ; preds = %7
%12 = tail call i32 @strcpy(i8* %0, i8* %1)
%13 = tail call i8* @strrchr(i8* dereferenceable(1) %1, i8 signext 47)
%14 = icmp eq i8* %13, null
%15 = getelementptr inbounds i8, i8* %13, i64 1
%16 = select i1 %14, i8* %1, i8* %15
%17 = ptrtoint i8* %16 to i64
%18 = ptrtoint i8* %1 to i64
%19 = sub i64 %17, %18
%20 = shl i64 %19, 32
%21 = ashr exact i64 %20, 32
%22 = getelementptr inbounds i8, i8* %0, i64 %21
store i8 0, i8* %22
%23 = tail call i64 @strncmp(i8* %16, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%24 = icmp eq i64 %23, 0
br i1 %24, label %27, label %25
25: ; preds = %11
%26 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %25, %11
%28 = tail call i32 @strcat(i8* %0, i8* %16)
%29 = tail call i32 @strlen(i8* %0)
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %0, i64 %30
%32 = getelementptr inbounds i8, i8* %31, i64 -3
%33 = tail call i64 @strcmp(i8* %32, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%34 = icmp eq i64 %33, 0
br i1 %34, label %37, label %35
35: ; preds = %27
%36 = tail call i32 @strcat(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %27, %35, %3, %7
%38 = phi i8 [ 0, %7 ], [ 0, %3 ], [ 1, %35 ], [ 1, %27 ]
ret i8 %38
}