#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 */
scalar_t__ sscanf(char *, char *, unsigned int *);
int /*<<< orphan*/ strncpy(char *, char const *, int);
char *strtok(char *, char *);
int /*<<< orphan*/ trace(int, char *, char const *);
int gen_hex(const char *msg, unsigned char *buff) {
  unsigned char *q = buff;
  char mbuff[1024] = "", *args[256], *p;
  unsigned int byte;
  int i, narg = 0;
  trace(4, "gen_hex: msg=%s\n", msg);
  strncpy(mbuff, msg, 1023);
  for (p = strtok(mbuff, " "); p && narg < 256; p = strtok(NULL, " ")) {
    args[narg++] = p;
  }
  for (i = 0; i < narg; i++) {
    if (sscanf(args[i], "%x", &byte))
      *q++ = (unsigned char)byte;
  }
  return (int)(q - buff);
}


x86 assembly (clang, -O0):
<gen_hex>:
push   %rbp
mov    %rsp,%rbp
sub    $0xc40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x18(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
movl   $0x0,-0xc34(%rbp)
mov    -0x8(%rbp),%rdx
mov    $0x4,%edi
lea    0xe68(%rip),%rsi
call   1060 <trace@plt>
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    -0x420(%rbp),%rdi
lea    0xe51(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0xc28(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0xc28(%rbp)
mov    %al,-0xc35(%rbp)
je     11f5 <gen_hex+0xa5>
cmpl   $0x100,-0xc34(%rbp)
setl   %al
mov    %al,-0xc35(%rbp)
mov    -0xc35(%rbp),%al
test   $0x1,%al
jne    1208 <gen_hex+0xb8>
jmp    1246 <gen_hex+0xf6>
mov    -0xc28(%rbp),%rcx
mov    -0xc34(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0xc34(%rbp)
cltq
mov    %rcx,-0xc20(%rbp,%rax,8)
ff
xor    %eax,%eax
mov    %eax,%edi
lea    0xddc(%rip),%rsi
call   1070 <strtok@plt>
mov    %rax,-0xc28(%rbp)
jmp    11cc <gen_hex+0x7c>
movl   $0x0,-0xc30(%rbp)
mov    -0xc30(%rbp),%eax
cmp    -0xc34(%rbp),%eax
jge    12c0 <gen_hex+0x170>
movslq -0xc30(%rbp),%rax
mov    -0xc20(%rbp,%rax,8),%rdi
ff
lea    0xd9b(%rip),%rsi
lea    -0xc2c(%rbp),%rdx
call   1050 <sscanf@plt>
cmp    $0x0,%rax
je     12a7 <gen_hex+0x157>
mov    -0xc2c(%rbp),%eax
mov    %al,%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    12ac <gen_hex+0x15c>
mov    -0xc30(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc30(%rbp)
jmp    1250 <gen_hex+0x100>
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0xc40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gen_hex>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc18,%rsp
mov    %rsi,%rbp
mov    %rdi,%rbx
lea    0x810(%rsp),%r15
mov    $0x400,%edx
mov    %r15,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
lea    0xe7b(%rip),%rsi
mov    $0x4,%edi
mov    %rbx,%rdx
call   1060 <trace@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
mov    %rbp,%rbx
mov    $0x3ff,%edx
call   1030 <strncpy@plt>
lea    0xe65(%rip),%rsi
mov    %r15,%rdi
call   1070 <strtok@plt>
mov    %rbp,%r13
test   %rax,%rax
je     123d <gen_hex+0xed>
mov    %rbx,%r13
xor    %ebx,%ebx
lea    0xe45(%rip),%r15
nopl   0x0(%rax)
lea    0x1(%rbx),%r14
mov    %rax,0x10(%rsp,%rbx,8)
xor    %edi,%edi
mov    %r15,%rsi
call   1070 <strtok@plt>
test   %rax,%rax
je     11f4 <gen_hex+0xa4>
cmp    $0xff,%rbx
mov    %r14,%rbx
jb     11d0 <gen_hex+0x80>
add    $0xffffffffffffffff,%r14
mov    $0xffffffffffffffff,%rbp
lea    0xe0d(%rip),%r15
lea    0xc(%rsp),%r12
mov    %r13,%rbx
jmp    1219 <gen_hex+0xc9>
add    $0x1,%rbp
cmp    %rbp,%r14
je     123d <gen_hex+0xed>
mov    0x18(%rsp,%rbp,8),%rdi
mov    %r15,%rsi
mov    %r12,%rdx
call   1050 <sscanf@plt>
test   %rax,%rax
je     1210 <gen_hex+0xc0>
movzbl 0xc(%rsp),%eax
mov    %al,0x0(%r13)
add    $0x1,%r13
jmp    1210 <gen_hex+0xc0>
sub    %ebx,%r13d
mov    %r13d,%eax
add    $0xc18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [17 x i8] c"gen_hex: msg=%s\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [3 x i8] c"%x\00"
define i32 @gen_hex(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca [1024 x i8]
%7 = alloca [256 x i8*]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
%12 = load i8*, i8** %4
store i8* %12, i8** %5
%13 = bitcast [1024 x i8]* %6 to i8*
store i32 0, i32* %11
%14 = load i8*, i8** %3
%15 = call i32 @trace(i32 4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %14)
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%17 = load i8*, i8** %3
%18 = call i32 @strncpy(i8* %16, i8* %17, i32 1023)
%19 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%20 = call i8* @strtok(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %20, i8** %8
br label %21
21: ; preds = %35, %2
%22 = load i8*, i8** %8
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %21
%25 = load i32, i32* %11
%26 = icmp slt i32 %25, 256
br label %27
27: ; preds = %24, %21
%28 = phi i1 [ false, %21 ], [ %26, %24 ]
br i1 %28, label %29, label %37
29: ; preds = %27
%30 = load i8*, i8** %8
%31 = load i32, i32* %11
%32 = add nsw i32 %31, 1
store i32 %32, i32* %11
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds [256 x i8*], [256 x i8*]* %7, i64 0, i64 %33
store i8* %30, i8** %34
br label %35
35: ; preds = %29
%36 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i8* %36, i8** %8
br label %21
37: ; preds = %27
store i32 0, i32* %10
br label %38
38: ; preds = %55, %37
%39 = load i32, i32* %10
%40 = load i32, i32* %11
%41 = icmp slt i32 %39, %40
br i1 %41, label %42, label %58
42: ; preds = %38
%43 = load i32, i32* %10
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [256 x i8*], [256 x i8*]* %7, i64 0, i64 %44
%46 = load i8*, i8** %45
%47 = call i64 @sscanf(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %9)
%48 = icmp ne i64 %47, 0
br i1 %48, label %49, label %54
49: ; preds = %42
%50 = load i32, i32* %9
%51 = trunc i32 %50 to i8
%52 = load i8*, i8** %5
%53 = getelementptr inbounds i8, i8* %52, i32 1
store i8* %53, i8** %5
store i8 %51, i8* %52
br label %54
54: ; preds = %49, %42
br label %55
55: ; preds = %54
%56 = load i32, i32* %10
%57 = add nsw i32 %56, 1
store i32 %57, i32* %10
br label %38
58: ; preds = %38
%59 = load i8*, i8** %5
%60 = load i8*, i8** %4
%61 = ptrtoint i8* %59 to i64
%62 = ptrtoint i8* %60 to i64
%63 = sub i64 %61, %62
%64 = trunc i64 %63 to i32
ret i32 %64
}

LLVM IR (-O2):
@.str = [17 x i8] c"gen_hex: msg=%s\0A\00"
@.str.1 = [2 x i8] c" \00"
@.str.2 = [3 x i8] c"%x\00"
define i32 @gen_hex(i8* %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = alloca [256 x i8*]
%5 = alloca i32
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast [256 x i8*]* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = tail call i32 @trace(i32 4, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = call i32 @strncpy(i8* %6, i8* %0, i32 1023)
%11 = call i8* @strtok(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%12 = icmp eq i8* %11, null
br i1 %12, label %39, label %15
13: ; preds = %15
%14 = and i64 %18, 4294967295
br label %24
15: ; preds = %2, %15
%16 = phi i64 [ %18, %15 ], [ 0, %2 ]
%17 = phi i8* [ %20, %15 ], [ %11, %2 ]
%18 = add nuw nsw i64 %16, 1
%19 = getelementptr inbounds [256 x i8*], [256 x i8*]* %4, i64 0, i64 %16
store i8* %17, i8** %19
%20 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%21 = icmp ne i8* %20, null
%22 = icmp ult i64 %16, 255
%23 = select i1 %21, i1 %22, i1 false
br i1 %23, label %15, label %13
24: ; preds = %13, %35
%25 = phi i64 [ 0, %13 ], [ %37, %35 ]
%26 = phi i8* [ %1, %13 ], [ %36, %35 ]
%27 = getelementptr inbounds [256 x i8*], [256 x i8*]* %4, i64 0, i64 %25
%28 = load i8*, i8** %27
%29 = call i64 @sscanf(i8* %28, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32* %5)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %24
%32 = load i32, i32* %5
%33 = trunc i32 %32 to i8
%34 = getelementptr inbounds i8, i8* %26, i64 1
store i8 %33, i8* %26
br label %35
35: ; preds = %24, %31
%36 = phi i8* [ %34, %31 ], [ %26, %24 ]
%37 = add nuw nsw i64 %25, 1
%38 = icmp eq i64 %37, %14
br i1 %38, label %39, label %24
39: ; preds = %35, %2
%40 = phi i8* [ %1, %2 ], [ %36, %35 ]
%41 = ptrtoint i8* %40 to i64
%42 = ptrtoint i8* %1 to i64
%43 = sub i64 %41, %42
%44 = trunc i64 %43 to i32
ret i32 %44
}