#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 */
unsigned long strlen(char *);
char *strtok(char *, char *);
unsigned long strtoul(char *, char **, int);
int busid_strtok(char *str, unsigned long max_len, unsigned long *val) {
  char *token, *end;
  token = strtok(str, ".");
  if (!token)
    return -1;
  if (strlen(token) > max_len)
    return -1;
  *val = strtoul(token, &end, 16);
  if (*end)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<busid_strtok>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xeb1(%rip),%rsi
call   1040 <strtok@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    116f <busid_strtok+0x3f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <busid_strtok+0x9a>
mov    -0x28(%rbp),%rdi
call   1030 <strlen@plt>
cmp    -0x18(%rbp),%rax
jbe    118e <busid_strtok+0x5e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <busid_strtok+0x9a>
mov    -0x28(%rbp),%rdi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   1050 <strtoul@plt>
mov    %rax,%rcx
mov    -0x20(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x30(%rbp),%rax
cmpb   $0x0,(%rax)
je     11c3 <busid_strtok+0x93>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ca <busid_strtok+0x9a>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<busid_strtok>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r15
lea    0xebc(%rip),%rsi
call   1040 <strtok@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
je     1180 <busid_strtok+0x50>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <strlen@plt>
cmp    %r15,%rax
ja     1180 <busid_strtok+0x50>
mov    %rsp,%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
call   1050 <strtoul@plt>
mov    %rax,(%r14)
mov    (%rsp),%rax
xor    %ecx,%ecx
cmp    (%rax),%cl
sbb    %ebp,%ebp
mov    %ebp,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

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