#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 */
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
unsigned char EOF;
int /*<<< orphan*/ free(unsigned char *);
unsigned char getc(int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
unsigned char *realloc(unsigned char *, size_t);
unsigned char *read_key(FILE *keyfile) {
  unsigned char *key = NULL;
  unsigned char *aux_pointer = NULL;
  size_t string_size = 0;
  size_t position = 0;
  unsigned char read_char = EOF;
  while (read_char) {
    read_char = getc(keyfile);
    if (read_char == EOF || read_char == '\n' || read_char == '\0') {
      key[position] = '\0';
      return key;
    }
    if (string_size <= position) {
      string_size++;
      aux_pointer = realloc(key, string_size);
      if (!aux_pointer) {
        printf("Error on realloc!\n");
        free(key);
        return NULL;
      }
      key = aux_pointer;
    }
    key[position] = read_char;
    position++;
  }
  return key;
}


x86 assembly (clang, -O0):
<read_key>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e75(%rip),%rax
mov    (%rax),%al
mov    %al,-0x31(%rbp)
cmpb   $0x0,-0x31(%rbp)
je     1256 <read_key+0x116>
mov    -0x10(%rbp),%rdi
call   1060 <getc@plt>
mov    %al,-0x31(%rbp)
movzbl -0x31(%rbp),%eax
mov    0x2e4f(%rip),%rcx
movzbl (%rcx),%ecx
cmp    %ecx,%eax
je     11be <read_key+0x7e>
movzbl -0x31(%rbp),%eax
cmp    $0xa,%eax
je     11be <read_key+0x7e>
movzbl -0x31(%rbp),%eax
cmp    $0x0,%eax
jne    11d7 <read_key+0x97>
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    125e <read_key+0x11e>
mov    -0x28(%rbp),%rax
cmp    -0x30(%rbp),%rax
ja     1237 <read_key+0xf7>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rsi
call   1050 <realloc@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    122f <read_key+0xef>
lea    0xdec(%rip),%rdi
call   1040 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    125e <read_key+0x11e>
mov    -0x20(%rbp),%rax
mov    %rax,-0x18(%rbp)
mov    -0x31(%rbp),%dl
mov    -0x18(%rbp),%rax
mov    -0x30(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x30(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x30(%rbp)
jmp    1178 <read_key+0x38>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_key>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e96(%rip),%r15
cmpb   $0x0,(%r15)
je     11dd <read_key+0x9d>
mov    %rdi,%r14
call   1060 <getc@plt>
xor    %ebp,%ebp
mov    $0x0,%r13d
cmp    (%r15),%al
je     11e5 <read_key+0xa5>
mov    %eax,%ebx
xor    %r12d,%r12d
xor    %r15d,%r15d
xor    %ebp,%ebp
jmp    11a4 <read_key+0x64>
nopl   (%rax)
mov    %r12,%r13
mov    %bl,0x0(%r13,%rbp,1)
add    $0x1,%rbp
mov    %r14,%rdi
call   1060 <getc@plt>
mov    %eax,%ebx
mov    %r13,%r12
mov    0x2e48(%rip),%rax
cmp    (%rax),%bl
je     11e5 <read_key+0xa5>
cmp    $0xa,%bl
je     11e2 <read_key+0xa2>
test   %bl,%bl
je     11e2 <read_key+0xa2>
cmp    %rbp,%r15
ja     1180 <read_key+0x40>
add    $0x1,%r15
mov    %r12,%rdi
mov    %r15,%rsi
call   1050 <realloc@plt>
mov    %rax,%r13
test   %rax,%rax
jne    1183 <read_key+0x43>
lea    0xe30(%rip),%rdi
call   1040 <puts@plt>
mov    %r12,%rdi
call   1030 <free@plt>
xor    %r13d,%r13d
jmp    11eb <read_key+0xab>
mov    %r12,%r13
movb   $0x0,0x0(%r13,%rbp,1)
mov    %r13,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EOF = global i8 0
@.str = [19 x i8] c"Error on realloc!\0A\00"
define i8* @read_key(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8
store i32* %0, i32** %3
store i8* null, i8** %4
store i8* null, i8** %5
store i64 0, i64* %6
store i64 0, i64* %7
%9 = load i8, i8* @EOF
store i8 %9, i8* %8
br label %10
10: ; preds = %52, %1
%11 = load i8, i8* %8
%12 = icmp ne i8 %11, 0
br i1 %12, label %13, label %59
13: ; preds = %10
%14 = load i32*, i32** %3
%15 = call zeroext i8 @getc(i32* %14)
store i8 %15, i8* %8
%16 = load i8, i8* %8
%17 = zext i8 %16 to i32
%18 = load i8, i8* @EOF
%19 = zext i8 %18 to i32
%20 = icmp eq i32 %17, %19
br i1 %20, label %29, label %21
21: ; preds = %13
%22 = load i8, i8* %8
%23 = zext i8 %22 to i32
%24 = icmp eq i32 %23, 10
br i1 %24, label %29, label %25
25: ; preds = %21
%26 = load i8, i8* %8
%27 = zext i8 %26 to i32
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %25, %21, %13
%30 = load i8*, i8** %4
%31 = load i64, i64* %7
%32 = getelementptr inbounds i8, i8* %30, i64 %31
store i8 0, i8* %32
%33 = load i8*, i8** %4
store i8* %33, i8** %2
br label %61
34: ; preds = %25
%35 = load i64, i64* %6
%36 = load i64, i64* %7
%37 = icmp ule i64 %35, %36
br i1 %37, label %38, label %52
38: ; preds = %34
%39 = load i64, i64* %6
%40 = add i64 %39, 1
store i64 %40, i64* %6
%41 = load i8*, i8** %4
%42 = load i64, i64* %6
%43 = call i8* @realloc(i8* %41, i64 %42)
store i8* %43, i8** %5
%44 = load i8*, i8** %5
%45 = icmp ne i8* %44, null
br i1 %45, label %50, label %46
46: ; preds = %38
%47 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0))
%48 = load i8*, i8** %4
%49 = call i32 @free(i8* %48)
store i8* null, i8** %2
br label %61
50: ; preds = %38
%51 = load i8*, i8** %5
store i8* %51, i8** %4
br label %52
52: ; preds = %50, %34
%53 = load i8, i8* %8
%54 = load i8*, i8** %4
%55 = load i64, i64* %7
%56 = getelementptr inbounds i8, i8* %54, i64 %55
store i8 %53, i8* %56
%57 = load i64, i64* %7
%58 = add i64 %57, 1
store i64 %58, i64* %7
br label %10
59: ; preds = %10
%60 = load i8*, i8** %4
store i8* %60, i8** %2
br label %61
61: ; preds = %59, %46, %29
%62 = load i8*, i8** %2
ret i8* %62
}

LLVM IR (-O2):
@EOF = global i8 0
@str = [18 x i8] c"Error on realloc!\00"
define noalias i8* @read_key(i32* nocapture %0) {
%2 = load i8, i8* @EOF
%3 = icmp eq i8 %2, 0
br i1 %3, label %36, label %4
4: ; preds = %1
%5 = tail call zeroext i8 @getc(i32* %0)
%6 = load i8, i8* @EOF
%7 = freeze i8 %6
%8 = icmp eq i8 %5, %7
br i1 %8, label %14, label %9
9: ; preds = %4, %27
%10 = phi i8 [ %32, %27 ], [ %5, %4 ]
%11 = phi i8* [ %28, %27 ], [ null, %4 ]
%12 = phi i64 [ %29, %27 ], [ 0, %4 ]
%13 = phi i64 [ %31, %27 ], [ 0, %4 ]
switch i8 %10, label %18 [
i8 10, label %14
i8 0, label %14
]
14: ; preds = %27, %9, %9, %4
%15 = phi i64 [ 0, %4 ], [ %31, %27 ], [ %13, %9 ], [ %13, %9 ]
%16 = phi i8* [ null, %4 ], [ %28, %27 ], [ %11, %9 ], [ %11, %9 ]
%17 = getelementptr inbounds i8, i8* %16, i64 %15
store i8 0, i8* %17
br label %36
18: ; preds = %9
%19 = icmp ugt i64 %12, %13
br i1 %19, label %27, label %20
20: ; preds = %18
%21 = add i64 %12, 1
%22 = tail call i8* @realloc(i8* %11, i64 %21)
%23 = icmp eq i8* %22, null
br i1 %23, label %24, label %27
24: ; preds = %20
%25 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @str, i64 0, i64 0))
%26 = tail call i32 @free(i8* %11)
br label %36
27: ; preds = %20, %18
%28 = phi i8* [ %11, %18 ], [ %22, %20 ]
%29 = phi i64 [ %12, %18 ], [ %21, %20 ]
%30 = getelementptr inbounds i8, i8* %28, i64 %13
store i8 %10, i8* %30
%31 = add i64 %13, 1
%32 = tail call zeroext i8 @getc(i32* %0)
%33 = load i8, i8* @EOF
%34 = freeze i8 %33
%35 = icmp eq i8 %32, %34
br i1 %35, label %14, label %9
36: ; preds = %1, %24, %14
%37 = phi i8* [ %16, %14 ], [ null, %24 ], [ null, %1 ]
ret i8* %37
}