#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 */
int /*<<< orphan*/ add_ro_region(unsigned int, unsigned int);
int fromhex(char);
int /*<<< orphan*/ sprintf(char *, char *);
void decode_ro_packet(char *buffer, char *response) {
  char ch;
  int i;
  unsigned int *address;
  unsigned int start_address;
  unsigned int end_address;
  i = 0;
  start_address = 0;
  end_address = 0;
  address = &start_address;
  while ((ch = buffer[i]) != '\0') {
    switch (ch) {
    case ',': {
      address = &end_address;
      i++;
    } break;
    case ':': {
      address = &start_address;
      add_ro_region(start_address, end_address);
      start_address = 0;
      end_address = 0;
      address = &start_address;
      i++;
    } break;
    default: {
      *address = *address << 4;
      *address |= fromhex(ch) & 0x0f;
      i++;
    }
    }
  }
  if ((ch == 0) && ((start_address != 0) || (end_address != 0))) {
    add_ro_region(start_address, end_address);
  }
  sprintf(response, "OK");
}


x86 assembly (clang, -O0):
<decode_ro_packet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x24(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x8(%rbp),%rax
movslq -0x18(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x11(%rbp)
movsbl %al,%eax
cmp    $0x0,%eax
je     121d <decode_ro_packet+0xed>
movsbl -0x11(%rbp),%eax
mov    %eax,-0x2c(%rbp)
sub    $0x2c,%eax
je     119d <decode_ro_packet+0x6d>
jmp    118c <decode_ro_packet+0x5c>
mov    -0x2c(%rbp),%eax
sub    $0x3a,%eax
je     11b3 <decode_ro_packet+0x83>
jmp    11ea <decode_ro_packet+0xba>
lea    -0x28(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1218 <decode_ro_packet+0xe8>
lea    -0x24(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1030 <add_ro_region@plt>
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
lea    -0x24(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    1218 <decode_ro_packet+0xe8>
mov    -0x20(%rbp),%rax
mov    (%rax),%ecx
shl    $0x4,%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
movsbl -0x11(%rbp),%edi
call   1040 <fromhex@plt>
mov    %eax,%ecx
and    $0xf,%ecx
mov    -0x20(%rbp),%rax
or     (%rax),%ecx
mov    %ecx,(%rax)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x18(%rbp)
jmp    115d <decode_ro_packet+0x2d>
movsbl -0x11(%rbp),%eax
cmp    $0x0,%eax
jne    1249 <decode_ro_packet+0x119>
cmpl   $0x0,-0x24(%rbp)
jne    123e <decode_ro_packet+0x10e>
cmpl   $0x0,-0x28(%rbp)
je     1249 <decode_ro_packet+0x119>
mov    -0x24(%rbp),%edi
mov    -0x28(%rbp),%esi
call   1030 <add_ro_region@plt>
mov    -0x10(%rbp),%rdi
lea    0xdac(%rip),%rsi
call   1050 <sprintf@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<decode_ro_packet>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%r14
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    (%rdi),%al
test   %al,%al
je     11be <decode_ro_packet+0x9e>
mov    %rdi,%rbx
add    $0x1,%rbx
lea    0xc(%rsp),%r15
lea    0x8(%rsp),%r12
mov    %r15,%r13
jmp    1182 <decode_ro_packet+0x62>
nopl   0x0(%rax)
shll   $0x4,0x0(%r13)
call   1040 <fromhex@plt>
and    $0xf,%eax
or     %eax,0x0(%r13)
mov    %r13,%rcx
movzbl (%rbx),%eax
add    $0x1,%rbx
mov    %rcx,%r13
test   %al,%al
je     11b4 <decode_ro_packet+0x94>
movsbl %al,%edi
mov    %r12,%rcx
cmp    $0x2c,%edi
je     1174 <decode_ro_packet+0x54>
cmp    $0x3a,%edi
jne    1160 <decode_ro_packet+0x40>
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
call   1030 <add_ro_region@plt>
movl   $0x0,0xc(%rsp)
movl   $0x0,0x8(%rsp)
mov    %r15,%rcx
jmp    1174 <decode_ro_packet+0x54>
mov    0xc(%rsp),%edi
mov    0x8(%rsp),%esi
jmp    11c2 <decode_ro_packet+0xa2>
xor    %esi,%esi
xor    %edi,%edi
mov    %edi,%eax
or     %esi,%eax
je     11cd <decode_ro_packet+0xad>
call   1030 <add_ro_region@plt>
movb   $0x0,0x2(%r14)
movw   $0x4b4f,(%r14)
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"OK\00"
define void @decode_ro_packet(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i32
%9 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %6
store i32 0, i32* %8
store i32 0, i32* %9
store i32* %8, i32** %7
br label %10
10: ; preds = %43, %2
%11 = load i8*, i8** %3
%12 = load i32, i32* %6
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = load i8, i8* %14
store i8 %15, i8* %5
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 0
br i1 %17, label %18, label %44
18: ; preds = %10
%19 = load i8, i8* %5
%20 = sext i8 %19 to i32
switch i32 %20, label %30 [
i32 44, label %21
i32 58, label %24
]
21: ; preds = %18
store i32* %9, i32** %7
%22 = load i32, i32* %6
%23 = add nsw i32 %22, 1
store i32 %23, i32* %6
br label %43
24: ; preds = %18
store i32* %8, i32** %7
%25 = load i32, i32* %8
%26 = load i32, i32* %9
%27 = call i32 @add_ro_region(i32 %25, i32 %26)
store i32 0, i32* %8
store i32 0, i32* %9
store i32* %8, i32** %7
%28 = load i32, i32* %6
%29 = add nsw i32 %28, 1
store i32 %29, i32* %6
br label %43
30: ; preds = %18
%31 = load i32*, i32** %7
%32 = load i32, i32* %31
%33 = shl i32 %32, 4
%34 = load i32*, i32** %7
store i32 %33, i32* %34
%35 = load i8, i8* %5
%36 = call i32 @fromhex(i8 signext %35)
%37 = and i32 %36, 15
%38 = load i32*, i32** %7
%39 = load i32, i32* %38
%40 = or i32 %39, %37
store i32 %40, i32* %38
%41 = load i32, i32* %6
%42 = add nsw i32 %41, 1
store i32 %42, i32* %6
br label %43
43: ; preds = %30, %24, %21
br label %10
44: ; preds = %10
%45 = load i8, i8* %5
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 0
br i1 %47, label %48, label %58
48: ; preds = %44
%49 = load i32, i32* %8
%50 = icmp ne i32 %49, 0
br i1 %50, label %54, label %51
51: ; preds = %48
%52 = load i32, i32* %9
%53 = icmp ne i32 %52, 0
br i1 %53, label %54, label %58
54: ; preds = %51, %48
%55 = load i32, i32* %8
%56 = load i32, i32* %9
%57 = call i32 @add_ro_region(i32 %55, i32 %56)
br label %58
58: ; preds = %54, %51, %44
%59 = load i8*, i8** %4
%60 = call i32 @sprintf(i8* %59, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0))
ret void
}

LLVM IR (-O2):
@.str = [3 x i8] c"OK\00"
define void @decode_ro_packet(i8* nocapture %0, i8* nocapture writeonly %1) {
%3 = alloca i32
%4 = alloca i32
%5 = bitcast i32* %3 to i8*
%6 = bitcast i32* %4 to i8*
store i32 0, i32* %3
store i32 0, i32* %4
%7 = load i8, i8* %0
%8 = icmp eq i8 %7, 0
br i1 %8, label %34, label %9
9: ; preds = %2, %25
%10 = phi i64 [ %27, %25 ], [ 0, %2 ]
%11 = phi i8 [ %29, %25 ], [ %7, %2 ]
%12 = phi i32* [ %26, %25 ], [ %3, %2 ]
%13 = sext i8 %11 to i32
switch i32 %13, label %18 [
i32 44, label %25
i32 58, label %14
]
14: ; preds = %9
%15 = load i32, i32* %3
%16 = load i32, i32* %4
%17 = tail call i32 @add_ro_region(i32 %15, i32 %16)
store i32 0, i32* %3
store i32 0, i32* %4
br label %25
18: ; preds = %9
%19 = load i32, i32* %12
%20 = shl i32 %19, 4
store i32 %20, i32* %12
%21 = tail call i32 @fromhex(i8 signext %11)
%22 = and i32 %21, 15
%23 = load i32, i32* %12
%24 = or i32 %23, %22
store i32 %24, i32* %12
br label %25
25: ; preds = %9, %18, %14
%26 = phi i32* [ %12, %18 ], [ %3, %14 ], [ %4, %9 ]
%27 = add nuw i64 %10, 1
%28 = getelementptr inbounds i8, i8* %0, i64 %27
%29 = load i8, i8* %28
%30 = icmp eq i8 %29, 0
br i1 %30, label %31, label %9
31: ; preds = %25
%32 = load i32, i32* %3
%33 = load i32, i32* %4
br label %34
34: ; preds = %31, %2
%35 = phi i32 [ %33, %31 ], [ 0, %2 ]
%36 = phi i32 [ %32, %31 ], [ 0, %2 ]
%37 = icmp ne i32 %36, 0
%38 = icmp ne i32 %35, 0
%39 = select i1 %37, i1 true, i1 %38
br i1 %39, label %40, label %42
40: ; preds = %34
%41 = tail call i32 @add_ro_region(i32 %36, i32 %35)
br label %42
42: ; preds = %34, %40
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(3) %1, i8* align 1 dereferenceable(3) getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i64 3, i1 false)
ret void
}