#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__ strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int hexread(char *buffer, char *string, int buflen) {
  char *s;
  int pos = 0;
  while ((s = strtok(string, ", ")) != NULL && pos < buflen) {
    string = NULL;
    buffer[pos++] = (char)strtol(s, NULL, 0);
  }
  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),%rdi
lea    0xebb(%rip),%rsi
call   1040 <strtok@plt>
mov    %rax,%rcx
mov    %rcx,-0x20(%rbp)
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x25(%rbp)
je     116c <hexread+0x4c>
mov    -0x24(%rbp),%eax
cmp    -0x14(%rbp),%eax
setl   %al
mov    %al,-0x25(%rbp)
mov    -0x25(%rbp),%al
test   $0x1,%al
jne    117c <hexread+0x5c>
jmp    11af <hexread+0x8f>
movq   $0x0,-0x10(%rbp)
mov    -0x20(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1030 <strtol@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)
jmp    113a <hexread+0x1a>
mov    -0x24(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<hexread>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%rax
mov    %rdi,%r14
lea    0xeca(%rip),%rsi
mov    %rax,%rdi
call   1040 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1188 <hexread+0x68>
test   %ebp,%ebp
jle    1188 <hexread+0x68>
mov    %ebp,%ebp
xor    %ebx,%ebx
lea    0xeac(%rip),%r15
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %rax,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1030 <strtol@plt>
mov    %al,(%r14,%rbx,1)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r15,%rsi
call   1040 <strtok@plt>
test   %rax,%rax
je     1188 <hexread+0x68>
cmp    %rbp,%rbx
jb     1160 <hexread+0x40>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
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
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 0, i32* %8
br label %9
9: ; preds = %19, %3
%10 = load i8*, i8** %5
%11 = call i8* @strtok(i8* %10, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
store i8* %11, i8** %7
%12 = icmp ne i8* %11, null
br i1 %12, label %13, label %17
13: ; preds = %9
%14 = load i32, i32* %8
%15 = load i32, i32* %6
%16 = icmp slt i32 %14, %15
br label %17
17: ; preds = %13, %9
%18 = phi i1 [ false, %9 ], [ %16, %13 ]
br i1 %18, label %19, label %28
19: ; preds = %17
store i8* null, i8** %5
%20 = load i8*, i8** %7
%21 = call i64 @strtol(i8* %20, i32* null, i32 0)
%22 = trunc i64 %21 to i8
%23 = load i8*, i8** %4
%24 = load i32, i32* %8
%25 = add nsw i32 %24, 1
store i32 %25, i32* %8
%26 = sext i32 %24 to i64
%27 = getelementptr inbounds i8, i8* %23, i64 %26
store i8 %22, i8* %27
br label %9
28: ; preds = %17
%29 = load i32, i32* %8
ret i32 %29
}

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 ne i8* %4, null
%6 = icmp sgt i32 %2, 0
%7 = and i1 %5, %6
br i1 %7, label %8, label %23
8: ; preds = %3
%9 = zext i32 %2 to i64
br label %10
10: ; preds = %8, %10
%11 = phi i64 [ 0, %8 ], [ %15, %10 ]
%12 = phi i8* [ %4, %8 ], [ %17, %10 ]
%13 = tail call i64 @strtol(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 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
%18 = icmp ne i8* %17, null
%19 = icmp ult i64 %15, %9
%20 = select i1 %18, i1 %19, i1 false
br i1 %20, label %10, label %21
21: ; preds = %10
%22 = trunc i64 %15 to i32
br label %23
23: ; preds = %21, %3
%24 = phi i32 [ 0, %3 ], [ %22, %21 ]
ret i32 %24
}