#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*/ scanf(char *, char *);
int read_string(char str[]) {
  int i = 0;
  char c;
  scanf("%c", &c);
  while ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z') ||
         (c >= '0' && c <= '9')) {
    str[i] = c;
    scanf("%c", &c);
    i++;
  }
  str[i] = '\0';
  return i;
}


x86 assembly (clang, -O0):
<read_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %rdi,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
lea    0xed6(%rip),%rdi
lea    -0xd(%rbp),%rsi
call   1030 <scanf@plt>
movsbl -0xd(%rbp),%eax
cmp    $0x41,%eax
jl     1152 <read_string+0x42>
movsbl -0xd(%rbp),%ecx
mov    $0x1,%al
cmp    $0x5a,%ecx
mov    %al,-0xe(%rbp)
jle    1196 <read_string+0x86>
movsbl -0xd(%rbp),%eax
cmp    $0x61,%eax
jl     1171 <read_string+0x61>
movsbl -0xd(%rbp),%ecx
mov    $0x1,%al
cmp    $0x7a,%ecx
mov    %al,-0xe(%rbp)
jle    1196 <read_string+0x86>
movsbl -0xd(%rbp),%ecx
xor    %eax,%eax
cmp    $0x30,%ecx
mov    %al,-0xf(%rbp)
jl     1190 <read_string+0x80>
movsbl -0xd(%rbp),%eax
cmp    $0x39,%eax
setle  %al
mov    %al,-0xf(%rbp)
mov    -0xf(%rbp),%al
mov    %al,-0xe(%rbp)
mov    -0xe(%rbp),%al
test   $0x1,%al
jne    11a6 <read_string+0x96>
jmp    11d2 <read_string+0xc2>
mov    -0xd(%rbp),%dl
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
lea    0xe45(%rip),%rdi
lea    -0xd(%rbp),%rsi
call   1030 <scanf@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1133 <read_string+0x23>
mov    -0x8(%rbp),%rax
movslq -0xc(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0xc(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_string>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0xede(%rip),%rdi
lea    0x7(%rsp),%rsi
call   1030 <scanf@plt>
mov    0x7(%rsp),%al
mov    %eax,%ecx
and    $0xdf,%cl
add    $0xbf,%cl
cmp    $0x1a,%cl
jb     1147 <read_string+0x37>
lea    -0x30(%rax),%ecx
xor    %ebx,%ebx
cmp    $0x9,%cl
ja     118d <read_string+0x7d>
xor    %ebx,%ebx
lea    0xeb0(%rip),%r15
lea    0x7(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
nop
mov    %al,(%r14,%rbx,1)
mov    %r15,%rdi
mov    %r12,%rsi
call   1030 <scanf@plt>
add    $0x1,%rbx
movzbl 0x7(%rsp),%eax
mov    %eax,%edx
and    $0xdf,%dl
add    $0xbf,%dl
lea    -0x30(%rax),%ecx
cmp    $0x1a,%dl
jb     1160 <read_string+0x50>
cmp    $0xa,%cl
jb     1160 <read_string+0x50>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%c\00"
define i32 @read_string(i8* %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i8
store i8* %0, i8** %2
store i32 0, i32* %3
%5 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
br label %6
6: ; preds = %34, %1
%7 = load i8, i8* %4
%8 = sext i8 %7 to i32
%9 = icmp sge i32 %8, 65
br i1 %9, label %10, label %14
10: ; preds = %6
%11 = load i8, i8* %4
%12 = sext i8 %11 to i32
%13 = icmp sle i32 %12, 90
br i1 %13, label %32, label %14
14: ; preds = %10, %6
%15 = load i8, i8* %4
%16 = sext i8 %15 to i32
%17 = icmp sge i32 %16, 97
br i1 %17, label %18, label %22
18: ; preds = %14
%19 = load i8, i8* %4
%20 = sext i8 %19 to i32
%21 = icmp sle i32 %20, 122
br i1 %21, label %32, label %22
22: ; preds = %18, %14
%23 = load i8, i8* %4
%24 = sext i8 %23 to i32
%25 = icmp sge i32 %24, 48
br i1 %25, label %26, label %30
26: ; preds = %22
%27 = load i8, i8* %4
%28 = sext i8 %27 to i32
%29 = icmp sle i32 %28, 57
br label %30
30: ; preds = %26, %22
%31 = phi i1 [ false, %22 ], [ %29, %26 ]
br label %32
32: ; preds = %30, %18, %10
%33 = phi i1 [ true, %18 ], [ true, %10 ], [ %31, %30 ]
br i1 %33, label %34, label %43
34: ; preds = %32
%35 = load i8, i8* %4
%36 = load i8*, i8** %2
%37 = load i32, i32* %3
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 %35, i8* %39
%40 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %4)
%41 = load i32, i32* %3
%42 = add nsw i32 %41, 1
store i32 %42, i32* %3
br label %6
43: ; preds = %32
%44 = load i8*, i8** %2
%45 = load i32, i32* %3
%46 = sext i32 %45 to i64
%47 = getelementptr inbounds i8, i8* %44, i64 %46
store i8 0, i8* %47
%48 = load i32, i32* %3
ret i32 %48
}

LLVM IR (-O2):
@.str = [3 x i8] c"%c\00"
define i32 @read_string(i8* nocapture writeonly %0) {
%2 = alloca i8
%3 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %2)
%4 = load i8, i8* %2
%5 = and i8 %4, -33
%6 = add i8 %5, -65
%7 = icmp ult i8 %6, 26
%8 = add i8 %4, -48
%9 = icmp ult i8 %8, 10
%10 = select i1 %7, i1 true, i1 %9
br i1 %10, label %11, label %26
11: ; preds = %1, %11
%12 = phi i64 [ %16, %11 ], [ 0, %1 ]
%13 = phi i8 [ %17, %11 ], [ %4, %1 ]
%14 = getelementptr inbounds i8, i8* %0, i64 %12
store i8 %13, i8* %14
%15 = call i32 @scanf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %2)
%16 = add nuw i64 %12, 1
%17 = load i8, i8* %2
%18 = and i8 %17, -33
%19 = add i8 %18, -65
%20 = icmp ult i8 %19, 26
%21 = add i8 %17, -48
%22 = icmp ult i8 %21, 10
%23 = select i1 %20, i1 true, i1 %22
br i1 %23, label %11, label %24
24: ; preds = %11
%25 = trunc i64 %16 to i32
br label %26
26: ; preds = %24, %1
%27 = phi i32 [ 0, %1 ], [ %25, %24 ]
%28 = zext i32 %27 to i64
%29 = getelementptr inbounds i8, i8* %0, i64 %28
store i8 0, i8* %29
ret i32 %27
}