#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*/ free(char *);
scalar_t__ malloc(size_t);
scalar_t__ strcmp(char const *, char *);
int /*<<< orphan*/ strcpy(char *, char const *);
size_t strlen(char const *);
char tolower(char);
int to_boolean(const char *val) {
  size_t len = strlen(val);
  char *dup = (char *)malloc((len + 1) * sizeof(char));
  int result;
  if (dup) {
    char *c;
    strcpy(dup, val);
    for (c = dup; *c; c += 1) {
      *c = tolower(*c);
    }
    val = dup;
  }
  if (len > 5) {
    result = -1;
  } else if (!(strcmp(val, "1") && strcmp(val, "yes") && strcmp(val, "true"))) {
    result = 1;
  } else if (!(strcmp(val, "0") && strcmp(val, "no") && strcmp(val, "false"))) {
    result = 0;
  } else {
    result = -1;
  }
  free(dup);
  return result;
}


x86 assembly (clang, -O0):
<to_boolean>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
call   1050 <strlen@plt>
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
add    $0x1,%rdi
shl    $0x0,%rdi
call   1080 <malloc@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
je     11e8 <to_boolean+0x88>
mov    -0x18(%rbp),%rdi
mov    -0x8(%rbp),%rsi
call   1040 <strcpy@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmpb   $0x0,(%rax)
je     11e0 <to_boolean+0x80>
mov    -0x28(%rbp),%rax
movsbl (%rax),%edi
call   1070 <tolower@plt>
mov    %al,%cl
mov    -0x28(%rbp),%rax
mov    %cl,(%rax)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11ae <to_boolean+0x4e>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
cmpq   $0x5,-0x10(%rbp)
jbe    11ff <to_boolean+0x9f>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12c4 <to_boolean+0x164>
mov    -0x8(%rbp),%rdi
lea    0xdf6(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     124d <to_boolean+0xed>
mov    -0x8(%rbp),%rdi
lea    0xdde(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     124d <to_boolean+0xed>
mov    -0x8(%rbp),%rdi
lea    0xdc8(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    1259 <to_boolean+0xf9>
movl   $0x1,-0x1c(%rbp)
jmp    12bf <to_boolean+0x15f>
mov    -0x8(%rbp),%rdi
lea    0xda7(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12a7 <to_boolean+0x147>
mov    -0x8(%rbp),%rdi
lea    0xd8f(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     12a7 <to_boolean+0x147>
mov    -0x8(%rbp),%rdi
lea    0xd78(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
jne    12b3 <to_boolean+0x153>
movl   $0x0,-0x1c(%rbp)
jmp    12ba <to_boolean+0x15a>
movl   $0xffffffff,-0x1c(%rbp)
jmp    12bf <to_boolean+0x15f>
jmp    12c4 <to_boolean+0x164>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<to_boolean>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdi,%rbx
call   1050 <strlen@plt>
mov    %rax,%rbp
lea    0x1(%rax),%rdi
call   1080 <malloc@plt>
mov    %rax,%r14
test   %rax,%rax
je     11b9 <to_boolean+0x59>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1040 <strcpy@plt>
mov    (%r14),%al
test   %al,%al
je     11b6 <to_boolean+0x56>
mov    %r14,%rbx
add    $0x1,%rbx
nopl   0x0(%rax)
movsbl %al,%edi
call   1070 <tolower@plt>
mov    %al,-0x1(%rbx)
movzbl (%rbx),%eax
add    $0x1,%rbx
test   %al,%al
jne    11a0 <to_boolean+0x40>
mov    %r14,%rbx
mov    $0xffffffff,%r15d
cmp    $0x5,%rbp
ja     124b <to_boolean+0xeb>
lea    0xe30(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
mov    $0x1,%r15d
test   %rax,%rax
je     124b <to_boolean+0xeb>
lea    0xe18(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     124b <to_boolean+0xeb>
lea    0xe08(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     124b <to_boolean+0xeb>
lea    0xdf9(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
xor    %r15d,%r15d
test   %rax,%rax
je     124b <to_boolean+0xeb>
lea    0xde4(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
test   %rax,%rax
je     124b <to_boolean+0xeb>
lea    0xdd3(%rip),%rsi
mov    %rbx,%rdi
call   1060 <strcmp@plt>
neg    %rax
sbb    %r15d,%r15d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"1\00"
@.str.1 = [4 x i8] c"yes\00"
@.str.2 = [5 x i8] c"true\00"
@.str.3 = [2 x i8] c"0\00"
@.str.4 = [3 x i8] c"no\00"
@.str.5 = [6 x i8] c"false\00"
define i32 @to_boolean(i8* %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %2
%7 = load i8*, i8** %2
%8 = call i64 @strlen(i8* %7)
store i64 %8, i64* %3
%9 = load i64, i64* %3
%10 = add i64 %9, 1
%11 = mul i64 %10, 1
%12 = call i64 @malloc(i64 %11)
%13 = inttoptr i64 %12 to i8*
store i8* %13, i8** %4
%14 = load i8*, i8** %4
%15 = icmp ne i8* %14, null
br i1 %15, label %16, label %35
16: ; preds = %1
%17 = load i8*, i8** %4
%18 = load i8*, i8** %2
%19 = call i32 @strcpy(i8* %17, i8* %18)
%20 = load i8*, i8** %4
store i8* %20, i8** %6
br label %21
21: ; preds = %30, %16
%22 = load i8*, i8** %6
%23 = load i8, i8* %22
%24 = icmp ne i8 %23, 0
br i1 %24, label %25, label %33
25: ; preds = %21
%26 = load i8*, i8** %6
%27 = load i8, i8* %26
%28 = call signext i8 @tolower(i8 signext %27)
%29 = load i8*, i8** %6
store i8 %28, i8* %29
br label %30
30: ; preds = %25
%31 = load i8*, i8** %6
%32 = getelementptr inbounds i8, i8* %31, i64 1
store i8* %32, i8** %6
br label %21
33: ; preds = %21
%34 = load i8*, i8** %4
store i8* %34, i8** %2
br label %35
35: ; preds = %33, %1
%36 = load i64, i64* %3
%37 = icmp ugt i64 %36, 5
br i1 %37, label %38, label %39
38: ; preds = %35
store i32 -1, i32* %5
br label %68
39: ; preds = %35
%40 = load i8*, i8** %2
%41 = call i64 @strcmp(i8* %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %51
43: ; preds = %39
%44 = load i8*, i8** %2
%45 = call i64 @strcmp(i8* %44, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %51
47: ; preds = %43
%48 = load i8*, i8** %2
%49 = call i64 @strcmp(i8* %48, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%50 = icmp ne i64 %49, 0
br i1 %50, label %52, label %51
51: ; preds = %47, %43, %39
store i32 1, i32* %5
br label %67
52: ; preds = %47
%53 = load i8*, i8** %2
%54 = call i64 @strcmp(i8* %53, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%55 = icmp ne i64 %54, 0
br i1 %55, label %56, label %64
56: ; preds = %52
%57 = load i8*, i8** %2
%58 = call i64 @strcmp(i8* %57, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %56
%61 = load i8*, i8** %2
%62 = call i64 @strcmp(i8* %61, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%63 = icmp ne i64 %62, 0
br i1 %63, label %65, label %64
64: ; preds = %60, %56, %52
store i32 0, i32* %5
br label %66
65: ; preds = %60
store i32 -1, i32* %5
br label %66
66: ; preds = %65, %64
br label %67
67: ; preds = %66, %51
br label %68
68: ; preds = %67, %38
%69 = load i8*, i8** %4
%70 = call i32 @free(i8* %69)
%71 = load i32, i32* %5
ret i32 %71
}

LLVM IR (-O2):
@.str = [2 x i8] c"1\00"
@.str.1 = [4 x i8] c"yes\00"
@.str.2 = [5 x i8] c"true\00"
@.str.3 = [2 x i8] c"0\00"
@.str.4 = [3 x i8] c"no\00"
@.str.5 = [6 x i8] c"false\00"
define i32 @to_boolean(i8* %0) {
%2 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%3 = add i64 %2, 1
%4 = tail call i64 @malloc(i64 %3)
%5 = inttoptr i64 %4 to i8*
%6 = icmp eq i64 %4, 0
br i1 %6, label %18, label %7
7: ; preds = %1
%8 = tail call i32 @strcpy(i8* %5, i8* %0)
%9 = load i8, i8* %5
%10 = icmp eq i8 %9, 0
br i1 %10, label %18, label %11
11: ; preds = %7, %11
%12 = phi i8 [ %16, %11 ], [ %9, %7 ]
%13 = phi i8* [ %15, %11 ], [ %5, %7 ]
%14 = tail call signext i8 @tolower(i8 signext %12)
store i8 %14, i8* %13
%15 = getelementptr inbounds i8, i8* %13, i64 1
%16 = load i8, i8* %15
%17 = icmp eq i8 %16, 0
br i1 %17, label %18, label %11
18: ; preds = %11, %7, %1
%19 = phi i8* [ %0, %1 ], [ %5, %7 ], [ %5, %11 ]
%20 = icmp ugt i64 %2, 5
br i1 %20, label %40, label %21
21: ; preds = %18
%22 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %40, label %24
24: ; preds = %21
%25 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0))
%26 = icmp eq i64 %25, 0
br i1 %26, label %40, label %27
27: ; preds = %24
%28 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%29 = icmp eq i64 %28, 0
br i1 %29, label %40, label %30
30: ; preds = %27
%31 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0))
%32 = icmp eq i64 %31, 0
br i1 %32, label %40, label %33
33: ; preds = %30
%34 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0))
%35 = icmp eq i64 %34, 0
br i1 %35, label %40, label %36
36: ; preds = %33
%37 = tail call i64 @strcmp(i8* %19, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%38 = icmp ne i64 %37, 0
%39 = sext i1 %38 to i32
br label %40
40: ; preds = %36, %30, %33, %21, %24, %27, %18
%41 = phi i32 [ -1, %18 ], [ 1, %27 ], [ 1, %24 ], [ 1, %21 ], [ 0, %33 ], [ 0, %30 ], [ %39, %36 ]
%42 = tail call i32 @free(i8* %5)
ret i32 %41
}