#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 */
char *realloc(char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char, char *);
char *strdup(char *);
int strlen(char *);
char *build_names(char *argv[]) {
  char *names = NULL;
  while (argv && *argv) {
    if (!names)
      names = strdup(*argv);
    else {
      names = realloc(names, strlen(names) + strlen(*argv) + 1 + 1);
      sprintf(names + strlen(names), "%c%s", ',', *argv);
    }
    argv++;
  }
  return names;
}


x86 assembly (clang, -O0):
<build_names>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x8(%rbp)
mov    %al,-0x11(%rbp)
je     1172 <build_names+0x32>
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
setne  %al
mov    %al,-0x11(%rbp)
mov    -0x11(%rbp),%al
test   $0x1,%al
jne    1182 <build_names+0x42>
jmp    121d <build_names+0xdd>
cmpq   $0x0,-0x10(%rbp)
jne    11a2 <build_names+0x62>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x10(%rbp)
jmp    120c <build_names+0xcc>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1030 <strlen@plt>
mov    -0x2c(%rbp),%esi
mov    -0x28(%rbp),%rdi
add    %eax,%esi
add    $0x1,%esi
add    $0x1,%esi
call   1040 <realloc@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x20(%rbp),%rdi
cltq
add    %rax,%rdi
mov    -0x8(%rbp),%rax
mov    (%rax),%rcx
lea    0xdfe(%rip),%rsi
mov    $0x2c,%edx
call   1050 <sprintf@plt>
mov    -0x8(%rbp),%rax
add    $0x8,%rax
mov    %rax,-0x8(%rbp)
jmp    1154 <build_names+0x14>
mov    -0x10(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_names>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
test   %rdi,%rdi
je     11cf <build_names+0x8f>
mov    %rdi,%rbx
mov    (%rdi),%rdi
xor    %r14d,%r14d
test   %rdi,%rdi
je     11d2 <build_names+0x92>
lea    0xe9a(%rip),%r15
jmp    11c0 <build_names+0x80>
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
call   1030 <strlen@plt>
mov    %eax,%r12d
mov    (%rbx),%rdi
call   1030 <strlen@plt>
lea    (%r12,%rax,1),%esi
add    $0x2,%esi
mov    %r14,%rdi
call   1040 <realloc@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <strlen@plt>
movslq %eax,%rdi
add    %r14,%rdi
mov    (%rbx),%rcx
mov    %r15,%rsi
mov    $0x2c,%edx
call   1050 <sprintf@plt>
mov    0x8(%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
je     11d2 <build_names+0x92>
test   %r14,%r14
jne    1170 <build_names+0x30>
call   1060 <strdup@plt>
mov    %rax,%r14
jmp    11b3 <build_names+0x73>
xor    %r14d,%r14d
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%c%s\00"
define i8* @build_names(i8** %0) {
%2 = alloca i8**
%3 = alloca i8*
store i8** %0, i8*** %2
store i8* null, i8** %3
br label %4
4: ; preds = %39, %1
%5 = load i8**, i8*** %2
%6 = icmp ne i8** %5, null
br i1 %6, label %7, label %11
7: ; preds = %4
%8 = load i8**, i8*** %2
%9 = load i8*, i8** %8
%10 = icmp ne i8* %9, null
br label %11
11: ; preds = %7, %4
%12 = phi i1 [ false, %4 ], [ %10, %7 ]
br i1 %12, label %13, label %42
13: ; preds = %11
%14 = load i8*, i8** %3
%15 = icmp ne i8* %14, null
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i8**, i8*** %2
%18 = load i8*, i8** %17
%19 = call i8* @strdup(i8* %18)
store i8* %19, i8** %3
br label %39
20: ; preds = %13
%21 = load i8*, i8** %3
%22 = load i8*, i8** %3
%23 = call i32 @strlen(i8* %22)
%24 = load i8**, i8*** %2
%25 = load i8*, i8** %24
%26 = call i32 @strlen(i8* %25)
%27 = add nsw i32 %23, %26
%28 = add nsw i32 %27, 1
%29 = add nsw i32 %28, 1
%30 = call i8* @realloc(i8* %21, i32 %29)
store i8* %30, i8** %3
%31 = load i8*, i8** %3
%32 = load i8*, i8** %3
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
%36 = load i8**, i8*** %2
%37 = load i8*, i8** %36
%38 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext 44, i8* %37)
br label %39
39: ; preds = %20, %16
%40 = load i8**, i8*** %2
%41 = getelementptr inbounds i8*, i8** %40, i32 1
store i8** %41, i8*** %2
br label %4
42: ; preds = %11
%43 = load i8*, i8** %3
ret i8* %43
}

LLVM IR (-O2):
@.str = [5 x i8] c"%c%s\00"
define i8* @build_names(i8** %0) {
%2 = icmp eq i8** %0, null
br i1 %2, label %30, label %3
3: ; preds = %1
%4 = load i8*, i8** %0
%5 = icmp eq i8* %4, null
br i1 %5, label %30, label %6
6: ; preds = %3, %25
%7 = phi i8* [ %28, %25 ], [ %4, %3 ]
%8 = phi i8** [ %27, %25 ], [ %0, %3 ]
%9 = phi i8* [ %26, %25 ], [ null, %3 ]
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %13
11: ; preds = %6
%12 = tail call i8* @strdup(i8* %7)
br label %25
13: ; preds = %6
%14 = tail call i32 @strlen(i8* %9)
%15 = load i8*, i8** %8
%16 = tail call i32 @strlen(i8* %15)
%17 = add i32 %14, 2
%18 = add i32 %17, %16
%19 = tail call i8* @realloc(i8* %9, i32 %18)
%20 = tail call i32 @strlen(i8* %19)
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %19, i64 %21
%23 = load i8*, i8** %8
%24 = tail call i32 @sprintf(i8* dereferenceable(1) %22, i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext 44, i8* %23)
br label %25
25: ; preds = %13, %11
%26 = phi i8* [ %19, %13 ], [ %12, %11 ]
%27 = getelementptr inbounds i8*, i8** %8, i64 1
%28 = load i8*, i8** %27
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %6
30: ; preds = %25, %3, %1
%31 = phi i8* [ null, %1 ], [ null, %3 ], [ %26, %25 ]
ret i8* %31
}