#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*/ exit(int);
int /*<<< orphan*/ free(char *);
char *malloc(int /*<<< orphan*/);
int /*<<< orphan*/ puts(char *);
int snprintf(char *, int, char *, int, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
void nameformat(char *name, char *QS) {
  char *bungle, *x;
  char elem[128];
  *QS = 0;
  bungle = malloc(strlen(name) + 3);
  strcpy(bungle, name);
  x = strtok(bungle, ".");
  while (x != NULL) {
    if (snprintf(elem, 128, "%c%s", strlen(x), x) == 128) {
      puts("String overflow.");
      exit(1);
    }
    strcat(QS, elem);
    x = strtok(NULL, ".");
  }
  free(bungle);
}


x86 assembly (clang, -O0):
<nameformat>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%edi
add    $0x3,%edi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rdi
lea    0xe3b(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     126c <nameformat+0xdc>
lea    -0xa0(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    -0x20(%rbp),%rdi
call   1060 <strlen@plt>
mov    -0xa8(%rbp),%rdi
mov    %eax,%ecx
mov    -0x20(%rbp),%r8
mov    $0x80,%esi
lea    0xde3(%rip),%rdx
call   1070 <snprintf@plt>
cmp    $0x80,%eax
jne    1243 <nameformat+0xb3>
lea    0xdd1(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>
mov    -0x10(%rbp),%rdi
lea    -0xa0(%rbp),%rsi
call   10a0 <strcat@plt>
xor    %eax,%eax
mov    %eax,%edi
lea    0xdb6(%rip),%rsi
call   1090 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    11e2 <nameformat+0x52>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<nameformat>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x88,%rsp
mov    %rsi,%r12
mov    %rdi,%rbx
movb   $0x0,(%rsi)
call   1060 <strlen@plt>
lea    0x3(%rax),%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
lea    0xe48(%rip),%rsi
mov    %r14,%rdi
call   1090 <strtok@plt>
test   %rax,%rax
je     1231 <nameformat+0xa1>
mov    %rax,%rbp
lea    0xe1d(%rip),%r15
mov    %rsp,%rbx
lea    0xe27(%rip),%r13
nopl   (%rax)
mov    %rbp,%rdi
call   1060 <strlen@plt>
mov    %rbx,%rdi
mov    $0x80,%esi
mov    %r15,%rdx
mov    %eax,%ecx
mov    %rbp,%r8
call   1070 <snprintf@plt>
cmp    $0x80,%eax
je     124b <nameformat+0xbb>
mov    %r12,%rdi
mov    %rbx,%rsi
call   10a0 <strcat@plt>
xor    %edi,%edi
mov    %r13,%rsi
call   1090 <strtok@plt>
mov    %rax,%rbp
test   %rax,%rax
jne    11f0 <nameformat+0x60>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x88,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdb3(%rip),%rdi
call   1050 <puts@plt>
mov    $0x1,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
@.str = [2 x i8] c".\00"
@.str.1 = [5 x i8] c"%c%s\00"
@.str.2 = [17 x i8] c"String overflow.\00"
define void @nameformat(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca [128 x i8]
store i8* %0, i8** %3
store i8* %1, i8** %4
%8 = load i8*, i8** %4
store i8 0, i8* %8
%9 = load i8*, i8** %3
%10 = call i32 @strlen(i8* %9)
%11 = add nsw i32 %10, 3
%12 = call i8* @malloc(i32 %11)
store i8* %12, i8** %5
%13 = load i8*, i8** %5
%14 = load i8*, i8** %3
%15 = call i32 @strcpy(i8* %13, i8* %14)
%16 = load i8*, i8** %5
%17 = call i8* @strtok(i8* %16, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %17, i8** %6
br label %18
18: ; preds = %31, %2
%19 = load i8*, i8** %6
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %36
21: ; preds = %18
%22 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%23 = load i8*, i8** %6
%24 = call i32 @strlen(i8* %23)
%25 = load i8*, i8** %6
%26 = call i32 @snprintf(i8* %22, i32 128, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %24, i8* %25)
%27 = icmp eq i32 %26, 128
br i1 %27, label %28, label %31
28: ; preds = %21
%29 = call i32 @puts(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %21
%32 = load i8*, i8** %4
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%34 = call i32 @strcat(i8* %32, i8* %33)
%35 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i8* %35, i8** %6
br label %18
36: ; preds = %18
%37 = load i8*, i8** %5
%38 = call i32 @free(i8* %37)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c".\00"
@.str.1 = [5 x i8] c"%c%s\00"
@.str.2 = [17 x i8] c"String overflow.\00"
define void @nameformat(i8* %0, i8* %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
store i8 0, i8* %1
%5 = tail call i32 @strlen(i8* %0)
%6 = add nsw i32 %5, 3
%7 = tail call i8* @malloc(i32 %6)
%8 = tail call i32 @strcpy(i8* %7, i8* %0)
%9 = tail call i8* @strtok(i8* %7, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i8* %9, null
br i1 %10, label %23, label %11
11: ; preds = %2, %19
%12 = phi i8* [ %21, %19 ], [ %9, %2 ]
%13 = call i32 @strlen(i8* %12)
%14 = call i32 @snprintf(i8* %4, i32 128, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i32 %13, i8* %12)
%15 = icmp eq i32 %14, 128
br i1 %15, label %16, label %19
16: ; preds = %11
%17 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
%18 = call i32 @exit(i32 1)
unreachable
19: ; preds = %11
%20 = call i32 @strcat(i8* %1, i8* %4)
%21 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %11
23: ; preds = %19, %2
%24 = call i32 @free(i8* %7)
ret void
}