#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*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, int);
scalar_t__ malloc(int);
int /*<<< orphan*/ perror(char *);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
int /*<<< orphan*/ trimRight(char *);
char *readLine(int socket) {
  char tline[1024];
  char *line;
  int lineLength = 0;
  ssize_t charactersRead = 0;
  while ((charactersRead = read(socket, tline + lineLength, 1)) < 1024) {
    if (charactersRead == -1) {
      perror("Failure reading from socket");
      fprintf(stderr, "Error: Problem reading HTTP headers (%d)\n", errno);
      exit(2);
    }
    lineLength += charactersRead;
    if (tline[lineLength - 1] == '\n') {
      break;
    }
  }
  tline[lineLength] = 0;
  trimRight(tline);
  line = (char *)malloc((strlen(tline) + 1) * sizeof(char));
  strcpy(line, tline);
  return line;
}


x86 assembly (clang, -O0):
<readLine>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x41c(%rbp)
movl   $0x0,-0x420(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
movslq -0x41c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x420(%rbp)
cmp    $0x400,%eax
jge    124a <readLine+0xca>
cmpl   $0xffffffff,-0x420(%rbp)
jne    1212 <readLine+0x92>
lea    0xe1b(%rip),%rdi
call   1090 <perror@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%edi
mov    0x2dde(%rip),%rax
mov    (%rax),%edx
lea    0xe19(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
mov    -0x420(%rbp),%eax
add    -0x41c(%rbp),%eax
mov    %eax,-0x41c(%rbp)
mov    -0x41c(%rbp),%eax
sub    $0x1,%eax
cltq
movsbl -0x410(%rbp,%rax,1),%eax
ff
cmp    $0xa,%eax
jne    1245 <readLine+0xc5>
jmp    124a <readLine+0xca>
jmp    11a2 <readLine+0x22>
movslq -0x41c(%rbp),%rax
movb   $0x0,-0x410(%rbp,%rax,1)
lea    -0x410(%rbp),%rdi
call   1080 <trimRight@plt>
lea    -0x410(%rbp),%rdi
call   1040 <strlen@plt>
add    $0x1,%eax
cltq
shl    $0x0,%rax
mov    %eax,%edi
call   1070 <malloc@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
lea    -0x410(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x418(%rbp),%rax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<readLine>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x408,%rsp
mov    %edi,%r15d
xor    %ebp,%ebp
mov    %rsp,%r14
cs nopw 0x0(%rax,%rax,1)
nop
movslq %ebp,%rbx
lea    (%rsp,%rbx,1),%rsi
mov    %r15d,%edi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    $0x3ff,%eax
jg     11cd <readLine+0x4d>
cmp    $0xffffffff,%eax
je     120b <readLine+0x8b>
add    %eax,%ebp
movslq %ebp,%rbx
cmpb   $0xa,-0x1(%rbx,%r14,1)
jne    11a0 <readLine+0x20>
movb   $0x0,(%rsp,%rbx,1)
mov    %rsp,%r14
mov    %r14,%rdi
call   1080 <trimRight@plt>
mov    %r14,%rdi
call   1040 <strlen@plt>
lea    0x1(%rax),%edi
call   1070 <malloc@plt>
mov    %rax,%rbx
mov    %rax,%rdi
mov    %r14,%rsi
call   1030 <strcpy@plt>
mov    %rbx,%rax
add    $0x408,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdee(%rip),%rdi
call   1090 <perror@plt>
mov    0x2dc2(%rip),%rax
mov    (%rax),%edi
mov    0x2db1(%rip),%rax
mov    (%rax),%edx
lea    0xdec(%rip),%rsi
call   1060 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

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

LLVM IR (-O2):
@.str = [28 x i8] c"Failure reading from socket\00"
@stderr = global i32 0
@.str.1 = [42 x i8] c"Error: Problem reading HTTP headers (%d)\0A\00"
@errno = global i32 0
define i8* @readLine(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %18, %1
%5 = phi i32 [ 0, %1 ], [ %19, %18 ]
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %6
%8 = call i32 @read(i32 %0, i8* %7, i32 1)
%9 = icmp slt i32 %8, 1024
br i1 %9, label %10, label %27
10: ; preds = %4
%11 = icmp eq i32 %8, -1
br i1 %11, label %12, label %18
12: ; preds = %10
%13 = tail call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str, i64 0, i64 0))
%14 = load i32, i32* @stderr
%15 = load i32, i32* @errno
%16 = tail call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.1, i64 0, i64 0), i32 %15)
%17 = tail call i32 @exit(i32 2)
unreachable
18: ; preds = %10
%19 = add nsw i32 %8, %5
%20 = add nsw i32 %19, -1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %21
%23 = load i8, i8* %22
%24 = icmp eq i8 %23, 10
br i1 %24, label %25, label %4
25: ; preds = %18
%26 = sext i32 %19 to i64
br label %27
27: ; preds = %4, %25
%28 = phi i64 [ %26, %25 ], [ %6, %4 ]
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i32 @trimRight(i8* %3)
%31 = call i32 @strlen(i8* %3)
%32 = add nsw i32 %31, 1
%33 = call i64 @malloc(i32 %32)
%34 = inttoptr i64 %33 to i8*
%35 = call i32 @strcpy(i8* %34, i8* %3)
ret i8* %34
}