#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 /*<<< orphan*/ close(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int read_line(int newSd, char *line_to_return) {
  static int rcv_ptr = 0;
  static char rcv_msg[100];
  static int n;
  int offset;
  offset = 0;
  while (1) {
    if (rcv_ptr == 0) {
      memset(rcv_msg, 0x0, 100);
      n = recv(newSd, rcv_msg, 100, 0);
      if (n < 0) {
        perror(" cannot receive data ");
        return 1;
      } else if (n == 0) {
        printf(" connection closed by client\n");
        close(newSd);
        return 1;
      }
    }
    while (*(rcv_msg + rcv_ptr) != 0x0 && rcv_ptr < n) {
      memcpy(line_to_return + offset, rcv_msg + rcv_ptr, 1);
      offset++;
      rcv_ptr++;
    }
    if (rcv_ptr == n - 1) {
      *(line_to_return + offset) = 0x0;
      rcv_ptr = 0;
      return ++offset;
    }
    if (rcv_ptr < n - 1) {
      *(line_to_return + offset) = 0x0;
      rcv_ptr++;
      return ++offset;
    }
    if (rcv_ptr == n) {
      rcv_ptr = 0;
    }
  }
}


x86 assembly (clang, -O0):
<read_line>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
cmpl   $0x0,0x2ee3(%rip)
jne    120e <read_line+0xae>
lea    0x2ee6(%rip),%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    0x2ed0(%rip),%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,0x2f22(%rip)
cmpl   $0x0,0x2f1b(%rip)
jge    11d7 <read_line+0x77>
lea    0xe3a(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    133a <read_line+0x1da>
cmpl   $0x0,0x2ef6(%rip)
jne    1204 <read_line+0xa4>
lea    0xe2b(%rip),%rdi
call   1040 <printf@plt>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    133a <read_line+0x1da>
jmp    1209 <read_line+0xa9>
jmp    120e <read_line+0xae>
jmp    1213 <read_line+0xb3>
movslq 0x2e46(%rip),%rcx
lea    0x2e4f(%rip),%rax
movsbl (%rax,%rcx,1),%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x15(%rbp)
je     1245 <read_line+0xe5>
mov    0x2e27(%rip),%eax
cmp    0x2e95(%rip),%eax
setl   %al
mov    %al,-0x15(%rbp)
mov    -0x15(%rbp),%al
test   $0x1,%al
jne    1255 <read_line+0xf5>
jmp    1298 <read_line+0x138>
mov    -0x10(%rbp),%rdi
movslq -0x14(%rbp),%rax
add    %rax,%rdi
movslq 0x2df9(%rip),%rax
lea    0x2e02(%rip),%rsi
add    %rax,%rsi
mov    $0x1,%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    0x2dd6(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2dcd(%rip)
jmp    1213 <read_line+0xb3>
mov    0x2dc2(%rip),%eax
mov    0x2e30(%rip),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jne    12d6 <read_line+0x176>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
movl   $0x0,0x2d9b(%rip)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    %eax,-0x4(%rbp)
jmp    133a <read_line+0x1da>
mov    0x2d84(%rip),%eax
mov    0x2df2(%rip),%ecx
sub    $0x1,%ecx
cmp    %ecx,%eax
jge    1319 <read_line+0x1b9>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    0x2d61(%rip),%eax
add    $0x1,%eax
mov    %eax,0x2d58(%rip)
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
mov    %eax,-0x4(%rbp)
jmp    133a <read_line+0x1da>
mov    0x2d41(%rip),%eax
cmp    0x2daf(%rip),%eax
jne    1335 <read_line+0x1d5>
movl   $0x0,0x2d2b(%rip)
jmp    1176 <read_line+0x16>
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_line>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %edi,%r14d
xor    %ebx,%ebx
mov    0x2eea(%rip),%ecx
lea    0x2ef3(%rip),%r12
nopl   (%rax)
test   %ecx,%ecx
je     1190 <read_line+0x30>
mov    0x2f4a(%rip),%eax
jmp    11cb <read_line+0x6b>
nopl   0x0(%rax)
mov    %r12,%rdi
xor    %esi,%esi
mov    $0x64,%edx
call   1050 <memset@plt>
mov    %r14d,%edi
mov    %r12,%rsi
mov    $0x64,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,0x2f1d(%rip)
test   %eax,%eax
js     12a2 <read_line+0x142>
je     127e <read_line+0x11e>
mov    0x2e95(%rip),%ecx
movslq %ecx,%rsi
cmpb   $0x0,(%rsi,%r12,1)
je     122f <read_line+0xcf>
cmp    %eax,%ecx
jge    122f <read_line+0xcf>
add    %r12,%rsi
movslq %ebx,%rbp
add    %r15,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    %rbp,%rdi
mov    $0x1,%edx
call   1070 <memcpy@plt>
movslq 0x2e5c(%rip),%rdx
lea    0x1(%rdx),%rcx
mov    %ecx,0x2e52(%rip)
add    $0x1,%ebx
cmpb   $0x0,0x1(%rdx,%r12,1)
mov    0x2eb7(%rip),%eax
je     122f <read_line+0xcf>
lea    (%rdx,%r12,1),%rsi
add    $0x1,%rsi
add    $0x1,%rbp
cmp    %eax,%ecx
jl     11f0 <read_line+0x90>
lea    -0x1(%rax),%edx
cmp    %edx,%ecx
je     1251 <read_line+0xf1>
jl     1268 <read_line+0x108>
cmp    %eax,%ecx
jne    1180 <read_line+0x20>
movl   $0x0,0x2e16(%rip)
xor    %ecx,%ecx
jmp    1180 <read_line+0x20>
movslq %ebx,%rax
movb   $0x0,(%r15,%rax,1)
movl   $0x0,0x2dfd(%rip)
add    $0x1,%ebx
jmp    1297 <read_line+0x137>
movslq %ebx,%rbx
movb   $0x0,(%r15,%rbx,1)
add    $0x1,%ecx
mov    %ecx,0x2de7(%rip)
add    $0x1,%ebx
jmp    1297 <read_line+0x137>
lea    0xd91(%rip),%rdi
call   1040 <puts@plt>
mov    %r14d,%edi
call   1060 <close@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd57(%rip),%rdi
call   1080 <perror@plt>
jmp    1292 <read_line+0x132>

LLVM IR (-O0):
@read_line.rcv_ptr = internal global i32 0
@read_line.rcv_msg = internal global [100 x i8] zeroinitializer
@read_line.n = internal global i32 0
@.str = [22 x i8] c" cannot receive data \00"
@.str.1 = [30 x i8] c" connection closed by client\0A\00"
define i32 @read_line(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
br label %7
7: ; preds = %2, %85
%8 = load i32, i32* @read_line.rcv_ptr
%9 = icmp eq i32 %8, 0
br i1 %9, label %10, label %27
10: ; preds = %7
%11 = call i32 @memset(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @read_line.rcv_msg, i64 0, i64 0), i32 0, i32 100)
%12 = load i32, i32* %4
%13 = call i32 @recv(i32 %12, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @read_line.rcv_msg, i64 0, i64 0), i32 100, i32 0)
store i32 %13, i32* @read_line.n
%14 = load i32, i32* @read_line.n
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %10
%17 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %86
18: ; preds = %10
%19 = load i32, i32* @read_line.n
%20 = icmp eq i32 %19, 0
br i1 %20, label %21, label %25
21: ; preds = %18
%22 = call i32 @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0))
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
store i32 1, i32* %3
br label %86
25: ; preds = %18
br label %26
26: ; preds = %25
br label %27
27: ; preds = %26, %7
br label %28
28: ; preds = %41, %27
%29 = load i32, i32* @read_line.rcv_ptr
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds i8, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @read_line.rcv_msg, i64 0, i64 0), i64 %30
%32 = load i8, i8* %31
%33 = sext i8 %32 to i32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %39
35: ; preds = %28
%36 = load i32, i32* @read_line.rcv_ptr
%37 = load i32, i32* @read_line.n
%38 = icmp slt i32 %36, %37
br label %39
39: ; preds = %35, %28
%40 = phi i1 [ false, %28 ], [ %38, %35 ]
br i1 %40, label %41, label %54
41: ; preds = %39
%42 = load i8*, i8** %5
%43 = load i32, i32* %6
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds i8, i8* %42, i64 %44
%46 = load i32, i32* @read_line.rcv_ptr
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds i8, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @read_line.rcv_msg, i64 0, i64 0), i64 %47
%49 = call i32 @memcpy(i8* %45, i8* %48, i32 1)
%50 = load i32, i32* %6
%51 = add nsw i32 %50, 1
store i32 %51, i32* %6
%52 = load i32, i32* @read_line.rcv_ptr
%53 = add nsw i32 %52, 1
store i32 %53, i32* @read_line.rcv_ptr
br label %28
54: ; preds = %39
%55 = load i32, i32* @read_line.rcv_ptr
%56 = load i32, i32* @read_line.n
%57 = sub nsw i32 %56, 1
%58 = icmp eq i32 %55, %57
br i1 %58, label %59, label %66
59: ; preds = %54
%60 = load i8*, i8** %5
%61 = load i32, i32* %6
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds i8, i8* %60, i64 %62
store i8 0, i8* %63
store i32 0, i32* @read_line.rcv_ptr
%64 = load i32, i32* %6
%65 = add nsw i32 %64, 1
store i32 %65, i32* %6
store i32 %65, i32* %3
br label %86
66: ; preds = %54
%67 = load i32, i32* @read_line.rcv_ptr
%68 = load i32, i32* @read_line.n
%69 = sub nsw i32 %68, 1
%70 = icmp slt i32 %67, %69
br i1 %70, label %71, label %80
71: ; preds = %66
%72 = load i8*, i8** %5
%73 = load i32, i32* %6
%74 = sext i32 %73 to i64
%75 = getelementptr inbounds i8, i8* %72, i64 %74
store i8 0, i8* %75
%76 = load i32, i32* @read_line.rcv_ptr
%77 = add nsw i32 %76, 1
store i32 %77, i32* @read_line.rcv_ptr
%78 = load i32, i32* %6
%79 = add nsw i32 %78, 1
store i32 %79, i32* %6
store i32 %79, i32* %3
br label %86
80: ; preds = %66
%81 = load i32, i32* @read_line.rcv_ptr
%82 = load i32, i32* @read_line.n
%83 = icmp eq i32 %81, %82
br i1 %83, label %84, label %85
84: ; preds = %80
store i32 0, i32* @read_line.rcv_ptr
br label %85
85: ; preds = %84, %80
br label %7
86: ; preds = %71, %59, %21, %16
%87 = load i32, i32* %3
ret i32 %87
}

