#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 */
typedef int ptrdiff_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ carriage(int /*<<< orphan*/ *, int);
int /*<<< orphan*/ carriage_return(int /*<<< orphan*/ *, int);
scalar_t__ fit_horizontally(char const *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char *);
char *goto_nextstop(char const *, int);
scalar_t__ isspace(char const);
scalar_t__ iswordbreak(char const);
char *print_stretch(int /*<<< orphan*/ *, char const *, char const *);
void fprint_justified(FILE *f, const char *p, int blankheader, int margin,
                      int hor) {
  const char *q;
  ptrdiff_t rem, HOR;
  assert(margin >= 0);
  assert(hor > 0);
  HOR = (ptrdiff_t)hor;
  rem = HOR;
  carriage(f, blankheader);
  while (*p != '\0') {
    if (rem == HOR && isspace(*p)) {
      p++;
      continue;
    }
    if (rem > 0 && isspace(*p)) {
      fprintf(f, "%s", *p == '\n' ? "\n" : " ");
      p++;
      rem--;
      continue;
    }
    if (rem == 0) {
      carriage_return(f, margin);
      rem = HOR;
      continue;
    }
    q = goto_nextstop(p, rem);
    if (iswordbreak(*q)) {
      const char *x;
      x = print_stretch(f, p, q);
      rem -= q - p;
      p = x;
    } else {
      if (fit_horizontally(p, HOR)) {
        carriage_return(f, margin);
        rem = HOR;
      } else {
        print_stretch(f, p, p + rem);
        p += rem;
        rem = HOR;
        carriage_return(f, margin);
      }
    }
  }
}


