#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 *grab_word(char *, char *);
int /*<<< orphan*/ mudstrlcat(char *, char *, int);
int /*<<< orphan*/ mudstrlcpy(char *, char *, int);
char *strchr(char *, char);
int strlen(char *);
char *wordwrap(char *txt, short wrap) {
  static char buf[4096];
  char *bufp;
  buf[0] = '\0';
  bufp = buf;
  if (txt != NULL) {
    char line[4096];
    char temp[4096];
    char *ptr, *p;
    int ln, x;
    ++bufp;
    line[0] = '\0';
    ptr = txt;
    while (*ptr) {
      ptr = grab_word(ptr, temp);
      ln = strlen(line);
      x = strlen(temp);
      if ((ln + x + 1) < wrap) {
        if (ln > 0 && line[ln - 1] == '.')
          mudstrlcat(line, "  ", 4096);
        else
          mudstrlcat(line, " ", 4096);
        mudstrlcat(line, temp, 4096);
        p = strchr(line, '\n');
        if (!p)
          p = strchr(line, '\r');
        if (p) {
          mudstrlcat(buf, line, 4096);
          line[0] = '\0';
        }
      } else {
        mudstrlcat(line, "\r\n", 4096);
        mudstrlcat(buf, line, 4096);
        mudstrlcpy(line, temp, 4096);
      }
    }
    if (line[0] != '\0')
      mudstrlcat(buf, line, 4096);
  }
  return bufp;
}


