#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 *malloc(size_t);
int /*<<< orphan*/ perror(char *);
size_t strlen(char const *);
int /*<<< orphan*/ strncpy(char *, char const *, size_t);
char *build_template(const char *directory, const char *prefix,
                     const char *suffix) {
  const char pattern[] = "XXXXXX";
  size_t len, directory_len, pathsep_len, prefix_len, suffix_len;
  char *template;
  directory_len = strlen(directory);
  if (directory_len > 0) {
    pathsep_len = 1;
    if (directory[directory_len - 1] == '/') {
      --directory_len;
    }
  } else {
    pathsep_len = 0;
  }
  prefix_len = strlen(prefix);
  suffix_len = strlen(suffix);
  len = directory_len + pathsep_len + prefix_len + sizeof pattern + suffix_len;
  if ((template = malloc(len))) {
    char *tp = template;
    (void)strncpy(tp, directory, directory_len);
    tp += directory_len;
    if (pathsep_len == 1) {
      *tp++ = '/';
    }
    (void)strncpy(tp, prefix, prefix_len);
    tp += prefix_len;
    (void)strncpy(tp, pattern, sizeof pattern - 1);
    tp += sizeof pattern - 1;
    (void)strncpy(tp, suffix, suffix_len);
    template[len - 1] = '\0';
    return template;
  }
  perror("malloc");
  return NULL;
}


