#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__ isalnum(char);
char *malloc(size_t);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char *, size_t);
char *id_from_path(const char *path) {
  size_t len = strlen(path);
  size_t prelen = strlen("file___");
  char *result = malloc(len + prelen + 1);
  strncpy(result, "file___", prelen);
  for (int i = 0; i < len; i++) {
    char c = path[i];
    result[i + prelen] = isalnum(c) ? c : '_';
  }
  result[len + prelen] = '\0';
  return result;
}


x86 assembly (clang, -O0):
<id_from_path>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x10(%rbp)
movq   $0x7,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
add    -0x18(%rbp),%rdi
add    $0x1,%rdi
call   1060 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x18(%rbp),%rdx
lea    0xe7b(%rip),%rsi
call   1030 <strncpy@plt>
movl   $0x0,-0x24(%rbp)
movslq -0x24(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11fb <id_from_path+0xbb>
mov    -0x8(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x25(%rbp)
movsbl -0x25(%rbp),%edi
call   1050 <isalnum@plt>
cmp    $0x0,%rax
je     11cc <id_from_path+0x8c>
movsbl -0x25(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11d9 <id_from_path+0x99>
mov    $0x5f,%eax
mov    %eax,-0x2c(%rbp)
jmp    11d9 <id_from_path+0x99>
mov    -0x2c(%rbp),%eax
mov    %al,%dl
mov    -0x20(%rbp),%rax
movslq -0x24(%rbp),%rcx
add    -0x18(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    1191 <id_from_path+0x51>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
add    -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<id_from_path>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
call   1040 <strlen@plt>
mov    %rax,%r15
lea    0x8(%rax),%rdi
call   1060 <malloc@plt>
mov    %rax,%r12
lea    0xe97(%rip),%rsi
mov    $0x7,%edx
mov    %rax,%rdi
call   1030 <strncpy@plt>
test   %r15,%r15
je     11b1 <id_from_path+0x71>
xor    %ebx,%ebx
mov    $0x5f,%r13d
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movsbl (%r14,%rbx,1),%ebp
mov    %ebp,%edi
call   1050 <isalnum@plt>
test   %rax,%rax
cmove  %r13d,%ebp
mov    %bpl,0x7(%r12,%rbx,1)
add    $0x1,%rbx
cmp    %rbx,%r15
jne    1190 <id_from_path+0x50>
movb   $0x0,0x7(%r15,%r12,1)
mov    %r12,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"file___\00"
define i8* @id_from_path(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call i64 @strlen(i8* %8)
store i64 %9, i64* %3
store i64 7, i64* %4
%10 = load i64, i64* %3
%11 = load i64, i64* %4
%12 = add i64 %10, %11
%13 = add i64 %12, 1
%14 = call i8* @malloc(i64 %13)
store i8* %14, i8** %5
%15 = load i8*, i8** %5
%16 = load i64, i64* %4
%17 = call i32 @strncpy(i8* %15, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 %16)
store i32 0, i32* %6
br label %18
18: ; preds = %45, %1
%19 = load i32, i32* %6
%20 = sext i32 %19 to i64
%21 = load i64, i64* %3
%22 = icmp ult i64 %20, %21
br i1 %22, label %23, label %48
23: ; preds = %18
%24 = load i8*, i8** %2
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i8, i8* %27
store i8 %28, i8* %7
%29 = load i8, i8* %7
%30 = call i64 @isalnum(i8 signext %29)
%31 = icmp ne i64 %30, 0
br i1 %31, label %32, label %35
32: ; preds = %23
%33 = load i8, i8* %7
%34 = sext i8 %33 to i32
br label %36
35: ; preds = %23
br label %36
36: ; preds = %35, %32
%37 = phi i32 [ %34, %32 ], [ 95, %35 ]
%38 = trunc i32 %37 to i8
%39 = load i8*, i8** %5
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = load i64, i64* %4
%43 = add i64 %41, %42
%44 = getelementptr inbounds i8, i8* %39, i64 %43
store i8 %38, i8* %44
br label %45
45: ; preds = %36
%46 = load i32, i32* %6
%47 = add nsw i32 %46, 1
store i32 %47, i32* %6
br label %18
48: ; preds = %18
%49 = load i8*, i8** %5
%50 = load i64, i64* %3
%51 = load i64, i64* %4
%52 = add i64 %50, %51
%53 = getelementptr inbounds i8, i8* %49, i64 %52
store i8 0, i8* %53
%54 = load i8*, i8** %5
ret i8* %54
}

LLVM IR (-O2):
@.str = [8 x i8] c"file___\00"
define i8* @id_from_path(i8* nocapture %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add i64 %2, 8
%4 = tail call i8* @malloc(i64 %3)
%5 = tail call i32 @strncpy(i8* %4, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i64 7)
%6 = icmp eq i64 %2, 0
br i1 %6, label %7, label %10
7: ; preds = %10, %1
%8 = add i64 %2, 7
%9 = getelementptr inbounds i8, i8* %4, i64 %8
store i8 0, i8* %9
ret i8* %4
10: ; preds = %1, %10
%11 = phi i64 [ %19, %10 ], [ 0, %1 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = tail call i64 @isalnum(i8 signext %13)
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i8 95, i8 %13
%17 = add nuw nsw i64 %11, 7
%18 = getelementptr inbounds i8, i8* %4, i64 %17
store i8 %16, i8* %18
%19 = add nuw i64 %11, 1
%20 = icmp eq i64 %19, %2
br i1 %20, label %7, label %10
}