#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*/ assert(int);
int /*<<< orphan*/ memset(char *, int, int /*<<< orphan*/);
char *strdup(char *);
int /*<<< orphan*/ strlen(char *);
char *strrchr(char *, char);
char *newname(char *old, int pat) {
  char *new;
  char *base;
  assert(old != NULL);
  assert(strlen(old) > 0);
  assert(pat != 0);
  assert(pat <= 0xff);
  new = strdup(old);
  if (new) {
    if ((base = strrchr(new, '/')))
      base++;
    else
      base = new;
    memset(base, pat, strlen(base));
  }
  return new;
}


x86 assembly (clang, -O0):
<newname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x0,%eax
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0x0,-0xc(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0xff,-0xc(%rbp)
setle  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x8(%rbp),%rdi
call   1070 <strdup@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     1221 <newname+0xd1>
mov    -0x18(%rbp),%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
je     11f4 <newname+0xa4>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11fc <newname+0xac>
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x30(%rbp),%rdi
mov    -0x24(%rbp),%esi
mov    %eax,%edx
call   1050 <memset@plt>
mov    -0x18(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<newname>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   1030 <strlen@plt>
xor    %edi,%edi
test   %eax,%eax
setg   %dil
call   1060 <assert@plt>
xor    %edi,%edi
test   %r14d,%r14d
setne  %dil
call   1060 <assert@plt>
xor    %edi,%edi
cmp    $0x100,%r14d
setl   %dil
call   1060 <assert@plt>
mov    %rbx,%rdi
call   1070 <strdup@plt>
mov    %rax,%rbx
test   %rax,%rax
je     11da <newname+0x8a>
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1040 <strrchr@plt>
lea    0x1(%rax),%rbp
test   %rax,%rax
cmove  %rbx,%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %rbp,%rdi
mov    %r14d,%esi
mov    %eax,%edx
call   1050 <memset@plt>
mov    %rbx,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i8* @newname(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 = icmp ne i8* %7, null
%9 = zext i1 %8 to i32
%10 = call i32 @assert(i32 %9)
%11 = load i8*, i8** %3
%12 = call i32 @strlen(i8* %11)
%13 = icmp sgt i32 %12, 0
%14 = zext i1 %13 to i32
%15 = call i32 @assert(i32 %14)
%16 = load i32, i32* %4
%17 = icmp ne i32 %16, 0
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %4
%21 = icmp sle i32 %20, 255
%22 = zext i1 %21 to i32
%23 = call i32 @assert(i32 %22)
%24 = load i8*, i8** %3
%25 = call i8* @strdup(i8* %24)
store i8* %25, i8** %5
%26 = load i8*, i8** %5
%27 = icmp ne i8* %26, null
br i1 %27, label %28, label %43
28: ; preds = %2
%29 = load i8*, i8** %5
%30 = call i8* @strrchr(i8* %29, i8 signext 47)
store i8* %30, i8** %6
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %28
%33 = load i8*, i8** %6
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %6
br label %37
35: ; preds = %28
%36 = load i8*, i8** %5
store i8* %36, i8** %6
br label %37
37: ; preds = %35, %32
%38 = load i8*, i8** %6
%39 = load i32, i32* %4
%40 = load i8*, i8** %6
%41 = call i32 @strlen(i8* %40)
%42 = call i32 @memset(i8* %38, i32 %39, i32 %41)
br label %43
43: ; preds = %37, %2
%44 = load i8*, i8** %5
ret i8* %44
}

LLVM IR (-O2):
define i8* @newname(i8* %0, i32 %1) {
%3 = icmp ne i8* %0, null
%4 = zext i1 %3 to i32
%5 = tail call i32 @assert(i32 %4)
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp sgt i32 %6, 0
%8 = zext i1 %7 to i32
%9 = tail call i32 @assert(i32 %8)
%10 = icmp ne i32 %1, 0
%11 = zext i1 %10 to i32
%12 = tail call i32 @assert(i32 %11)
%13 = icmp slt i32 %1, 256
%14 = zext i1 %13 to i32
%15 = tail call i32 @assert(i32 %14)
%16 = tail call i8* @strdup(i8* %0)
%17 = icmp eq i8* %16, null
br i1 %17, label %25, label %18
18: ; preds = %2
%19 = tail call i8* @strrchr(i8* %16, i8 signext 47)
%20 = icmp eq i8* %19, null
%21 = getelementptr inbounds i8, i8* %19, i64 1
%22 = select i1 %20, i8* %16, i8* %21
%23 = tail call i32 @strlen(i8* %22)
%24 = tail call i32 @memset(i8* %22, i32 %1, i32 %23)
br label %25
25: ; preds = %18, %2
ret i8* %16
}