#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 *strtok(char *, char *);
scalar_t__ strtoul(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int hexread(char *buffer, char *string, int buflen) {
  char *s;
  int pos = 0;
  char *p = string;
  s = strtok(p, ", ");
  while (s != NULL) {
    buffer[pos++] = (char)strtoul(s, NULL, 0);
    if (pos >= buflen)
      break;
    s = strtok(NULL, ", ");
  }
  return pos;
}


x86 assembly (clang, -O0):
<hexread>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x30(%rbp)
mov    -0x30(%rbp),%rdi
lea    0xeb3(%rip),%rsi
call   1030 <strtok@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11b1 <hexread+0x91>
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1040 <strtoul@plt>
mov    %al,%dl
mov    -0x8(%rbp),%rax
mov    -0x24(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x24(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
jl     1198 <hexread+0x78>
jmp    11b1 <hexread+0x91>
xor    %eax,%eax
mov    %eax,%edi
lea    0xe5d(%rip),%rsi
call   1030 <strtok@plt>
mov    %rax,-0x20(%rbp)
jmp    1156 <hexread+0x36>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexread>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%ebp
mov    %rsi,%rax
mov    %rdi,%r12
lea    0xec9(%rip),%rsi
mov    %rax,%rdi
call   1030 <strtok@plt>
test   %rax,%rax
je     118d <hexread+0x6d>
cmp    $0x2,%ebp
mov    $0x1,%r14d
cmovge %ebp,%r14d
lea    -0x1(%r14),%ebx
xor    %ebp,%ebp
lea    0xea2(%rip),%r15
xchg   %ax,%ax
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1040 <strtoul@plt>
mov    %al,(%r12,%rbp,1)
cmp    %rbp,%rbx
je     1190 <hexread+0x70>
xor    %edi,%edi
mov    %r15,%rsi
call   1030 <strtok@plt>
add    $0x1,%rbp
test   %rax,%rax
jne    1160 <hexread+0x40>
mov    %ebp,%r14d
jmp    1190 <hexread+0x70>
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c", \00"
define i32 @hexread(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
%10 = load i8*, i8** %5
store i8* %10, i8** %9
%11 = load i8*, i8** %9
%12 = call i8* @strtok(i8* %11, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %12, i8** %7
br label %13
13: ; preds = %29, %3
%14 = load i8*, i8** %7
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %31
16: ; preds = %13
%17 = load i8*, i8** %7
%18 = call i64 @strtoul(i8* %17, i32* null, i32 0)
%19 = trunc i64 %18 to i8
%20 = load i8*, i8** %4
%21 = load i32, i32* %8
%22 = add nsw i32 %21, 1
store i32 %22, i32* %8
%23 = sext i32 %21 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
store i8 %19, i8* %24
%25 = load i32, i32* %8
%26 = load i32, i32* %6
%27 = icmp sge i32 %25, %26
br i1 %27, label %28, label %29
28: ; preds = %16
br label %31
29: ; preds = %16
%30 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %30, i8** %7
br label %13
31: ; preds = %28, %13
%32 = load i32, i32* %8
ret i32 %32
}

LLVM IR (-O2):
@.str = [3 x i8] c", \00"
define i32 @hexread(i8* nocapture writeonly %0, i8* %1, i32 %2) {
%4 = tail call i8* @strtok(i8* %1, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %23, label %6
6: ; preds = %3
%7 = call i32 @llvm.smax.i32(i32 %2, i32 1)
%8 = add nsw i32 %7, -1
%9 = zext i32 %8 to i64
br label %10
10: ; preds = %6, %18
%11 = phi i64 [ 0, %6 ], [ %15, %18 ]
%12 = phi i8* [ %4, %6 ], [ %19, %18 ]
%13 = tail call i64 @strtoul(i8* nocapture %12, i32* null, i32 0)
%14 = trunc i64 %13 to i8
%15 = add nuw nsw i64 %11, 1
%16 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 %14, i8* %16
%17 = icmp eq i64 %11, %9
br i1 %17, label %23, label %18
18: ; preds = %10
%19 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i8* %19, null
br i1 %20, label %21, label %10
21: ; preds = %18
%22 = trunc i64 %15 to i32
br label %23
23: ; preds = %10, %21, %3
%24 = phi i32 [ 0, %3 ], [ %22, %21 ], [ %7, %10 ]
ret i32 %24
}