void luaL_chunkid(char *out, char *source, int len) {
  len -= 13;
  if (*source == '@')
    sprintf(out, "file `%.*s'", len, source + 1);
  else if (*source == '(')
    strcpy(out, "(C code)");
  else {
    char *b = strchr(source, '\n');
    int lim = (b && (b - source) < len) ? b - source : len;
    sprintf(out, "string `%.*s'", lim, source);
    strcpy(out + lim + (13 - 5), "...'");
  }
}


x86 assembly (clang, -O0):
<luaL_chunkid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x14(%rbp),%eax
sub    $0xd,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x40,%eax
jne    117e <luaL_chunkid+0x4e>
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%edx
mov    -0x10(%rbp),%rcx
add    $0x1,%rcx
lea    0xe8e(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
jmp    1234 <luaL_chunkid+0x104>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x28,%eax
jne    11a3 <luaL_chunkid+0x73>
mov    -0x8(%rbp),%rdi
lea    0xe73(%rip),%rsi
call   1030 <strcpy@plt>
jmp    122f <luaL_chunkid+0xff>
mov    -0x10(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11ec <luaL_chunkid+0xbc>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
movslq -0x14(%rbp),%rcx
cmp    %rcx,%rax
jge    11ec <luaL_chunkid+0xbc>
mov    -0x20(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
mov    %rax,-0x30(%rbp)
jmp    11f4 <luaL_chunkid+0xc4>
movslq -0x14(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rax
mov    %eax,-0x24(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x24(%rbp),%edx
mov    -0x10(%rbp),%rcx
lea    0xe08(%rip),%rsi
mov    $0x0,%al
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rdi
movslq -0x24(%rbp),%rax
add    %rax,%rdi
add    $0x8,%rdi
lea    0xdf9(%rip),%rsi
call   1030 <strcpy@plt>
jmp    1234 <luaL_chunkid+0x104>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<luaL_chunkid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    (%rsi),%al
cmp    $0x28,%al
je     1160 <luaL_chunkid+0x40>
mov    %edx,%ebp
mov    %rsi,%rbx
add    $0xfffffff3,%ebp
cmp    $0x40,%al
jne    1174 <luaL_chunkid+0x54>
add    $0x1,%rbx
lea    0xeb9(%rip),%rsi
mov    %r14,%rdi
mov    %ebp,%edx
mov    %rbx,%rcx
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <sprintf@plt>
movabs $0x2965646f63204328,%rax
64 65 29
mov    %rax,(%r14)
movb   $0x0,0x8(%r14)
jmp    11c4 <luaL_chunkid+0xa4>
mov    %rbx,%rdi
mov    $0xa,%esi
call   1030 <strchr@plt>
test   %rax,%rax
je     1196 <luaL_chunkid+0x76>
mov    %rax,%r15
sub    %rbx,%r15
movslq %ebp,%rax
cmp    %rax,%r15
jge    1198 <luaL_chunkid+0x78>
jmp    119b <luaL_chunkid+0x7b>
mov    %ebp,%eax
mov    %rax,%r15
lea    0xe73(%rip),%rsi
mov    %r14,%rdi
mov    %r15d,%edx
mov    %rbx,%rcx
xor    %eax,%eax
call   1040 <sprintf@plt>
movslq %r15d,%rax
movl   $0x272e2e2e,0x8(%r14,%rax,1)
2e 27
movb   $0x0,0xc(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"file `%.*s'\00"
@.str.1 = [9 x i8] c"(C code)\00"
@.str.2 = [14 x i8] c"string `%.*s'\00"
@.str.3 = [5 x i8] c"...'\00"
define void @luaL_chunkid(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i32, i32* %6
%10 = sub nsw i32 %9, 13
store i32 %10, i32* %6
%11 = load i8*, i8** %5
%12 = load i8, i8* %11
%13 = sext i8 %12 to i32
%14 = icmp eq i32 %13, 64
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = load i8*, i8** %4
%17 = load i32, i32* %6
%18 = load i8*, i8** %5
%19 = getelementptr inbounds i8, i8* %18, i64 1
%20 = call i32 (i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %17, i8* %19)
br label %66
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = load i8, i8* %22
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 40
br i1 %25, label %26, label %29
26: ; preds = %21
%27 = load i8*, i8** %4
%28 = call i8* @strcpy(i8* %27, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
br label %65
29: ; preds = %21
%30 = load i8*, i8** %5
%31 = call i8* @strchr(i8* %30, i32 10)
store i8* %31, i8** %7
%32 = load i8*, i8** %7
%33 = icmp ne i8* %32, null
br i1 %33, label %34, label %49
34: ; preds = %29
%35 = load i8*, i8** %7
%36 = load i8*, i8** %5
%37 = ptrtoint i8* %35 to i64
%38 = ptrtoint i8* %36 to i64
%39 = sub i64 %37, %38
%40 = load i32, i32* %6
%41 = sext i32 %40 to i64
%42 = icmp slt i64 %39, %41
br i1 %42, label %43, label %49
43: ; preds = %34
%44 = load i8*, i8** %7
%45 = load i8*, i8** %5
%46 = ptrtoint i8* %44 to i64
%47 = ptrtoint i8* %45 to i64
%48 = sub i64 %46, %47
br label %52
49: ; preds = %34, %29
%50 = load i32, i32* %6
%51 = sext i32 %50 to i64
br label %52
52: ; preds = %49, %43
%53 = phi i64 [ %48, %43 ], [ %51, %49 ]
%54 = trunc i64 %53 to i32
store i32 %54, i32* %8
%55 = load i8*, i8** %4
%56 = load i32, i32* %8
%57 = load i8*, i8** %5
%58 = call i32 (i8*, i8*, ...) @sprintf(i8* %55, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %56, i8* %57)
%59 = load i8*, i8** %4
%60 = load i32, i32* %8
%61 = sext i32 %60 to i64
%62 = getelementptr inbounds i8, i8* %59, i64 %61
%63 = getelementptr inbounds i8, i8* %62, i64 8
%64 = call i8* @strcpy(i8* %63, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %65
65: ; preds = %52, %26
br label %66
66: ; preds = %65, %15
ret void
}

LLVM IR (-O2):
@.str = [12 x i8] c"file `%.*s'\00"
@.str.1 = [9 x i8] c"(C code)\00"
@.str.2 = [14 x i8] c"string `%.*s'\00"
@.str.3 = [5 x i8] c"...'\00"
define void @luaL_chunkid(i8* nocapture writeonly %0, i8* %1, i32 %2) {
%4 = add nsw i32 %2, -13
%5 = load i8, i8* %1
switch i8 %5, label %10 [
i8 64, label %6
i8 40, label %9
]
6: ; preds = %3
%7 = getelementptr inbounds i8, i8* %1, i64 1
%8 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i32 %4, i8* %7)
br label %31
9: ; preds = %3
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(9) %0, i8* align 1 dereferenceable(9) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i64 9, i1 false)
br label %31
10: ; preds = %3
%11 = tail call i8* @strchr(i8* %1, i32 10)
%12 = icmp eq i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %10
%14 = zext i32 %4 to i64
br label %21
15: ; preds = %10
%16 = ptrtoint i8* %11 to i64
%17 = ptrtoint i8* %1 to i64
%18 = sub i64 %16, %17
%19 = sext i32 %4 to i64
%20 = icmp slt i64 %18, %19
br i1 %20, label %23, label %21
21: ; preds = %13, %15
%22 = phi i64 [ %14, %13 ], [ %19, %15 ]
br label %23
23: ; preds = %15, %21
%24 = phi i64 [ %22, %21 ], [ %18, %15 ]
%25 = trunc i64 %24 to i32
%26 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0), i32 %25, i8* %1)
%27 = shl i64 %24, 32
%28 = ashr exact i64 %27, 32
%29 = getelementptr inbounds i8, i8* %0, i64 %28
%30 = getelementptr inbounds i8, i8* %29, i64 8
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(5) %30, i8* align 1 dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0), i64 5, i1 false)
br label %31
31: ; preds = %9, %23, %6
ret void
}