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

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EIO;
int /*<<< orphan*/ STDIN_FILENO;
scalar_t__ errno;
int /*<<< orphan*/ fflush(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char *);
char *malloc(int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdout;
char *read_line(const char *prompt) {
  char *input = malloc(sizeof(char) * 1024);
  ssize_t r;
  fprintf(stdout, "%s", prompt);
  fflush(stdout);
  for (;;) {
    r = read(STDIN_FILENO, input, 1024 - 1);
    if (r < 0) {
      if (errno == EINTR) {
        continue;
      }
      if (errno == EIO) {
        fprintf(stderr, "Lost control of terminal\n");
        return NULL;
      }
    }
    if (r > 0) {
      char *t;
      *(input + 1024 - 1) = '\0';
      for (t = input; *t && *t != '\n'; t++)
        ;
      *(t + 1) = '\0';
      return input;
    }
    if (r == 0) {
      free(input);
      return NULL;
    }
    perror("read");
    free(input);
    return NULL;
  }
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e3f(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xe72(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    0x2e24(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    0x2e0e(%rip),%rax
mov    (%rax),%edi
mov    -0x18(%rbp),%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    1229 <read_line+0xc9>
mov    0x2e02(%rip),%rax
mov    (%rax),%rax
mov    0x2df0(%rip),%rcx
cmp    (%rcx),%rax
jne    11e6 <read_line+0x86>
jmp    11a3 <read_line+0x43>
mov    0x2de3(%rip),%rax
mov    (%rax),%rax
mov    0x2de9(%rip),%rcx
cmp    (%rcx),%rax
jne    1224 <read_line+0xc4>
mov    0x2dd1(%rip),%rax
mov    (%rax),%edi
lea    0xdf3(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <read_line+0x183>
jmp    1229 <read_line+0xc9>
cmpl   $0x0,-0x1c(%rbp)
jle    12a6 <read_line+0x146>
mov    -0x18(%rbp),%rax
movb   $0x0,0x3ff(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rax
movsbl (%rax),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x29(%rbp)
je     126b <read_line+0x10b>
mov    -0x28(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xa,%eax
setne  %al
mov    %al,-0x29(%rbp)
mov    -0x29(%rbp),%al
test   $0x1,%al
jne    127b <read_line+0x11b>
jmp    1291 <read_line+0x131>
jmp    1280 <read_line+0x120>
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1246 <read_line+0xe6>
mov    -0x28(%rbp),%rax
movb   $0x0,0x1(%rax)
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    12e3 <read_line+0x183>
cmpl   $0x0,-0x1c(%rbp)
jne    12c6 <read_line+0x166>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
jmp    12e3 <read_line+0x183>
lea    0xd50(%rip),%rdi
call   1080 <perror@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %r15
push   %r14
push   %r12
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x400,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
mov    0x2e41(%rip),%rbx
mov    (%rbx),%edi
lea    0xe78(%rip),%rsi
mov    %r14,%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    (%rbx),%edi
call   1070 <fflush@plt>
mov    0x2e18(%rip),%r14
mov    0x2e29(%rip),%r12
mov    0x2e1a(%rip),%rbx
xchg   %ax,%ax
mov    (%r14),%edi
mov    %r15,%rsi
mov    $0x3ff,%edx
call   1040 <read@plt>
test   %eax,%eax
jns    11f5 <read_line+0x95>
mov    (%r12),%rax
cmp    (%rbx),%rax
je     11b0 <read_line+0x50>
mov    0x2e0c(%rip),%rcx
cmp    (%rcx),%rax
jne    1226 <read_line+0xc6>
mov    0x2df8(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
xor    %r15d,%r15d
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    123d <read_line+0xdd>
je     1232 <read_line+0xd2>
movb   $0x0,0x3ff(%r15)
mov    %r15,%rax
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
add    $0x1,%rax
movzbl -0x1(%rax),%ecx
test   %cl,%cl
je     1221 <read_line+0xc1>
cmp    $0xa,%cl
jne    1210 <read_line+0xb0>
movb   $0x0,(%rax)
jmp    123d <read_line+0xdd>
lea    0xdf0(%rip),%rdi
call   1080 <perror@plt>
mov    %r15,%rdi
call   1030 <free@plt>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@stdout = global i32 0
@.str = [3 x i8] c"%s\00"
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EIO = global i64 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"Lost control of terminal\0A\00"
@.str.2 = [5 x i8] c"read\00"
define i8* @read_line(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8*
store i8* %0, i8** %3
%7 = call i8* @malloc(i32 1024)
store i8* %7, i8** %4
%8 = load i32, i32* @stdout
%9 = load i8*, i8** %3
%10 = call i32 (i32, i8*, ...) @fprintf(i32 %8, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @stdout
%12 = call i32 @fflush(i32 %11)
br label %13
13: ; preds = %23, %1
%14 = load i32, i32* @STDIN_FILENO
%15 = load i8*, i8** %4
%16 = call i32 @read(i32 %14, i8* %15, i32 1023)
store i32 %16, i32* %5
%17 = load i32, i32* %5
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %32
19: ; preds = %13
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %24
23: ; preds = %19
br label %13
24: ; preds = %19
%25 = load i64, i64* @errno
%26 = load i64, i64* @EIO
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
store i8* null, i8** %2
br label %70
31: ; preds = %24
br label %32
32: ; preds = %31, %13
%33 = load i32, i32* %5
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %60
35: ; preds = %32
%36 = load i8*, i8** %4
%37 = getelementptr inbounds i8, i8* %36, i64 1024
%38 = getelementptr inbounds i8, i8* %37, i64 -1
store i8 0, i8* %38
%39 = load i8*, i8** %4
store i8* %39, i8** %6
br label %40
40: ; preds = %53, %35
%41 = load i8*, i8** %6
%42 = load i8, i8* %41
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 0
br i1 %44, label %45, label %50
45: ; preds = %40
%46 = load i8*, i8** %6
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp ne i32 %48, 10
br label %50
50: ; preds = %45, %40
%51 = phi i1 [ false, %40 ], [ %49, %45 ]
br i1 %51, label %52, label %56
52: ; preds = %50
br label %53
53: ; preds = %52
%54 = load i8*, i8** %6
%55 = getelementptr inbounds i8, i8* %54, i32 1
store i8* %55, i8** %6
br label %40
56: ; preds = %50
%57 = load i8*, i8** %6
%58 = getelementptr inbounds i8, i8* %57, i64 1
store i8 0, i8* %58
%59 = load i8*, i8** %4
store i8* %59, i8** %2
br label %70
60: ; preds = %32
%61 = load i32, i32* %5
%62 = icmp eq i32 %61, 0
br i1 %62, label %63, label %66
63: ; preds = %60
%64 = load i8*, i8** %4
%65 = call i32 @free(i8* %64)
store i8* null, i8** %2
br label %70
66: ; preds = %60
%67 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%68 = load i8*, i8** %4
%69 = call i32 @free(i8* %68)
store i8* null, i8** %2
br label %70
70: ; preds = %66, %63, %56, %28
%71 = load i8*, i8** %2
ret i8* %71
}

LLVM IR (-O2):
@stdout = global i32 0
@.str = [3 x i8] c"%s\00"
@STDIN_FILENO = global i32 0
@errno = global i64 0
@EINTR = global i64 0
@EIO = global i64 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"Lost control of terminal\0A\00"
@.str.2 = [5 x i8] c"read\00"
define noalias i8* @read_line(i8* %0) {
%2 = tail call dereferenceable_or_null(1024) i8* @malloc(i32 1024)
%3 = load i32, i32* @stdout
%4 = tail call i32 (i32, i8*, ...) @fprintf(i32 %3, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%5 = load i32, i32* @stdout
%6 = tail call i32 @fflush(i32 %5)
br label %7
7: ; preds = %11, %1
%8 = load i32, i32* @STDIN_FILENO
%9 = tail call i32 @read(i32 %8, i8* %2, i32 1023)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %21
11: ; preds = %7
%12 = load i64, i64* @errno
%13 = load i64, i64* @EINTR
%14 = icmp eq i64 %12, %13
br i1 %14, label %7, label %15
15: ; preds = %11
%16 = load i64, i64* @EIO
%17 = icmp eq i64 %12, %16
br i1 %17, label %18, label %34
18: ; preds = %15
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
br label %37
21: ; preds = %7
%22 = icmp eq i32 %9, 0
br i1 %22, label %32, label %23
23: ; preds = %21
%24 = getelementptr inbounds i8, i8* %2, i64 1023
store i8 0, i8* %24
br label %25
25: ; preds = %28, %23
%26 = phi i8* [ %2, %23 ], [ %29, %28 ]
%27 = load i8, i8* %26
switch i8 %27, label %28 [
i8 0, label %30
i8 10, label %30
]
28: ; preds = %25
%29 = getelementptr inbounds i8, i8* %26, i64 1
br label %25
30: ; preds = %25, %25
%31 = getelementptr inbounds i8, i8* %26, i64 1
store i8 0, i8* %31
br label %37
32: ; preds = %21
%33 = tail call i32 @free(i8* %2)
br label %37
34: ; preds = %15
%35 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
%36 = tail call i32 @free(i8* %2)
br label %37
37: ; preds = %34, %32, %30, %18
%38 = phi i8* [ null, %18 ], [ %2, %30 ], [ null, %32 ], [ null, %34 ]
ret i8* %38
}