x86 assembly (clang, -O0):
<wordwrap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2040,%rsp
mov    %si,%ax
mov    %rdi,-0x8(%rbp)
mov    %ax,-0xa(%rbp)
movb   $0x0,0x2ef3(%rip)
lea    0x2eec(%rip),%rax
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x8(%rbp)
je     136a <wordwrap+0x21a>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
movb   $0x0,-0x1020(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x2028(%rbp)
mov    -0x2028(%rbp),%rax
cmpb   $0x0,(%rax)
je     133d <wordwrap+0x1ed>
mov    -0x2028(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
call   1060 <grab_word@plt>
mov    %rax,-0x2028(%rbp)
lea    -0x1020(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2034(%rbp)
lea    -0x2020(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x2038(%rbp)
mov    -0x2034(%rbp),%eax
add    -0x2038(%rbp),%eax
add    $0x1,%eax
movswl -0xa(%rbp),%ecx
cmp    %ecx,%eax
jge    12f0 <wordwrap+0x1a0>
cmpl   $0x0,-0x2034(%rbp)
jle    1250 <wordwrap+0x100>
mov    -0x2034(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x1020(%rbp,%rax,1),%eax
ff
cmp    $0x2e,%eax
jne    1250 <wordwrap+0x100>
lea    -0x1020(%rbp),%rdi
lea    0xdbf(%rip),%rsi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
jmp    1268 <wordwrap+0x118>
lea    -0x1020(%rbp),%rdi
lea    0xda3(%rip),%rsi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
lea    -0x1020(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
lea    -0x1020(%rbp),%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
mov    %rax,-0x2030(%rbp)
cmpq   $0x0,-0x2030(%rbp)
jne    12be <wordwrap+0x16e>
lea    -0x1020(%rbp),%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
mov    %rax,-0x2030(%rbp)
cmpq   $0x0,-0x2030(%rbp)
je     12eb <wordwrap+0x19b>
lea    -0x1020(%rbp),%rsi
lea    0x2d86(%rip),%rdi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
movb   $0x0,-0x1020(%rbp)
jmp    1338 <wordwrap+0x1e8>
lea    -0x1020(%rbp),%rdi
lea    0xd05(%rip),%rsi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
lea    -0x1020(%rbp),%rsi
lea    0x2d4a(%rip),%rdi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
lea    -0x1020(%rbp),%rdi
lea    -0x2020(%rbp),%rsi
mov    $0x1000,%edx
call   1050 <mudstrlcpy@plt>
jmp    11a1 <wordwrap+0x51>
movsbl -0x1020(%rbp),%eax
cmp    $0x0,%eax
je     1365 <wordwrap+0x215>
lea    -0x1020(%rbp),%rsi
lea    0x2d05(%rip),%rdi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
jmp    136a <wordwrap+0x21a>
mov    -0x18(%rbp),%rax
add    $0x2040,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wordwrap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x2008,%rsp
movb   $0x0,0x2ef8(%rip)
test   %rdi,%rdi
je     1280 <wordwrap+0x130>
mov    %rdi,%rbx
movb   $0x0,(%rsp)
cmpb   $0x0,(%rdi)
je     12a3 <wordwrap+0x153>
movswl %si,%r13d
lea    0x1000(%rsp),%r14
mov    %rsp,%r15
lea    0x2ec9(%rip),%r12
jmp    11dd <wordwrap+0x8d>
nopl   0x0(%rax)
mov    %r15,%rdi
lea    0xe59(%rip),%rsi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1050 <mudstrlcpy@plt>
cmpb   $0x0,(%rbx)
je     1289 <wordwrap+0x139>
mov    %rbx,%rdi
mov    %r14,%rsi
call   1060 <grab_word@plt>
mov    %rax,%rbx
mov    %r15,%rdi
call   1030 <strlen@plt>
mov    %eax,%ebp
mov    %r14,%rdi
call   1030 <strlen@plt>
add    %ebp,%eax
add    $0x1,%eax
cmp    %r13d,%eax
jge    11a0 <wordwrap+0x50>
test   %ebp,%ebp
jle    121b <wordwrap+0xcb>
add    $0xffffffff,%ebp
cmpb   $0x2e,(%rsp,%rbp,1)
lea    0xde7(%rip),%rsi
je     1222 <wordwrap+0xd2>
lea    0xddf(%rip),%rsi
mov    %r15,%rdi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
mov    %r15,%rdi
mov    %r14,%rsi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
mov    %r15,%rdi
mov    $0xa,%esi
call   1040 <strchr@plt>
test   %rax,%rax
jne    1267 <wordwrap+0x117>
mov    %r15,%rdi
mov    $0xd,%esi
call   1040 <strchr@plt>
test   %rax,%rax
je     11d4 <wordwrap+0x84>
mov    %r12,%rdi
mov    %r15,%rsi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
movb   $0x0,(%rsp)
jmp    11d4 <wordwrap+0x84>
lea    0x2dd9(%rip),%rax
jmp    12aa <wordwrap+0x15a>
cmpb   $0x0,(%rsp)
je     12a3 <wordwrap+0x153>
lea    0x2dca(%rip),%rdi
mov    %rsp,%rsi
mov    $0x1000,%edx
call   1070 <mudstrlcat@plt>
lea    0x2db7(%rip),%rax
add    $0x2008,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@wordwrap.buf = internal global [4096 x i8] zeroinitializer
@.str = [3 x i8] c" \00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [3 x i8] c"\0D\0A\00"
define i8* @wordwrap(i8* %0, i16 signext %1) {
%3 = alloca [4096 x i8]
%4 = alloca [4096 x i8]
store i8 0, i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @wordwrap.buf, i64 0, i64 0)
%5 = icmp eq i8* %0, null
br i1 %5, label %54, label %6
6: ; preds = %2
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%8 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
store i8 0, i8* %7
%9 = load i8, i8* %0
%10 = icmp eq i8 %9, 0
br i1 %10, label %53, label %11
11: ; preds = %6
%12 = sext i16 %1 to i32
br label %13
13: ; preds = %11, %45
%14 = phi i8* [ %0, %11 ], [ %15, %45 ]
%15 = call i8* @grab_word(i8* %14, i8* %8)
%16 = call i32 @strlen(i8* %7)
%17 = call i32 @strlen(i8* %8)
%18 = add i32 %16, 1
%19 = add i32 %18, %17
%20 = icmp slt i32 %19, %12
br i1 %20, label %21, label %41
21: ; preds = %13
%22 = icmp sgt i32 %16, 0
br i1 %22, label %23, label %29
23: ; preds = %21
%24 = add nsw i32 %16, -1
%25 = zext i32 %24 to i64
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 %25
%27 = load i8, i8* %26
%28 = icmp eq i8 %27, 46
br i1 %28, label %30, label %29
29: ; preds = %23, %21
br label %30
30: ; preds = %23, %29
%31 = phi i8* [ getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), %29 ], [ getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), %23 ]
%32 = call i32 @mudstrlcat(i8* %7, i8* %31, i32 4096)
%33 = call i32 @mudstrlcat(i8* %7, i8* %8, i32 4096)
%34 = call i8* @strchr(i8* %7, i8 signext 10)
%35 = icmp eq i8* %34, null
br i1 %35, label %36, label %39
36: ; preds = %30
%37 = call i8* @strchr(i8* %7, i8 signext 13)
%38 = icmp eq i8* %37, null
br i1 %38, label %45, label %39
39: ; preds = %30, %36
%40 = call i32 @mudstrlcat(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @wordwrap.buf, i64 0, i64 0), i8* %7, i32 4096)
store i8 0, i8* %7
br label %45
41: ; preds = %13
%42 = call i32 @mudstrlcat(i8* %7, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 4096)
%43 = call i32 @mudstrlcat(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @wordwrap.buf, i64 0, i64 0), i8* %7, i32 4096)
%44 = call i32 @mudstrlcpy(i8* %7, i8* %8, i32 4096)
br label %45
45: ; preds = %36, %39, %41
%46 = load i8, i8* %15
%47 = icmp eq i8 %46, 0
br i1 %47, label %48, label %13
48: ; preds = %45
%49 = load i8, i8* %7
%50 = icmp eq i8 %49, 0
br i1 %50, label %53, label %51
51: ; preds = %48
%52 = call i32 @mudstrlcat(i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @wordwrap.buf, i64 0, i64 0), i8* %7, i32 4096)
br label %53
53: ; preds = %6, %51, %48
br label %54
54: ; preds = %53, %2
%55 = phi i8* [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @wordwrap.buf, i64 0, i64 1), %53 ], [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @wordwrap.buf, i64 0, i64 0), %2 ]
ret i8* %55
}