#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 int uint32_t;

/* Variables and functions */
int strlen(char const *);
int strtoul(char const *, char **, int);
bool parse_hex_color(const char *str, uint32_t *val) {
  int len = strlen(str);
  if (len != 7 && len != 9) {
    return false;
  }
  char *end;
  *val = strtoul(str + 1, &end, 16);
  if (*end) {
    return false;
  }
  if (len == 7) {
    *val = (*val << 8) | 0xFF;
  }
  return true;
}


x86 assembly (clang, -O0):
<parse_hex_color>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x7,-0x1c(%rbp)
je     115c <parse_hex_color+0x3c>
cmpl   $0x9,-0x1c(%rbp)
je     115c <parse_hex_color+0x3c>
movl   $0x0,-0x4(%rbp)
jmp    11b9 <parse_hex_color+0x99>
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1040 <strtoul@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     1193 <parse_hex_color+0x73>
movl   $0x0,-0x4(%rbp)
jmp    11b9 <parse_hex_color+0x99>
cmpl   $0x7,-0x1c(%rbp)
jne    11b2 <parse_hex_color+0x92>
mov    -0x18(%rbp),%rax
mov    (%rax),%ecx
shl    $0x8,%ecx
or     $0xff,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<parse_hex_color>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
mov    %rdi,%rbx
call   1030 <strlen@plt>
mov    %eax,%ebp
cmp    $0x9,%eax
je     1141 <parse_hex_color+0x21>
xor    %eax,%eax
cmp    $0x7,%ebp
jne    117e <parse_hex_color+0x5e>
add    $0x1,%rbx
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x10,%edx
call   1040 <strtoul@plt>
mov    %eax,%ecx
mov    %eax,(%r14)
mov    0x8(%rsp),%rdx
xor    %eax,%eax
cmpb   $0x0,(%rdx)
jne    117e <parse_hex_color+0x5e>
mov    $0x1,%eax
cmp    $0x7,%ebp
jne    117e <parse_hex_color+0x5e>
shl    $0x8,%ecx
or     $0xff,%ecx
mov    %ecx,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @parse_hex_color(i8* %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca i32
%7 = alloca i8*
store i8* %0, i8** %4
store i32* %1, i32** %5
%8 = load i8*, i8** %4
%9 = call i32 @strlen(i8* %8)
store i32 %9, i32* %6
%10 = load i32, i32* %6
%11 = icmp ne i32 %10, 7
br i1 %11, label %12, label %16
12: ; preds = %2
%13 = load i32, i32* %6
%14 = icmp ne i32 %13, 9
br i1 %14, label %15, label %16
15: ; preds = %12
store i32 0, i32* %3
br label %35
16: ; preds = %12, %2
%17 = load i8*, i8** %4
%18 = getelementptr inbounds i8, i8* %17, i64 1
%19 = call i32 @strtoul(i8* %18, i8** %7, i32 16)
%20 = load i32*, i32** %5
store i32 %19, i32* %20
%21 = load i8*, i8** %7
%22 = load i8, i8* %21
%23 = icmp ne i8 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %16
store i32 0, i32* %3
br label %35
25: ; preds = %16
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, 7
br i1 %27, label %28, label %34
28: ; preds = %25
%29 = load i32*, i32** %5
%30 = load i32, i32* %29
%31 = shl i32 %30, 8
%32 = or i32 %31, 255
%33 = load i32*, i32** %5
store i32 %32, i32* %33
br label %34
34: ; preds = %28, %25
store i32 1, i32* %3
br label %35
35: ; preds = %34, %24, %15
%36 = load i32, i32* %3
ret i32 %36
}

LLVM IR (-O2):
define i32 @parse_hex_color(i8* %0, i32* nocapture writeonly %1) {
%3 = alloca i8*
%4 = tail call i32 @strlen(i8* %0)
switch i32 %4, label %19 [
i32 9, label %5
i32 7, label %5
]
5: ; preds = %2, %2
%6 = bitcast i8** %3 to i8*
%7 = getelementptr inbounds i8, i8* %0, i64 1
%8 = call i32 @strtoul(i8* %7, i8** %3, i32 16)
store i32 %8, i32* %1
%9 = load i8*, i8** %3
%10 = load i8, i8* %9
%11 = icmp eq i8 %10, 0
br i1 %11, label %12, label %17
12: ; preds = %5
%13 = icmp eq i32 %4, 7
br i1 %13, label %14, label %17
14: ; preds = %12
%15 = shl i32 %8, 8
%16 = or i32 %15, 255
store i32 %16, i32* %1
br label %17
17: ; preds = %12, %14, %5
%18 = phi i32 [ 0, %5 ], [ 1, %14 ], [ 1, %12 ]
br label %19
19: ; preds = %2, %17
%20 = phi i32 [ %18, %17 ], [ 0, %2 ]
ret i32 %20
}