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

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int /*<<< orphan*/ fwrite(unsigned char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ sprintf(char *, char *, unsigned char);
void encode_body_quoted(FILE *fh, unsigned char *buf, unsigned int len) {
  int line_len = 0;
  while (len > 0) {
    unsigned char *next_str = buf;
    unsigned char c = *next_str;
    char digit_buf[16];
    int next_len = 1;
    if ((c < 33 || c == 61 || c > 126) && c != 10) {
      sprintf(digit_buf, "=%02X", c);
      next_str = (unsigned char *)digit_buf;
      next_len = 3;
    }
    if (line_len + next_len > 75) {
      if (c != 10)
        fprintf(fh, "=\n");
      else {
        fprintf(fh, "\n");
        next_len = 0;
      }
      line_len = 0;
    }
    if (next_len) {
      fwrite(next_str, 1, next_len, fh);
      line_len += next_len;
    }
    if (c == 10)
      line_len = 0;
    buf++;
    len--;
  }
}


x86 assembly (clang, -O0):
<encode_body_quoted>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x14(%rbp)
jbe    1268 <encode_body_quoted+0x138>
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    (%rax),%al
mov    %al,-0x21(%rbp)
movl   $0x1,-0x44(%rbp)
movzbl -0x21(%rbp),%eax
cmp    $0x21,%eax
jl     1193 <encode_body_quoted+0x63>
movzbl -0x21(%rbp),%eax
cmp    $0x3d,%eax
je     1193 <encode_body_quoted+0x63>
movzbl -0x21(%rbp),%eax
cmp    $0x7e,%eax
jle    11c3 <encode_body_quoted+0x93>
movzbl -0x21(%rbp),%eax
cmp    $0xa,%eax
je     11c3 <encode_body_quoted+0x93>
lea    -0x40(%rbp),%rdi
lea    0xe55(%rip),%rsi
movzbl -0x21(%rbp),%edx
call   1040 <sprintf@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x20(%rbp)
movl   $0x3,-0x44(%rbp)
mov    -0x18(%rbp),%eax
add    -0x44(%rbp),%eax
cmp    $0x4b,%eax
jle    1212 <encode_body_quoted+0xe2>
movzbl -0x21(%rbp),%eax
cmp    $0xa,%eax
je     11f4 <encode_body_quoted+0xc4>
mov    -0x8(%rbp),%rdi
lea    0xe1c(%rip),%rsi
call   1030 <fprintf@plt>
jmp    120b <encode_body_quoted+0xdb>
mov    -0x8(%rbp),%rdi
lea    0xe08(%rip),%rsi
call   1030 <fprintf@plt>
movl   $0x0,-0x44(%rbp)
movl   $0x0,-0x18(%rbp)
cmpl   $0x0,-0x44(%rbp)
je     123a <encode_body_quoted+0x10a>
mov    -0x20(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x8(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fwrite@plt>
mov    -0x44(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
movzbl -0x21(%rbp),%eax
cmp    $0xa,%eax
jne    124e <encode_body_quoted+0x11e>
movl   $0x0,-0x18(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    114a <encode_body_quoted+0x1a>
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_body_quoted>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rdi,0x8(%rsp)
test   %edx,%edx
je     122a <encode_body_quoted+0x10a>
mov    %edx,%ebp
mov    %rsi,%rbx
xor    %r14d,%r14d
jmp    11a0 <encode_body_quoted+0x80>
cs nopw 0x0(%rax,%rax,1)
nop
mov    $0x2,%esi
mov    $0x1,%edx
lea    0xea5(%rip),%rdi
mov    0x8(%rsp),%rcx
call   1040 <fwrite@plt>
xor    %r14d,%r14d
mov    %r15,%rdi
mov    $0x1,%esi
mov    %r13d,%edx
mov    0x8(%rsp),%rcx
call   1040 <fwrite@plt>
add    %r13d,%r14d
cmp    $0xa,%r12b
mov    $0x0,%eax
cmove  %eax,%r14d
add    $0x1,%rbx
add    $0xffffffff,%ebp
je     122a <encode_body_quoted+0x10a>
movzbl (%rbx),%r12d
cmp    $0x3d,%r12b
sete   %al
lea    -0x7f(%r12),%ecx
cmp    $0xa2,%cl
setb   %cl
mov    %rbx,%r15
mov    $0x1,%r13d
cmp    $0xa,%r12b
je     11f0 <encode_body_quoted+0xd0>
or     %cl,%al
mov    %rbx,%r15
mov    $0x1,%r13d
je     11f0 <encode_body_quoted+0xd0>
movzbl %r12b,%edx
lea    0x10(%rsp),%r15
mov    %r15,%rdi
lea    0xe1b(%rip),%rsi
call   1030 <sprintf@plt>
mov    $0x3,%r13d
lea    (%r14,%r13,1),%eax
cmp    $0x4c,%eax
jl     116e <encode_body_quoted+0x4e>
cmp    $0xa,%r12b
jne    1150 <encode_body_quoted+0x30>
mov    $0x1,%esi
mov    $0x1,%edx
lea    0xdef(%rip),%rdi
mov    0x8(%rsp),%rcx
call   1040 <fwrite@plt>
xor    %r14d,%r14d
jmp    1186 <encode_body_quoted+0x66>
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [6 x i8] c"=%02X\00"
@.str.1 = [3 x i8] c"=\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @encode_body_quoted(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8
%10 = alloca [16 x i8]
%11 = alloca i32
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %7
br label %12
12: ; preds = %71, %3
%13 = load i32, i32* %6
%14 = icmp ugt i32 %13, 0
br i1 %14, label %15, label %76
15: ; preds = %12
%16 = load i8*, i8** %5
store i8* %16, i8** %8
%17 = load i8*, i8** %8
%18 = load i8, i8* %17
store i8 %18, i8* %9
store i32 1, i32* %11
%19 = load i8, i8* %9
%20 = zext i8 %19 to i32
%21 = icmp slt i32 %20, 33
br i1 %21, label %30, label %22
22: ; preds = %15
%23 = load i8, i8* %9
%24 = zext i8 %23 to i32
%25 = icmp eq i32 %24, 61
br i1 %25, label %30, label %26
26: ; preds = %22
%27 = load i8, i8* %9
%28 = zext i8 %27 to i32
%29 = icmp sgt i32 %28, 126
br i1 %29, label %30, label %39
30: ; preds = %26, %22, %15
%31 = load i8, i8* %9
%32 = zext i8 %31 to i32
%33 = icmp ne i32 %32, 10
br i1 %33, label %34, label %39
34: ; preds = %30
%35 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
%36 = load i8, i8* %9
%37 = call i32 @sprintf(i8* %35, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %36)
%38 = getelementptr inbounds [16 x i8], [16 x i8]* %10, i64 0, i64 0
store i8* %38, i8** %8
store i32 3, i32* %11
br label %39
39: ; preds = %34, %30, %26
%40 = load i32, i32* %7
%41 = load i32, i32* %11
%42 = add nsw i32 %40, %41
%43 = icmp sgt i32 %42, 75
br i1 %43, label %44, label %55
44: ; preds = %39
%45 = load i8, i8* %9
%46 = zext i8 %45 to i32
%47 = icmp ne i32 %46, 10
br i1 %47, label %48, label %51
48: ; preds = %44
%49 = load i32*, i32** %4
%50 = call i32 @fprintf(i32* %49, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0))
br label %54
51: ; preds = %44
%52 = load i32*, i32** %4
%53 = call i32 @fprintf(i32* %52, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %11
br label %54
54: ; preds = %51, %48
store i32 0, i32* %7
br label %55
55: ; preds = %54, %39
%56 = load i32, i32* %11
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %66
58: ; preds = %55
%59 = load i8*, i8** %8
%60 = load i32, i32* %11
%61 = load i32*, i32** %4
%62 = call i32 @fwrite(i8* %59, i32 1, i32 %60, i32* %61)
%63 = load i32, i32* %11
%64 = load i32, i32* %7
%65 = add nsw i32 %64, %63
store i32 %65, i32* %7
br label %66
66: ; preds = %58, %55
%67 = load i8, i8* %9
%68 = zext i8 %67 to i32
%69 = icmp eq i32 %68, 10
br i1 %69, label %70, label %71
70: ; preds = %66
store i32 0, i32* %7
br label %71
71: ; preds = %70, %66
%72 = load i8*, i8** %5
%73 = getelementptr inbounds i8, i8* %72, i32 1
store i8* %73, i8** %5
%74 = load i32, i32* %6
%75 = add i32 %74, -1
store i32 %75, i32* %6
br label %12
76: ; preds = %12
ret void
}

LLVM IR (-O2):
@.str = [6 x i8] c"=%02X\00"
@.str.1 = [3 x i8] c"=\0A\00"
@.str.2 = [2 x i8] c"\0A\00"
define void @encode_body_quoted(i32* %0, i8* nocapture %1, i32 %2) {
%4 = alloca [16 x i8]
%5 = icmp eq i32 %2, 0
br i1 %5, label %42, label %6
6: ; preds = %3
%7 = getelementptr inbounds [16 x i8], [16 x i8]* %4, i64 0, i64 0
br label %8
8: ; preds = %6, %35
%9 = phi i8* [ %1, %6 ], [ %39, %35 ]
%10 = phi i32 [ 0, %6 ], [ %38, %35 ]
%11 = phi i32 [ %2, %6 ], [ %40, %35 ]
%12 = load i8, i8* %9
%13 = icmp eq i8 %12, 61
%14 = add i8 %12, -127
%15 = icmp ult i8 %14, -94
%16 = or i1 %13, %15
%17 = icmp ne i8 %12, 10
%18 = and i1 %17, %16
br i1 %18, label %19, label %21
19: ; preds = %8
%20 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0), i8 zeroext %12)
br label %21
21: ; preds = %8, %19
%22 = phi i8* [ %7, %19 ], [ %9, %8 ]
%23 = phi i32 [ 3, %19 ], [ 1, %8 ]
%24 = add nsw i32 %23, %10
%25 = icmp sgt i32 %24, 75
br i1 %25, label %26, label %31
26: ; preds = %21
br i1 %17, label %27, label %29
27: ; preds = %26
%28 = tail call i64 bitcast (i32 (i8*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i64 2, i64 1, i32* %0)
br label %31
29: ; preds = %26
%30 = tail call i64 bitcast (i32 (i8*, i32, i32, i32*)* @fwrite to i64 (i8*, i64, i64, i32*)*)(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i64 1, i64 1, i32* %0)
br label %35
31: ; preds = %21, %27
%32 = phi i32 [ 0, %27 ], [ %10, %21 ]
%33 = call i32 @fwrite(i8* %22, i32 1, i32 %23, i32* %0)
%34 = add nsw i32 %32, %23
br label %35
35: ; preds = %29, %31
%36 = phi i32 [ %34, %31 ], [ 0, %29 ]
%37 = icmp eq i8 %12, 10
%38 = select i1 %37, i32 0, i32 %36
%39 = getelementptr inbounds i8, i8* %9, i64 1
%40 = add i32 %11, -1
%41 = icmp eq i32 %40, 0
br i1 %41, label %42, label %8
42: ; preds = %35, %3
ret void
}