#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 */
int CommWrite(int, char *, int);
int /*<<< orphan*/ memcpy(char *, char const *, int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
char *strchr(char const *, char);
int /*<<< orphan*/ strcpy(char *, char const *);
scalar_t__ strlen(char const *);
scalar_t__ strtoul(char *, int, int);
int CommWriteHexString(int fd_com, const char *hexstring) {
  if (fd_com < 0 || strlen(hexstring) <= 0)
    return -1;
  char result[256] = "";
  int result_len = 0;
  const char *cur = hexstring;
  while (cur != NULL) {
    const char *p = strchr(cur, ' ');
    char buf[8];
    memset(buf, 0x0, 8);
    if (p) {
      memcpy(buf, cur, p - cur);
      cur = ++p;
    } else {
      strcpy(buf, cur);
      cur = NULL;
    }
    if (strlen(buf) <= 0)
      break;
    result[result_len++] = (int)strtoul(buf, 0x0, 16);
  }
  int len = CommWrite(fd_com, result, result_len);
  do {
    int i;
    for (i = 0; i < result_len - 1; i++) {
      if (0 == i % 16) {
        if (0 != i)
          printf("\n");
        printf("0x%04x: ", i);
      }
      printf("%02x ", ((char *)result)[i]);
    }
    printf("%02x\n", ((char *)result)[i]);
  } while (0);
  return 0;
}


x86 assembly (clang, -O0):
<CommWriteHexString>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x0,-0x8(%rbp)
jl     11af <CommWriteHexString+0x2f>
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jg     11bb <CommWriteHexString+0x3b>
movl   $0xffffffff,-0x4(%rbp)
jmp    13b4 <CommWriteHexString+0x234>
lea    -0x110(%rbp),%rdi
xor    %esi,%esi
mov    $0x100,%edx
call   1070 <memset@plt>
movl   $0x0,-0x114(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x120(%rbp)
cmpq   $0x0,-0x120(%rbp)
je     12db <CommWriteHexString+0x15b>
mov    -0x120(%rbp),%rdi
mov    $0x20,%esi
call   1050 <strchr@plt>
mov    %rax,-0x128(%rbp)
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
cmpq   $0x0,-0x128(%rbp)
je     126e <CommWriteHexString+0xee>
lea    -0x130(%rbp),%rdi
mov    -0x120(%rbp),%rsi
mov    -0x128(%rbp),%rax
mov    -0x120(%rbp),%rcx
sub    %rcx,%rax
mov    %eax,%edx
call   1080 <memcpy@plt>
mov    -0x128(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x128(%rbp)
mov    %rax,-0x120(%rbp)
jmp    128c <CommWriteHexString+0x10c>
lea    -0x130(%rbp),%rdi
mov    -0x120(%rbp),%rsi
call   1030 <strcpy@plt>
movq   $0x0,-0x120(%rbp)
lea    -0x130(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x0,%rax
jg     12a7 <CommWriteHexString+0x127>
jmp    12db <CommWriteHexString+0x15b>
lea    -0x130(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10a0 <strtoul@plt>
mov    %al,%cl
mov    -0x114(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x114(%rbp)
cltq
mov    %cl,-0x110(%rbp,%rax,1)
jmp    11e3 <CommWriteHexString+0x63>
mov    -0x8(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    -0x114(%rbp),%edx
call   1090 <CommWrite@plt>
mov    %eax,-0x134(%rbp)
movl   $0x0,-0x138(%rbp)
mov    -0x138(%rbp),%eax
mov    -0x114(%rbp),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1390 <CommWriteHexString+0x210>
mov    -0x138(%rbp),%eax
mov    $0x10,%ecx
cltd
idiv   %ecx
xor    %eax,%eax
cmp    %edx,%eax
jne    135f <CommWriteHexString+0x1df>
xor    %eax,%eax
cmp    -0x138(%rbp),%eax
je     134b <CommWriteHexString+0x1cb>
lea    0xccf(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x138(%rbp),%esi
lea    0xca8(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movslq -0x138(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%esi
ff
lea    0xc94(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x138(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x138(%rbp)
jmp    1300 <CommWriteHexString+0x180>
movslq -0x138(%rbp),%rax
movsbl -0x110(%rbp,%rax,1),%esi
ff
lea    0xc69(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<CommWriteHexString>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x110,%rsp
mov    $0xffffffff,%ebx
test   %edi,%edi
js     1395 <CommWriteHexString+0x205>
mov    %rsi,%r12
mov    %edi,%r14d
mov    %rsi,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
jle    1395 <CommWriteHexString+0x205>
xorps  %xmm0,%xmm0
movaps %xmm0,0x100(%rsp)
movaps %xmm0,0xf0(%rsp)
movaps %xmm0,0xe0(%rsp)
movaps %xmm0,0xd0(%rsp)
movaps %xmm0,0xc0(%rsp)
movaps %xmm0,0xb0(%rsp)
movaps %xmm0,0xa0(%rsp)
movaps %xmm0,0x90(%rsp)
movaps %xmm0,0x80(%rsp)
movaps %xmm0,0x70(%rsp)
movaps %xmm0,0x60(%rsp)
movaps %xmm0,0x50(%rsp)
movaps %xmm0,0x40(%rsp)
movaps %xmm0,0x30(%rsp)
movaps %xmm0,0x20(%rsp)
movaps %xmm0,0x10(%rsp)
test   %r12,%r12
je     1366 <CommWriteHexString+0x1d6>
xor    %ebx,%ebx
lea    0x8(%rsp),%r15
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
mov    %r12,%rdi
mov    $0x20,%esi
call   1060 <strchr@plt>
mov    %rax,%rbp
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1080 <memset@plt>
test   %rbp,%rbp
je     1290 <CommWriteHexString+0x100>
mov    %ebp,%edx
sub    %r12d,%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1090 <memcpy@plt>
add    $0x1,%rbp
mov    %rbp,%r12
jmp    129e <CommWriteHexString+0x10e>
nopl   (%rax)
mov    %r15,%rdi
mov    %r12,%rsi
call   1040 <strcpy@plt>
xor    %r12d,%r12d
mov    %r15,%rdi
call   1050 <strlen@plt>
test   %rax,%rax
jle    12c7 <CommWriteHexString+0x137>
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   10b0 <strtoul@plt>
mov    %al,0x10(%rsp,%rbx,1)
add    $0x1,%rbx
test   %r12,%r12
jne    1250 <CommWriteHexString+0xc0>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    %ebx,%edx
call   10a0 <CommWrite@plt>
cmp    $0x2,%ebx
jl     1379 <CommWriteHexString+0x1e9>
add    $0xffffffff,%ebx
lea    0xd17(%rip),%rdi
xor    %esi,%esi
xor    %eax,%eax
call   1070 <printf@plt>
movsbl 0x10(%rsp),%esi
lea    0xd0b(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0x1,%ebp
cmp    $0x1,%ebx
je     1380 <CommWriteHexString+0x1f0>
mov    $0x1,%ebp
lea    0xce5(%rip),%r14
lea    0xce7(%rip),%r15
jmp    1348 <CommWriteHexString+0x1b8>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movsbl 0x10(%rsp,%rbp,1),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%rbx
je     137d <CommWriteHexString+0x1ed>
test   $0xf,%bpl
jne    1330 <CommWriteHexString+0x1a0>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1070 <printf@plt>
jmp    1330 <CommWriteHexString+0x1a0>
lea    0x10(%rsp),%rsi
xor    %ebp,%ebp
mov    %r14d,%edi
xor    %edx,%edx
call   10a0 <CommWrite@plt>
jmp    1380 <CommWriteHexString+0x1f0>
xor    %ebp,%ebp
jmp    1380 <CommWriteHexString+0x1f0>
mov    %rbx,%rbp
movsbl 0x10(%rsp,%rbp,1),%esi
lea    0xc83(%rip),%rdi
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <printf@plt>
mov    %ebx,%eax
add    $0x110,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"\0A\00"
@.str.1 = [9 x i8] c"0x%04x: \00"
@.str.2 = [6 x i8] c"%02x \00"
@.str.3 = [6 x i8] c"%02x\0A\00"
define i32 @CommWriteHexString(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [256 x i8]
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca [8 x i8]
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%13 = load i32, i32* %4
%14 = icmp slt i32 %13, 0
br i1 %14, label %19, label %15
15: ; preds = %2
%16 = load i8*, i8** %5
%17 = call i64 @strlen(i8* %16)
%18 = icmp sle i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %15, %2
store i32 -1, i32* %3
br label %106
20: ; preds = %15
%21 = bitcast [256 x i8]* %6 to i8*
store i32 0, i32* %7
%22 = load i8*, i8** %5
store i8* %22, i8** %8
br label %23
23: ; preds = %54, %20
%24 = load i8*, i8** %8
%25 = icmp ne i8* %24, null
br i1 %25, label %26, label %63
26: ; preds = %23
%27 = load i8*, i8** %8
%28 = call i8* @strchr(i8* %27, i8 signext 32)
store i8* %28, i8** %9
%29 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%30 = call i32 @memset(i8* %29, i32 0, i32 8)
%31 = load i8*, i8** %9
%32 = icmp ne i8* %31, null
br i1 %32, label %33, label %45
33: ; preds = %26
%34 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%35 = load i8*, i8** %8
%36 = load i8*, i8** %9
%37 = load i8*, i8** %8
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = trunc i64 %40 to i32
%42 = call i32 @memcpy(i8* %34, i8* %35, i32 %41)
%43 = load i8*, i8** %9
%44 = getelementptr inbounds i8, i8* %43, i32 1
store i8* %44, i8** %9
store i8* %44, i8** %8
br label %49
45: ; preds = %26
%46 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%47 = load i8*, i8** %8
%48 = call i32 @strcpy(i8* %46, i8* %47)
store i8* null, i8** %8
br label %49
49: ; preds = %45, %33
%50 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%51 = call i64 @strlen(i8* %50)
%52 = icmp sle i64 %51, 0
br i1 %52, label %53, label %54
53: ; preds = %49
br label %63
54: ; preds = %49
%55 = getelementptr inbounds [8 x i8], [8 x i8]* %10, i64 0, i64 0
%56 = call i64 @strtoul(i8* %55, i32 0, i32 16)
%57 = trunc i64 %56 to i32
%58 = trunc i32 %57 to i8
%59 = load i32, i32* %7
%60 = add nsw i32 %59, 1
store i32 %60, i32* %7
%61 = sext i32 %59 to i64
%62 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 %61
store i8 %58, i8* %62
br label %23
63: ; preds = %53, %23
%64 = load i32, i32* %4
%65 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%66 = load i32, i32* %7
%67 = call i32 @CommWrite(i32 %64, i8* %65, i32 %66)
store i32 %67, i32* %11
br label %68
68: ; preds = %63
store i32 0, i32* %12
br label %69
69: ; preds = %94, %68
%70 = load i32, i32* %12
%71 = load i32, i32* %7
%72 = sub nsw i32 %71, 1
%73 = icmp slt i32 %70, %72
br i1 %73, label %74, label %97
74: ; preds = %69
%75 = load i32, i32* %12
%76 = srem i32 %75, 16
%77 = icmp eq i32 0, %76
br i1 %77, label %78, label %86
78: ; preds = %74
%79 = load i32, i32* %12
%80 = icmp ne i32 0, %79
br i1 %80, label %81, label %83
81: ; preds = %78
%82 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %83
83: ; preds = %81, %78
%84 = load i32, i32* %12
%85 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %84)
br label %86
86: ; preds = %83, %74
%87 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%88 = load i32, i32* %12
%89 = sext i32 %88 to i64
%90 = getelementptr inbounds i8, i8* %87, i64 %89
%91 = load i8, i8* %90
%92 = sext i8 %91 to i32
%93 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %92)
br label %94
94: ; preds = %86
%95 = load i32, i32* %12
%96 = add nsw i32 %95, 1
store i32 %96, i32* %12
br label %69
97: ; preds = %69
%98 = getelementptr inbounds [256 x i8], [256 x i8]* %6, i64 0, i64 0
%99 = load i32, i32* %12
%100 = sext i32 %99 to i64
%101 = getelementptr inbounds i8, i8* %98, i64 %100
%102 = load i8, i8* %101
%103 = sext i8 %102 to i32
%104 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %103)
br label %105
105: ; preds = %97
store i32 0, i32* %3
br label %106
106: ; preds = %105, %19
%107 = load i32, i32* %3
ret i32 %107
}

LLVM IR (-O2):
@.str.1 = [9 x i8] c"0x%04x: \00"
@.str.2 = [6 x i8] c"%02x \00"
@.str.3 = [6 x i8] c"%02x\0A\00"
define i32 @CommWriteHexString(i32 %0, i8* %1) {
%3 = alloca [256 x i8]
%4 = alloca [8 x i8]
%5 = icmp slt i32 %0, 0
br i1 %5, label %76, label %6
6: ; preds = %2
%7 = tail call i64 @strlen(i8* dereferenceable(1) %1)
%8 = icmp slt i64 %7, 1
br i1 %8, label %76, label %9
9: ; preds = %6
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 0
%11 = icmp eq i8* %1, null
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = call i32 @CommWrite(i32 %0, i8* %10, i32 0)
br label %70
14: ; preds = %9
%15 = getelementptr inbounds [8 x i8], [8 x i8]* %4, i64 0, i64 0
br label %16
16: ; preds = %14, %36
%17 = phi i64 [ 0, %14 ], [ %39, %36 ]
%18 = phi i8* [ %1, %14 ], [ %32, %36 ]
%19 = call i8* @strchr(i8* %18, i8 signext 32)
%20 = call i32 @memset(i8* %15, i32 0, i32 8)
%21 = icmp eq i8* %19, null
br i1 %21, label %29, label %22
22: ; preds = %16
%23 = ptrtoint i8* %19 to i64
%24 = ptrtoint i8* %18 to i64
%25 = sub i64 %23, %24
%26 = trunc i64 %25 to i32
%27 = call i32 @memcpy(i8* %15, i8* %18, i32 %26)
%28 = getelementptr inbounds i8, i8* %19, i64 1
br label %31
29: ; preds = %16
%30 = call i32 @strcpy(i8* %15, i8* %18)
br label %31
31: ; preds = %29, %22
%32 = phi i8* [ %28, %22 ], [ null, %29 ]
%33 = call i64 @strlen(i8* %15)
%34 = icmp slt i64 %33, 1
br i1 %34, label %35, label %36
35: ; preds = %31
br label %42
36: ; preds = %31
%37 = call i64 @strtoul(i8* %15, i32 0, i32 16)
%38 = trunc i64 %37 to i8
%39 = add nuw i64 %17, 1
%40 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %17
store i8 %38, i8* %40
%41 = icmp eq i8* %32, null
br i1 %41, label %42, label %16
42: ; preds = %36, %35
%43 = phi i64 [ %17, %35 ], [ %39, %36 ]
%44 = trunc i64 %43 to i32
%45 = call i32 @CommWrite(i32 %0, i8* %10, i32 %44)
%46 = icmp sgt i32 %44, 1
br i1 %46, label %47, label %70
47: ; preds = %42
%48 = add nsw i32 %44, -1
%49 = zext i32 %48 to i64
%50 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 0)
%51 = load i8, i8* %10
%52 = sext i8 %51 to i32
%53 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %52)
%54 = icmp eq i32 %48, 1
br i1 %54, label %70, label %55
55: ; preds = %47, %63
%56 = phi i64 [ %68, %63 ], [ 1, %47 ]
%57 = trunc i64 %56 to i32
%58 = and i32 %57, 15
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %63
60: ; preds = %55
%61 = call i32 @putchar(i32 10)
%62 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), i32 %57)
br label %63
63: ; preds = %60, %55
%64 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %56
%65 = load i8, i8* %64
%66 = sext i8 %65 to i32
%67 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0), i32 %66)
%68 = add nuw nsw i64 %56, 1
%69 = icmp eq i64 %68, %49
br i1 %69, label %70, label %55
70: ; preds = %63, %47, %12, %42
%71 = phi i64 [ 0, %42 ], [ 0, %12 ], [ 1, %47 ], [ %49, %63 ]
%72 = getelementptr inbounds [256 x i8], [256 x i8]* %3, i64 0, i64 %71
%73 = load i8, i8* %72
%74 = sext i8 %73 to i32
%75 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0), i32 %74)
br label %76
76: ; preds = %2, %6, %70
%77 = phi i32 [ 0, %70 ], [ -1, %6 ], [ -1, %2 ]
ret i32 %77
}