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

/* Variables and functions */
int errno;
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int /*<<< orphan*/ trimRight(char *);
int getLine(int socket, char *buffer, int bufferSize) {
  int lineLength = 0;
  ssize_t charactersRead = 0;
  while ((charactersRead = read(socket, buffer + lineLength, 1)) < bufferSize) {
    if (charactersRead == -1) {
      perror("Failure reading from socket");
      printf("Error: Problem reading line (%d)\n", errno);
      return -1;
    }
    if (charactersRead == 0) {
      return -1;
    }
    lineLength += charactersRead;
    if (buffer[lineLength - 1] == '\n') {
      break;
    }
  }
  buffer[lineLength] = 0;
  trimRight(buffer);
  return 0;
}


x86 assembly (clang, -O0):
<getLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x18(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
mov    %eax,-0x1c(%rbp)
cmp    -0x14(%rbp),%eax
jge    11fe <getLine+0xbe>
cmpl   $0xffffffff,-0x1c(%rbp)
jne    11bb <getLine+0x7b>
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
lea    0xe72(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <getLine+0xda>
cmpl   $0x0,-0x1c(%rbp)
jne    11d1 <getLine+0x91>
movl   $0xffffffff,-0x4(%rbp)
jmp    121a <getLine+0xda>
mov    -0x1c(%rbp),%eax
add    -0x18(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    11f9 <getLine+0xb9>
jmp    11fe <getLine+0xbe>
jmp    1160 <getLine+0x20>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rdi
call   1050 <trimRight@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getLine>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r14d
mov    %rsi,%r12
mov    %edi,%r15d
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
movslq %ebp,%rbx
lea    (%r12,%rbx,1),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
call   1040 <read@plt>
cmp    %r14d,%eax
jge    118f <getLine+0x4f>
test   %eax,%eax
je     11c1 <getLine+0x81>
cmp    $0xffffffff,%eax
je     11a0 <getLine+0x60>
add    %eax,%ebp
movslq %ebp,%rbx
cmpb   $0xa,-0x1(%rbx,%r12,1)
jne    1160 <getLine+0x20>
movb   $0x0,(%r12,%rbx,1)
mov    %r12,%rdi
call   1050 <trimRight@plt>
xor    %eax,%eax
jmp    11c6 <getLine+0x86>
lea    0xe59(%rip),%rdi
call   1060 <perror@plt>
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
lea    0xe60(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [28 x i8] c"Failure reading from socket\00"
@.str.1 = [34 x i8] c"Error: Problem reading line (%d)\0A\00"
@errno = global i32 0
define i32 @getLine(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
br label %10
10: ; preds = %43, %3
%11 = load i32, i32* %5
%12 = load i8*, i8** %6
%13 = load i32, i32* %8
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
%16 = call i32 @read(i32 %11, i8* %15, i32 1)
store i32 %16, i32* %9
%17 = load i32, i32* %7
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %44
19: ; preds = %10
%20 = load i32, i32* %9
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %26
22: ; preds = %19
%23 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%24 = load i32, i32* @errno
%25 = call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %24)
store i32 -1, i32* %4
br label %51
26: ; preds = %19
%27 = load i32, i32* %9
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %30
29: ; preds = %26
store i32 -1, i32* %4
br label %51
30: ; preds = %26
%31 = load i32, i32* %9
%32 = load i32, i32* %8
%33 = add nsw i32 %32, %31
store i32 %33, i32* %8
%34 = load i8*, i8** %6
%35 = load i32, i32* %8
%36 = sub nsw i32 %35, 1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %34, i64 %37
%39 = load i8, i8* %38
%40 = sext i8 %39 to i32
%41 = icmp eq i32 %40, 10
br i1 %41, label %42, label %43
42: ; preds = %30
br label %44
43: ; preds = %30
br label %10
44: ; preds = %42, %10
%45 = load i8*, i8** %6
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* %45, i64 %47
store i8 0, i8* %48
%49 = load i8*, i8** %6
%50 = call i32 @trimRight(i8* %49)
store i32 0, i32* %4
br label %51
51: ; preds = %44, %29, %22
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
@.str = [28 x i8] c"Failure reading from socket\00"
@.str.1 = [34 x i8] c"Error: Problem reading line (%d)\0A\00"
@errno = global i32 0
define i32 @getLine(i32 %0, i8* %1, i32 %2) {
br label %4
4: ; preds = %15, %3
%5 = phi i32 [ 0, %3 ], [ %16, %15 ]
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %1, i64 %6
%8 = tail call i32 @read(i32 %0, i8* %7, i32 1)
%9 = icmp slt i32 %8, %2
br i1 %9, label %10, label %24
10: ; preds = %4
switch i32 %8, label %15 [
i32 -1, label %11
i32 0, label %28
]
11: ; preds = %10
%12 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%13 = load i32, i32* @errno
%14 = tail call i32 @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %28
15: ; preds = %10
%16 = add nsw i32 %8, %5
%17 = add nsw i32 %16, -1
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %1, i64 %18
%20 = load i8, i8* %19
%21 = icmp eq i8 %20, 10
br i1 %21, label %22, label %4
22: ; preds = %15
%23 = sext i32 %16 to i64
br label %24
24: ; preds = %4, %22
%25 = phi i64 [ %23, %22 ], [ %6, %4 ]
%26 = getelementptr inbounds i8, i8* %1, i64 %25
store i8 0, i8* %26
%27 = tail call i32 @trimRight(i8* %1)
br label %28
28: ; preds = %10, %24, %11
%29 = phi i32 [ -1, %11 ], [ 0, %24 ], [ -1, %10 ]
ret i32 %29
}