x86 assembly (clang, -O0):
<build_template>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    0xea6(%rip),%eax
mov    %eax,-0x27(%rbp)
mov    0xea0(%rip),%ax
mov    %ax,-0x23(%rbp)
mov    0xe98(%rip),%al
mov    %al,-0x21(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x38(%rbp)
cmpq   $0x0,-0x38(%rbp)
jbe    11bb <build_template+0x7b>
movq   $0x1,-0x40(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0x2f,%eax
jne    11b6 <build_template+0x76>
mov    -0x38(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x38(%rbp)
jmp    11c3 <build_template+0x83>
movq   $0x0,-0x40(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x48(%rbp)
mov    -0x20(%rbp),%rdi
call   1040 <strlen@plt>
mov    %rax,-0x50(%rbp)
mov    -0x38(%rbp),%rax
add    -0x40(%rbp),%rax
add    -0x48(%rbp),%rax
add    $0x7,%rax
add    -0x50(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x58(%rbp)
cmp    $0x0,%rax
je     12b7 <build_template+0x177>
mov    -0x58(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x38(%rbp),%rax
add    -0x60(%rbp),%rax
mov    %rax,-0x60(%rbp)
cmpq   $0x1,-0x40(%rbp)
jne    124e <build_template+0x10e>
mov    -0x60(%rbp),%rax
mov    %rax,%rcx
add    $0x1,%rcx
mov    %rcx,-0x60(%rbp)
movb   $0x2f,(%rax)
mov    -0x60(%rbp),%rdi
mov    -0x18(%rbp),%rsi
mov    -0x48(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x48(%rbp),%rax
add    -0x60(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
lea    -0x27(%rbp),%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
mov    -0x60(%rbp),%rax
add    $0x6,%rax
mov    %rax,-0x60(%rbp)
mov    -0x60(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x50(%rbp),%rdx
call   1030 <strncpy@plt>
mov    -0x58(%rbp),%rax
mov    -0x30(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x58(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12cb <build_template+0x18b>
lea    0xd49(%rip),%rdi
call   1060 <perror@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_template>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdx,%r13
mov    %rsi,%rbp
mov    %rdi,%rbx
movl   $0x585858,0x3(%rsp)
movl   $0x58585858,(%rsp)
call   1040 <strlen@plt>
mov    %rax,%r14
test   %rax,%rax
mov    %rbx,0x10(%rsp)
je     118d <build_template+0x4d>
cmpb   $0x2f,-0x1(%rbx,%r14,1)
lea    -0x1(%r14),%r15
cmovne %r14,%r15
mov    $0x8,%ebx
jmp    1195 <build_template+0x55>
mov    $0x7,%ebx
xor    %r15d,%r15d
mov    %rbp,0x18(%rsp)
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %rax,%r12
mov    %r13,0x20(%rsp)
mov    %r13,%rdi
call   1040 <strlen@plt>
mov    %rax,%rbp
add    %r15,%rbx
add    %r12,%rbx
add    %rax,%rbx
mov    %rbx,%rdi
call   1050 <malloc@plt>
mov    %rax,%r13
test   %rax,%rax
je     1246 <build_template+0x106>
mov    %rbp,0x8(%rsp)
mov    %r13,%rdi
mov    0x10(%rsp),%rsi
mov    %r15,%rdx
call   1030 <strncpy@plt>
mov    %r13,%rbp
add    %r15,%rbp
test   %r14,%r14
je     11f6 <build_template+0xb6>
movb   $0x2f,0x0(%rbp)
add    $0x1,%rbp
mov    %rbp,%rdi
mov    0x18(%rsp),%rsi
mov    %r12,%rdx
call   1030 <strncpy@plt>
lea    (%r12,%rbp,1),%rdi
mov    %rsp,%rsi
mov    $0x6,%edx
call   1030 <strncpy@plt>
lea    (%r12,%rbp,1),%rdi
add    $0x6,%rdi
mov    0x20(%rsp),%rsi
mov    0x8(%rsp),%rdx
call   1030 <strncpy@plt>
movb   $0x0,-0x1(%rbx,%r13,1)
mov    %r13,%rax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdba(%rip),%rdi
call   1060 <perror@plt>
jmp    1234 <build_template+0xf4>

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

LLVM IR (-O2):
@__const.build_template.pattern = [7 x i8] c"XXXXXX\00"
@.str = [7 x i8] c"malloc\00"
define i8* @build_template(i8* %0, i8* %1, i8* %2) {
%4 = alloca [7 x i8]
%5 = getelementptr inbounds [7 x i8], [7 x i8]* %4, i64 0, i64 0
%6 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%7 = icmp eq i64 %6, 0
br i1 %7, label %14, label %8
8: ; preds = %3
%9 = add i64 %6, -1
%10 = getelementptr inbounds i8, i8* %0, i64 %9
%11 = load i8, i8* %10
%12 = icmp eq i8 %11, 47
%13 = select i1 %12, i64 %9, i64 %6
br label %14
14: ; preds = %8, %3
%15 = phi i64 [ 8, %8 ], [ 7, %3 ]
%16 = phi i64 [ %13, %8 ], [ 0, %3 ]
%17 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%18 = tail call i64 @strlen(i8* dereferenceable(1) %2)
%19 = add i64 %15, %16
%20 = add i64 %19, %17
%21 = add i64 %20, %18
%22 = tail call i8* @malloc(i64 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %38, label %24
24: ; preds = %14
%25 = tail call i32 @strncpy(i8* %22, i8* %0, i64 %16)
%26 = getelementptr inbounds i8, i8* %22, i64 %16
br i1 %7, label %29, label %27
27: ; preds = %24
%28 = getelementptr inbounds i8, i8* %26, i64 1
store i8 47, i8* %26
br label %29
29: ; preds = %27, %24
%30 = phi i8* [ %28, %27 ], [ %26, %24 ]
%31 = tail call i32 @strncpy(i8* %30, i8* %1, i64 %17)
%32 = getelementptr inbounds i8, i8* %30, i64 %17
%33 = call i32 @strncpy(i8* %32, i8* %5, i64 6)
%34 = getelementptr inbounds i8, i8* %32, i64 6
%35 = call i32 @strncpy(i8* %34, i8* %2, i64 %18)
%36 = add i64 %21, -1
%37 = getelementptr inbounds i8, i8* %22, i64 %36
store i8 0, i8* %37
br label %40
38: ; preds = %14
%39 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %40
40: ; preds = %38, %29
ret i8* %22
}