#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*/ SEEK_SET;
int /*<<< orphan*/ encode_body_base64(int /*<<< orphan*/ *, unsigned char *,
                                      unsigned int);
int /*<<< orphan*/ encode_body_quoted(int /*<<< orphan*/ *, unsigned char *,
                                      unsigned int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
char *get_best_encoding(unsigned char *, unsigned int, char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, unsigned char *, unsigned int);
int /*<<< orphan*/ mystricmp(char *, char *);
int /*<<< orphan*/ *tmpfile();
char *encode_body(unsigned char *buf, unsigned int len, char *content_type,
                  unsigned int *ret_len, char **encoding) {
  char *body = NULL;
  FILE *fh;
  if ((fh = tmpfile())) {
    int body_len;
    if (!mystricmp(content_type, "text/plain")) {
      *encoding = get_best_encoding(buf, len, *encoding);
      if (!(mystricmp(*encoding, "8bit")) || !(mystricmp(*encoding, "7bit"))) {
        if ((body = (char *)malloc(len + 1))) {
          memcpy(body, buf, len);
          body[len] = 0;
          *ret_len = len;
        }
        return body;
      } else {
        encode_body_quoted(fh, buf, len);
        *encoding = "quoted-printable";
      }
    } else {
      encode_body_base64(fh, buf, len);
      *encoding = "base64";
    }
    body_len = ftell(fh);
    fseek(fh, 0, SEEK_SET);
    if ((body = (char *)malloc(body_len + 1))) {
      fread(body, 1, body_len, fh);
      body[body_len] = 0;
      *ret_len = body_len;
    }
    fclose(fh);
  }
  return body;
}


x86 assembly (clang, -O0):
<encode_body>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
mov    %r8,-0x30(%rbp)
movq   $0x0,-0x38(%rbp)
mov    $0x0,%al
call   1070 <tmpfile@plt>
mov    %rax,-0x40(%rbp)
cmp    $0x0,%rax
je     134d <encode_body+0x19d>
mov    -0x20(%rbp),%rdi
lea    0xe0d(%rip),%rsi
call   10a0 <mystricmp@plt>
cmp    $0x0,%eax
jne    12c3 <encode_body+0x113>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%rdx
call   1040 <get_best_encoding@plt>
mov    %rax,%rcx
mov    -0x30(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xddf(%rip),%rsi
call   10a0 <mystricmp@plt>
cmp    $0x0,%eax
je     1256 <encode_body+0xa6>
mov    -0x30(%rbp),%rax
mov    (%rax),%rdi
lea    0xdc8(%rip),%rsi
call   10a0 <mystricmp@plt>
cmp    $0x0,%eax
jne    12a0 <encode_body+0xf0>
mov    -0x14(%rbp),%edi
add    $0x1,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1293 <encode_body+0xe3>
mov    -0x38(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1090 <memcpy@plt>
mov    -0x38(%rbp),%rax
mov    -0x14(%rbp),%ecx
movb   $0x0,(%rax,%rcx,1)
mov    -0x14(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    1355 <encode_body+0x1a5>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   10d0 <encode_body_quoted@plt>
mov    -0x30(%rbp),%rax
lea    0xd5a(%rip),%rcx
mov    %rcx,(%rax)
jmp    12e1 <encode_body+0x131>
mov    -0x40(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <encode_body_base64@plt>
mov    -0x30(%rbp),%rax
lea    0xd48(%rip),%rcx
mov    %rcx,(%rax)
mov    -0x40(%rbp),%rdi
call   1080 <ftell@plt>
mov    %eax,-0x44(%rbp)
mov    -0x40(%rbp),%rdi
mov    0x2ce8(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x44(%rbp),%edi
add    $0x1,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x38(%rbp)
cmp    $0x0,%rax
je     1344 <encode_body+0x194>
mov    -0x38(%rbp),%rdi
mov    -0x44(%rbp),%edx
mov    -0x40(%rbp),%rcx
mov    $0x1,%esi
call   1050 <fread@plt>
mov    -0x38(%rbp),%rax
movslq -0x44(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x44(%rbp),%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x40(%rbp),%rdi
call   1060 <fclose@plt>
mov    -0x38(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_body>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %r8,%r12
mov    %rcx,(%rsp)
mov    %rdx,%rbx
mov    %esi,%r15d
mov    %rdi,%r13
xor    %ebp,%ebp
xor    %eax,%eax
call   1070 <tmpfile@plt>
test   %rax,%rax
je     1265 <encode_body+0xb5>
mov    %rax,%r14
lea    0xe19(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <mystricmp@plt>
test   %eax,%eax
je     1277 <encode_body+0xc7>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   1030 <encode_body_base64@plt>
lea    0xe1a(%rip),%rax
mov    %rax,(%r12)
mov    %r14,%rdi
call   1080 <ftell@plt>
mov    %eax,%ebx
mov    0x2dbf(%rip),%rax
mov    (%rax),%edx
mov    %r14,%rdi
xor    %esi,%esi
call   10c0 <fseek@plt>
lea    0x1(%rbx),%edi
call   10b0 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     125d <encode_body+0xad>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %ebx,%edx
mov    %r14,%rcx
call   1050 <fread@plt>
movslq %ebx,%rax
movb   $0x0,0x0(%rbp,%rax,1)
mov    (%rsp),%rax
mov    %ebx,(%rax)
mov    %r14,%rdi
call   1060 <fclose@plt>
mov    %rbp,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%r12),%rdx
mov    %r13,%rdi
mov    %r15d,%esi
call   1040 <get_best_encoding@plt>
mov    %rax,(%r12)
lea    0xd7a(%rip),%rsi
mov    %rax,%rdi
call   10a0 <mystricmp@plt>
test   %eax,%eax
je     12cb <encode_body+0x11b>
mov    (%r12),%rdi
lea    0xd68(%rip),%rsi
call   10a0 <mystricmp@plt>
test   %eax,%eax
je     12cb <encode_body+0x11b>
mov    %r14,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   10d0 <encode_body_quoted@plt>
lea    0xd4f(%rip),%rax
jmp    120c <encode_body+0x5c>
lea    0x1(%r15),%edi
call   10b0 <malloc@plt>
mov    %rax,%rbp
test   %rax,%rax
je     1265 <encode_body+0xb5>
mov    %rbp,%rdi
mov    %r13,%rsi
mov    %r15d,%edx
call   1090 <memcpy@plt>
mov    %r15d,%eax
movb   $0x0,0x0(%rbp,%rax,1)
mov    (%rsp),%rax
mov    %r15d,(%rax)
jmp    1265 <encode_body+0xb5>

LLVM IR (-O0):
@.str = [11 x i8] c"text/plain\00"
@.str.1 = [5 x i8] c"8bit\00"
@.str.2 = [5 x i8] c"7bit\00"
@.str.3 = [17 x i8] c"quoted-printable\00"
@.str.4 = [7 x i8] c"base64\00"
@SEEK_SET = global i32 0
define i8* @encode_body(i8* %0, i32 %1, i8* %2, i32* %3, i8** %4) {
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32*
%11 = alloca i8**
%12 = alloca i8*
%13 = alloca i32*
%14 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i8* %2, i8** %9
store i32* %3, i32** %10
store i8** %4, i8*** %11
store i8* null, i8** %12
%15 = call i32* (...) @tmpfile()
store i32* %15, i32** %13
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %94
17: ; preds = %5
%18 = load i8*, i8** %9
%19 = call i32 @mystricmp(i8* %18, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%20 = icmp ne i32 %19, 0
br i1 %20, label %63, label %21
21: ; preds = %17
%22 = load i8*, i8** %7
%23 = load i32, i32* %8
%24 = load i8**, i8*** %11
%25 = load i8*, i8** %24
%26 = call i8* @get_best_encoding(i8* %22, i32 %23, i8* %25)
%27 = load i8**, i8*** %11
store i8* %26, i8** %27
%28 = load i8**, i8*** %11
%29 = load i8*, i8** %28
%30 = call i32 @mystricmp(i8* %29, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%31 = icmp ne i32 %30, 0
br i1 %31, label %32, label %37
32: ; preds = %21
%33 = load i8**, i8*** %11
%34 = load i8*, i8** %33
%35 = call i32 @mystricmp(i8* %34, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%36 = icmp ne i32 %35, 0
br i1 %36, label %56, label %37
37: ; preds = %32, %21
%38 = load i32, i32* %8
%39 = add i32 %38, 1
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %12
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %54
43: ; preds = %37
%44 = load i8*, i8** %12
%45 = load i8*, i8** %7
%46 = load i32, i32* %8
%47 = call i32 @memcpy(i8* %44, i8* %45, i32 %46)
%48 = load i8*, i8** %12
%49 = load i32, i32* %8
%50 = zext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
%52 = load i32, i32* %8
%53 = load i32*, i32** %10
store i32 %52, i32* %53
br label %54
54: ; preds = %43, %37
%55 = load i8*, i8** %12
store i8* %55, i8** %6
br label %96
56: ; preds = %32
%57 = load i32*, i32** %13
%58 = load i8*, i8** %7
%59 = load i32, i32* %8
%60 = call i32 @encode_body_quoted(i32* %57, i8* %58, i32 %59)
%61 = load i8**, i8*** %11
store i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), i8** %61
br label %62
62: ; preds = %56
br label %69
63: ; preds = %17
%64 = load i32*, i32** %13
%65 = load i8*, i8** %7
%66 = load i32, i32* %8
%67 = call i32 @encode_body_base64(i32* %64, i8* %65, i32 %66)
%68 = load i8**, i8*** %11
store i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), i8** %68
br label %69
69: ; preds = %63, %62
%70 = load i32*, i32** %13
%71 = call i32 @ftell(i32* %70)
store i32 %71, i32* %14
%72 = load i32*, i32** %13
%73 = load i32, i32* @SEEK_SET
%74 = call i32 @fseek(i32* %72, i32 0, i32 %73)
%75 = load i32, i32* %14
%76 = add nsw i32 %75, 1
%77 = call i64 @malloc(i32 %76)
%78 = inttoptr i64 %77 to i8*
store i8* %78, i8** %12
%79 = icmp ne i8* %78, null
br i1 %79, label %80, label %91
80: ; preds = %69
%81 = load i8*, i8** %12
%82 = load i32, i32* %14
%83 = load i32*, i32** %13
%84 = call i32 @fread(i8* %81, i32 1, i32 %82, i32* %83)
%85 = load i8*, i8** %12
%86 = load i32, i32* %14
%87 = sext i32 %86 to i64
%88 = getelementptr inbounds i8, i8* %85, i64 %87
store i8 0, i8* %88
%89 = load i32, i32* %14
%90 = load i32*, i32** %10
store i32 %89, i32* %90
br label %91
91: ; preds = %80, %69
%92 = load i32*, i32** %13
%93 = call i32 @fclose(i32* %92)
br label %94
94: ; preds = %91, %5
%95 = load i8*, i8** %12
store i8* %95, i8** %6
br label %96
96: ; preds = %94, %54
%97 = load i8*, i8** %6
ret i8* %97
}

LLVM IR (-O2):
@.str = [11 x i8] c"text/plain\00"
@.str.1 = [5 x i8] c"8bit\00"
@.str.2 = [5 x i8] c"7bit\00"
@.str.3 = [17 x i8] c"quoted-printable\00"
@.str.4 = [7 x i8] c"base64\00"
@SEEK_SET = global i32 0
define i8* @encode_body(i8* %0, i32 %1, i8* %2, i32* nocapture writeonly %3, i8** nocapture %4) {
%6 = tail call i32* (...) @tmpfile()
%7 = icmp eq i32* %6, null
br i1 %7, label %48, label %8
8: ; preds = %5
%9 = tail call i32 @mystricmp(i8* %2, i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0))
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %31
11: ; preds = %8
%12 = load i8*, i8** %4
%13 = tail call i8* @get_best_encoding(i8* %0, i32 %1, i8* %12)
store i8* %13, i8** %4
%14 = tail call i32 @mystricmp(i8* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%15 = icmp eq i32 %14, 0
br i1 %15, label %20, label %16
16: ; preds = %11
%17 = load i8*, i8** %4
%18 = tail call i32 @mystricmp(i8* %17, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i32 %18, 0
br i1 %19, label %20, label %29
20: ; preds = %16, %11
%21 = add i32 %1, 1
%22 = tail call i64 @malloc(i32 %21)
%23 = inttoptr i64 %22 to i8*
%24 = icmp eq i64 %22, 0
br i1 %24, label %48, label %25
25: ; preds = %20
%26 = tail call i32 @memcpy(i8* %23, i8* %0, i32 %1)
%27 = zext i32 %1 to i64
%28 = getelementptr inbounds i8, i8* %23, i64 %27
store i8 0, i8* %28
store i32 %1, i32* %3
br label %48
29: ; preds = %16
%30 = tail call i32 @encode_body_quoted(i32* %6, i8* %0, i32 %1)
br label %33
31: ; preds = %8
%32 = tail call i32 @encode_body_base64(i32* %6, i8* %0, i32 %1)
br label %33
33: ; preds = %31, %29
%34 = phi i8* [ getelementptr inbounds ([17 x i8], [17 x i8]* @.str.3, i64 0, i64 0), %29 ], [ getelementptr inbounds ([7 x i8], [7 x i8]* @.str.4, i64 0, i64 0), %31 ]
store i8* %34, i8** %4
%35 = tail call i32 @ftell(i32* %6)
%36 = load i32, i32* @SEEK_SET
%37 = tail call i32 @fseek(i32* %6, i32 0, i32 %36)
%38 = add nsw i32 %35, 1
%39 = tail call i64 @malloc(i32 %38)
%40 = inttoptr i64 %39 to i8*
%41 = icmp eq i64 %39, 0
br i1 %41, label %46, label %42
42: ; preds = %33
%43 = tail call i32 @fread(i8* %40, i32 1, i32 %35, i32* %6)
%44 = sext i32 %35 to i64
%45 = getelementptr inbounds i8, i8* %40, i64 %44
store i8 0, i8* %45
store i32 %35, i32* %3
br label %46
46: ; preds = %33, %42
%47 = tail call i32 @fclose(i32* %6)
br label %48
48: ; preds = %20, %25, %5, %46
%49 = phi i8* [ %40, %46 ], [ null, %5 ], [ %23, %25 ], [ %23, %20 ]
ret i8* %49
}