#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 */
char *strchr(char *, char);
int strlen(char *);
int twiddler_chord_to_int(char *chord) {
  char *convert = "0LMR";
  char *tmp;
  int result = 0;
  int shift = 0;
  if (strlen(chord) != 4)
    return -1;
  while (*chord) {
    if (!(tmp = strchr(convert, *chord)))
      return -1;
    result |= (tmp - convert) << shift;
    chord++;
    shift += 2;
  }
  return result;
}


x86 assembly (clang, -O0):
<twiddler_chord_to_int>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    0xecd(%rip),%rax
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x24(%rbp)
movl   $0x0,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
cmp    $0x4,%eax
je     1163 <twiddler_chord_to_int+0x43>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <twiddler_chord_to_int+0xbd>
jmp    1168 <twiddler_chord_to_int+0x48>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
je     11d7 <twiddler_chord_to_int+0xb7>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movsbl (%rax),%esi
call   1040 <strchr@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    119f <twiddler_chord_to_int+0x7f>
movl   $0xffffffff,-0x4(%rbp)
jmp    11dd <twiddler_chord_to_int+0xbd>
mov    -0x20(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    %rcx,%rax
mov    -0x28(%rbp),%ecx
shl    %cl,%rax
mov    %rax,%rcx
movslq -0x24(%rbp),%rax
or     %rcx,%rax
mov    %eax,-0x24(%rbp)
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x28(%rbp),%eax
add    $0x2,%eax
mov    %eax,-0x28(%rbp)
jmp    1168 <twiddler_chord_to_int+0x48>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<twiddler_chord_to_int>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1030 <strlen@plt>
cmp    $0x4,%eax
jne    117c <twiddler_chord_to_int+0x5c>
mov    (%rbx),%al
test   %al,%al
je     1184 <twiddler_chord_to_int+0x64>
add    $0x1,%rbx
xor    %ebp,%ebp
lea    0xeb9(%rip),%r15
xor    %r14d,%r14d
nopw   0x0(%rax,%rax,1)
movsbl %al,%esi
mov    %r15,%rdi
call   1040 <strchr@plt>
test   %rax,%rax
je     117c <twiddler_chord_to_int+0x5c>
sub    %r15,%rax
mov    %ebp,%ecx
shl    %cl,%rax
or     %eax,%r14d
add    $0x2,%rbp
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    1150 <twiddler_chord_to_int+0x30>
jmp    1187 <twiddler_chord_to_int+0x67>
mov    $0xffffffff,%r14d
jmp    1187 <twiddler_chord_to_int+0x67>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [5 x i8] c"0LMR\00"
define i32 @twiddler_chord_to_int(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
store i8* %0, i8** %3
store i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8** %4
store i32 0, i32* %6
store i32 0, i32* %7
%8 = load i8*, i8** %3
%9 = call i32 @strlen(i8* %8)
%10 = icmp ne i32 %9, 4
br i1 %10, label %11, label %12
11: ; preds = %1
store i32 -1, i32* %2
br label %43
12: ; preds = %1
br label %13
13: ; preds = %24, %12
%14 = load i8*, i8** %3
%15 = load i8, i8* %14
%16 = icmp ne i8 %15, 0
br i1 %16, label %17, label %41
17: ; preds = %13
%18 = load i8*, i8** %4
%19 = load i8*, i8** %3
%20 = load i8, i8* %19
%21 = call i8* @strchr(i8* %18, i8 signext %20)
store i8* %21, i8** %5
%22 = icmp ne i8* %21, null
br i1 %22, label %24, label %23
23: ; preds = %17
store i32 -1, i32* %2
br label %43
24: ; preds = %17
%25 = load i8*, i8** %5
%26 = load i8*, i8** %4
%27 = ptrtoint i8* %25 to i64
%28 = ptrtoint i8* %26 to i64
%29 = sub i64 %27, %28
%30 = load i32, i32* %7
%31 = zext i32 %30 to i64
%32 = shl i64 %29, %31
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = or i64 %34, %32
%36 = trunc i64 %35 to i32
store i32 %36, i32* %6
%37 = load i8*, i8** %3
%38 = getelementptr inbounds i8, i8* %37, i32 1
store i8* %38, i8** %3
%39 = load i32, i32* %7
%40 = add nsw i32 %39, 2
store i32 %40, i32* %7
br label %13
41: ; preds = %13
%42 = load i32, i32* %6
store i32 %42, i32* %2
br label %43
43: ; preds = %41, %23, %11
%44 = load i32, i32* %2
ret i32 %44
}

LLVM IR (-O2):
@.str = [5 x i8] c"0LMR\00"
define i32 @twiddler_chord_to_int(i8* %0) {
%2 = tail call i32 @strlen(i8* %0)
%3 = icmp eq i32 %2, 4
br i1 %3, label %4, label %24
4: ; preds = %1
%5 = load i8, i8* %0
%6 = icmp eq i8 %5, 0
br i1 %6, label %24, label %7
7: ; preds = %4, %14
%8 = phi i64 [ %21, %14 ], [ 0, %4 ]
%9 = phi i8 [ %22, %14 ], [ %5, %4 ]
%10 = phi i32 [ %19, %14 ], [ 0, %4 ]
%11 = phi i8* [ %20, %14 ], [ %0, %4 ]
%12 = tail call i8* @strchr(i8* dereferenceable(5) getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0), i8 signext %9)
%13 = icmp eq i8* %12, null
br i1 %13, label %24, label %14
14: ; preds = %7
%15 = ptrtoint i8* %12 to i64
%16 = sub i64 %15, ptrtoint ([5 x i8]* @.str to i64)
%17 = shl i64 %16, %8
%18 = trunc i64 %17 to i32
%19 = or i32 %10, %18
%20 = getelementptr inbounds i8, i8* %11, i64 1
%21 = add nuw i64 %8, 2
%22 = load i8, i8* %20
%23 = icmp eq i8 %22, 0
br i1 %23, label %24, label %7
24: ; preds = %7, %14, %4, %1
%25 = phi i32 [ -1, %1 ], [ 0, %4 ], [ -1, %7 ], [ %19, %14 ]
ret i32 %25
}