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

/* Variables and functions */
char *alloc_string(char *);
char fgetc(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int strlen(char *);
char *getstring(FILE *f) {
  static char buf[((2048) * 4)];
  char c;
  if (fgets(buf, sizeof(buf), f) == NULL) {
    return alloc_string("");
  }
  if (strlen(buf) == ((2048) * 4) - 1) {
    if (buf[((2048) * 4) - 2] != '\n')
      while ((c = fgetc(f)) != '\n')
        ;
  }
  for (char *p = buf; *p; p++) {
    if (*p == '\n') {
      *p = '\0';
      break;
    }
  }
  return alloc_string(buf);
}


x86 assembly (clang, -O0):
<getstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    -0x10(%rbp),%rdx
lea    0x2ef9(%rip),%rdi
mov    $0x2000,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
jne    1180 <getstring+0x40>
lea    0xe8e(%rip),%rdi
call   1060 <alloc_string@plt>
mov    %rax,-0x8(%rbp)
jmp    122d <getstring+0xed>
lea    0x2ec9(%rip),%rdi
call   1030 <strlen@plt>
cmp    $0x1fff,%eax
jne    11d3 <getstring+0x93>
movsbl 0x4eb0(%rip),%eax
cmp    $0xa,%eax
je     11ce <getstring+0x8e>
jmp    11ac <getstring+0x6c>
mov    -0x10(%rbp),%rdi
call   1040 <fgetc@plt>
mov    %al,-0x11(%rbp)
movsbl %al,%eax
cmp    $0xa,%eax
je     11c9 <getstring+0x89>
jmp    11ac <getstring+0x6c>
jmp    11ce <getstring+0x8e>
jmp    11d3 <getstring+0x93>
lea    0x2e76(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpb   $0x0,(%rax)
je     121d <getstring+0xdd>
mov    -0x20(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
jne    1207 <getstring+0xc7>
mov    -0x20(%rbp),%rax
movb   $0x0,(%rax)
jmp    121d <getstring+0xdd>
jmp    120c <getstring+0xcc>
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
jmp    11de <getstring+0x9e>
lea    0x2e2c(%rip),%rdi
call   1060 <alloc_string@plt>
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getstring>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
lea    0x2f02(%rip),%rdi
mov    $0x2000,%esi
mov    %r14,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11ab <getstring+0x6b>
lea    0x2ee9(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
cmp    $0x1fff,%eax
jne    1194 <getstring+0x54>
cmpb   $0xa,0x4ed1(%rip)
je     1194 <getstring+0x54>
nop
mov    %r14,%rdi
call   1040 <fgetc@plt>
cmp    $0xa,%al
jne    1180 <getstring+0x40>
jmp    1194 <getstring+0x54>
xchg   %ax,%ax
add    $0x1,%rbx
movzbl (%rbx),%eax
test   %al,%al
je     11a2 <getstring+0x62>
cmp    $0xa,%al
jne    1190 <getstring+0x50>
movb   $0x0,(%rbx)
lea    0x2ea7(%rip),%rdi
jmp    11b2 <getstring+0x72>
lea    0xe4e(%rip),%rdi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <alloc_string@plt>

LLVM IR (-O0):
@getstring.buf = internal global [8192 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
define i8* @getstring(i32* %0) {
%2 = alloca i8*
%3 = alloca i32*
%4 = alloca i8
%5 = alloca i8*
store i32* %0, i32** %3
%6 = load i32*, i32** %3
%7 = call i32* @fgets(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 0), i32 8192, i32* %6)
%8 = icmp eq i32* %7, null
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i8* @alloc_string(i8* getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0))
store i8* %10, i8** %2
br label %45
11: ; preds = %1
%12 = call i32 @strlen(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 0))
%13 = icmp eq i32 %12, 8191
br i1 %13, label %14, label %27
14: ; preds = %11
%15 = load i8, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 8190)
%16 = sext i8 %15 to i32
%17 = icmp ne i32 %16, 10
br i1 %17, label %18, label %26
18: ; preds = %14
br label %19
19: ; preds = %24, %18
%20 = load i32*, i32** %3
%21 = call signext i8 @fgetc(i32* %20)
store i8 %21, i8* %4
%22 = sext i8 %21 to i32
%23 = icmp ne i32 %22, 10
br i1 %23, label %24, label %25
24: ; preds = %19
br label %19
25: ; preds = %19
br label %26
26: ; preds = %25, %14
br label %27
27: ; preds = %26, %11
store i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 0), i8** %5
br label %28
28: ; preds = %40, %27
%29 = load i8*, i8** %5
%30 = load i8, i8* %29
%31 = icmp ne i8 %30, 0
br i1 %31, label %32, label %43
32: ; preds = %28
%33 = load i8*, i8** %5
%34 = load i8, i8* %33
%35 = sext i8 %34 to i32
%36 = icmp eq i32 %35, 10
br i1 %36, label %37, label %39
37: ; preds = %32
%38 = load i8*, i8** %5
store i8 0, i8* %38
br label %43
39: ; preds = %32
br label %40
40: ; preds = %39
%41 = load i8*, i8** %5
%42 = getelementptr inbounds i8, i8* %41, i32 1
store i8* %42, i8** %5
br label %28
43: ; preds = %37, %28
%44 = call i8* @alloc_string(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 0))
store i8* %44, i8** %2
br label %45
45: ; preds = %43, %9
%46 = load i8*, i8** %2
ret i8* %46
}

LLVM IR (-O2):
@getstring.buf = internal global [8192 x i8] zeroinitializer
@.str = [1 x i8] zeroinitializer
define i8* @getstring(i32* nocapture %0) {
%2 = tail call i32* @fgets(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 0), i32 8192, i32* %0)
%3 = icmp eq i32* %2, null
br i1 %3, label %20, label %4
4: ; preds = %1
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 0))
%6 = icmp eq i32 %5, 8191
%7 = load i8, i8* getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 8190)
%8 = icmp ne i8 %7, 10
%9 = select i1 %6, i1 %8, i1 false
br i1 %9, label %10, label %13
10: ; preds = %4, %10
%11 = tail call signext i8 @fgetc(i32* %0)
%12 = icmp eq i8 %11, 10
br i1 %12, label %13, label %10
13: ; preds = %10, %4
br label %14
14: ; preds = %13, %18
%15 = phi i8* [ %19, %18 ], [ getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 0), %13 ]
%16 = load i8, i8* %15
switch i8 %16, label %18 [
i8 0, label %20
i8 10, label %17
]
17: ; preds = %14
store i8 0, i8* %15
br label %20
18: ; preds = %14
%19 = getelementptr inbounds i8, i8* %15, i64 1
br label %14
20: ; preds = %14, %17, %1
%21 = phi i8* [ getelementptr inbounds ([1 x i8], [1 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 0), %17 ], [ getelementptr inbounds ([8192 x i8], [8192 x i8]* @getstring.buf, i64 0, i64 0), %14 ]
%22 = tail call i8* @alloc_string(i8* %21)
ret i8* %22
}