#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*/ memset(char *, int, int);
int /*<<< orphan*/ print_centered(char *, int, char);
int /*<<< orphan*/ strcat(char *, char *);
int strcspn(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
void greedy_align_paragraph(char *line, int max_width) {
  char tmp[max_width];
  char delims[] = " ";
  int token_len = 0;
  char *token = strtok(line, delims);
  tmp[0] = '\0';
  while (true) {
    if (token) {
      token_len = strcspn(token, delims);
    }
    if (token_len + strlen(tmp) > (max_width - 4) || !token) {
      print_centered(tmp, max_width, '*');
      memset(tmp, -1, max_width);
      tmp[0] = '\0';
    }
    if (!token)
      break;
    if (strlen(tmp) > 0) {
      strcat(tmp, " ");
    }
    strcat(tmp, token);
    token = strtok(NULL, delims);
  }
}


x86 assembly (clang, -O0):
<greedy_align_paragraph>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    0xe64(%rip),%ax
mov    %ax,-0x22(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%rdi
lea    -0x22(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,%rcx
mov    -0x38(%rbp),%rax
mov    %rcx,-0x30(%rbp)
movb   $0x0,(%rax)
jmp    11c7 <greedy_align_paragraph+0x67>
cmpq   $0x0,-0x30(%rbp)
je     11e2 <greedy_align_paragraph+0x82>
mov    -0x30(%rbp),%rdi
lea    -0x22(%rbp),%rsi
call   1060 <strcspn@plt>
mov    %eax,-0x28(%rbp)
mov    -0x38(%rbp),%rdi
mov    -0x28(%rbp),%eax
mov    %eax,-0x3c(%rbp)
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x3c(%rbp),%eax
add    %ecx,%eax
mov    -0xc(%rbp),%ecx
sub    $0x4,%ecx
cmp    %ecx,%eax
jg     1211 <greedy_align_paragraph+0xb1>
cmpq   $0x0,-0x30(%rbp)
jne    123a <greedy_align_paragraph+0xda>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%esi
mov    $0x2a,%edx
call   1030 <print_centered@plt>
mov    -0x38(%rbp),%rdi
mov    -0xc(%rbp),%edx
mov    $0xffffffff,%esi
call   1050 <memset@plt>
mov    -0x38(%rbp),%rax
movb   $0x0,(%rax)
cmpq   $0x0,-0x30(%rbp)
jne    124a <greedy_align_paragraph+0xea>
jmp    128f <greedy_align_paragraph+0x12f>
mov    -0x38(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%eax
jle    126c <greedy_align_paragraph+0x10c>
mov    -0x38(%rbp),%rdi
lea    0xd99(%rip),%rsi
call   1080 <strcat@plt>
mov    -0x38(%rbp),%rdi
mov    -0x30(%rbp),%rsi
call   1080 <strcat@plt>
lea    -0x22(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edi
call   1070 <strtok@plt>
mov    %rax,-0x30(%rbp)
jmp    11c2 <greedy_align_paragraph+0x62>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<greedy_align_paragraph>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%r14d
mov    %esi,%r15d
mov    %rsp,%r12
add    $0xf,%r15
and    $0xfffffffffffffff0,%r15
mov    %r12,%r13
sub    %r15,%r13
mov    %r13,%rsp
neg    %r15
movw   $0x20,-0x2a(%rbp)
lea    -0x2a(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,%rbx
movb   $0x0,(%r12,%r15,1)
lea    -0x4(%r14),%r15d
xor    %r12d,%r12d
jmp    11c9 <greedy_align_paragraph+0x69>
nopl   0x0(%rax,%rax,1)
mov    %r13,%rdi
mov    %rbx,%rsi
call   1080 <strcat@plt>
xor    %edi,%edi
lea    -0x2a(%rbp),%rsi
call   1070 <strtok@plt>
mov    %rax,%rbx
test   %rbx,%rbx
je     11dd <greedy_align_paragraph+0x7d>
mov    %rbx,%rdi
lea    -0x2a(%rbp),%rsi
call   1060 <strcspn@plt>
mov    %eax,%r12d
mov    %r13,%rdi
call   1040 <strlen@plt>
add    %r12d,%eax
cmp    %r15d,%eax
jg     11f2 <greedy_align_paragraph+0x92>
test   %rbx,%rbx
jne    1217 <greedy_align_paragraph+0xb7>
mov    %r13,%rdi
mov    %r14d,%esi
mov    $0x2a,%edx
call   1030 <print_centered@plt>
mov    %r13,%rdi
mov    $0xffffffff,%esi
mov    %r14d,%edx
call   1050 <memset@plt>
movb   $0x0,0x0(%r13)
test   %rbx,%rbx
je     123c <greedy_align_paragraph+0xdc>
mov    %r13,%rdi
call   1040 <strlen@plt>
test   %eax,%eax
jle    11b0 <greedy_align_paragraph+0x50>
mov    %r13,%rdi
lea    0xdce(%rip),%rsi
call   1080 <strcat@plt>
jmp    11b0 <greedy_align_paragraph+0x50>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.greedy_align_paragraph.delims = [2 x i8] c" \00"
@.str = [2 x i8] c" \00"
define void @greedy_align_paragraph(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [2 x i8]
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%10 = load i32, i32* %4
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
%14 = bitcast [2 x i8]* %7 to i8*
store i32 0, i32* %8
%15 = load i8*, i8** %3
%16 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%17 = call i8* @strtok(i8* %15, i8* %16)
store i8* %17, i8** %9
%18 = getelementptr inbounds i8, i8* %13, i64 0
store i8 0, i8* %18
br label %19
19: ; preds = %52, %2
br label %20
20: ; preds = %19
%21 = load i8*, i8** %9
%22 = icmp ne i8* %21, null
br i1 %22, label %23, label %27
23: ; preds = %20
%24 = load i8*, i8** %9
%25 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%26 = call i32 @strcspn(i8* %24, i8* %25)
store i32 %26, i32* %8
br label %27
27: ; preds = %23, %20
%28 = load i32, i32* %8
%29 = call i32 @strlen(i8* %13)
%30 = add nsw i32 %28, %29
%31 = load i32, i32* %4
%32 = sub nsw i32 %31, 4
%33 = icmp sgt i32 %30, %32
br i1 %33, label %37, label %34
34: ; preds = %27
%35 = load i8*, i8** %9
%36 = icmp ne i8* %35, null
br i1 %36, label %43, label %37
37: ; preds = %34, %27
%38 = load i32, i32* %4
%39 = call i32 @print_centered(i8* %13, i32 %38, i8 signext 42)
%40 = load i32, i32* %4
%41 = call i32 @memset(i8* %13, i32 -1, i32 %40)
%42 = getelementptr inbounds i8, i8* %13, i64 0
store i8 0, i8* %42
br label %43
43: ; preds = %37, %34
%44 = load i8*, i8** %9
%45 = icmp ne i8* %44, null
br i1 %45, label %47, label %46
46: ; preds = %43
br label %57
47: ; preds = %43
%48 = call i32 @strlen(i8* %13)
%49 = icmp sgt i32 %48, 0
br i1 %49, label %50, label %52
50: ; preds = %47
%51 = call i32 @strcat(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %52
52: ; preds = %50, %47
%53 = load i8*, i8** %9
%54 = call i32 @strcat(i8* %13, i8* %53)
%55 = getelementptr inbounds [2 x i8], [2 x i8]* %7, i64 0, i64 0
%56 = call i8* @strtok(i8* null, i8* %55)
store i8* %56, i8** %9
br label %19
57: ; preds = %46
%58 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c" \00"
define void @greedy_align_paragraph(i8* %0, i32 %1) {
%3 = alloca i16
%4 = zext i32 %1 to i64
%5 = alloca i8, i64 %4
%6 = bitcast i16* %3 to i8*
store i16 32, i16* %3
%7 = call i8* @strtok(i8* %0, i8* %6)
store i8 0, i8* %5
%8 = add nsw i32 %1, -4
br label %9
9: ; preds = %30, %2
%10 = phi i32 [ 0, %2 ], [ %16, %30 ]
%11 = phi i8* [ %7, %2 ], [ %32, %30 ]
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %15
13: ; preds = %9
%14 = call i32 @strcspn(i8* %11, i8* %6)
br label %15
15: ; preds = %13, %9
%16 = phi i32 [ %14, %13 ], [ %10, %9 ]
%17 = call i32 @strlen(i8* %5)
%18 = add nsw i32 %17, %16
%19 = icmp sle i32 %18, %8
%20 = select i1 %19, i1 %12, i1 false
br i1 %20, label %24, label %21
21: ; preds = %15
%22 = call i32 @print_centered(i8* %5, i32 %1, i8 signext 42)
%23 = call i32 @memset(i8* %5, i32 -1, i32 %1)
store i8 0, i8* %5
br label %24
24: ; preds = %15, %21
br i1 %12, label %25, label %33
25: ; preds = %24
%26 = call i32 @strlen(i8* %5)
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %25
%29 = call i32 @strcat(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %25
%31 = call i32 @strcat(i8* %5, i8* %11)
%32 = call i8* @strtok(i8* null, i8* %6)
br label %9
33: ; preds = %24
ret void
}