#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 */
unsigned int optimal_merge_aux(int /*<<< orphan*/, int /*<<< orphan*/, char **,
                               int, size_t);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ swap(char **, int /*<<< orphan*/, size_t);
unsigned optimal_merge(char *strs[], size_t strs_sz) {
  unsigned res = -1;
  size_t i;
  for (i = 0; i < strs_sz; i++) {
    swap(strs, 0, i);
    unsigned res_tmp;
    res_tmp = optimal_merge_aux(0, strlen(strs[0]), strs, 1, strs_sz);
    if (res_tmp < res) {
      res = res_tmp;
    }
    swap(strs, 0, i);
  }
  return res;
}


x86 assembly (clang, -O0):
<optimal_merge>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11c3 <optimal_merge+0x93>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1050 <swap@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
call   1040 <strlen@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%rdx
mov    -0x10(%rbp),%r8
xor    %edi,%edi
mov    $0x1,%ecx
call   1030 <optimal_merge_aux@plt>
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jae    11a3 <optimal_merge+0x73>
mov    -0x24(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rdx
xor    %esi,%esi
call   1050 <swap@plt>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    114f <optimal_merge+0x1f>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<optimal_merge>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %rsi,%rsi
je     1196 <optimal_merge+0x66>
mov    %rsi,%r14
mov    %rdi,%r15
mov    $0xffffffff,%ebp
xor    %ebx,%ebx
nopl   0x0(%rax)
mov    %r15,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <swap@plt>
mov    (%r15),%rdi
call   1040 <strlen@plt>
xor    %edi,%edi
mov    %eax,%esi
mov    %r15,%rdx
mov    $0x1,%ecx
mov    %r14,%r8
call   1030 <optimal_merge_aux@plt>
cmp    %ebp,%eax
cmovb  %eax,%ebp
mov    %r15,%rdi
xor    %esi,%esi
mov    %rbx,%rdx
call   1050 <swap@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    1150 <optimal_merge+0x20>
jmp    119b <optimal_merge+0x6b>
mov    $0xffffffff,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @optimal_merge(i8** %0, i64 %1) {
%3 = alloca i8**
%4 = alloca i64
%5 = alloca i32
%6 = alloca i64
%7 = alloca i32
store i8** %0, i8*** %3
store i64 %1, i64* %4
store i32 -1, i32* %5
store i64 0, i64* %6
br label %8
8: ; preds = %32, %2
%9 = load i64, i64* %6
%10 = load i64, i64* %4
%11 = icmp ult i64 %9, %10
br i1 %11, label %12, label %35
12: ; preds = %8
%13 = load i8**, i8*** %3
%14 = load i64, i64* %6
%15 = call i32 @swap(i8** %13, i32 0, i64 %14)
%16 = load i8**, i8*** %3
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 @strlen(i8* %18)
%20 = load i8**, i8*** %3
%21 = load i64, i64* %4
%22 = call i32 @optimal_merge_aux(i32 0, i32 %19, i8** %20, i32 1, i64 %21)
store i32 %22, i32* %7
%23 = load i32, i32* %7
%24 = load i32, i32* %5
%25 = icmp ult i32 %23, %24
br i1 %25, label %26, label %28
26: ; preds = %12
%27 = load i32, i32* %7
store i32 %27, i32* %5
br label %28
28: ; preds = %26, %12
%29 = load i8**, i8*** %3
%30 = load i64, i64* %6
%31 = call i32 @swap(i8** %29, i32 0, i64 %30)
br label %32
32: ; preds = %28
%33 = load i64, i64* %6
%34 = add i64 %33, 1
store i64 %34, i64* %6
br label %8
35: ; preds = %8
%36 = load i32, i32* %5
ret i32 %36
}

LLVM IR (-O2):
define i32 @optimal_merge(i8** %0, i64 %1) {
%3 = icmp eq i64 %1, 0
br i1 %3, label %16, label %4
4: ; preds = %2, %4
%5 = phi i32 [ %12, %4 ], [ -1, %2 ]
%6 = phi i64 [ %14, %4 ], [ 0, %2 ]
%7 = tail call i32 @swap(i8** %0, i32 0, i64 %6)
%8 = load i8*, i8** %0
%9 = tail call i32 @strlen(i8* %8)
%10 = tail call i32 @optimal_merge_aux(i32 0, i32 %9, i8** %0, i32 1, i64 %1)
%11 = icmp ult i32 %10, %5
%12 = select i1 %11, i32 %10, i32 %5
%13 = tail call i32 @swap(i8** %0, i32 0, i64 %6)
%14 = add nuw i64 %6, 1
%15 = icmp eq i64 %14, %1
br i1 %15, label %16, label %4
16: ; preds = %4, %2
%17 = phi i32 [ -1, %2 ], [ %12, %4 ]
ret i32 %17
}