#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 */
scalar_t__ malloc(scalar_t__);
int /*<<< orphan*/ strcat(char *, char const *);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
scalar_t__ strncmp(char const *, char *, int);
char *strrchr(char const *, char);
char *gen_library_name(const char *name, int genlib) {
  char *newarg, *newext;
  char *cp;
  newarg = (char *)malloc(strlen(name) + 10);
  *newarg = '\0';
  cp = strrchr(name, '/');
  if (cp) {
    cp++;
    strcpy(newarg, name);
    newarg[cp - name] = '\0';
    name = cp;
  } else
    strcpy(newarg, ".libs/");
  if (genlib == 2 && strncmp(name, "lib", 3) == 0) {
    name += 3;
  }
  strcat(newarg, name);
  newext = strrchr(newarg, '.') + 1;
  switch (genlib) {
  case 0:
    strcpy(newext, "a");
    break;
  case 1:
    strcpy(newext, "so");
    break;
  case 2:
    strcpy(newext, "so");
    break;
  }
  return newarg;
}


x86 assembly (clang, -O0):
<gen_library_name>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,%rdi
add    $0xa,%rdi
call   1070 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11e5 <gen_library_name+0x85>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    -0x8(%rbp),%rdx
sub    %rdx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11f5 <gen_library_name+0x95>
mov    -0x18(%rbp),%rdi
lea    0xe10(%rip),%rsi
call   1040 <strcpy@plt>
cmpl   $0x2,-0xc(%rbp)
jne    122a <gen_library_name+0xca>
mov    -0x8(%rbp),%rdi
lea    0xdfd(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    122a <gen_library_name+0xca>
mov    -0x8(%rbp),%rax
add    $0x3,%rax
mov    %rax,-0x8(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1080 <strcat@plt>
mov    -0x18(%rbp),%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x2c(%rbp)
test   %eax,%eax
je     1282 <gen_library_name+0x122>
jmp    1260 <gen_library_name+0x100>
mov    -0x2c(%rbp),%eax
sub    $0x1,%eax
je     1297 <gen_library_name+0x137>
jmp    1271 <gen_library_name+0x111>
mov    -0x2c(%rbp),%eax
sub    $0x2,%eax
je     12ac <gen_library_name+0x14c>
jmp    12bc <gen_library_name+0x15c>
mov    -0x20(%rbp),%rdi
lea    0xd7e(%rip),%rsi
call   1040 <strcpy@plt>
jmp    12bc <gen_library_name+0x15c>
mov    -0x20(%rbp),%rdi
lea    0xd6b(%rip),%rsi
call   1040 <strcpy@plt>
jmp    12bc <gen_library_name+0x15c>
mov    -0x20(%rbp),%rdi
lea    0xd56(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_library_name>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    0xa(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%r15
movb   $0x0,(%rax)
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strrchr@plt>
test   %rax,%rax
je     11fe <gen_library_name+0x9e>
mov    %rax,%rbp
add    $0x1,%rbp
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    %rbp,%rax
sub    %rbx,%rax
movb   $0x0,(%r15,%rax,1)
mov    %rbp,%rbx
cmp    $0x2,%r14d
jne    1213 <gen_library_name+0xb3>
lea    0xe47(%rip),%rsi
mov    %rbx,%rdi
mov    $0x3,%edx
call   1030 <strncmp@plt>
xor    %ecx,%ecx
test   %rax,%rax
sete   %cl
lea    (%rcx,%rcx,2),%rsi
add    %rbx,%rsi
mov    %r15,%rdi
call   1080 <strcat@plt>
mov    %r15,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
lea    0x1(%rax),%rdi
lea    0xe11(%rip),%rsi
jmp    124d <gen_library_name+0xed>
lea    0xdfb(%rip),%rsi
mov    %r15,%rdi
call   1040 <strcpy@plt>
cmp    $0x2,%r14d
je     11b9 <gen_library_name+0x59>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
mov    %r15,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
add    $0x1,%rax
cmp    $0x1,%r14d
je     1243 <gen_library_name+0xe3>
test   %r14d,%r14d
jne    1252 <gen_library_name+0xf2>
lea    0xdca(%rip),%rsi
jmp    124a <gen_library_name+0xea>
lea    0xdc3(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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