LLVM IR (-O2):
@read_line.rcv_ptr = internal unnamed_addr global i32 0
@read_line.rcv_msg = internal global [100 x i8] zeroinitializer
@read_line.n = internal unnamed_addr global i32 0
@.str = [22 x i8] c" cannot receive data \00"
@str = [29 x i8] c" connection closed by client\00"
define i32 @read_line(i32 %0, i8* %1) {
%3 = load i32, i32* @read_line.rcv_ptr
br label %4
4: ; preds = %71, %2
%5 = phi i32 [ %3, %2 ], [ %72, %71 ]
%6 = phi i32 [ 0, %2 ], [ %53, %71 ]
%7 = icmp eq i32 %5, 0
br i1 %7, label %10, label %8
8: ; preds = %4
%9 = load i32, i32* @read_line.n
br label %23
10: ; preds = %4
%11 = tail call i32 @memset(i8* getelementptr inbounds ([100 x i8], [100 x i8]* @read_line.rcv_msg, i64 0, i64 0), i32 0, i32 100)
%12 = tail call i32 @recv(i32 %0, i8* getelementptr inbounds ([100 x i8], [100 x i8]* @read_line.rcv_msg, i64 0, i64 0), i32 100, i32 0)
store i32 %12, i32* @read_line.n
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %10
%15 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %73
16: ; preds = %10
%17 = icmp eq i32 %12, 0
br i1 %17, label %20, label %18
18: ; preds = %16
%19 = load i32, i32* @read_line.rcv_ptr
br label %23
20: ; preds = %16
%21 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
%22 = tail call i32 @close(i32 %0)
br label %73
23: ; preds = %8, %18
%24 = phi i32 [ %12, %18 ], [ %9, %8 ]
%25 = phi i32 [ %19, %18 ], [ %5, %8 ]
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds [100 x i8], [100 x i8]* @read_line.rcv_msg, i64 0, i64 %26
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
%30 = icmp slt i32 %25, %24
%31 = select i1 %29, i1 %30, i1 false
br i1 %31, label %32, label %51
32: ; preds = %23
%33 = sext i32 %6 to i64
br label %34
34: ; preds = %32, %34
%35 = phi i64 [ %33, %32 ], [ %39, %34 ]
%36 = phi i8* [ %27, %32 ], [ %43, %34 ]
%37 = getelementptr inbounds i8, i8* %1, i64 %35
%38 = tail call i32 @memcpy(i8* %37, i8* %36, i32 1)
%39 = add i64 %35, 1
%40 = load i32, i32* @read_line.rcv_ptr
%41 = add nsw i32 %40, 1
store i32 %41, i32* @read_line.rcv_ptr
%42 = sext i32 %41 to i64
%43 = getelementptr inbounds [100 x i8], [100 x i8]* @read_line.rcv_msg, i64 0, i64 %42
%44 = load i8, i8* %43
%45 = icmp ne i8 %44, 0
%46 = load i32, i32* @read_line.n
%47 = icmp slt i32 %41, %46
%48 = select i1 %45, i1 %47, i1 false
br i1 %48, label %34, label %49
49: ; preds = %34
%50 = trunc i64 %39 to i32
br label %51
51: ; preds = %49, %23
%52 = phi i32 [ %25, %23 ], [ %41, %49 ]
%53 = phi i32 [ %6, %23 ], [ %50, %49 ]
%54 = phi i32 [ %24, %23 ], [ %46, %49 ]
%55 = add nsw i32 %54, -1
%56 = icmp eq i32 %52, %55
br i1 %56, label %57, label %61
57: ; preds = %51
%58 = sext i32 %53 to i64
%59 = getelementptr inbounds i8, i8* %1, i64 %58
store i8 0, i8* %59
store i32 0, i32* @read_line.rcv_ptr
%60 = add nsw i32 %53, 1
br label %73
61: ; preds = %51
%62 = icmp slt i32 %52, %55
br i1 %62, label %63, label %68
63: ; preds = %61
%64 = sext i32 %53 to i64
%65 = getelementptr inbounds i8, i8* %1, i64 %64
store i8 0, i8* %65
%66 = add nsw i32 %52, 1
store i32 %66, i32* @read_line.rcv_ptr
%67 = add nsw i32 %53, 1
br label %73
68: ; preds = %61
%69 = icmp eq i32 %52, %54
br i1 %69, label %70, label %71
70: ; preds = %68
store i32 0, i32* @read_line.rcv_ptr
br label %71
71: ; preds = %70, %68
%72 = phi i32 [ 0, %70 ], [ %52, %68 ]
br label %4
73: ; preds = %63, %57, %20, %14
%74 = phi i32 [ 1, %14 ], [ 1, %20 ], [ %60, %57 ], [ %67, %63 ]
ret i32 %74
}