#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*/ sprintf(char *, char *, char const *);
int /*<<< orphan*/ strcpy(char *, char *);
int strcspn(char const *, char *);
int strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
void luaO_chunkid(char *out, const char *source, int bufflen) {
  if (*source == '=') {
    strncpy(out, source + 1, bufflen);
    out[bufflen - 1] = '\0';
  } else {
    if (*source == '@') {
      int l;
      source++;
      bufflen -= sizeof("file `...%s'");
      l = strlen(source);
      if (l > bufflen) {
        source += (l - bufflen);
        sprintf(out, "file `...%.99s'", source);
      } else
        sprintf(out, "file `%.99s'", source);
    } else {
      int len = strcspn(source, "\n");
      bufflen -= sizeof("string \"%.*s...\"");
      if (len > bufflen)
        len = bufflen;
      if (source[len] != '\0') {
        strcpy(out, "string \"");
        out += strlen(out);
        strncpy(out, source, len);
        strcpy(out + len, "...\"");
      } else
        sprintf(out, "string \"%.99s\"", source);
    }
  }
}


x86 assembly (clang, -O0):
<luaO_chunkid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x3d,%eax
jne    119d <luaO_chunkid+0x4d>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x1,%rsi
mov    -0x14(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rax
mov    -0x14(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12da <luaO_chunkid+0x18a>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x40,%eax
jne    1222 <luaO_chunkid+0xd2>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
movslq -0x14(%rbp),%rax
sub    $0xd,%rax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1209 <luaO_chunkid+0xb9>
mov    -0x18(%rbp),%ecx
sub    -0x14(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xe01(%rip),%rsi
call   1070 <sprintf@plt>
jmp    121d <luaO_chunkid+0xcd>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xdf8(%rip),%rsi
call   1070 <sprintf@plt>
jmp    12d5 <luaO_chunkid+0x185>
mov    -0x10(%rbp),%rdi
lea    0xdf0(%rip),%rsi
call   1060 <strcspn@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x14(%rbp),%rax
sub    $0x11,%rax
mov    %eax,-0x14(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jle    1252 <luaO_chunkid+0x102>
mov    -0x14(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x0,%eax
je     12bc <luaO_chunkid+0x16c>
mov    -0x8(%rbp),%rdi
lea    0xdad(%rip),%rsi
call   1040 <strcpy@plt>
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x1c(%rbp),%edx
call   1030 <strncpy@plt>
mov    -0x8(%rbp),%rdi
movslq -0x1c(%rbp),%rax
add    %rax,%rdi
lea    0xd76(%rip),%rsi
call   1040 <strcpy@plt>
jmp    12d0 <luaO_chunkid+0x180>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    0xd62(%rip),%rsi
call   1070 <sprintf@plt>
jmp    12d5 <luaO_chunkid+0x185>
jmp    12da <luaO_chunkid+0x18a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<luaO_chunkid>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
mov    (%rsi),%al
cmp    $0x40,%al
je     118e <luaO_chunkid+0x3e>
cmp    $0x3d,%al
jne    11af <luaO_chunkid+0x5f>
add    $0x1,%rbx
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1030 <strncpy@plt>
movslq %ebp,%rax
movb   $0x0,-0x1(%rax,%r14,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
add    $0x1,%rbx
add    $0xfffffff3,%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
sub    %ebp,%eax
jle    1217 <luaO_chunkid+0xc7>
cltq
add    %rax,%rbx
lea    0xe53(%rip),%rsi
jmp    1227 <luaO_chunkid+0xd7>
lea    0xe67(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcspn@plt>
add    $0xffffffef,%ebp
cmp    %ebp,%eax
cmovle %eax,%ebp
movslq %ebp,%r15
cmpb   $0x0,(%rbx,%r15,1)
je     1220 <luaO_chunkid+0xd0>
lea    0xe48(%rip),%rsi
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
movslq %eax,%rbp
add    %r14,%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1030 <strncpy@plt>
add    %r15,%rbp
lea    0xe23(%rip),%rsi
mov    %rbp,%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <strcpy@plt>
lea    0xdf2(%rip),%rsi
jmp    1227 <luaO_chunkid+0xd7>
lea    0xe06(%rip),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1070 <sprintf@plt>

LLVM IR (-O0):
@.str = [16 x i8] c"file `...%.99s'\00"
@.str.1 = [13 x i8] c"file `%.99s'\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [9 x i8] c"string \22\00"
@.str.4 = [5 x i8] c"...\22\00"
@.str.5 = [15 x i8] c"string \22%.99s\22\00"
define void @luaO_chunkid(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = load i8, i8* %9
%11 = sext i8 %10 to i32
%12 = icmp eq i32 %11, 61
br i1 %12, label %13, label %24
13: ; preds = %3
%14 = load i8*, i8** %4
%15 = load i8*, i8** %5
%16 = getelementptr inbounds i8, i8* %15, i64 1
%17 = load i32, i32* %6
%18 = call i32 @strncpy(i8* %14, i8* %16, i32 %17)
%19 = load i8*, i8** %4
%20 = load i32, i32* %6
%21 = sub nsw i32 %20, 1
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8 0, i8* %23
br label %99
24: ; preds = %3
%25 = load i8*, i8** %5
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 64
br i1 %28, label %29, label %56
29: ; preds = %24
%30 = load i8*, i8** %5
%31 = getelementptr inbounds i8, i8* %30, i32 1
store i8* %31, i8** %5
%32 = load i32, i32* %6
%33 = sext i32 %32 to i64
%34 = sub i64 %33, 13
%35 = trunc i64 %34 to i32
store i32 %35, i32* %6
%36 = load i8*, i8** %5
%37 = call i32 @strlen(i8* %36)
store i32 %37, i32* %7
%38 = load i32, i32* %7
%39 = load i32, i32* %6
%40 = icmp sgt i32 %38, %39
br i1 %40, label %41, label %51
41: ; preds = %29
%42 = load i32, i32* %7
%43 = load i32, i32* %6
%44 = sub nsw i32 %42, %43
%45 = load i8*, i8** %5
%46 = sext i32 %44 to i64
%47 = getelementptr inbounds i8, i8* %45, i64 %46
store i8* %47, i8** %5
%48 = load i8*, i8** %4
%49 = load i8*, i8** %5
%50 = call i32 @sprintf(i8* %48, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %49)
br label %55
51: ; preds = %29
%52 = load i8*, i8** %4
%53 = load i8*, i8** %5
%54 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %51, %41
br label %98
56: ; preds = %24
%57 = load i8*, i8** %5
%58 = call i32 @strcspn(i8* %57, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 %58, i32* %8
%59 = load i32, i32* %6
%60 = sext i32 %59 to i64
%61 = sub i64 %60, 17
%62 = trunc i64 %61 to i32
store i32 %62, i32* %6
%63 = load i32, i32* %8
%64 = load i32, i32* %6
%65 = icmp sgt i32 %63, %64
br i1 %65, label %66, label %68
66: ; preds = %56
%67 = load i32, i32* %6
store i32 %67, i32* %8
br label %68
68: ; preds = %66, %56
%69 = load i8*, i8** %5
%70 = load i32, i32* %8
%71 = sext i32 %70 to i64
%72 = getelementptr inbounds i8, i8* %69, i64 %71
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = icmp ne i32 %74, 0
br i1 %75, label %76, label %93
76: ; preds = %68
%77 = load i8*, i8** %4
%78 = call i32 @strcpy(i8* %77, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%79 = load i8*, i8** %4
%80 = call i32 @strlen(i8* %79)
%81 = load i8*, i8** %4
%82 = sext i32 %80 to i64
%83 = getelementptr inbounds i8, i8* %81, i64 %82
store i8* %83, i8** %4
%84 = load i8*, i8** %4
%85 = load i8*, i8** %5
%86 = load i32, i32* %8
%87 = call i32 @strncpy(i8* %84, i8* %85, i32 %86)
%88 = load i8*, i8** %4
%89 = load i32, i32* %8
%90 = sext i32 %89 to i64
%91 = getelementptr inbounds i8, i8* %88, i64 %90
%92 = call i32 @strcpy(i8* %91, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %97
93: ; preds = %68
%94 = load i8*, i8** %4
%95 = load i8*, i8** %5
%96 = call i32 @sprintf(i8* %94, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %95)
br label %97
97: ; preds = %93, %76
br label %98
98: ; preds = %97, %55
br label %99
99: ; preds = %98, %13
ret void
}

LLVM IR (-O2):
@.str = [16 x i8] c"file `...%.99s'\00"
@.str.1 = [13 x i8] c"file `%.99s'\00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [9 x i8] c"string \22\00"
@.str.4 = [5 x i8] c"...\22\00"
@.str.5 = [15 x i8] c"string \22%.99s\22\00"
define void @luaO_chunkid(i8* %0, i8* %1, i32 %2) {
%4 = load i8, i8* %1
switch i8 %4, label %23 [
i8 61, label %5
i8 64, label %11
]
5: ; preds = %3
%6 = getelementptr inbounds i8, i8* %1, i64 1
%7 = tail call i32 @strncpy(i8* %0, i8* %6, i32 %2)
%8 = add nsw i32 %2, -1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8, i8* %0, i64 %9
store i8 0, i8* %10
br label %42
11: ; preds = %3
%12 = getelementptr inbounds i8, i8* %1, i64 1
%13 = add i32 %2, -13
%14 = tail call i32 @strlen(i8* %12)
%15 = icmp sgt i32 %14, %13
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = sub nsw i32 %14, %13
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %12, i64 %18
%20 = tail call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* %19)
br label %42
21: ; preds = %11
%22 = tail call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i8* %12)
br label %42
23: ; preds = %3
%24 = tail call i32 @strcspn(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%25 = add i32 %2, -17
%26 = icmp sgt i32 %24, %25
%27 = select i1 %26, i32 %25, i32 %24
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %1, i64 %28
%30 = load i8, i8* %29
%31 = icmp eq i8 %30, 0
br i1 %31, label %40, label %32
32: ; preds = %23
%33 = tail call i32 @strcpy(i8* %0, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
%34 = tail call i32 @strlen(i8* %0)
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds i8, i8* %0, i64 %35
%37 = tail call i32 @strncpy(i8* %36, i8* %1, i32 %27)
%38 = getelementptr inbounds i8, i8* %36, i64 %28
%39 = tail call i32 @strcpy(i8* %38, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.4, i64 0, i64 0))
br label %42
40: ; preds = %23
%41 = tail call i32 @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.5, i64 0, i64 0), i8* %1)
br label %42
42: ; preds = %32, %40, %16, %21, %5
ret void
}