#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*/ printf(char *, ...);
scalar_t__ strcpy_limit(char *, char *, unsigned int);
int /*<<< orphan*/ substr_assemble(char *, int, unsigned int);
scalar_t__ substr_len(int);
unsigned int substr_size(int);
int substr_combine(char *dst, int *substr_ids, unsigned int n_ints,
                   size_t dst_sz) {
  printf("1111\n");
  unsigned int i = 0;
  size_t total_len = 0;
  for (i = 0; i < n_ints; ++i) {
    total_len += substr_len(substr_ids[i]);
    printf("total_len %d\n", total_len);
  }
  printf("22222\n");
  if (total_len >= dst_sz) {
    printf("33333\n");
    return -1;
  }
  unsigned int offs = 0;
  unsigned int max = 0;
  for (i = 0; i < n_ints; ++i) {
    int id = substr_ids[i];
    max = substr_size(id);
    char src[max];
    substr_assemble(src, id, max);
    offs += strcpy_limit(&dst[offs], src, max);
    printf("offs %d, id%d, max %d\n", offs, id, max);
  }
  printf("44444\n");
  dst[total_len] = '\0';
  return offs;
}


x86 assembly (clang, -O0):
<substr_combine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %rcx,-0x28(%rbp)
lea    0xe92(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x2c(%rbp)
movq   $0x0,-0x38(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    11ce <substr_combine+0x7e>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    (%rax,%rcx,4),%edi
call   1030 <substr_len@plt>
add    -0x38(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rsi
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    118b <substr_combine+0x3b>
lea    0xe3f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x38(%rbp),%rax
cmp    -0x28(%rbp),%rax
jb     1204 <substr_combine+0xb4>
lea    0xe2a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12dc <substr_combine+0x18c>
movl   $0x0,-0x3c(%rbp)
movl   $0x0,-0x40(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0x1c(%rbp),%eax
jae    12bc <substr_combine+0x16c>
mov    -0x18(%rbp),%rax
mov    -0x2c(%rbp),%ecx
mov    (%rax,%rcx,4),%eax
mov    %eax,-0x44(%rbp)
mov    -0x44(%rbp),%edi
call   1070 <substr_size@plt>
mov    %eax,-0x40(%rbp)
mov    -0x40(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rdi
sub    %rcx,%rdi
mov    %rdi,-0x60(%rbp)
mov    %rdi,%rsp
mov    %rax,-0x58(%rbp)
mov    -0x44(%rbp),%esi
mov    -0x40(%rbp),%edx
call   1050 <substr_assemble@plt>
mov    -0x60(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x3c(%rbp),%eax
add    %rax,%rdi
mov    -0x40(%rbp),%edx
call   1060 <strcpy_limit@plt>
mov    %rax,%rcx
mov    -0x3c(%rbp),%eax
add    %rcx,%rax
mov    %eax,-0x3c(%rbp)
mov    -0x3c(%rbp),%esi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%ecx
lea    0xd82(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1219 <substr_combine+0xc9>
lea    0xd76(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x10(%rbp),%rax
mov    -0x38(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x3c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<substr_combine>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x38,%rsp
mov    %rcx,%r14
mov    %edx,%ebx
mov    %rsi,%r15
mov    %rdi,%r12
lea    0xea2(%rip),%rdi
call   1030 <puts@plt>
test   %ebx,%ebx
mov    %r12,-0x48(%rbp)
je     1291 <substr_combine+0x131>
mov    %r14,-0x38(%rbp)
mov    %ebx,-0x2c(%rbp)
mov    %ebx,%r14d
lea    0xe5b(%rip),%r12
xor    %ebx,%ebx
xor    %r13d,%r13d
nopw   0x0(%rax,%rax,1)
mov    (%r15,%rbx,4),%edi
call   1040 <substr_len@plt>
add    %rax,%r13
mov    %r12,%rdi
mov    %r13,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r14
jne    11b0 <substr_combine+0x50>
mov    %r15,%r12
lea    0xe4e(%rip),%rdi
call   1030 <puts@plt>
cmp    -0x38(%rbp),%r13
jae    12be <substr_combine+0x15e>
cmpl   $0x0,-0x2c(%rbp)
je     12a5 <substr_combine+0x145>
xor    %r15d,%r15d
xor    %ebx,%ebx
mov    %r12,-0x58(%rbp)
mov    %r14,-0x50(%rbp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,-0x2c(%rbp)
mov    -0x58(%rbp),%rax
mov    (%rax,%r15,4),%r12d
mov    %r12d,%edi
mov    %r12d,-0x3c(%rbp)
call   1080 <substr_size@plt>
mov    %eax,%r14d
mov    %rsp,-0x38(%rbp)
mov    %eax,%eax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
mov    %rsp,%rbx
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rbx,%rdi
mov    %r12d,%esi
mov    %r14d,%edx
call   1060 <substr_assemble@plt>
mov    -0x2c(%rbp),%r12d
mov    %r12d,%edi
add    -0x48(%rbp),%rdi
mov    %rbx,%rsi
mov    %r14d,%edx
call   1070 <strcpy_limit@plt>
mov    %rax,%rbx
add    %r12d,%ebx
lea    0xd9c(%rip),%rdi
mov    %ebx,%esi
mov    -0x3c(%rbp),%edx
mov    %r14d,%ecx
xor    %eax,%eax
call   1050 <printf@plt>
mov    -0x38(%rbp),%rsp
add    $0x1,%r15
cmp    %r15,-0x50(%rbp)
jne    1210 <substr_combine+0xb0>
jmp    12a7 <substr_combine+0x147>
lea    0xd92(%rip),%rdi
call   1030 <puts@plt>
test   %r14,%r14
je     12be <substr_combine+0x15e>
xor    %r13d,%r13d
xor    %ebx,%ebx
lea    0xd82(%rip),%rdi
call   1030 <puts@plt>
mov    -0x48(%rbp),%rax
movb   $0x0,(%rax,%r13,1)
jmp    12cf <substr_combine+0x16f>
lea    0xd71(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"1111\0A\00"
@.str.1 = [14 x i8] c"total_len %d\0A\00"
@.str.2 = [7 x i8] c"22222\0A\00"
@.str.3 = [7 x i8] c"33333\0A\00"
@.str.4 = [23 x i8] c"offs %d, id%d, max %d\0A\00"
@.str.5 = [7 x i8] c"44444\0A\00"
define i32 @substr_combine(i8* %0, i32* %1, i32 %2, i64 %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i64
%10 = alloca i32
%11 = alloca i64
%12 = alloca i32
%13 = alloca i32
%14 = alloca i32
%15 = alloca i8*
%16 = alloca i64
store i8* %0, i8** %6
store i32* %1, i32** %7
store i32 %2, i32* %8
store i64 %3, i64* %9
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %10
store i64 0, i64* %11
store i32 0, i32* %10
br label %18
18: ; preds = %33, %4
%19 = load i32, i32* %10
%20 = load i32, i32* %8
%21 = icmp ult i32 %19, %20
br i1 %21, label %22, label %36
22: ; preds = %18
%23 = load i32*, i32** %7
%24 = load i32, i32* %10
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds i32, i32* %23, i64 %25
%27 = load i32, i32* %26
%28 = call i64 @substr_len(i32 %27)
%29 = load i64, i64* %11
%30 = add i64 %29, %28
store i64 %30, i64* %11
%31 = load i64, i64* %11
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64 %31)
br label %33
33: ; preds = %22
%34 = load i32, i32* %10
%35 = add i32 %34, 1
store i32 %35, i32* %10
br label %18
36: ; preds = %18
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
%38 = load i64, i64* %11
%39 = load i64, i64* %9
%40 = icmp uge i64 %38, %39
br i1 %40, label %41, label %43
41: ; preds = %36
%42 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %5
br label %87
43: ; preds = %36
store i32 0, i32* %12
store i32 0, i32* %13
store i32 0, i32* %10
br label %44
44: ; preds = %78, %43
%45 = load i32, i32* %10
%46 = load i32, i32* %8
%47 = icmp ult i32 %45, %46
br i1 %47, label %48, label %81
48: ; preds = %44
%49 = load i32*, i32** %7
%50 = load i32, i32* %10
%51 = zext i32 %50 to i64
%52 = getelementptr inbounds i32, i32* %49, i64 %51
%53 = load i32, i32* %52
store i32 %53, i32* %14
%54 = load i32, i32* %14
%55 = call i32 @substr_size(i32 %54)
store i32 %55, i32* %13
%56 = load i32, i32* %13
%57 = zext i32 %56 to i64
%58 = call i8* @llvm.stacksave()
store i8* %58, i8** %15
%59 = alloca i8, i64 %57
store i64 %57, i64* %16
%60 = load i32, i32* %14
%61 = load i32, i32* %13
%62 = call i32 @substr_assemble(i8* %59, i32 %60, i32 %61)
%63 = load i8*, i8** %6
%64 = load i32, i32* %12
%65 = zext i32 %64 to i64
%66 = getelementptr inbounds i8, i8* %63, i64 %65
%67 = load i32, i32* %13
%68 = call i64 @strcpy_limit(i8* %66, i8* %59, i32 %67)
%69 = load i32, i32* %12
%70 = zext i32 %69 to i64
%71 = add nsw i64 %70, %68
%72 = trunc i64 %71 to i32
store i32 %72, i32* %12
%73 = load i32, i32* %12
%74 = load i32, i32* %14
%75 = load i32, i32* %13
%76 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %73, i32 %74, i32 %75)
%77 = load i8*, i8** %15
br label %78
78: ; preds = %48
%79 = load i32, i32* %10
%80 = add i32 %79, 1
store i32 %80, i32* %10
br label %44
81: ; preds = %44
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
%83 = load i8*, i8** %6
%84 = load i64, i64* %11
%85 = getelementptr inbounds i8, i8* %83, i64 %84
store i8 0, i8* %85
%86 = load i32, i32* %12
store i32 %86, i32* %5
br label %87
87: ; preds = %81, %41
%88 = load i32, i32* %5
ret i32 %88
}

LLVM IR (-O2):
@.str.1 = [14 x i8] c"total_len %d\0A\00"
@.str.4 = [23 x i8] c"offs %d, id%d, max %d\0A\00"
@str = [5 x i8] c"1111\00"
@str.6 = [6 x i8] c"22222\00"
@str.7 = [6 x i8] c"44444\00"
@str.8 = [6 x i8] c"33333\00"
define i32 @substr_combine(i8* %0, i32* nocapture %1, i32 %2, i64 %3) {
%5 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([5 x i8], [5 x i8]* @str, i64 0, i64 0))
%6 = icmp eq i32 %2, 0
br i1 %6, label %22, label %7
7: ; preds = %4
%8 = zext i32 %2 to i64
br label %9
9: ; preds = %7, %9
%10 = phi i64 [ 0, %7 ], [ %17, %9 ]
%11 = phi i64 [ 0, %7 ], [ %15, %9 ]
%12 = getelementptr inbounds i32, i32* %1, i64 %10
%13 = load i32, i32* %12
%14 = tail call i64 @substr_len(i32 %13)
%15 = add i64 %14, %11
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i64 %15)
%17 = add nuw nsw i64 %10, 1
%18 = icmp eq i64 %17, %8
br i1 %18, label %19, label %9
19: ; preds = %9
%20 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.6, i64 0, i64 0))
%21 = icmp ult i64 %15, %3
br i1 %21, label %25, label %28
22: ; preds = %4
%23 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.6, i64 0, i64 0))
%24 = icmp eq i64 %3, 0
br i1 %24, label %28, label %48
25: ; preds = %19
br i1 %6, label %48, label %26
26: ; preds = %25
%27 = zext i32 %2 to i64
br label %30
28: ; preds = %22, %19
%29 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.8, i64 0, i64 0))
br label %53
30: ; preds = %26, %30
%31 = phi i64 [ 0, %26 ], [ %46, %30 ]
%32 = phi i32 [ 0, %26 ], [ %44, %30 ]
%33 = getelementptr inbounds i32, i32* %1, i64 %31
%34 = load i32, i32* %33
%35 = call i32 @substr_size(i32 %34)
%36 = zext i32 %35 to i64
%37 = call i8* @llvm.stacksave()
%38 = alloca i8, i64 %36
%39 = call i32 @substr_assemble(i8* %38, i32 %34, i32 %35)
%40 = zext i32 %32 to i64
%41 = getelementptr inbounds i8, i8* %0, i64 %40
%42 = call i64 @strcpy_limit(i8* %41, i8* %38, i32 %35)
%43 = trunc i64 %42 to i32
%44 = add i32 %32, %43
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32 %44, i32 %34, i32 %35)
%46 = add nuw nsw i64 %31, 1
%47 = icmp eq i64 %46, %27
br i1 %47, label %48, label %30
48: ; preds = %30, %22, %25
%49 = phi i64 [ %15, %25 ], [ 0, %22 ], [ %15, %30 ]
%50 = phi i32 [ 0, %25 ], [ 0, %22 ], [ %44, %30 ]
%51 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @str.7, i64 0, i64 0))
%52 = getelementptr inbounds i8, i8* %0, i64 %49
store i8 0, i8* %52
br label %53
53: ; preds = %48, %28
%54 = phi i32 [ -1, %28 ], [ %50, %48 ]
ret i32 %54
}