#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 size_t uint8_t;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
char *strdup(char const *);
char *strtok(char *, char *);
scalar_t__ strtol(char *, int /*<<< orphan*/ *, int);
size_t pack_duid(const char *str, char *dst) {
  char *tmp = strdup(str);
  char *tmp_to_free = tmp;
  char *ptr;
  uint8_t write_pos = 0;
  while ((ptr = strtok(tmp, ":"))) {
    dst[write_pos] = (uint8_t)strtol(ptr, NULL, 16);
    write_pos += 1;
    tmp = NULL;
  }
  free(tmp_to_free);
  return write_pos;
}


x86 assembly (clang, -O0):
<pack_duid>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%rdi
call   1060 <strdup@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x20(%rbp)
movq   $0x0,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
lea    0xe88(%rip),%rsi
call   1050 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     11c3 <pack_duid+0x83>
mov    -0x28(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x18(%rbp)
jmp    116d <pack_duid+0x2d>
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
mov    -0x30(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pack_duid>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rsi,%r15
call   1060 <strdup@plt>
mov    %rax,%r14
lea    0xea6(%rip),%rsi
mov    %rax,%rdi
call   1050 <strtok@plt>
xor    %ebx,%ebx
test   %rax,%rax
je     1196 <pack_duid+0x56>
lea    0xe90(%rip),%r12
mov    %rax,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1040 <strtol@plt>
mov    %al,(%r15,%rbx,1)
add    $0x1,%rbx
xor    %edi,%edi
mov    %r12,%rsi
call   1050 <strtok@plt>
test   %rax,%rax
jne    1170 <pack_duid+0x30>
mov    %r14,%rdi
call   1030 <free@plt>
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

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

LLVM IR (-O2):
@.str = [2 x i8] c":\00"
define i64 @pack_duid(i8* nocapture %0, i8* nocapture writeonly %1) {
%3 = tail call i8* @strdup(i8* %0)
%4 = tail call i8* @strtok(i8* %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i8* %4, null
br i1 %5, label %15, label %6
6: ; preds = %2, %6
%7 = phi i8* [ %13, %6 ], [ %4, %2 ]
%8 = phi i64 [ %12, %6 ], [ 0, %2 ]
%9 = tail call i64 @strtol(i8* nocapture %7, i32* null, i32 16)
%10 = trunc i64 %9 to i8
%11 = getelementptr inbounds i8, i8* %1, i64 %8
store i8 %10, i8* %11
%12 = add i64 %8, 1
%13 = tail call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%14 = icmp eq i8* %13, null
br i1 %14, label %15, label %6
15: ; preds = %6, %2
%16 = phi i64 [ 0, %2 ], [ %12, %6 ]
%17 = tail call i32 @free(i8* %3)
ret i64 %16
}