#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*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
size_t strlen(char *);
int get_regex(char *regex) {
  size_t len = 0;
  printf("Please input a regex: \n");
  if (NULL == fgets(regex, 128, stdin)) {
    fprintf(stderr, "Error: did you input a correct regex?\n");
    return -1;
  }
  len = strlen(regex);
  if (regex[len - 1] == '\n') {
    regex[len - 1] = 0;
  }
  return 0;
}


x86 assembly (clang, -O0):
<get_regex>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    0xea5(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
mov    0x2e7d(%rip),%rax
mov    (%rax),%edx
mov    $0x80,%esi
call   1050 <fgets@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
jne    11a6 <get_regex+0x66>
mov    0x2e4c(%rip),%rax
mov    (%rax),%edi
lea    0xe83(%rip),%rsi
call   1060 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e3 <get_regex+0xa3>
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11dc <get_regex+0x9c>
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%rcx
sub    $0x1,%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_regex>:
push   %rbx
mov    %rdi,%rbx
lea    0xedc(%rip),%rdi
call   1030 <puts@plt>
mov    0x2e91(%rip),%rax
mov    (%rax),%edx
mov    %rbx,%rdi
mov    $0x80,%esi
call   1050 <fgets@plt>
test   %rax,%rax
je     1186 <get_regex+0x46>
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmpb   $0xa,-0x1(%rcx,%rbx,1)
jne    1184 <get_regex+0x44>
movb   $0x0,-0x1(%rbx,%rcx,1)
pop    %rbx
ret
mov    0x2e4b(%rip),%rax
mov    (%rax),%edi
lea    0xe6a(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0xffffffff,%eax
pop    %rbx
ret

LLVM IR (-O0):
@.str = [24 x i8] c"Please input a regex: \0A\00"
@stdin = global i32 0
@stderr = global i32 0
@.str.1 = [39 x i8] c"Error: did you input a correct regex?\0A\00"
define i32 @get_regex(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i64
store i8* %0, i8** %3
store i64 0, i64* %4
%5 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%6 = load i8*, i8** %3
%7 = load i32, i32* @stdin
%8 = call i32* @fgets(i8* %6, i32 128, i32 %7)
%9 = icmp eq i32* null, %8
br i1 %9, label %10, label %13
10: ; preds = %1
%11 = load i32, i32* @stderr
%12 = call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %29
13: ; preds = %1
%14 = load i8*, i8** %3
%15 = call i64 @strlen(i8* %14)
store i64 %15, i64* %4
%16 = load i8*, i8** %3
%17 = load i64, i64* %4
%18 = sub i64 %17, 1
%19 = getelementptr inbounds i8, i8* %16, i64 %18
%20 = load i8, i8* %19
%21 = sext i8 %20 to i32
%22 = icmp eq i32 %21, 10
br i1 %22, label %23, label %28
23: ; preds = %13
%24 = load i8*, i8** %3
%25 = load i64, i64* %4
%26 = sub i64 %25, 1
%27 = getelementptr inbounds i8, i8* %24, i64 %26
store i8 0, i8* %27
br label %28
28: ; preds = %23, %13
store i32 0, i32* %2
br label %29
29: ; preds = %28, %10
%30 = load i32, i32* %2
ret i32 %30
}

LLVM IR (-O2):
@stdin = global i32 0
@stderr = global i32 0
@.str.1 = [39 x i8] c"Error: did you input a correct regex?\0A\00"
@str = [23 x i8] c"Please input a regex: \00"
define i32 @get_regex(i8* %0) {
%2 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%3 = load i32, i32* @stdin
%4 = tail call i32* @fgets(i8* %0, i32 128, i32 %3)
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %9
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %16
9: ; preds = %1
%10 = tail call i64 @strlen(i8* dereferenceable(1) %0)
%11 = add i64 %10, -1
%12 = getelementptr inbounds i8, i8* %0, i64 %11
%13 = load i8, i8* %12
%14 = icmp eq i8 %13, 10
br i1 %14, label %15, label %16
15: ; preds = %9
store i8 0, i8* %12
br label %16
16: ; preds = %9, %15, %6
%17 = phi i32 [ -1, %6 ], [ 0, %15 ], [ 0, %9 ]
ret i32 %17
}