#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 u_int8_t;

/* Variables and functions */
int /*<<< orphan*/ sprintf(unsigned char *, char *, int);
int /*<<< orphan*/ strcat(unsigned char *, unsigned char *);
int gen_hex_string(void *packet, int len, int flag, unsigned char *hexbuf,
                   int *hexbuf_len) {
  u_int8_t *bytes = (u_int8_t *)packet;
  int i = 0;
  int j;
  unsigned char hex[2 + flag];
  for (j = 0; j < len * 3; j++) {
    hexbuf[j] = '\0';
  }
  while (i < len) {
    if (flag == 0) {
      sprintf(hex, "%02X", bytes[i]);
    } else {
      sprintf(hex, " %02X", bytes[i]);
    }
    strcat(hexbuf, hex);
    i++;
  }
  *hexbuf_len = len * (2 + flag);
  return 0;
}


x86 assembly (clang, -O0):
<gen_hex_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %rcx,-0x18(%rbp)
mov    %r8,-0x20(%rbp)
mov    -0x8(%rbp),%rax
mov    %rax,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x10(%rbp),%eax
add    $0x2,%eax
mov    %rsp,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x48(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x40(%rbp)
movl   $0x0,-0x30(%rbp)
mov    -0x30(%rbp),%eax
imul   $0x3,-0xc(%rbp),%ecx
cmp    %ecx,%eax
jge    11a2 <gen_hex_string+0x82>
mov    -0x18(%rbp),%rax
movslq -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x30(%rbp)
jmp    1179 <gen_hex_string+0x59>
jmp    11a7 <gen_hex_string+0x87>
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    1213 <gen_hex_string+0xf3>
cmpl   $0x0,-0x10(%rbp)
jne    11dd <gen_hex_string+0xbd>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe2e(%rip),%rsi
call   1040 <sprintf@plt>
jmp    11f8 <gen_hex_string+0xd8>
mov    -0x48(%rbp),%rdi
mov    -0x28(%rbp),%rax
movslq -0x2c(%rbp),%rcx
mov    (%rax,%rcx,4),%edx
lea    0xe0d(%rip),%rsi
call   1040 <sprintf@plt>
mov    -0x48(%rbp),%rsi
mov    -0x18(%rbp),%rdi
call   1030 <strcat@plt>
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    11a7 <gen_hex_string+0x87>
mov    -0xc(%rbp),%ecx
mov    -0x10(%rbp),%eax
add    $0x2,%eax
imul   %eax,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x38(%rbp),%rax
mov    %rax,%rsp
xor    %eax,%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_hex_string>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,-0x48(%rbp)
mov    %rcx,-0x38(%rbp)
mov    %edx,%r14d
lea    0x2(%r14),%eax
mov    %rsp,%rbx
mov    %rax,-0x40(%rbp)
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %rsi,-0x30(%rbp)
test   %esi,%esi
jle    11d5 <gen_hex_string+0xa5>
mov    %rdi,%r13
mov    -0x30(%rbp),%r12
lea    (%r12,%r12,2),%eax
cmp    $0x2,%eax
mov    $0x1,%edx
cmovge %eax,%edx
xor    %r15d,%r15d
mov    -0x38(%rbp),%rdi
xor    %esi,%esi
call   1030 <memset@plt>
test   %r14d,%r14d
mov    %r12d,%r12d
lea    0xe63(%rip),%rax
lea    0xe5b(%rip),%r14
cmove  %rax,%r14
nopl   0x0(%rax)
mov    0x0(%r13,%r15,4),%edx
mov    %rbx,%rdi
mov    %r14,%rsi
call   1050 <sprintf@plt>
mov    -0x38(%rbp),%rdi
mov    %rbx,%rsi
call   1040 <strcat@plt>
add    $0x1,%r15
cmp    %r15,%r12
jne    11b0 <gen_hex_string+0x80>
mov    -0x40(%rbp),%rcx
imul   -0x30(%rbp),%ecx
mov    -0x48(%rbp),%rax
mov    %ecx,(%rax)
xor    %eax,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
@.str = [5 x i8] c"%02X\00"
@.str.1 = [6 x i8] c" %02X\00"
define i32 @gen_hex_string(i8* nocapture %0, i32 %1, i32 %2, i8* %3, i32* nocapture writeonly %4) {
%6 = bitcast i8* %0 to i32*
%7 = add nsw i32 %2, 2
%8 = zext i32 %7 to i64
%9 = alloca i8, i64 %8
%10 = icmp sgt i32 %1, 0
br i1 %10, label %11, label %26
11: ; preds = %5
%12 = mul i32 %1, 3
%13 = call i32 @llvm.smax.i32(i32 %12, i32 1)
%14 = zext i32 %13 to i64
%15 = icmp eq i32 %2, 0
%16 = zext i32 %1 to i64
%17 = select i1 %15, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0)
br label %18
18: ; preds = %11, %18
%19 = phi i64 [ 0, %11 ], [ %24, %18 ]
%20 = getelementptr inbounds i32, i32* %6, i64 %19
%21 = load i32, i32* %20
%22 = call i32 @sprintf(i8* %9, i8* dereferenceable(1) %17, i32 %21)
%23 = call i32 @strcat(i8* %3, i8* %9)
%24 = add nuw nsw i64 %19, 1
%25 = icmp eq i64 %24, %16
br i1 %25, label %26, label %18
26: ; preds = %18, %5
%27 = mul nsw i32 %7, %1
store i32 %27, i32* %4
ret i32 0
}