#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;
char *encode_header_str(char const *, int *, int /*<<< orphan*/);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *);
int /*<<< orphan*/ fputc(char, int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ free(char *);
int /*<<< orphan*/ fseek(int /*<<< orphan*/ *, int /*<<< orphan*/,
                         int /*<<< orphan*/);
int ftell(int /*<<< orphan*/ *);
scalar_t__ malloc(int);
int strlen(char const *);
int /*<<< orphan*/ *tmpfile();
char *encode_header_field(const char *field_name, const char *field_contents) {
  char *header = NULL;
  int line_len;
  FILE *fh;
  if ((fh = tmpfile())) {
    char *encoded;
    int header_len;
    fprintf(fh, "%s: ", field_name);
    line_len = strlen(field_name) + 2;
    if ((encoded = encode_header_str(field_contents, &line_len, 0))) {
      fprintf(fh, "%s", encoded);
      free(encoded);
    }
    fputc('\n', fh);
    if ((header_len = ftell(fh))) {
      fseek(fh, 0, SEEK_SET);
      if ((header = (char *)malloc(header_len + 1))) {
        fread(header, 1, header_len, fh);
        header[header_len] = 0;
      }
    }
    fclose(fh);
  }
  return header;
}


x86 assembly (clang, -O0):
<encode_header_field>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
mov    $0x0,%al
call   1080 <tmpfile@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     12b9 <encode_header_field+0x109>
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe14(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x8(%rbp),%rdi
call   1060 <strlen@plt>
add    $0x2,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x1c(%rbp),%rsi
xor    %edx,%edx
call   10d0 <encode_header_str@plt>
mov    %rax,-0x30(%rbp)
cmp    $0x0,%rax
je     123a <encode_header_field+0x8a>
mov    -0x28(%rbp),%rdi
mov    -0x30(%rbp),%rdx
lea    0xdd9(%rip),%rsi
call   1090 <fprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <free@plt>
mov    -0x28(%rbp),%rsi
mov    $0xa,%edi
call   1070 <fputc@plt>
mov    -0x28(%rbp),%rdi
call   10a0 <ftell@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
je     12b0 <encode_header_field+0x100>
mov    -0x28(%rbp),%rdi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
xor    %esi,%esi
call   10c0 <fseek@plt>
mov    -0x34(%rbp),%edi
add    $0x1,%edi
call   10b0 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
je     12ab <encode_header_field+0xfb>
mov    -0x18(%rbp),%rdi
mov    -0x34(%rbp),%edx
mov    -0x28(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    -0x18(%rbp),%rax
movslq -0x34(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    12b0 <encode_header_field+0x100>
mov    -0x28(%rbp),%rdi
call   1050 <fclose@plt>
mov    -0x18(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_header_field>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
mov    %rdi,%rbx
xor    %r14d,%r14d
xor    %eax,%eax
call   1090 <tmpfile@plt>
test   %rax,%rax
je     129c <encode_header_field+0xdc>
mov    %rax,%r12
lea    0xe15(%rip),%rsi
mov    %rax,%rdi
mov    %rbx,%rdx
call   10a0 <fprintf@plt>
mov    %rbx,%rdi
call   1060 <strlen@plt>
add    $0x2,%eax
mov    %eax,0x4(%rsp)
lea    0x4(%rsp),%rsi
mov    %r15,%rdi
xor    %edx,%edx
call   10e0 <encode_header_str@plt>
test   %rax,%rax
je     122f <encode_header_field+0x6f>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r12,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
call   1030 <free@plt>
mov    $0xa,%edi
mov    %r12,%rsi
call   1080 <fputc@plt>
mov    %r12,%rdi
call   10b0 <ftell@plt>
test   %eax,%eax
je     1291 <encode_header_field+0xd1>
mov    %eax,%r15d
mov    0x2d8e(%rip),%rax
mov    (%rax),%edx
xor    %r14d,%r14d
mov    %r12,%rdi
xor    %esi,%esi
call   10d0 <fseek@plt>
lea    0x1(%r15),%edi
call   10c0 <malloc@plt>
test   %rax,%rax
je     1294 <encode_header_field+0xd4>
mov    %rax,%rbx
mov    %rax,%rdi
mov    $0x1,%esi
mov    %r15d,%edx
mov    %r12,%rcx
call   1040 <fread@plt>
movslq %r15d,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%r14
jmp    1294 <encode_header_field+0xd4>
xor    %r14d,%r14d
mov    %r12,%rdi
call   1050 <fclose@plt>
mov    %r14,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [5 x i8] c"%s: \00"
@.str.1 = [3 x i8] c"%s\00"
@SEEK_SET = global i32 0
define i8* @encode_header_field(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i8* null, i8** %5
%10 = call i32* (...) @tmpfile()
store i32* %10, i32** %7
%11 = icmp ne i32* %10, null
br i1 %11, label %12, label %56
12: ; preds = %2
%13 = load i32*, i32** %7
%14 = load i8*, i8** %3
%15 = call i32 @fprintf(i32* %13, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = load i8*, i8** %3
%17 = call i32 @strlen(i8* %16)
%18 = add nsw i32 %17, 2
store i32 %18, i32* %6
%19 = load i8*, i8** %4
%20 = call i8* @encode_header_str(i8* %19, i32* %6, i32 0)
store i8* %20, i8** %8
%21 = icmp ne i8* %20, null
br i1 %21, label %22, label %28
22: ; preds = %12
%23 = load i32*, i32** %7
%24 = load i8*, i8** %8
%25 = call i32 @fprintf(i32* %23, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %24)
%26 = load i8*, i8** %8
%27 = call i32 @free(i8* %26)
br label %28
28: ; preds = %22, %12
%29 = load i32*, i32** %7
%30 = call i32 @fputc(i8 signext 10, i32* %29)
%31 = load i32*, i32** %7
%32 = call i32 @ftell(i32* %31)
store i32 %32, i32* %9
%33 = icmp ne i32 %32, 0
br i1 %33, label %34, label %53
34: ; preds = %28
%35 = load i32*, i32** %7
%36 = load i32, i32* @SEEK_SET
%37 = call i32 @fseek(i32* %35, i32 0, i32 %36)
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
%40 = call i64 @malloc(i32 %39)
%41 = inttoptr i64 %40 to i8*
store i8* %41, i8** %5
%42 = icmp ne i8* %41, null
br i1 %42, label %43, label %52
43: ; preds = %34
%44 = load i8*, i8** %5
%45 = load i32, i32* %9
%46 = load i32*, i32** %7
%47 = call i32 @fread(i8* %44, i32 1, i32 %45, i32* %46)
%48 = load i8*, i8** %5
%49 = load i32, i32* %9
%50 = sext i32 %49 to i64
%51 = getelementptr inbounds i8, i8* %48, i64 %50
store i8 0, i8* %51
br label %52
52: ; preds = %43, %34
br label %53
53: ; preds = %52, %28
%54 = load i32*, i32** %7
%55 = call i32 @fclose(i32* %54)
br label %56
56: ; preds = %53, %2
%57 = load i8*, i8** %5
ret i8* %57
}

LLVM IR (-O2):
@.str = [5 x i8] c"%s: \00"
@SEEK_SET = global i32 0
define i8* @encode_header_field(i8* %0, i8* %1) {
%3 = alloca i32
%4 = bitcast i32* %3 to i8*
%5 = tail call i32* (...) @tmpfile()
%6 = icmp eq i32* %5, null
br i1 %6, label %34, label %7
7: ; preds = %2
%8 = tail call i32 @fprintf(i32* %5, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* %0)
%9 = tail call i32 @strlen(i8* %0)
%10 = add nsw i32 %9, 2
store i32 %10, i32* %3
%11 = call i8* @encode_header_str(i8* %1, i32* %3, i32 0)
%12 = icmp eq i8* %11, null
br i1 %12, label %16, label %13
13: ; preds = %7
%14 = call i32 @fputs(i8* %11, i32* %5)
%15 = call i32 @free(i8* %11)
br label %16
16: ; preds = %13, %7
%17 = call i32 @fputc(i8 signext 10, i32* %5)
%18 = call i32 @ftell(i32* %5)
%19 = icmp eq i32 %18, 0
br i1 %19, label %31, label %20
20: ; preds = %16
%21 = load i32, i32* @SEEK_SET
%22 = call i32 @fseek(i32* %5, i32 0, i32 %21)
%23 = add nsw i32 %18, 1
%24 = call i64 @malloc(i32 %23)
%25 = inttoptr i64 %24 to i8*
%26 = icmp eq i64 %24, 0
br i1 %26, label %31, label %27
27: ; preds = %20
%28 = call i32 @fread(i8* %25, i32 1, i32 %18, i32* %5)
%29 = sext i32 %18 to i64
%30 = getelementptr inbounds i8, i8* %25, i64 %29
store i8 0, i8* %30
br label %31
31: ; preds = %20, %27, %16
%32 = phi i8* [ %25, %27 ], [ null, %20 ], [ null, %16 ]
%33 = call i32 @fclose(i32* %5)
br label %34
34: ; preds = %31, %2
%35 = phi i8* [ %32, %31 ], [ null, %2 ]
ret i8* %35
}