#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 */
char *malloc(int);
scalar_t__ memcmp(char const *, char *, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
size_t strlen(char const *);
char *strrchr(char *, char);
char *argv2lib(char const *argv, char **libname) {
  size_t len;
  char *p, *name;
  len = strlen(argv);
  if ((len > 3) && (memcmp(argv, "lib", 3) == 0)) {
    name = malloc(len + sizeof(".so"));
    if (!name)
      return NULL;
    memcpy(name, argv, len);
    memcpy(name + len, ".so", sizeof(".so"));
  } else {
    name = malloc(len + 3 + sizeof(".so"));
    if (!name)
      return NULL;
    memcpy(name, "lib", 3);
    memcpy(name + 3, argv, len);
    memcpy(name + 3 + len, ".so", sizeof(".so"));
  }
  p = strrchr(name, '/');
  if (p) {
    *libname = p + 1;
  } else {
    *libname = name;
  }
  return name;
}


x86 assembly (clang, -O0):
<argv2lib>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x3,-0x20(%rbp)
jbe    11f3 <argv2lib+0xa3>
mov    -0x10(%rbp),%rdi
lea    0xe7d(%rip),%rsi
mov    $0x3,%edx
call   1050 <memcmp@plt>
cmp    $0x0,%rax
jne    11f3 <argv2lib+0xa3>
mov    -0x20(%rbp),%rax
add    $0x4,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11c2 <argv2lib+0x72>
movq   $0x0,-0x8(%rbp)
jmp    12af <argv2lib+0x15f>
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
add    -0x20(%rbp),%rdi
lea    0xe20(%rip),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
jmp    126b <argv2lib+0x11b>
mov    -0x20(%rbp),%rax
add    $0x3,%rax
add    $0x4,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1222 <argv2lib+0xd2>
movq   $0x0,-0x8(%rbp)
jmp    12af <argv2lib+0x15f>
mov    -0x30(%rbp),%rdi
lea    0xdd3(%rip),%rsi
mov    $0x3,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
add    $0x3,%rdi
mov    -0x10(%rbp),%rsi
mov    -0x20(%rbp),%rax
mov    %eax,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
add    $0x3,%rdi
add    -0x20(%rbp),%rdi
lea    0xda3(%rip),%rsi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    -0x30(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     129c <argv2lib+0x14c>
mov    -0x28(%rbp),%rcx
add    $0x1,%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
jmp    12a7 <argv2lib+0x157>
mov    -0x30(%rbp),%rcx
mov    -0x18(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<argv2lib>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
call   1030 <strlen@plt>
mov    %rax,%r12
cmp    $0x4,%rax
jb     1186 <argv2lib+0x36>
lea    0xe8c(%rip),%rsi
mov    %r15,%rdi
mov    $0x3,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     11f9 <argv2lib+0xa9>
lea    0x7(%r12),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     121e <argv2lib+0xce>
mov    %rax,%r13
lea    0xe5d(%rip),%rsi
mov    %rax,%rdi
mov    $0x3,%edx
call   1060 <memcpy@plt>
mov    %r13,%rbx
add    $0x3,%rbx
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1060 <memcpy@plt>
add    %r12,%rbx
lea    0xe35(%rip),%rsi
mov    %rbx,%rdi
mov    $0x4,%edx
call   1060 <memcpy@plt>
mov    %r13,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rcx
test   %rax,%rax
cmove  %r13,%rcx
mov    %rcx,(%r14)
jmp    1221 <argv2lib+0xd1>
lea    0x4(%r12),%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     121e <argv2lib+0xce>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
mov    %r12d,%edx
call   1060 <memcpy@plt>
mov    %rbx,%r13
jmp    11c5 <argv2lib+0x75>
xor    %r13d,%r13d
mov    %r13,%rax
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [4 x i8] c"lib\00"
@.str.1 = [4 x i8] c".so\00"
define i8* @argv2lib(i8* %0, i8** nocapture writeonly %1) {
%3 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%4 = icmp ugt i64 %3, 3
br i1 %4, label %5, label %15
5: ; preds = %2
%6 = tail call i64 @memcmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %8, label %15
8: ; preds = %5
%9 = trunc i64 %3 to i32
%10 = add i32 %9, 4
%11 = tail call i8* @malloc(i32 %10)
%12 = icmp eq i8* %11, null
br i1 %12, label %33, label %13
13: ; preds = %8
%14 = tail call i32 @memcpy(i8* %11, i8* %0, i32 %9)
br label %24
15: ; preds = %5, %2
%16 = trunc i64 %3 to i32
%17 = add i32 %16, 7
%18 = tail call i8* @malloc(i32 %17)
%19 = icmp eq i8* %18, null
br i1 %19, label %33, label %20
20: ; preds = %15
%21 = tail call i32 @memcpy(i8* %18, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i32 3)
%22 = getelementptr inbounds i8, i8* %18, i64 3
%23 = tail call i32 @memcpy(i8* %22, i8* %0, i32 %16)
br label %24
24: ; preds = %20, %13
%25 = phi i8* [ %22, %20 ], [ %11, %13 ]
%26 = phi i8* [ %18, %20 ], [ %11, %13 ]
%27 = getelementptr inbounds i8, i8* %25, i64 %3
%28 = tail call i32 @memcpy(i8* %27, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 4)
%29 = tail call i8* @strrchr(i8* %26, i8 signext 47)
%30 = icmp eq i8* %29, null
%31 = getelementptr inbounds i8, i8* %29, i64 1
%32 = select i1 %30, i8* %26, i8* %31
store i8* %32, i8** %1
br label %33
33: ; preds = %15, %8, %24
%34 = phi i8* [ %26, %24 ], [ null, %8 ], [ null, %15 ]
ret i8* %34
}