#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*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ str_try_shorten(char *);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
void str_reduce(char *dest, char *src, int dest_len) {
  char *b;
  int l = strlen(src);
  if (dest_len <= 0)
    return;
  if (l < dest_len) {
    strncpy(dest, src, dest_len);
  } else {
    b = malloc(l + 1);
    strcpy(b, src);
    while (strlen(b) > dest_len)
      str_try_shorten(b);
    strncpy(dest, b, dest_len);
    free(b);
  }
  dest[dest_len] = '\0';
}


x86 assembly (clang, -O0):
<str_reduce>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <strlen@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x14(%rbp)
jg     118e <str_reduce+0x2e>
jmp    1210 <str_reduce+0xb0>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11af <str_reduce+0x4f>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <strncpy@plt>
jmp    1204 <str_reduce+0xa4>
mov    -0x24(%rbp),%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1060 <strcpy@plt>
mov    -0x20(%rbp),%rdi
call   1070 <strlen@plt>
cmp    -0x14(%rbp),%eax
jle    11eb <str_reduce+0x8b>
mov    -0x20(%rbp),%rdi
call   1030 <str_try_shorten@plt>
jmp    11cb <str_reduce+0x6b>
mov    -0x8(%rbp),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1050 <strncpy@plt>
mov    -0x20(%rbp),%rdi
call   1040 <free@plt>
mov    -0x8(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<str_reduce>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1070 <strlen@plt>
test   %ebp,%ebp
jle    11e3 <str_reduce+0x83>
cmp    %ebp,%eax
jge    118e <str_reduce+0x2e>
mov    %r14,%rdi
mov    %r15,%rsi
mov    %ebp,%edx
call   1050 <strncpy@plt>
jmp    11db <str_reduce+0x7b>
add    $0x1,%eax
mov    %eax,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r15,%rsi
call   1060 <strcpy@plt>
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
call   1070 <strlen@plt>
cmp    %ebp,%eax
jle    11c6 <str_reduce+0x66>
mov    %rbx,%rdi
call   1030 <str_try_shorten@plt>
jmp    11b0 <str_reduce+0x50>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1050 <strncpy@plt>
mov    %rbx,%rdi
call   1040 <free@plt>
movslq %ebp,%rax
movb   $0x0,(%r14,%rax,1)
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define void @str_reduce(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %5
%10 = call i32 @strlen(i8* %9)
store i32 %10, i32* %8
%11 = load i32, i32* %6
%12 = icmp sle i32 %11, 0
br i1 %12, label %13, label %14
13: ; preds = %3
br label %50
14: ; preds = %3
%15 = load i32, i32* %8
%16 = load i32, i32* %6
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %23
18: ; preds = %14
%19 = load i8*, i8** %4
%20 = load i8*, i8** %5
%21 = load i32, i32* %6
%22 = call i32 @strncpy(i8* %19, i8* %20, i32 %21)
br label %45
23: ; preds = %14
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
%26 = call i8* @malloc(i32 %25)
store i8* %26, i8** %7
%27 = load i8*, i8** %7
%28 = load i8*, i8** %5
%29 = call i32 @strcpy(i8* %27, i8* %28)
br label %30
30: ; preds = %35, %23
%31 = load i8*, i8** %7
%32 = call i32 @strlen(i8* %31)
%33 = load i32, i32* %6
%34 = icmp sgt i32 %32, %33
br i1 %34, label %35, label %38
35: ; preds = %30
%36 = load i8*, i8** %7
%37 = call i32 @str_try_shorten(i8* %36)
br label %30
38: ; preds = %30
%39 = load i8*, i8** %4
%40 = load i8*, i8** %7
%41 = load i32, i32* %6
%42 = call i32 @strncpy(i8* %39, i8* %40, i32 %41)
%43 = load i8*, i8** %7
%44 = call i32 @free(i8* %43)
br label %45
45: ; preds = %38, %18
%46 = load i8*, i8** %4
%47 = load i32, i32* %6
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8, i8* %46, i64 %48
store i8 0, i8* %49
br label %50
50: ; preds = %45, %13
ret void
}

LLVM IR (-O2):
define void @str_reduce(i8* %0, i8* %1, i32 %2) {
%4 = tail call i32 @strlen(i8* %1)
%5 = icmp slt i32 %2, 1
br i1 %5, label %26, label %6
6: ; preds = %3
%7 = icmp slt i32 %4, %2
br i1 %7, label %8, label %10
8: ; preds = %6
%9 = tail call i32 @strncpy(i8* %0, i8* %1, i32 %2)
br label %23
10: ; preds = %6
%11 = add nsw i32 %4, 1
%12 = tail call i8* @malloc(i32 %11)
%13 = tail call i32 @strcpy(i8* %12, i8* %1)
%14 = tail call i32 @strlen(i8* %12)
%15 = icmp sgt i32 %14, %2
br i1 %15, label %16, label %20
16: ; preds = %10, %16
%17 = tail call i32 @str_try_shorten(i8* %12)
%18 = tail call i32 @strlen(i8* %12)
%19 = icmp sgt i32 %18, %2
br i1 %19, label %16, label %20
20: ; preds = %16, %10
%21 = tail call i32 @strncpy(i8* %0, i8* %12, i32 %2)
%22 = tail call i32 @free(i8* %12)
br label %23
23: ; preds = %20, %8
%24 = sext i32 %2 to i64
%25 = getelementptr inbounds i8, i8* %0, i64 %24
store i8 0, i8* %25
br label %26
26: ; preds = %3, %23
ret void
}