x86 assembly (clang, -O0):
<fprint_justified>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
mov    %r8d,-0x1c(%rbp)
cmpl   $0x0,-0x18(%rbp)
setge  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
cmpl   $0x0,-0x1c(%rbp)
setg   %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10a0 <carriage@plt>
mov    -0x10(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     137d <fprint_justified+0x1ed>
mov    -0x2c(%rbp),%eax
cmp    -0x30(%rbp),%eax
jne    1227 <fprint_justified+0x97>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     1227 <fprint_justified+0x97>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
jmp    11e4 <fprint_justified+0x54>
cmpl   $0x0,-0x2c(%rbp)
jle    128d <fprint_justified+0xfd>
mov    -0x10(%rbp),%rax
movsbl (%rax),%edi
call   1050 <isspace@plt>
cmp    $0x0,%rax
je     128d <fprint_justified+0xfd>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rax
movsbl (%rax),%ecx
lea    0xdac(%rip),%rdx
lea    0xda3(%rip),%rax
cmp    $0xa,%ecx
cmove  %rax,%rdx
lea    0xd92(%rip),%rsi
call   1070 <fprintf@plt>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x2c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x2c(%rbp)
jmp    11e4 <fprint_justified+0x54>
cmpl   $0x0,-0x2c(%rbp)
jne    12ae <fprint_justified+0x11e>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1090 <carriage_return@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    11e4 <fprint_justified+0x54>
mov    -0x10(%rbp),%rdi
mov    -0x2c(%rbp),%esi
call   10b0 <goto_nextstop@plt>
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%edi
call   1080 <iswordbreak@plt>
cmp    $0x0,%rax
je     130b <fprint_justified+0x17b>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x28(%rbp),%rdx
call   1040 <print_stretch@plt>
mov    %rax,-0x38(%rbp)
mov    -0x28(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
movslq -0x2c(%rbp),%rax
sub    %rcx,%rax
mov    %eax,-0x2c(%rbp)
mov    -0x38(%rbp),%rax
mov    %rax,-0x10(%rbp)
jmp    1378 <fprint_justified+0x1e8>
mov    -0x10(%rbp),%rdi
mov    -0x30(%rbp),%esi
call   1030 <fit_horizontally@plt>
cmp    $0x0,%rax
je     1338 <fprint_justified+0x1a8>
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1090 <carriage_return@plt>
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
jmp    1373 <fprint_justified+0x1e3>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x10(%rbp),%rdx
movslq -0x2c(%rbp),%rax
add    %rax,%rdx
call   1040 <print_stretch@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x30(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   1090 <carriage_return@plt>
jmp    1378 <fprint_justified+0x1e8>
jmp    11e4 <fprint_justified+0x54>
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<fprint_justified>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8d,%r14d
mov    %ecx,%r13d
mov    %edx,%ebx
mov    %rsi,%r12
mov    %rdi,%r15
mov    %ecx,%edi
not    %edi
shr    $0x1f,%edi
call   1070 <assert@plt>
xor    %edi,%edi
test   %r14d,%r14d
setg   %dil
call   1070 <assert@plt>
mov    %r15,%rdi
mov    %ebx,%esi
call   10a0 <carriage@plt>
mov    (%r12),%al
test   %al,%al
je     12f7 <fprint_justified+0x167>
mov    %r14d,%ebx
jmp    120d <fprint_justified+0x7d>
xchg   %ax,%ax
cmpb   $0xa,(%r12)
mov    $0x20,%edi
mov    $0xa,%eax
cmove  %eax,%edi
mov    %r15,%rsi
call   1060 <fputc@plt>
add    $0x1,%r12
add    $0xffffffff,%ebx
mov    (%r12),%al
test   %al,%al
je     12f7 <fprint_justified+0x167>
mov    %r12,%rbp
cmp    %r14d,%ebx
jne    1243 <fprint_justified+0xb3>
movsbl %al,%edi
call   1050 <isspace@plt>
test   %rax,%rax
je     1240 <fprint_justified+0xb0>
movzbl 0x1(%rbp),%eax
add    $0x1,%rbp
test   %al,%al
jne    1210 <fprint_justified+0x80>
jmp    12f7 <fprint_justified+0x167>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %rbp,%r12
test   %ebx,%ebx
jle    1260 <fprint_justified+0xd0>
movsbl (%r12),%edi
call   1050 <isspace@plt>
test   %rax,%rax
jne    11e0 <fprint_justified+0x50>
jmp    1262 <fprint_justified+0xd2>
nopl   0x0(%rax,%rax,1)
je     12b8 <fprint_justified+0x128>
mov    %r13d,%ebp
mov    %r12,%rdi
mov    %ebx,%esi
call   10b0 <goto_nextstop@plt>
mov    %rax,%r13
movsbl (%rax),%edi
call   1080 <iswordbreak@plt>
test   %rax,%rax
je     129e <fprint_justified+0x10e>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <print_stretch@plt>
sub    %r13d,%r12d
add    %r12d,%ebx
mov    %rax,%r12
mov    %ebp,%r13d
jmp    1201 <fprint_justified+0x71>
mov    %r12,%rdi
mov    %r14d,%esi
call   1030 <fit_horizontally@plt>
test   %rax,%rax
je     12cb <fprint_justified+0x13b>
mov    %r15,%rdi
mov    %ebp,%r13d
mov    %ebp,%esi
jmp    12be <fprint_justified+0x12e>
mov    %r15,%rdi
mov    %r13d,%esi
call   1090 <carriage_return@plt>
mov    %r14d,%ebx
jmp    1201 <fprint_justified+0x71>
movslq %ebx,%r13
add    %r12,%r13
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1040 <print_stretch@plt>
mov    %r15,%rdi
mov    %ebp,%esi
call   1090 <carriage_return@plt>
mov    %r14d,%ebx
mov    %r13,%r12
mov    %ebp,%r13d
jmp    1201 <fprint_justified+0x71>
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@.str.1 = [2 x i8] c"\0A\00"
@.str.2 = [2 x i8] c" \00"
define void @fprint_justified(i32* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i8*
%12 = alloca i32
%13 = alloca i32
%14 = alloca i8*
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%15 = load i32, i32* %9
%16 = icmp sge i32 %15, 0
%17 = zext i1 %16 to i32
%18 = call i32 @assert(i32 %17)
%19 = load i32, i32* %10
%20 = icmp sgt i32 %19, 0
%21 = zext i1 %20 to i32
%22 = call i32 @assert(i32 %21)
%23 = load i32, i32* %10
store i32 %23, i32* %13
%24 = load i32, i32* %13
store i32 %24, i32* %12
%25 = load i32*, i32** %6
%26 = load i32, i32* %8
%27 = call i32 @carriage(i32* %25, i32 %26)
br label %28
28: ; preds = %124, %69, %53, %42, %5
%29 = load i8*, i8** %7
%30 = load i8, i8* %29
%31 = sext i8 %30 to i32
%32 = icmp ne i32 %31, 0
br i1 %32, label %33, label %125
33: ; preds = %28
%34 = load i32, i32* %12
%35 = load i32, i32* %13
%36 = icmp eq i32 %34, %35
br i1 %36, label %37, label %45
37: ; preds = %33
%38 = load i8*, i8** %7
%39 = load i8, i8* %38
%40 = call i64 @isspace(i8 signext %39)
%41 = icmp ne i64 %40, 0
br i1 %41, label %42, label %45
42: ; preds = %37
%43 = load i8*, i8** %7
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %7
br label %28
45: ; preds = %37, %33
%46 = load i32, i32* %12
%47 = icmp sgt i32 %46, 0
br i1 %47, label %48, label %66
48: ; preds = %45
%49 = load i8*, i8** %7
%50 = load i8, i8* %49
%51 = call i64 @isspace(i8 signext %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %66
53: ; preds = %48
%54 = load i32*, i32** %6
%55 = load i8*, i8** %7
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 10
%59 = zext i1 %58 to i64
%60 = select i1 %58, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0)
%61 = call i32 @fprintf(i32* %54, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %60)
%62 = load i8*, i8** %7
%63 = getelementptr inbounds i8, i8* %62, i32 1
store i8* %63, i8** %7
%64 = load i32, i32* %12
%65 = add nsw i32 %64, -1
store i32 %65, i32* %12
br label %28
66: ; preds = %48, %45
%67 = load i32, i32* %12
%68 = icmp eq i32 %67, 0
br i1 %68, label %69, label %74
69: ; preds = %66
%70 = load i32*, i32** %6
%71 = load i32, i32* %9
%72 = call i32 @carriage_return(i32* %70, i32 %71)
%73 = load i32, i32* %13
store i32 %73, i32* %12
br label %28
74: ; preds = %66
%75 = load i8*, i8** %7
%76 = load i32, i32* %12
%77 = call i8* @goto_nextstop(i8* %75, i32 %76)
store i8* %77, i8** %11
%78 = load i8*, i8** %11
%79 = load i8, i8* %78
%80 = call i64 @iswordbreak(i8 signext %79)
%81 = icmp ne i64 %80, 0
br i1 %81, label %82, label %97
82: ; preds = %74
%83 = load i32*, i32** %6
%84 = load i8*, i8** %7
%85 = load i8*, i8** %11
%86 = call i8* @print_stretch(i32* %83, i8* %84, i8* %85)
store i8* %86, i8** %14
%87 = load i8*, i8** %11
%88 = load i8*, i8** %7
%89 = ptrtoint i8* %87 to i64
%90 = ptrtoint i8* %88 to i64
%91 = sub i64 %89, %90
%92 = load i32, i32* %12
%93 = sext i32 %92 to i64
%94 = sub nsw i64 %93, %91
%95 = trunc i64 %94 to i32
store i32 %95, i32* %12
%96 = load i8*, i8** %14
store i8* %96, i8** %7
br label %124
97: ; preds = %74
%98 = load i8*, i8** %7
%99 = load i32, i32* %13
%100 = call i64 @fit_horizontally(i8* %98, i32 %99)
%101 = icmp ne i64 %100, 0
br i1 %101, label %102, label %107
102: ; preds = %97
%103 = load i32*, i32** %6
%104 = load i32, i32* %9
%105 = call i32 @carriage_return(i32* %103, i32 %104)
%106 = load i32, i32* %13
store i32 %106, i32* %12
br label %123
107: ; preds = %97
%108 = load i32*, i32** %6
%109 = load i8*, i8** %7
%110 = load i8*, i8** %7
%111 = load i32, i32* %12
%112 = sext i32 %111 to i64
%113 = getelementptr inbounds i8, i8* %110, i64 %112
%114 = call i8* @print_stretch(i32* %108, i8* %109, i8* %113)
%115 = load i32, i32* %12
%116 = load i8*, i8** %7
%117 = sext i32 %115 to i64
%118 = getelementptr inbounds i8, i8* %116, i64 %117
store i8* %118, i8** %7
%119 = load i32, i32* %13
store i32 %119, i32* %12
%120 = load i32*, i32** %6
%121 = load i32, i32* %9
%122 = call i32 @carriage_return(i32* %120, i32 %121)
br label %123
123: ; preds = %107, %102
br label %124
124: ; preds = %123, %82
br label %28
125: ; preds = %28
ret void
}

LLVM IR (-O2):
define void @fprint_justified(i32* %0, i8* %1, i32 %2, i32 %3, i32 %4) {
%6 = xor i32 %3, -1
%7 = lshr i32 %6, 31
%8 = tail call i32 @assert(i32 %7)
%9 = icmp sgt i32 %4, 0
%10 = zext i1 %9 to i32
%11 = tail call i32 @assert(i32 %10)
%12 = tail call i32 @carriage(i32* %0, i32 %2)
%13 = load i8, i8* %1
%14 = icmp eq i8 %13, 0
br i1 %14, label %75, label %15
15: ; preds = %5, %44
%16 = phi i8 [ %47, %44 ], [ %13, %5 ]
%17 = phi i8* [ %46, %44 ], [ %1, %5 ]
%18 = phi i32 [ %45, %44 ], [ %4, %5 ]
%19 = icmp eq i32 %18, %4
br label %20
20: ; preds = %15, %26
%21 = phi i8 [ %16, %15 ], [ %28, %26 ]
%22 = phi i8* [ %17, %15 ], [ %27, %26 ]
br i1 %19, label %23, label %30
23: ; preds = %20
%24 = tail call i64 @isspace(i8 signext %21)
%25 = icmp eq i64 %24, 0
br i1 %25, label %30, label %26
26: ; preds = %23
%27 = getelementptr inbounds i8, i8* %22, i64 1
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 0
br i1 %29, label %75, label %20
30: ; preds = %23, %20
%31 = phi i8* [ %22, %23 ], [ %17, %20 ]
%32 = icmp sgt i32 %18, 0
br i1 %32, label %33, label %49
33: ; preds = %30
%34 = load i8, i8* %31
%35 = tail call i64 @isspace(i8 signext %34)
%36 = icmp eq i64 %35, 0
br i1 %36, label %53, label %37
37: ; preds = %33
%38 = load i8, i8* %31
%39 = icmp eq i8 %38, 10
%40 = select i1 %39, i32 10, i32 32
%41 = tail call i32 @fputc(i32 %40, i32* %0)
%42 = getelementptr inbounds i8, i8* %31, i64 1
%43 = add nsw i32 %18, -1
br label %44
44: ; preds = %58, %70, %68, %37, %51
%45 = phi i32 [ %4, %51 ], [ %43, %37 ], [ %64, %58 ], [ %4, %68 ], [ %4, %70 ]
%46 = phi i8* [ %31, %51 ], [ %42, %37 ], [ %59, %58 ], [ %31, %68 ], [ %72, %70 ]
%47 = load i8, i8* %46
%48 = icmp eq i8 %47, 0
br i1 %48, label %75, label %15
49: ; preds = %30
%50 = icmp eq i32 %18, 0
br i1 %50, label %51, label %53
51: ; preds = %49
%52 = tail call i32 @carriage_return(i32* %0, i32 %3)
br label %44
53: ; preds = %33, %49
%54 = tail call i8* @goto_nextstop(i8* %31, i32 %18)
%55 = load i8, i8* %54
%56 = tail call i64 @iswordbreak(i8 signext %55)
%57 = icmp eq i64 %56, 0
br i1 %57, label %65, label %58
58: ; preds = %53
%59 = tail call i8* @print_stretch(i32* %0, i8* %31, i8* %54)
%60 = ptrtoint i8* %54 to i64
%61 = ptrtoint i8* %31 to i64
%62 = sub i64 %61, %60
%63 = trunc i64 %62 to i32
%64 = add i32 %18, %63
br label %44
65: ; preds = %53
%66 = tail call i64 @fit_horizontally(i8* %31, i32 %4)
%67 = icmp eq i64 %66, 0
br i1 %67, label %70, label %68
68: ; preds = %65
%69 = tail call i32 @carriage_return(i32* %0, i32 %3)
br label %44
70: ; preds = %65
%71 = sext i32 %18 to i64
%72 = getelementptr inbounds i8, i8* %31, i64 %71
%73 = tail call i8* @print_stretch(i32* %0, i8* %31, i8* %72)
%74 = tail call i32 @carriage_return(i32* %0, i32 %3)
br label %44
75: ; preds = %44, %26, %5
ret void
}