#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 */
scalar_t__ EINTR;
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fileno(int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
void readstring(char *string, int maxlen, FILE *f) {
  int pos = 0;
  while (1) {
    if (read(fileno(f), string + pos, 1) == 1) {
      pos++;
      if (string[pos - 1] == '\n') {
        string[pos] = 0;
        break;
      }
    } else if (errno != EINTR) {
      fprintf(stderr, "Error reading from socket or unexpected EOF.\n");
      exit(1);
    }
  }
}


x86 assembly (clang, -O0):
<readstring>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x18(%rbp),%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    -0x8(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
jne    11bc <readstring+0x7c>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
mov    -0x8(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11b7 <readstring+0x77>
mov    -0x8(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    11ff <readstring+0xbf>
jmp    11fa <readstring+0xba>
mov    0x2e15(%rip),%rax
mov    (%rax),%rax
mov    0x2e03(%rip),%rcx
cmp    (%rcx),%rax
je     11f5 <readstring+0xb5>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
jmp    11fa <readstring+0xba>
jmp    115a <readstring+0x1a>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readstring>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r15
mov    %rdi,%r14
xor    %r12d,%r12d
mov    0x2e7d(%rip),%r13
mov    0x2e6e(%rip),%rbp
jmp    1179 <readstring+0x39>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
add    $0x1,%r12
cmpb   $0xa,(%rbx)
je     11c5 <readstring+0x85>
mov    %r12d,%r12d
lea    (%r14,%r12,1),%rbx
mov    %r15,%rdi
call   1050 <fileno@plt>
mov    %eax,%edi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <read@plt>
cmp    $0x1,%eax
je     1170 <readstring+0x30>
mov    0x0(%r13),%rax
cmp    0x0(%rbp),%rax
je     1180 <readstring+0x40>
mov    0x2e33(%rip),%rax
mov    (%rax),%edi
lea    0xe4a(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1060 <exit@plt>
movb   $0x0,(%r14,%r12,1)
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [46 x i8] c"Error reading from socket or unexpected EOF.\0A\00"
define void @readstring(i8* %0, i32 %1, i32* %2) {
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
store i32* %2, i32** %6
store i32 0, i32* %7
br label %8
8: ; preds = %3, %43
%9 = load i32*, i32** %6
%10 = call i32 @fileno(i32* %9)
%11 = load i8*, i8** %4
%12 = load i32, i32* %7
%13 = sext i32 %12 to i64
%14 = getelementptr inbounds i8, i8* %11, i64 %13
%15 = call i32 @read(i32 %10, i8* %14, i32 1)
%16 = icmp eq i32 %15, 1
br i1 %16, label %17, label %34
17: ; preds = %8
%18 = load i32, i32* %7
%19 = add nsw i32 %18, 1
store i32 %19, i32* %7
%20 = load i8*, i8** %4
%21 = load i32, i32* %7
%22 = sub nsw i32 %21, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %20, i64 %23
%25 = load i8, i8* %24
%26 = sext i8 %25 to i32
%27 = icmp eq i32 %26, 10
br i1 %27, label %28, label %33
28: ; preds = %17
%29 = load i8*, i8** %4
%30 = load i32, i32* %7
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %29, i64 %31
store i8 0, i8* %32
br label %44
33: ; preds = %17
br label %43
34: ; preds = %8
%35 = load i64, i64* @errno
%36 = load i64, i64* @EINTR
%37 = icmp ne i64 %35, %36
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%41 = call i32 @exit(i32 1)
unreachable
42: ; preds = %34
br label %43
43: ; preds = %42, %33
br label %8
44: ; preds = %28
ret void
}

LLVM IR (-O2):
@errno = global i64 0
@EINTR = global i64 0
@stderr = global i32 0
@.str = [46 x i8] c"Error reading from socket or unexpected EOF.\0A\00"
define void @readstring(i8* nocapture %0, i32 %1, i32* nocapture %2) {
br label %4
4: ; preds = %12, %3
%5 = phi i32 [ %13, %12 ], [ 0, %3 ]
%6 = zext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
br label %8
8: ; preds = %4, %19
%9 = tail call i32 @fileno(i32* %2)
%10 = tail call i32 @read(i32 %9, i8* %7, i32 1)
%11 = icmp eq i32 %10, 1
br i1 %11, label %12, label %19
12: ; preds = %8
%13 = add nuw nsw i32 %5, 1
%14 = load i8, i8* %7
%15 = icmp eq i8 %14, 10
br i1 %15, label %16, label %4
16: ; preds = %12
%17 = zext i32 %13 to i64
%18 = getelementptr inbounds i8, i8* %0, i64 %17
store i8 0, i8* %18
ret void
19: ; preds = %8
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINTR
%22 = icmp eq i64 %20, %21
br i1 %22, label %8, label %23
23: ; preds = %19
%24 = load i32, i32* @stderr
%25 = tail call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([46 x i8], [46 x i8]* @.str, i64 0, i64 0))
%26 = tail call i32 @exit(i32 1)
unreachable
}