#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 scalar_t__ u_int8_t;

/* Variables and functions */
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
char *strtok(char *, char *);
int strtoul(char *, char **, int);
int parse_mac_from_str(char *mac, u_int8_t *addr) {
  char tmpMac[(17) + 1];
  char *tmpField;
  int fieldNum = 0;
  if (strlen(mac) != (17)) {
    fprintf(stderr, "invalid MAC length\n");
    return (1);
  }
  if (addr == NULL) {
    fprintf(stderr, "invalid  output addr array\n");
    return (1);
  }
  strcpy(tmpMac, mac);
  tmpField = strtok(tmpMac, ":");
  while (tmpField != NULL && fieldNum < (6)) {
    char *chk;
    int tmpVal;
    tmpVal = strtoul(tmpField, &chk, (16));
    if (tmpVal > 0xff) {
      fprintf(stderr, "field %d value %X out of range\n", fieldNum, tmpVal);
      return (1);
    }
    if (*chk != 0) {
      fprintf(stderr, "Non-digit character %c (%0x) detected in field %d\n",
              *chk, *chk, fieldNum);
      return (1);
    }
    addr[fieldNum++] = (u_int8_t)tmpVal;
    tmpField = strtok(NULL, ":");
  }
  if (tmpField != NULL || fieldNum != (6)) {
    fprintf(stderr, "MAC address longer than six fields\n");
    return (1);
  }
  return (0);
}


