#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 */
scalar_t__ isalpha(int);
scalar_t__ isdigit(int);
char *readname(char *arg, char name[]) {
  int i = 0;
  int c;
  while ((isalpha(c = *arg) || c == '_' || isdigit(c)) && i < 20) {
    name[i++] = c;
    arg++;
  }
  name[i] = '\0';
  return arg;
}


x86 assembly (clang, -O0):
<readname>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x8(%rbp),%rax
movsbl (%rax),%edi
mov    %edi,-0x18(%rbp)
call   1040 <isalpha@plt>
cmp    $0x0,%rax
jne    1174 <readname+0x54>
cmpl   $0x5f,-0x18(%rbp)
je     1174 <readname+0x54>
mov    -0x18(%rbp),%edi
call   1030 <isdigit@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
je     117e <readname+0x5e>
cmpl   $0x14,-0x14(%rbp)
setl   %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
test   $0x1,%al
jne    118e <readname+0x6e>
jmp    11b9 <readname+0x99>
mov    -0x18(%rbp),%eax
mov    %al,%dl
mov    -0x10(%rbp),%rax
mov    -0x14(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x14(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x8(%rbp)
jmp    1137 <readname+0x17>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readname>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %rdi,%r15
xor    %ebx,%ebx
jmp    114e <readname+0x2e>
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
cmp    $0x13,%rbx
ja     117b <readname+0x5b>
mov    %r12b,(%r14,%rbx,1)
add    $0x1,%rbx
movzbl (%r15,%rbx,1),%r12d
movsbl %r12b,%ebp
mov    %ebp,%edi
call   1040 <isalpha@plt>
test   %rax,%rax
jne    1140 <readname+0x20>
cmp    $0x5f,%r12b
je     1140 <readname+0x20>
mov    %ebp,%edi
call   1030 <isdigit@plt>
test   %rax,%rax
je     117b <readname+0x5b>
cmp    $0x14,%rbx
jb     1146 <readname+0x26>
mov    %ebx,%eax
movb   $0x0,(%r14,%rax,1)
add    %rbx,%r15
mov    %r15,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @readname(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %5
br label %7
7: ; preds = %25, %2
%8 = load i8*, i8** %3
%9 = load i8, i8* %8
%10 = sext i8 %9 to i32
store i32 %10, i32* %6
%11 = call i64 @isalpha(i32 %10)
%12 = icmp ne i64 %11, 0
br i1 %12, label %20, label %13
13: ; preds = %7
%14 = load i32, i32* %6
%15 = icmp eq i32 %14, 95
br i1 %15, label %20, label %16
16: ; preds = %13
%17 = load i32, i32* %6
%18 = call i64 @isdigit(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %23
20: ; preds = %16, %13, %7
%21 = load i32, i32* %5
%22 = icmp slt i32 %21, 20
br label %23
23: ; preds = %20, %16
%24 = phi i1 [ false, %16 ], [ %22, %20 ]
br i1 %24, label %25, label %35
25: ; preds = %23
%26 = load i32, i32* %6
%27 = trunc i32 %26 to i8
%28 = load i8*, i8** %4
%29 = load i32, i32* %5
%30 = add nsw i32 %29, 1
store i32 %30, i32* %5
%31 = sext i32 %29 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8 %27, i8* %32
%33 = load i8*, i8** %3
%34 = getelementptr inbounds i8, i8* %33, i32 1
store i8* %34, i8** %3
br label %7
35: ; preds = %23
%36 = load i8*, i8** %4
%37 = load i32, i32* %5
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds i8, i8* %36, i64 %38
store i8 0, i8* %39
%40 = load i8*, i8** %3
ret i8* %40
}

LLVM IR (-O2):
define i8* @readname(i8* %0, i8* nocapture writeonly %1) {
br label %3
3: ; preds = %19, %2
%4 = phi i64 [ %20, %19 ], [ 0, %2 ]
%5 = phi i8* [ %22, %19 ], [ %0, %2 ]
%6 = load i8, i8* %5
%7 = sext i8 %6 to i32
%8 = tail call i64 @isalpha(i32 %7)
%9 = icmp ne i64 %8, 0
%10 = icmp eq i8 %6, 95
%11 = select i1 %9, i1 true, i1 %10
br i1 %11, label %17, label %12
12: ; preds = %3
%13 = tail call i64 @isdigit(i32 %7)
%14 = icmp ne i64 %13, 0
%15 = icmp ult i64 %4, 20
%16 = select i1 %14, i1 %15, i1 false
br i1 %16, label %19, label %23
17: ; preds = %3
%18 = icmp ult i64 %4, 20
br i1 %18, label %19, label %23
19: ; preds = %12, %17
%20 = add nuw nsw i64 %4, 1
%21 = getelementptr inbounds i8, i8* %1, i64 %4
store i8 %6, i8* %21
%22 = getelementptr inbounds i8, i8* %5, i64 1
br label %3
23: ; preds = %12, %17
%24 = and i64 %4, 4294967295
%25 = getelementptr inbounds i8, i8* %1, i64 %24
store i8 0, i8* %25
ret i8* %5
}