#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 */

/* Variables and functions */
char encode_quartet(unsigned char);
char *malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
char *encode_packet(unsigned char *packet, int size) {
  int i;
  char *result = malloc(2 * size * sizeof(char) + 1);
  memset(result, 0, 2 * size * sizeof(char) + 1);
  for (i = 0; i < size; i++) {
    result[2 * i + 1] = encode_quartet(packet[i] & 0xf);
    result[2 * i] = encode_quartet((packet[i] & 0xf0) >> 4);
  }
  return result;
}


x86 assembly (clang, -O0):
<encode_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0xc(%rbp),%eax
shl    $0x1,%eax
cltq
shl    $0x0,%rax
add    $0x1,%rax
mov    %eax,%edx
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x0,-0x10(%rbp)
mov    -0x10(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11f2 <encode_packet+0xc2>
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
and    $0xf,%eax
movzbl %al,%edi
call   1030 <encode_quartet@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%ecx
shl    $0x1,%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
movslq -0x10(%rbp),%rcx
movzbl (%rax,%rcx,1),%eax
and    $0xf0,%eax
sar    $0x4,%eax
movzbl %al,%edi
call   1030 <encode_quartet@plt>
mov    %al,%dl
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%ecx
shl    $0x1,%ecx
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x10(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x10(%rbp)
jmp    117e <encode_packet+0x4e>
mov    -0x18(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<encode_packet>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%r15d
mov    %rdi,%r14
lea    (%r15,%r15,1),%ebp
add    $0x1,%ebp
mov    %ebp,%edi
call   1050 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
xor    %esi,%esi
mov    %ebp,%edx
call   1040 <memset@plt>
test   %r15d,%r15d
jle    119d <encode_packet+0x6d>
mov    %r15d,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movzbl (%r14,%rbp,1),%edi
and    $0xf,%edi
call   1030 <encode_quartet@plt>
mov    %al,0x1(%rbx,%rbp,2)
movzbl (%r14,%rbp,1),%eax
shr    $0x4,%al
movzbl %al,%edi
call   1030 <encode_quartet@plt>
mov    %al,(%rbx,%rbp,2)
add    $0x1,%rbp
cmp    %rbp,%r15
jne    1170 <encode_packet+0x40>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @encode_packet(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
store i32 %1, i32* %4
%7 = load i32, i32* %4
%8 = mul nsw i32 2, %7
%9 = sext i32 %8 to i64
%10 = mul i64 %9, 1
%11 = add i64 %10, 1
%12 = trunc i64 %11 to i32
%13 = call i8* @malloc(i32 %12)
store i8* %13, i8** %6
%14 = load i8*, i8** %6
%15 = load i32, i32* %4
%16 = mul nsw i32 2, %15
%17 = sext i32 %16 to i64
%18 = mul i64 %17, 1
%19 = add i64 %18, 1
%20 = trunc i64 %19 to i32
%21 = call i32 @memset(i8* %14, i32 0, i32 %20)
store i32 0, i32* %5
br label %22
22: ; preds = %57, %2
%23 = load i32, i32* %5
%24 = load i32, i32* %4
%25 = icmp slt i32 %23, %24
br i1 %25, label %26, label %60
26: ; preds = %22
%27 = load i8*, i8** %3
%28 = load i32, i32* %5
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds i8, i8* %27, i64 %29
%31 = load i8, i8* %30
%32 = zext i8 %31 to i32
%33 = and i32 %32, 15
%34 = trunc i32 %33 to i8
%35 = call signext i8 @encode_quartet(i8 zeroext %34)
%36 = load i8*, i8** %6
%37 = load i32, i32* %5
%38 = mul nsw i32 2, %37
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8, i8* %36, i64 %40
store i8 %35, i8* %41
%42 = load i8*, i8** %3
%43 = load i32, i32* %5
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i8, i8* %45
%47 = zext i8 %46 to i32
%48 = and i32 %47, 240
%49 = ashr i32 %48, 4
%50 = trunc i32 %49 to i8
%51 = call signext i8 @encode_quartet(i8 zeroext %50)
%52 = load i8*, i8** %6
%53 = load i32, i32* %5
%54 = mul nsw i32 2, %53
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8, i8* %52, i64 %55
store i8 %51, i8* %56
br label %57
57: ; preds = %26
%58 = load i32, i32* %5
%59 = add nsw i32 %58, 1
store i32 %59, i32* %5
br label %22
60: ; preds = %22
%61 = load i8*, i8** %6
ret i8* %61
}

LLVM IR (-O2):
define i8* @encode_packet(i8* nocapture %0, i32 %1) {
%3 = shl nsw i32 %1, 1
%4 = or i32 %3, 1
%5 = tail call i8* @malloc(i32 %4)
%6 = tail call i32 @memset(i8* %5, i32 0, i32 %4)
%7 = icmp sgt i32 %1, 0
br i1 %7, label %8, label %25
8: ; preds = %2
%9 = zext i32 %1 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %23, %10 ]
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = and i8 %13, 15
%15 = tail call signext i8 @encode_quartet(i8 zeroext %14)
%16 = shl nuw nsw i64 %11, 1
%17 = or i64 %16, 1
%18 = getelementptr inbounds i8, i8* %5, i64 %17
store i8 %15, i8* %18
%19 = load i8, i8* %12
%20 = lshr i8 %19, 4
%21 = tail call signext i8 @encode_quartet(i8 zeroext %20)
%22 = getelementptr inbounds i8, i8* %5, i64 %16
store i8 %21, i8* %22
%23 = add nuw nsw i64 %11, 1
%24 = icmp eq i64 %23, %9
br i1 %24, label %25, label %10
25: ; preds = %10, %2
ret i8* %5
}