#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 */
struct Mx1 {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ Mx1_report_error(struct Mx1 *, char *, int /*<<< orphan*/);
int Mx1_substitute(struct Mx1 *, char *, char *, int /*<<< orphan*/, int, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ strncpy(char *, char *, int);
int Mx1_rewrap(struct Mx1 *m, char **read_pt, char **write_pt, char *write_base,
               char *envelope, char *front, char *back, int flag) {
  char *rpt, *wpt, *ept, content[256], msg[256];
  int l, ret;
  rpt = *read_pt;
  wpt = *write_pt;
  ept = strchr(rpt, '}');
  if (ept == NULL) {
    sprintf(msg, "No closing bracket found for '%s'", envelope);
    Mx1_report_error(m, msg, 0);
    return (-1);
  }
  l = ept - rpt;
  if (flag & 2)
    l = 0;
  if (l > 0) {
    ret = Mx1_substitute(m, rpt, content, 0, l, 1);
    if (ret <= 0)
      return (ret);
    l = strlen(content);
  }
  if ((wpt - write_base) + l + strlen(front) + strlen(back) > 255) {
    Mx1_report_error(m, "Line length overflow while text substitution", 0);
    return (-1);
  }
  strcpy(wpt, front);
  wpt += strlen(front);
  if (l > 0)
    strncpy(wpt, content, l);
  wpt += l;
  strcpy(wpt, back);
  wpt += strlen(back);
  (*read_pt) += ept - rpt;
  (*write_pt) = wpt;
  return (1);
}


x86 assembly (clang, -O0):
<Mx1_rewrap>:
push   %rbp
mov    %rsp,%rbp
sub    $0x270,%rsp
mov    0x18(%rbp),%eax
mov    0x10(%rbp),%rax
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
mov    %r9,-0x38(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x48(%rbp)
mov    -0x40(%rbp),%rdi
mov    $0x7d,%esi
call   1070 <strchr@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1202 <Mx1_rewrap+0x92>
lea    -0x250(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xe21(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x250(%rbp),%rsi
xor    %edx,%edx
call   1080 <Mx1_report_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b6 <Mx1_rewrap+0x246>
mov    -0x50(%rbp),%rax
mov    -0x40(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,-0x254(%rbp)
mov    0x18(%rbp),%eax
and    $0x2,%eax
cmp    $0x0,%eax
je     122c <Mx1_rewrap+0xbc>
movl   $0x0,-0x254(%rbp)
cmpl   $0x0,-0x254(%rbp)
jle    128f <Mx1_rewrap+0x11f>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rsi
lea    -0x150(%rbp),%rdx
mov    -0x254(%rbp),%r8d
xor    %ecx,%ecx
mov    $0x1,%r9d
call   1030 <Mx1_substitute@plt>
mov    %eax,-0x258(%rbp)
cmpl   $0x0,-0x258(%rbp)
jg     127d <Mx1_rewrap+0x10d>
mov    -0x258(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13b6 <Mx1_rewrap+0x246>
lea    -0x150(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,-0x254(%rbp)
mov    -0x48(%rbp),%rax
mov    -0x28(%rbp),%rcx
sub    %rcx,%rax
movslq -0x254(%rbp),%rcx
add    %rcx,%rax
mov    %rax,-0x268(%rbp)
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x268(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x260(%rbp)
mov    0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x260(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
cmp    $0xff,%rax
jle    130c <Mx1_rewrap+0x19c>
mov    -0x10(%rbp),%rdi
lea    0xd29(%rip),%rsi
xor    %edx,%edx
call   1080 <Mx1_report_error@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b6 <Mx1_rewrap+0x246>
mov    -0x48(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1050 <strcpy@plt>
mov    -0x38(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
cmpl   $0x0,-0x254(%rbp)
jle    1355 <Mx1_rewrap+0x1e5>
mov    -0x48(%rbp),%rdi
lea    -0x150(%rbp),%rsi
mov    -0x254(%rbp),%edx
call   1040 <strncpy@plt>
mov    -0x254(%rbp),%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    0x10(%rbp),%rsi
call   1050 <strcpy@plt>
mov    0x10(%rbp),%rdi
call   1060 <strlen@plt>
mov    %eax,%ecx
mov    -0x48(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x48(%rbp)
mov    -0x50(%rbp),%rcx
mov    -0x40(%rbp),%rax
sub    %rax,%rcx
mov    -0x18(%rbp),%rax
add    (%rax),%rcx
mov    %rcx,(%rax)
mov    -0x48(%rbp),%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x270,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<Mx1_rewrap>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x128,%rsp
mov    %r9,%r12
mov    %r8,%r14
mov    %rcx,(%rsp)
mov    %rdi,%r13
mov    %rsi,0x10(%rsp)
mov    (%rsi),%rbp
mov    %rdx,0x18(%rsp)
mov    (%rdx),%r15
mov    %rbp,%rdi
mov    $0x7d,%esi
call   1070 <strchr@plt>
test   %rax,%rax
je     1253 <Mx1_rewrap+0xe3>
mov    %rax,%rbx
mov    %r13,0x8(%rsp)
mov    0x168(%rsp),%eax
sub    %rbp,%rbx
xor    %r14d,%r14d
test   $0x2,%al
cmove  %ebx,%r14d
test   %r14d,%r14d
jle    1206 <Mx1_rewrap+0x96>
lea    0x20(%rsp),%rdx
mov    0x8(%rsp),%rdi
mov    %rbp,%rsi
xor    %ecx,%ecx
mov    %r14d,%r8d
mov    $0x1,%r9d
call   1030 <Mx1_substitute@plt>
test   %eax,%eax
jle    12e5 <Mx1_rewrap+0x175>
lea    0x20(%rsp),%rdi
call   1060 <strlen@plt>
mov    %eax,%r14d
mov    %r15,%r13
mov    (%rsp),%rax
mov    %r15,%rbp
sub    %rax,%rbp
movslq %r14d,%rax
mov    %rax,(%rsp)
add    %rax,%rbp
mov    %r12,%rdi
call   1060 <strlen@plt>
movslq %eax,%r15
add    %rbp,%r15
mov    0x160(%rsp),%rdi
call   1060 <strlen@plt>
cltq
add    %r15,%rax
cmp    $0x100,%rax
jl     127e <Mx1_rewrap+0x10e>
lea    0xdd6(%rip),%rsi
mov    0x8(%rsp),%rdi
jmp    1270 <Mx1_rewrap+0x100>
lea    0xda6(%rip),%rsi
lea    0x20(%rsp),%rbx
mov    %rbx,%rdi
mov    %r14,%rdx
call   1090 <sprintf@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %edx,%edx
call   1080 <Mx1_report_error@plt>
mov    $0xffffffff,%eax
jmp    12e5 <Mx1_rewrap+0x175>
mov    %r13,%rdi
mov    %r12,%rsi
call   1050 <strcpy@plt>
mov    %r12,%rdi
call   1060 <strlen@plt>
movslq %eax,%rbp
add    %r13,%rbp
test   %r14d,%r14d
jle    12ac <Mx1_rewrap+0x13c>
lea    0x20(%rsp),%rsi
mov    %rbp,%rdi
mov    %r14d,%edx
call   1040 <strncpy@plt>
add    (%rsp),%rbp
mov    %rbp,%rdi
mov    0x160(%rsp),%r14
mov    %r14,%rsi
call   1050 <strcpy@plt>
mov    %r14,%rdi
call   1060 <strlen@plt>
mov    0x10(%rsp),%rcx
add    %rbx,(%rcx)
cltq
add    %rbp,%rax
mov    0x18(%rsp),%rcx
mov    %rax,(%rcx)
mov    $0x1,%eax
add    $0x128,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.Mx1 = type { i32 }
@.str = [34 x i8] c"No closing bracket found for '%s'\00"
@.str.1 = [45 x i8] c"Line length overflow while text substitution\00"
define i32 @Mx1_rewrap(%struct.Mx1* %0, i8** %1, i8** %2, i8* %3, i8* %4, i8* %5, i8* %6, i32 %7) {
%9 = alloca i32
%10 = alloca %struct.Mx1*
%11 = alloca i8**
%12 = alloca i8**
%13 = alloca i8*
%14 = alloca i8*
%15 = alloca i8*
%16 = alloca i8*
%17 = alloca i32
%18 = alloca i8*
%19 = alloca i8*
%20 = alloca i8*
%21 = alloca [256 x i8]
%22 = alloca [256 x i8]
%23 = alloca i32
%24 = alloca i32
store %struct.Mx1* %0, %struct.Mx1** %10
store i8** %1, i8*** %11
store i8** %2, i8*** %12
store i8* %3, i8** %13
store i8* %4, i8** %14
store i8* %5, i8** %15
store i8* %6, i8** %16
store i32 %7, i32* %17
%25 = load i8**, i8*** %11
%26 = load i8*, i8** %25
store i8* %26, i8** %18
%27 = load i8**, i8*** %12
%28 = load i8*, i8** %27
store i8* %28, i8** %19
%29 = load i8*, i8** %18
%30 = call i8* @strchr(i8* %29, i8 signext 125)
store i8* %30, i8** %20
%31 = load i8*, i8** %20
%32 = icmp eq i8* %31, null
br i1 %32, label %33, label %40
33: ; preds = %8
%34 = getelementptr inbounds [256 x i8], [256 x i8]* %22, i64 0, i64 0
%35 = load i8*, i8** %14
%36 = call i32 @sprintf(i8* %34, i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %35)
%37 = load %struct.Mx1*, %struct.Mx1** %10
%38 = getelementptr inbounds [256 x i8], [256 x i8]* %22, i64 0, i64 0
%39 = call i32 @Mx1_report_error(%struct.Mx1* %37, i8* %38, i32 0)
store i32 -1, i32* %9
br label %127
40: ; preds = %8
%41 = load i8*, i8** %20
%42 = load i8*, i8** %18
%43 = ptrtoint i8* %41 to i64
%44 = ptrtoint i8* %42 to i64
%45 = sub i64 %43, %44
%46 = trunc i64 %45 to i32
store i32 %46, i32* %23
%47 = load i32, i32* %17
%48 = and i32 %47, 2
%49 = icmp ne i32 %48, 0
br i1 %49, label %50, label %51
50: ; preds = %40
store i32 0, i32* %23
br label %51
51: ; preds = %50, %40
%52 = load i32, i32* %23
%53 = icmp sgt i32 %52, 0
br i1 %53, label %54, label %67
54: ; preds = %51
%55 = load %struct.Mx1*, %struct.Mx1** %10
%56 = load i8*, i8** %18
%57 = getelementptr inbounds [256 x i8], [256 x i8]* %21, i64 0, i64 0
%58 = load i32, i32* %23
%59 = call i32 @Mx1_substitute(%struct.Mx1* %55, i8* %56, i8* %57, i32 0, i32 %58, i32 1)
store i32 %59, i32* %24
%60 = load i32, i32* %24
%61 = icmp sle i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %54
%63 = load i32, i32* %24
store i32 %63, i32* %9
br label %127
64: ; preds = %54
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %21, i64 0, i64 0
%66 = call i32 @strlen(i8* %65)
store i32 %66, i32* %23
br label %67
67: ; preds = %64, %51
%68 = load i8*, i8** %19
%69 = load i8*, i8** %13
%70 = ptrtoint i8* %68 to i64
%71 = ptrtoint i8* %69 to i64
%72 = sub i64 %70, %71
%73 = load i32, i32* %23
%74 = sext i32 %73 to i64
%75 = add nsw i64 %72, %74
%76 = load i8*, i8** %15
%77 = call i32 @strlen(i8* %76)
%78 = sext i32 %77 to i64
%79 = add nsw i64 %75, %78
%80 = load i8*, i8** %16
%81 = call i32 @strlen(i8* %80)
%82 = sext i32 %81 to i64
%83 = add nsw i64 %79, %82
%84 = icmp sgt i64 %83, 255
br i1 %84, label %85, label %88
85: ; preds = %67
%86 = load %struct.Mx1*, %struct.Mx1** %10
%87 = call i32 @Mx1_report_error(%struct.Mx1* %86, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 0)
store i32 -1, i32* %9
br label %127
88: ; preds = %67
%89 = load i8*, i8** %19
%90 = load i8*, i8** %15
%91 = call i32 @strcpy(i8* %89, i8* %90)
%92 = load i8*, i8** %15
%93 = call i32 @strlen(i8* %92)
%94 = load i8*, i8** %19
%95 = sext i32 %93 to i64
%96 = getelementptr inbounds i8, i8* %94, i64 %95
store i8* %96, i8** %19
%97 = load i32, i32* %23
%98 = icmp sgt i32 %97, 0
br i1 %98, label %99, label %104
99: ; preds = %88
%100 = load i8*, i8** %19
%101 = getelementptr inbounds [256 x i8], [256 x i8]* %21, i64 0, i64 0
%102 = load i32, i32* %23
%103 = call i32 @strncpy(i8* %100, i8* %101, i32 %102)
br label %104
104: ; preds = %99, %88
%105 = load i32, i32* %23
%106 = load i8*, i8** %19
%107 = sext i32 %105 to i64
%108 = getelementptr inbounds i8, i8* %106, i64 %107
store i8* %108, i8** %19
%109 = load i8*, i8** %19
%110 = load i8*, i8** %16
%111 = call i32 @strcpy(i8* %109, i8* %110)
%112 = load i8*, i8** %16
%113 = call i32 @strlen(i8* %112)
%114 = load i8*, i8** %19
%115 = sext i32 %113 to i64
%116 = getelementptr inbounds i8, i8* %114, i64 %115
store i8* %116, i8** %19
%117 = load i8*, i8** %20
%118 = load i8*, i8** %18
%119 = ptrtoint i8* %117 to i64
%120 = ptrtoint i8* %118 to i64
%121 = sub i64 %119, %120
%122 = load i8**, i8*** %11
%123 = load i8*, i8** %122
%124 = getelementptr inbounds i8, i8* %123, i64 %121
store i8* %124, i8** %122
%125 = load i8*, i8** %19
%126 = load i8**, i8*** %12
store i8* %125, i8** %126
store i32 1, i32* %9
br label %127
127: ; preds = %104, %85, %62, %33
%128 = load i32, i32* %9
ret i32 %128
}

LLVM IR (-O2):
%struct.Mx1 = type { i32 }
@.str = [34 x i8] c"No closing bracket found for '%s'\00"
@.str.1 = [45 x i8] c"Line length overflow while text substitution\00"
define i32 @Mx1_rewrap(%struct.Mx1* %0, i8** nocapture %1, i8** nocapture %2, i8* %3, i8* %4, i8* %5, i8* %6, i32 %7) {
%9 = alloca [256 x i8]
%10 = alloca [256 x i8]
%11 = getelementptr inbounds [256 x i8], [256 x i8]* %9, i64 0, i64 0
%12 = getelementptr inbounds [256 x i8], [256 x i8]* %10, i64 0, i64 0
%13 = load i8*, i8** %1
%14 = load i8*, i8** %2
%15 = tail call i8* @strchr(i8* dereferenceable(1) %13, i8 signext 125)
%16 = icmp eq i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %8
%18 = call i32 @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str, i64 0, i64 0), i8* %4)
%19 = call i32 @Mx1_report_error(%struct.Mx1* %0, i8* %12, i32 0)
br label %66
20: ; preds = %8
%21 = ptrtoint i8* %15 to i64
%22 = ptrtoint i8* %13 to i64
%23 = sub i64 %21, %22
%24 = trunc i64 %23 to i32
%25 = and i32 %7, 2
%26 = icmp eq i32 %25, 0
%27 = select i1 %26, i32 %24, i32 0
%28 = icmp sgt i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %20
%30 = call i32 @Mx1_substitute(%struct.Mx1* %0, i8* %13, i8* %11, i32 0, i32 %27, i32 1)
%31 = icmp slt i32 %30, 1
br i1 %31, label %66, label %32
32: ; preds = %29
%33 = call i32 @strlen(i8* %11)
br label %34
34: ; preds = %32, %20
%35 = phi i32 [ %33, %32 ], [ %27, %20 ]
%36 = ptrtoint i8* %14 to i64
%37 = ptrtoint i8* %3 to i64
%38 = sub i64 %36, %37
%39 = sext i32 %35 to i64
%40 = add nsw i64 %38, %39
%41 = call i32 @strlen(i8* %5)
%42 = sext i32 %41 to i64
%43 = add nsw i64 %40, %42
%44 = call i32 @strlen(i8* %6)
%45 = sext i32 %44 to i64
%46 = add nsw i64 %43, %45
%47 = icmp sgt i64 %46, 255
br i1 %47, label %48, label %50
48: ; preds = %34
%49 = call i32 @Mx1_report_error(%struct.Mx1* %0, i8* getelementptr inbounds ([45 x i8], [45 x i8]* @.str.1, i64 0, i64 0), i32 0)
br label %66
50: ; preds = %34
%51 = call i32 @strcpy(i8* %14, i8* %5)
%52 = call i32 @strlen(i8* %5)
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %14, i64 %53
%55 = icmp sgt i32 %35, 0
br i1 %55, label %56, label %58
56: ; preds = %50
%57 = call i32 @strncpy(i8* %54, i8* %11, i32 %35)
br label %58
58: ; preds = %56, %50
%59 = getelementptr inbounds i8, i8* %54, i64 %39
%60 = call i32 @strcpy(i8* %59, i8* %6)
%61 = call i32 @strlen(i8* %6)
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %59, i64 %62
%64 = load i8*, i8** %1
%65 = getelementptr inbounds i8, i8* %64, i64 %23
store i8* %65, i8** %1
store i8* %63, i8** %2
br label %66
66: ; preds = %29, %58, %48, %17
%67 = phi i32 [ -1, %17 ], [ -1, %48 ], [ 1, %58 ], [ %30, %29 ]
ret i32 %67
}