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

/* Variables and functions */
int EINTR;
int /*<<< orphan*/ debug(int, char *);
int read(int, char *, int);
int /*<<< orphan*/ socket_wait_read(int, int);
int socket_read_line(int socket, char *buffer, int size, int timeout) {
  int received;
  int readval;
  char lf, ret;
  for (received = 0, lf = 0, ret = 0; received < size;) {
    if (!socket_wait_read(socket, timeout)) {
      debug(2, "Socket read timed out.");
      return (received);
    }
    readval = read(socket, ((char *)buffer) + received, 1);
    if (readval == EINTR)
      continue;
    if (readval != 1) {
      return (readval);
    }
    if (buffer[received] == 0x0d) {
      lf = 1;
    } else if (buffer[received] == 0x0a) {
      ret = 1;
    } else
      lf = ret = 0;
    if (lf & ret) {
      buffer[received - 1] = 0;
      received -= 1;
      break;
    }
    received++;
  }
  return (received);
}


x86 assembly (clang, -O0):
<socket_read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %ecx,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movb   $0x0,-0x21(%rbp)
movb   $0x0,-0x22(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    1260 <socket_read_line+0x130>
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%esi
call   1030 <socket_wait_read@plt>
cmp    $0x0,%eax
jne    1190 <socket_read_line+0x60>
mov    $0x2,%edi
lea    0xe80(%rip),%rsi
call   1040 <debug@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <socket_read_line+0x136>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    %eax,-0x20(%rbp)
mov    -0x20(%rbp),%eax
mov    0x2e2b(%rip),%rcx
cmp    (%rcx),%eax
jne    11c2 <socket_read_line+0x92>
jmp    1154 <socket_read_line+0x24>
cmpl   $0x1,-0x20(%rbp)
je     11d7 <socket_read_line+0xa7>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1266 <socket_read_line+0x136>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xd,%eax
jne    11f5 <socket_read_line+0xc5>
movb   $0x1,-0x21(%rbp)
jmp    1220 <socket_read_line+0xf0>
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
movsbl (%rax,%rcx,1),%eax
cmp    $0xa,%eax
jne    1213 <socket_read_line+0xe3>
movb   $0x1,-0x22(%rbp)
jmp    121b <socket_read_line+0xeb>
movb   $0x0,-0x22(%rbp)
movb   $0x0,-0x21(%rbp)
jmp    1220 <socket_read_line+0xf0>
movsbl -0x21(%rbp),%eax
movsbl -0x22(%rbp),%ecx
and    %ecx,%eax
cmp    $0x0,%eax
je     1252 <socket_read_line+0x122>
mov    -0x10(%rbp),%rax
mov    -0x1c(%rbp),%ecx
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x1c(%rbp),%eax
sub    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1260 <socket_read_line+0x130>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1154 <socket_read_line+0x24>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_read_line>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
test   %edx,%edx
jle    11fa <socket_read_line+0xca>
mov    %ecx,%r12d
mov    %edx,%r15d
mov    %edi,%ebx
mov    %edx,%eax
mov    %rax,0x8(%rsp)
xor    %r13d,%r13d
mov    0x2e81(%rip),%r14
mov    %rsi,%rbp
xor    %ecx,%ecx
xor    %eax,%eax
mov    %rsi,0x10(%rsp)
mov    %ecx,(%rsp)
mov    %eax,0x4(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %ebx,%edi
mov    %r12d,%esi
call   1030 <socket_wait_read@plt>
test   %eax,%eax
je     11e4 <socket_read_line+0xb4>
mov    %ebx,%edi
mov    %rbp,%rsi
mov    $0x1,%edx
call   1050 <read@plt>
cmp    (%r14),%eax
je     1180 <socket_read_line+0x50>
cmp    $0x1,%eax
jne    120f <socket_read_line+0xdf>
mov    0x10(%rsp),%rsi
mov    (%rsi,%r13,1),%al
cmp    $0xd,%al
je     11c0 <socket_read_line+0x90>
cmp    $0xa,%al
jne    11c7 <socket_read_line+0x97>
mov    $0x1,%cl
mov    0x4(%rsp),%eax
jmp    11cb <socket_read_line+0x9b>
mov    $0x1,%al
mov    (%rsp),%ecx
jmp    11cb <socket_read_line+0x9b>
xor    %eax,%eax
xor    %ecx,%ecx
test   %al,%cl
jne    1214 <socket_read_line+0xe4>
lea    (%rsi,%r13,1),%rbp
add    $0x1,%rbp
add    $0x1,%r13
cmp    0x8(%rsp),%r13
jne    116b <socket_read_line+0x3b>
jmp    11fd <socket_read_line+0xcd>
lea    0xe15(%rip),%rsi
mov    $0x2,%edi
call   1040 <debug@plt>
mov    %r13d,%r15d
jmp    11fd <socket_read_line+0xcd>
xor    %r15d,%r15d
mov    %r15d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %eax,%r15d
jmp    11fd <socket_read_line+0xcd>
movslq %r13d,%r15
movb   $0x0,-0x1(%rsi,%r15,1)
add    $0xffffffffffffffff,%r15
jmp    11fd <socket_read_line+0xcd>

LLVM IR (-O0):
@.str = [23 x i8] c"Socket read timed out.\00"
@EINTR = global i32 0
define i32 @socket_read_line(i32 %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i8
%13 = alloca i8
store i32 %0, i32* %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
store i32 0, i32* %10
store i8 0, i8* %12
store i8 0, i8* %13
br label %14
14: ; preds = %77, %36, %4
%15 = load i32, i32* %10
%16 = load i32, i32* %8
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %80
18: ; preds = %14
%19 = load i32, i32* %6
%20 = load i32, i32* %9
%21 = call i32 @socket_wait_read(i32 %19, i32 %20)
%22 = icmp ne i32 %21, 0
br i1 %22, label %26, label %23
23: ; preds = %18
%24 = call i32 @debug(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
%25 = load i32, i32* %10
store i32 %25, i32* %5
br label %82
26: ; preds = %18
%27 = load i32, i32* %6
%28 = load i8*, i8** %7
%29 = load i32, i32* %10
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* %28, i64 %30
%32 = call i32 @read(i32 %27, i8* %31, i32 1)
store i32 %32, i32* %11
%33 = load i32, i32* %11
%34 = load i32, i32* @EINTR
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %26
br label %14
37: ; preds = %26
%38 = load i32, i32* %11
%39 = icmp ne i32 %38, 1
br i1 %39, label %40, label %42
40: ; preds = %37
%41 = load i32, i32* %11
store i32 %41, i32* %5
br label %82
42: ; preds = %37
%43 = load i8*, i8** %7
%44 = load i32, i32* %10
%45 = sext i32 %44 to i64
%46 = getelementptr inbounds i8, i8* %43, i64 %45
%47 = load i8, i8* %46
%48 = sext i8 %47 to i32
%49 = icmp eq i32 %48, 13
br i1 %49, label %50, label %51
50: ; preds = %42
store i8 1, i8* %12
br label %62
51: ; preds = %42
%52 = load i8*, i8** %7
%53 = load i32, i32* %10
%54 = sext i32 %53 to i64
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = load i8, i8* %55
%57 = sext i8 %56 to i32
%58 = icmp eq i32 %57, 10
br i1 %58, label %59, label %60
59: ; preds = %51
store i8 1, i8* %13
br label %61
60: ; preds = %51
store i8 0, i8* %13
store i8 0, i8* %12
br label %61
61: ; preds = %60, %59
br label %62
62: ; preds = %61, %50
%63 = load i8, i8* %12
%64 = sext i8 %63 to i32
%65 = load i8, i8* %13
%66 = sext i8 %65 to i32
%67 = and i32 %64, %66
%68 = icmp ne i32 %67, 0
br i1 %68, label %69, label %77
69: ; preds = %62
%70 = load i8*, i8** %7
%71 = load i32, i32* %10
%72 = sub nsw i32 %71, 1
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds i8, i8* %70, i64 %73
store i8 0, i8* %74
%75 = load i32, i32* %10
%76 = sub nsw i32 %75, 1
store i32 %76, i32* %10
br label %80
77: ; preds = %62
%78 = load i32, i32* %10
%79 = add nsw i32 %78, 1
store i32 %79, i32* %10
br label %14
80: ; preds = %69, %14
%81 = load i32, i32* %10
store i32 %81, i32* %5
br label %82
82: ; preds = %80, %40, %23
%83 = load i32, i32* %5
ret i32 %83
}

LLVM IR (-O2):
@.str = [23 x i8] c"Socket read timed out.\00"
@EINTR = global i32 0
define i32 @socket_read_line(i32 %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %43
6: ; preds = %4
%7 = zext i32 %2 to i64
br label %8
8: ; preds = %6, %39
%9 = phi i64 [ 0, %6 ], [ %40, %39 ]
%10 = phi i8* [ %1, %6 ], [ %41, %39 ]
%11 = phi i8 [ 0, %6 ], [ %31, %39 ]
%12 = phi i8 [ 0, %6 ], [ %30, %39 ]
br label %13
13: ; preds = %8, %19
%14 = tail call i32 @socket_wait_read(i32 %0, i32 %3)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %19
16: ; preds = %13
%17 = trunc i64 %9 to i32
%18 = tail call i32 @debug(i32 2, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0))
br label %43
19: ; preds = %13
%20 = tail call i32 @read(i32 %0, i8* %10, i32 1)
%21 = load i32, i32* @EINTR
%22 = icmp eq i32 %20, %21
br i1 %22, label %13, label %23
23: ; preds = %19
%24 = icmp eq i32 %20, 1
br i1 %24, label %25, label %43
25: ; preds = %23
%26 = load i8, i8* %10
switch i8 %26, label %28 [
i8 13, label %29
i8 10, label %27
]
27: ; preds = %25
br label %29
28: ; preds = %25
br label %29
29: ; preds = %25, %27, %28
%30 = phi i8 [ %12, %27 ], [ 0, %28 ], [ 1, %25 ]
%31 = phi i8 [ 1, %27 ], [ 0, %28 ], [ %11, %25 ]
%32 = and i8 %31, %30
%33 = icmp eq i8 %32, 0
br i1 %33, label %39, label %34
34: ; preds = %29
%35 = trunc i64 %9 to i32
%36 = add nsw i32 %35, -1
%37 = sext i32 %36 to i64
%38 = getelementptr inbounds i8, i8* %1, i64 %37
store i8 0, i8* %38
br label %43
39: ; preds = %29
%40 = add nuw nsw i64 %9, 1
%41 = getelementptr inbounds i8, i8* %1, i64 %40
%42 = icmp eq i64 %40, %7
br i1 %42, label %43, label %8
43: ; preds = %39, %23, %4, %34, %16
%44 = phi i32 [ %17, %16 ], [ %36, %34 ], [ 0, %4 ], [ %2, %39 ], [ %20, %23 ]
ret i32 %44
}