#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*/ buffer;

/* Variables and functions */
int flush_socket(int);
int readchar(int);
char *get_string(int fd) {
  static char buffer[4096];
  size_t len = 0;
  if (flush_socket(fd) == -1)
    return NULL;
  do {
    int key = readchar(fd);
    if (key == -1)
      return NULL;
    if (key == 10 || key == 13)
      break;
    buffer[len++] = key;
  } while (len < (sizeof(buffer) - 1));
  buffer[len] = 0x00;
  return buffer;
}


x86 assembly (clang, -O0):
<get_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
mov    -0xc(%rbp),%edi
call   1040 <flush_socket@plt>
cmp    $0xffffffff,%eax
jne    1151 <get_string+0x31>
movq   $0x0,-0x8(%rbp)
jmp    11d7 <get_string+0xb7>
jmp    1156 <get_string+0x36>
mov    -0xc(%rbp),%edi
call   1030 <readchar@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0xffffffff,-0x1c(%rbp)
jne    1178 <get_string+0x58>
movq   $0x0,-0x8(%rbp)
jmp    11d7 <get_string+0xb7>
cmpl   $0xa,-0x1c(%rbp)
je     118c <get_string+0x6c>
cmpl   $0xd,-0x1c(%rbp)
jne    1191 <get_string+0x71>
jmp    11bd <get_string+0x9d>
mov    -0x1c(%rbp),%eax
mov    %al,%dl
mov    -0x18(%rbp),%rcx
mov    %rcx,%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
lea    0x2e94(%rip),%rax
mov    %dl,(%rax,%rcx,1)
cmpq   $0xfff,-0x18(%rbp)
jb     1156 <get_string+0x36>
mov    -0x18(%rbp),%rcx
lea    0x2e78(%rip),%rax
movb   $0x0,(%rax,%rcx,1)
lea    0x2e6d(%rip),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_string>:
push   %rbp
push   %r14
push   %rbx
mov    %edi,%ebp
call   1040 <flush_socket@plt>
cmp    $0xffffffff,%eax
je     1173 <get_string+0x53>
xor    %ebx,%ebx
lea    0x2f07(%rip),%r14
nopl   0x0(%rax)
mov    %ebp,%edi
call   1030 <readchar@plt>
cmp    $0xffffffff,%eax
je     1173 <get_string+0x53>
cmp    $0xa,%eax
je     116c <get_string+0x4c>
cmp    $0xd,%eax
je     116c <get_string+0x4c>
mov    %al,(%rbx,%r14,1)
add    $0x1,%rbx
cmp    $0xfff,%rbx
jne    1140 <get_string+0x20>
mov    $0xfff,%ebx
movb   $0x0,(%rbx,%r14,1)
jmp    1176 <get_string+0x56>
xor    %r14d,%r14d
mov    %r14,%rax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@get_string.buffer = internal global [4096 x i8] zeroinitializer
define i8* @get_string(i32 %0) {
%2 = alloca i8*
%3 = alloca i32
%4 = alloca i64
%5 = alloca i32
store i32 %0, i32* %3
store i64 0, i64* %4
%6 = load i32, i32* %3
%7 = call i32 @flush_socket(i32 %6)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %10
9: ; preds = %1
store i8* null, i8** %2
br label %36
10: ; preds = %1
br label %11
11: ; preds = %30, %10
%12 = load i32, i32* %3
%13 = call i32 @readchar(i32 %12)
store i32 %13, i32* %5
%14 = load i32, i32* %5
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %11
store i8* null, i8** %2
br label %36
17: ; preds = %11
%18 = load i32, i32* %5
%19 = icmp eq i32 %18, 10
br i1 %19, label %23, label %20
20: ; preds = %17
%21 = load i32, i32* %5
%22 = icmp eq i32 %21, 13
br i1 %22, label %23, label %24
23: ; preds = %20, %17
br label %33
24: ; preds = %20
%25 = load i32, i32* %5
%26 = trunc i32 %25 to i8
%27 = load i64, i64* %4
%28 = add i64 %27, 1
store i64 %28, i64* %4
%29 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_string.buffer, i64 0, i64 %27
store i8 %26, i8* %29
br label %30
30: ; preds = %24
%31 = load i64, i64* %4
%32 = icmp ult i64 %31, 4095
br i1 %32, label %11, label %33
33: ; preds = %30, %23
%34 = load i64, i64* %4
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_string.buffer, i64 0, i64 %34
store i8 0, i8* %35
store i8* getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_string.buffer, i64 0, i64 0), i8** %2
br label %36
36: ; preds = %33, %16, %9
%37 = load i8*, i8** %2
ret i8* %37
}

LLVM IR (-O2):
@get_string.buffer = internal global [4096 x i8] zeroinitializer
define i8* @get_string(i32 %0) {
%2 = tail call i32 @flush_socket(i32 %0)
%3 = icmp eq i32 %2, -1
br i1 %3, label %15, label %4
4: ; preds = %1, %7
%5 = phi i64 [ %9, %7 ], [ 0, %1 ]
%6 = tail call i32 @readchar(i32 %0)
switch i32 %6, label %7 [
i32 -1, label %15
i32 13, label %12
i32 10, label %12
]
7: ; preds = %4
%8 = trunc i32 %6 to i8
%9 = add nuw nsw i64 %5, 1
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_string.buffer, i64 0, i64 %5
store i8 %8, i8* %10
%11 = icmp eq i64 %9, 4095
br i1 %11, label %12, label %4
12: ; preds = %4, %4, %7
%13 = phi i64 [ 4095, %7 ], [ %5, %4 ], [ %5, %4 ]
%14 = getelementptr inbounds [4096 x i8], [4096 x i8]* @get_string.buffer, i64 0, i64 %13
store i8 0, i8* %14
br label %15
15: ; preds = %4, %1, %12
%16 = phi i8* [ getelementptr inbounds ([4096 x i8], [4096 x i8]* @get_string.buffer, i64 0, i64 0), %12 ], [ null, %1 ], [ null, %4 ]
ret i8* %16
}