#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 *);
scalar_t__ strlen(char const *);
scalar_t__ strncmp(char const *, char *, int);
char *strrchr(char *, char);
char *gen_library_name(const char *name, int genlib) {
  char *newarg, *newext;
  newarg = (char *)malloc(strlen(name) + 10);
  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),%rdi
lea    0xe6d(%rip),%rsi
call   1040 <strcpy@plt>
cmpl   $0x2,-0xc(%rbp)
jne    11cd <gen_library_name+0x6d>
mov    -0x8(%rbp),%rdi
lea    0xe5a(%rip),%rsi
mov    $0x3,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%rax
jne    11cd <gen_library_name+0x6d>
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,-0x24(%rbp)
test   %eax,%eax
je     1225 <gen_library_name+0xc5>
jmp    1203 <gen_library_name+0xa3>
mov    -0x24(%rbp),%eax
sub    $0x1,%eax
je     123a <gen_library_name+0xda>
jmp    1214 <gen_library_name+0xb4>
mov    -0x24(%rbp),%eax
sub    $0x2,%eax
je     124f <gen_library_name+0xef>
jmp    125f <gen_library_name+0xff>
mov    -0x20(%rbp),%rdi
lea    0xddb(%rip),%rsi
call   1040 <strcpy@plt>
jmp    125f <gen_library_name+0xff>
mov    -0x20(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   1040 <strcpy@plt>
jmp    125f <gen_library_name+0xff>
mov    -0x20(%rbp),%rdi
lea    0xdb3(%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   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%rbx
call   1050 <strlen@plt>
lea    0xa(%rax),%rdi
call   1070 <malloc@plt>
mov    %rax,%r14
lea    0xe7f(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
cmp    $0x2,%ebp
jne    11d3 <gen_library_name+0x73>
lea    0xe72(%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    %r14,%rdi
call   1080 <strcat@plt>
mov    %r14,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
lea    0x1(%rax),%rdi
lea    0xe3c(%rip),%rsi
jmp    120b <gen_library_name+0xab>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
mov    %r14,%rdi
mov    $0x2e,%esi
call   1060 <strrchr@plt>
add    $0x1,%rax
cmp    $0x1,%ebp
je     1201 <gen_library_name+0xa1>
test   %ebp,%ebp
jne    1210 <gen_library_name+0xb0>
lea    0xe0c(%rip),%rsi
jmp    1208 <gen_library_name+0xa8>
lea    0xe05(%rip),%rsi
mov    %rax,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rax
pop    %rbx
pop    %r14
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*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i8*, i8** %3
%8 = call i64 @strlen(i8* %7)
%9 = add nsw i64 %8, 10
%10 = call i64 @malloc(i64 %9)
%11 = inttoptr i64 %10 to i8*
store i8* %11, i8** %5
%12 = load i8*, i8** %5
%13 = call i32 @strcpy(i8* %12, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, 2
br i1 %15, label %16, label %23
16: ; preds = %2
%17 = load i8*, i8** %3
%18 = call i64 @strncmp(i8* %17, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16
%21 = load i8*, i8** %3
%22 = getelementptr inbounds i8, i8* %21, i64 3
store i8* %22, i8** %3
br label %23
23: ; preds = %20, %16, %2
%24 = load i8*, i8** %5
%25 = load i8*, i8** %3
%26 = call i32 @strcat(i8* %24, i8* %25)
%27 = load i8*, i8** %5
%28 = call i8* @strrchr(i8* %27, i8 signext 46)
%29 = getelementptr inbounds i8, i8* %28, i64 1
store i8* %29, i8** %6
%30 = load i32, i32* %4
switch i32 %30, label %40 [
i32 0, label %31
i32 1, label %34
i32 2, label %37
]
31: ; preds = %23
%32 = load i8*, i8** %6
%33 = call i32 @strcpy(i8* %32, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %40
34: ; preds = %23
%35 = load i8*, i8** %6
%36 = call i32 @strcpy(i8* %35, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %40
37: ; preds = %23
%38 = load i8*, i8** %6
%39 = call i32 @strcpy(i8* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %40
40: ; preds = %23, %37, %34, %31
%41 = load i8*, i8** %5
ret i8* %41
}

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*
%7 = tail call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32 %1, 2
br i1 %8, label %9, label %18
9: ; preds = %2
%10 = tail call i64 @strncmp(i8* %0, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 3)
%11 = icmp eq i64 %10, 0
%12 = select i1 %11, i64 3, i64 0
%13 = getelementptr i8, i8* %0, i64 %12
%14 = tail call i32 @strcat(i8* %6, i8* %13)
%15 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 46)
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = tail call i32 @strcpy(i8* %16, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %26
18: ; preds = %2
%19 = tail call i32 @strcat(i8* %6, i8* %0)
%20 = tail call i8* @strrchr(i8* dereferenceable(1) %6, i8 signext 46)
%21 = getelementptr inbounds i8, i8* %20, i64 1
switch i32 %1, label %26 [
i32 0, label %22
i32 1, label %24
]
22: ; preds = %18
%23 = tail call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %26
24: ; preds = %18
%25 = tail call i32 @strcpy(i8* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %26
26: ; preds = %18, %9, %24, %22
ret i8* %6
}