x86 assembly (clang, -O0):
<parse_mac_from_str>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movl   $0x0,-0x3c(%rbp)
mov    -0x10(%rbp),%rdi
call   1040 <strlen@plt>
cmp    $0x11,%eax
je     119c <parse_mac_from_str+0x4c>
mov    0x2e60(%rip),%rax
mov    (%rax),%edi
lea    0xe77(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1317 <parse_mac_from_str+0x1c7>
cmpq   $0x0,-0x18(%rbp)
jne    11ca <parse_mac_from_str+0x7a>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe5d(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1317 <parse_mac_from_str+0x1c7>
lea    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1030 <strcpy@plt>
lea    -0x30(%rbp),%rdi
lea    0xe4e(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x38(%rbp)
xor    %eax,%eax
cmpq   $0x0,-0x38(%rbp)
mov    %al,-0x4d(%rbp)
je     1205 <parse_mac_from_str+0xb5>
cmpl   $0x6,-0x3c(%rbp)
setl   %al
mov    %al,-0x4d(%rbp)
mov    -0x4d(%rbp),%al
test   $0x1,%al
jne    1215 <parse_mac_from_str+0xc5>
jmp    12d8 <parse_mac_from_str+0x188>
mov    -0x38(%rbp),%rdi
lea    -0x48(%rbp),%rsi
mov    $0x10,%edx
call   1070 <strtoul@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xff,-0x4c(%rbp)
jle    1260 <parse_mac_from_str+0x110>
mov    0x2da2(%rip),%rax
mov    (%rax),%edi
mov    -0x3c(%rbp),%edx
mov    -0x4c(%rbp),%ecx
lea    0xde5(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1317 <parse_mac_from_str+0x1c7>
mov    -0x48(%rbp),%rax
movsbl (%rax),%eax
cmp    $0x0,%eax
je     12a5 <parse_mac_from_str+0x155>
mov    0x2d69(%rip),%rax
mov    (%rax),%edi
mov    -0x48(%rbp),%rax
movsbl (%rax),%edx
mov    -0x48(%rbp),%rax
movsbl (%rax),%ecx
mov    -0x3c(%rbp),%r8d
lea    0xdc0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1317 <parse_mac_from_str+0x1c7>
movslq -0x4c(%rbp),%rdx
mov    -0x18(%rbp),%rax
mov    -0x3c(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x3c(%rbp)
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
xor    %eax,%eax
mov    %eax,%edi
lea    0xd66(%rip),%rsi
call   1060 <strtok@plt>
mov    %rax,-0x38(%rbp)
jmp    11eb <parse_mac_from_str+0x9b>
cmpq   $0x0,-0x38(%rbp)
jne    12ed <parse_mac_from_str+0x19d>
cmpl   $0x6,-0x3c(%rbp)
je     1310 <parse_mac_from_str+0x1c0>
mov    0x2cec(%rip),%rax
mov    (%rax),%edi
lea    0xd88(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1317 <parse_mac_from_str+0x1c7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_mac_from_str>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x30,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1040 <strlen@plt>
cmp    $0x11,%eax
jne    121b <parse_mac_from_str+0xcb>
test   %r14,%r14
je     122d <parse_mac_from_str+0xdd>
lea    0x10(%rsp),%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1030 <strcpy@plt>
lea    0xe9f(%rip),%rsi
mov    %r15,%rdi
call   1060 <strtok@plt>
test   %rax,%rax
je     1209 <parse_mac_from_str+0xb9>
xor    %r13d,%r13d
lea    0x8(%rsp),%r15
lea    0xe83(%rip),%r12
nopl   (%rax)
mov    %rax,%rdi
mov    %r15,%rsi
mov    $0x10,%edx
call   1070 <strtoul@plt>
cmp    $0x100,%eax
jge    1257 <parse_mac_from_str+0x107>
mov    0x8(%rsp),%rcx
movsbl (%rcx),%ecx
test   %ecx,%ecx
jne    1275 <parse_mac_from_str+0x125>
cltq
lea    0x1(%r13),%rbx
mov    %rax,(%r14,%r13,8)
xor    %edi,%edi
mov    %r12,%rsi
call   1060 <strtok@plt>
test   %rax,%rax
je     11fd <parse_mac_from_str+0xad>
cmp    $0x5,%r13
mov    %rbx,%r13
jb     11b0 <parse_mac_from_str+0x60>
test   %rax,%rax
jne    1209 <parse_mac_from_str+0xb9>
xor    %eax,%eax
cmp    $0x6,%ebx
je     1249 <parse_mac_from_str+0xf9>
mov    0x2dd0(%rip),%rax
mov    (%rax),%edi
lea    0xe6c(%rip),%rsi
jmp    123d <parse_mac_from_str+0xed>
mov    0x2dbe(%rip),%rax
mov    (%rax),%edi
lea    0xdd5(%rip),%rsi
jmp    123d <parse_mac_from_str+0xed>
mov    0x2dac(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2d82(%rip),%rcx
mov    (%rcx),%edi
lea    0xdcb(%rip),%rsi
mov    %r13d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1244 <parse_mac_from_str+0xf4>
mov    0x2d64(%rip),%rax
mov    (%rax),%edi
lea    0xdcd(%rip),%rsi
mov    %ecx,%edx
mov    %r13d,%r8d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1244 <parse_mac_from_str+0xf4>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [20 x i8] c"invalid MAC length\0A\00"
@.str.1 = [28 x i8] c"invalid output addr array\0A\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [32 x i8] c"field %d value %X out of range\0A\00"
@.str.4 = [51 x i8] c"Non-digit character %c (%0x) detected in field %d\0A\00"
@.str.5 = [36 x i8] c"MAC address longer than six fields\0A\00"
define i32 @parse_mac_from_str(i8* %0, i64* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64*
%6 = alloca [18 x i8]
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i8*
%10 = alloca i32
store i8* %0, i8** %4
store i64* %1, i64** %5
store i32 0, i32* %8
%11 = load i8*, i8** %4
%12 = call i32 @strlen(i8* %11)
%13 = icmp ne i32 %12, 17
br i1 %13, label %14, label %17
14: ; preds = %2
%15 = load i32, i32* @stderr
%16 = call i32 (i32, i8*, ...) @fprintf(i32 %15, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %81
17: ; preds = %2
%18 = load i64*, i64** %5
%19 = icmp eq i64* %18, null
br i1 %19, label %20, label %23
20: ; preds = %17
%21 = load i32, i32* @stderr
%22 = call i32 (i32, i8*, ...) @fprintf(i32 %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %81
23: ; preds = %17
%24 = getelementptr inbounds [18 x i8], [18 x i8]* %6, i64 0, i64 0
%25 = load i8*, i8** %4
%26 = call i32 @strcpy(i8* %24, i8* %25)
%27 = getelementptr inbounds [18 x i8], [18 x i8]* %6, i64 0, i64 0
%28 = call i8* @strtok(i8* %27, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %28, i8** %7
br label %29
29: ; preds = %62, %23
%30 = load i8*, i8** %7
%31 = icmp ne i8* %30, null
br i1 %31, label %32, label %35
32: ; preds = %29
%33 = load i32, i32* %8
%34 = icmp slt i32 %33, 6
br label %35
35: ; preds = %32, %29
%36 = phi i1 [ false, %29 ], [ %34, %32 ]
br i1 %36, label %37, label %71
37: ; preds = %35
%38 = load i8*, i8** %7
%39 = call i32 @strtoul(i8* %38, i8** %9, i32 16)
store i32 %39, i32* %10
%40 = load i32, i32* %10
%41 = icmp sgt i32 %40, 255
br i1 %41, label %42, label %47
42: ; preds = %37
%43 = load i32, i32* @stderr
%44 = load i32, i32* %8
%45 = load i32, i32* %10
%46 = call i32 (i32, i8*, ...) @fprintf(i32 %43, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %44, i32 %45)
store i32 1, i32* %3
br label %81
47: ; preds = %37
%48 = load i8*, i8** %9
%49 = load i8, i8* %48
%50 = sext i8 %49 to i32
%51 = icmp ne i32 %50, 0
br i1 %51, label %52, label %62
52: ; preds = %47
%53 = load i32, i32* @stderr
%54 = load i8*, i8** %9
%55 = load i8, i8* %54
%56 = sext i8 %55 to i32
%57 = load i8*, i8** %9
%58 = load i8, i8* %57
%59 = sext i8 %58 to i32
%60 = load i32, i32* %8
%61 = call i32 (i32, i8*, ...) @fprintf(i32 %53, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i32 %56, i32 %59, i32 %60)
store i32 1, i32* %3
br label %81
62: ; preds = %47
%63 = load i32, i32* %10
%64 = sext i32 %63 to i64
%65 = load i64*, i64** %5
%66 = load i32, i32* %8
%67 = add nsw i32 %66, 1
store i32 %67, i32* %8
%68 = sext i32 %66 to i64
%69 = getelementptr inbounds i64, i64* %65, i64 %68
store i64 %64, i64* %69
%70 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i8* %70, i8** %7
br label %29
71: ; preds = %35
%72 = load i8*, i8** %7
%73 = icmp ne i8* %72, null
br i1 %73, label %77, label %74
74: ; preds = %71
%75 = load i32, i32* %8
%76 = icmp ne i32 %75, 6
br i1 %76, label %77, label %80
77: ; preds = %74, %71
%78 = load i32, i32* @stderr
%79 = call i32 (i32, i8*, ...) @fprintf(i32 %78, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %81
80: ; preds = %74
store i32 0, i32* %3
br label %81
81: ; preds = %80, %77, %52, %42, %20, %14
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [20 x i8] c"invalid MAC length\0A\00"
@.str.1 = [28 x i8] c"invalid output addr array\0A\00"
@.str.2 = [2 x i8] c":\00"
@.str.3 = [32 x i8] c"field %d value %X out of range\0A\00"
@.str.4 = [51 x i8] c"Non-digit character %c (%0x) detected in field %d\0A\00"
@.str.5 = [36 x i8] c"MAC address longer than six fields\0A\00"
define i32 @parse_mac_from_str(i8* %0, i64* writeonly %1) {
%3 = alloca [18 x i8]
%4 = alloca i8*
%5 = getelementptr inbounds [18 x i8], [18 x i8]* %3, i64 0, i64 0
%6 = tail call i32 @strlen(i8* %0)
%7 = icmp eq i32 %6, 17
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0))
br label %56
11: ; preds = %2
%12 = icmp eq i64* %1, null
br i1 %12, label %13, label %16
13: ; preds = %11
%14 = load i32, i32* @stderr
%15 = tail call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %56
16: ; preds = %11
%17 = call i32 @strcpy(i8* %5, i8* %0)
%18 = call i8* @strtok(i8* %5, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%19 = icmp eq i8* %18, null
br i1 %19, label %53, label %20
20: ; preds = %16
%21 = bitcast i8** %4 to i8*
br label %22
22: ; preds = %20, %41
%23 = phi i64 [ 0, %20 ], [ %43, %41 ]
%24 = phi i8* [ %18, %20 ], [ %45, %41 ]
%25 = call i32 @strtoul(i8* %24, i8** %4, i32 16)
%26 = icmp sgt i32 %25, 255
br i1 %26, label %27, label %31
27: ; preds = %22
%28 = trunc i64 %23 to i32
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i32 %28, i32 %25)
br label %40
31: ; preds = %22
%32 = load i8*, i8** %4
%33 = load i8, i8* %32
%34 = icmp eq i8 %33, 0
br i1 %34, label %41, label %35
35: ; preds = %31
%36 = trunc i64 %23 to i32
%37 = sext i8 %33 to i32
%38 = load i32, i32* @stderr
%39 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([51 x i8], [51 x i8]* @.str.4, i64 0, i64 0), i32 %37, i32 %37, i32 %36)
br label %40
40: ; preds = %27, %35
br label %56
41: ; preds = %31
%42 = sext i32 %25 to i64
%43 = add nuw nsw i64 %23, 1
%44 = getelementptr inbounds i64, i64* %1, i64 %23
store i64 %42, i64* %44
%45 = call i8* @strtok(i8* null, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%46 = icmp ne i8* %45, null
%47 = icmp ult i64 %23, 5
%48 = select i1 %46, i1 %47, i1 false
br i1 %48, label %22, label %49
49: ; preds = %41
%50 = trunc i64 %43 to i32
%51 = icmp ne i32 %50, 6
%52 = select i1 %46, i1 true, i1 %51
br i1 %52, label %53, label %56
53: ; preds = %16, %49
%54 = load i32, i32* @stderr
%55 = call i32 (i32, i8*, ...) @fprintf(i32 %54, i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.5, i64 0, i64 0))
br label %56
56: ; preds = %40, %49, %53, %13, %8
%57 = phi i32 [ 1, %8 ], [ 1, %13 ], [ 1, %53 ], [ 0, %49 ], [ 1, %40 ]
ret i32 %57
}