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

/* Variables and functions */
int /*<<< orphan*/ base64_encode(char *, int, unsigned char const *, int);
int base64_encoded_length(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char const *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, char const *, char *,
                           char *);
int /*<<< orphan*/ memcpy(char *, unsigned char const *, int);
int ntohu32(unsigned char const *);
void append_hostkey(const char *fn, const char *hostname,
                    const unsigned char *K_S, int K_S_len) {
  FILE *fp = fopen(fn, "a");
  if (fp == NULL)
    return;
  char algo[32];
  char buf[1536];
  int algo_len = ntohu32(K_S);
  if (algo_len >= sizeof(algo))
    goto end;
  memcpy(algo, K_S + 4, algo_len);
  algo[algo_len] = 0;
  int b64len = base64_encoded_length(K_S_len);
  if (b64len >= sizeof(buf))
    goto end;
  base64_encode(buf, sizeof(buf), K_S, K_S_len);
  buf[b64len] = 0;
  fprintf(fp, "%s %s %s\n", hostname, algo, buf);
end:
  fclose(fp);
  return;
}


x86 assembly (clang, -O0):
<append_hostkey>:
push   %rbp
mov    %rsp,%rbp
sub    $0x660,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xe6b(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    11ae <append_hostkey+0x3e>
jmp    126b <append_hostkey+0xfb>
mov    -0x18(%rbp),%rdi
call   1060 <ntohu32@plt>
mov    %eax,-0x654(%rbp)
movslq -0x654(%rbp),%rax
cmp    $0x20,%rax
jb     11d3 <append_hostkey+0x63>
jmp    1262 <append_hostkey+0xf2>
lea    -0x50(%rbp),%rdi
mov    -0x18(%rbp),%rsi
add    $0x4,%rsi
mov    -0x654(%rbp),%edx
call   1050 <memcpy@plt>
movslq -0x654(%rbp),%rax
movb   $0x0,-0x50(%rbp,%rax,1)
mov    -0x1c(%rbp),%edi
call   1080 <base64_encoded_length@plt>
mov    %eax,-0x658(%rbp)
movslq -0x658(%rbp),%rax
cmp    $0x600,%rax
jb     121c <append_hostkey+0xac>
jmp    1262 <append_hostkey+0xf2>
lea    -0x650(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    -0x1c(%rbp),%ecx
mov    $0x600,%esi
call   1070 <base64_encode@plt>
movslq -0x658(%rbp),%rax
movb   $0x0,-0x650(%rbp,%rax,1)
mov    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
lea    -0x50(%rbp),%rcx
lea    -0x650(%rbp),%r8
lea    0xda5(%rip),%rsi
call   1040 <fprintf@plt>
mov    -0x28(%rbp),%rdi
call   1030 <fclose@plt>
add    $0x660,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<append_hostkey>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x628,%rsp
mov    %ecx,%r15d
mov    %rdx,%rbp
mov    %rsi,%r14
lea    0xe6f(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1211 <append_hostkey+0xa1>
mov    %rax,%rbx
mov    %rbp,%rdi
call   1060 <ntohu32@plt>
cmp    $0x1f,%eax
ja     1209 <append_hostkey+0x99>
mov    %eax,%r12d
lea    0x4(%rbp),%rsi
mov    %rsp,%rdi
mov    %eax,%edx
call   1050 <memcpy@plt>
movb   $0x0,(%rsp,%r12,1)
mov    %r15d,%edi
call   1080 <base64_encoded_length@plt>
cmp    $0x5ff,%eax
ja     1209 <append_hostkey+0x99>
mov    %eax,%r13d
lea    0x20(%rsp),%r12
mov    %r12,%rdi
mov    $0x600,%esi
mov    %rbp,%rdx
mov    %r15d,%ecx
call   1070 <base64_encode@plt>
movb   $0x0,0x20(%rsp,%r13,1)
lea    0xe0a(%rip),%rsi
mov    %rsp,%rcx
mov    %rbx,%rdi
mov    %r14,%rdx
mov    %r12,%r8
call   1040 <fprintf@plt>
mov    %rbx,%rdi
call   1030 <fclose@plt>
add    $0x628,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"a\00"
@.str.1 = [10 x i8] c"%s %s %s\0A\00"
define void @append_hostkey(i8* %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca [32 x i8]
%11 = alloca [1536 x i8]
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 %3, i32* %8
%14 = load i8*, i8** %5
%15 = call i32* @fopen(i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %15, i32** %9
%16 = load i32*, i32** %9
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %19
18: ; preds = %4
br label %57
19: ; preds = %4
%20 = load i8*, i8** %7
%21 = call i32 @ntohu32(i8* %20)
store i32 %21, i32* %12
%22 = load i32, i32* %12
%23 = sext i32 %22 to i64
%24 = icmp uge i64 %23, 32
br i1 %24, label %25, label %26
25: ; preds = %19
br label %54
26: ; preds = %19
%27 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%28 = load i8*, i8** %7
%29 = getelementptr inbounds i8, i8* %28, i64 4
%30 = load i32, i32* %12
%31 = call i32 @memcpy(i8* %27, i8* %29, i32 %30)
%32 = load i32, i32* %12
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 %33
store i8 0, i8* %34
%35 = load i32, i32* %8
%36 = call i32 @base64_encoded_length(i32 %35)
store i32 %36, i32* %13
%37 = load i32, i32* %13
%38 = sext i32 %37 to i64
%39 = icmp uge i64 %38, 1536
br i1 %39, label %40, label %41
40: ; preds = %26
br label %54
41: ; preds = %26
%42 = getelementptr inbounds [1536 x i8], [1536 x i8]* %11, i64 0, i64 0
%43 = load i8*, i8** %7
%44 = load i32, i32* %8
%45 = call i32 @base64_encode(i8* %42, i32 1536, i8* %43, i32 %44)
%46 = load i32, i32* %13
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [1536 x i8], [1536 x i8]* %11, i64 0, i64 %47
store i8 0, i8* %48
%49 = load i32*, i32** %9
%50 = load i8*, i8** %6
%51 = getelementptr inbounds [32 x i8], [32 x i8]* %10, i64 0, i64 0
%52 = getelementptr inbounds [1536 x i8], [1536 x i8]* %11, i64 0, i64 0
%53 = call i32 @fprintf(i32* %49, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %50, i8* %51, i8* %52)
br label %54
54: ; preds = %41, %40, %25
%55 = load i32*, i32** %9
%56 = call i32 @fclose(i32* %55)
br label %57
57: ; preds = %54, %18
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"a\00"
@.str.1 = [10 x i8] c"%s %s %s\0A\00"
define void @append_hostkey(i8* nocapture %0, i8* %1, i8* %2, i32 %3) {
%5 = alloca [32 x i8]
%6 = alloca [1536 x i8]
%7 = tail call i32* @fopen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%8 = icmp eq i32* %7, null
br i1 %8, label %28, label %9
9: ; preds = %4
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%11 = getelementptr inbounds [1536 x i8], [1536 x i8]* %6, i64 0, i64 0
%12 = tail call i32 @ntohu32(i8* %2)
%13 = icmp ugt i32 %12, 31
br i1 %13, label %26, label %14
14: ; preds = %9
%15 = zext i32 %12 to i64
%16 = getelementptr inbounds i8, i8* %2, i64 4
%17 = call i32 @memcpy(i8* %10, i8* %16, i32 %12)
%18 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 %15
store i8 0, i8* %18
%19 = call i32 @base64_encoded_length(i32 %3)
%20 = icmp ugt i32 %19, 1535
br i1 %20, label %26, label %21
21: ; preds = %14
%22 = zext i32 %19 to i64
%23 = call i32 @base64_encode(i8* %11, i32 1536, i8* %2, i32 %3)
%24 = getelementptr inbounds [1536 x i8], [1536 x i8]* %6, i64 0, i64 %22
store i8 0, i8* %24
%25 = call i32 @fprintf(i32* %7, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %1, i8* %10, i8* %11)
br label %26
26: ; preds = %14, %9, %21
%27 = call i32 @fclose(i32* %7)
br label %28
28: ; preds = %4, %26
ret void
}