#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 */
char *calloc(int, int);
int recv(int, char *, int, int /*<<< orphan*/);
char *socket_gets(int sockid) {
  char c;
  char *s = calloc(1024, sizeof(*s));
  int r;
  int l = 0;
  while ((r = recv(sockid, &c, 1, 0)) > 0) {
    s[l++] = c;
    if (c == '\n') {
      break;
    }
  }
  s[l] = 0;
  return s;
}


x86 assembly (clang, -O0):
<socket_gets>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    $0x400,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x18(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jle    1193 <socket_gets+0x73>
mov    -0x5(%rbp),%dl
mov    -0x10(%rbp),%rax
mov    -0x18(%rbp),%ecx
mov    %ecx,%esi
add    $0x1,%esi
mov    %esi,-0x18(%rbp)
movslq %ecx,%rcx
mov    %dl,(%rax,%rcx,1)
movsbl -0x5(%rbp),%eax
cmp    $0xa,%eax
jne    118e <socket_gets+0x6e>
jmp    1193 <socket_gets+0x73>
jmp    1145 <socket_gets+0x25>
mov    -0x10(%rbp),%rax
movslq -0x18(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x10(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_gets>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%r15d
mov    $0x400,%edi
mov    $0x1,%esi
call   1040 <calloc@plt>
mov    %rax,%rbx
xor    %ebp,%ebp
lea    0x7(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    %r15d,%edi
mov    %r14,%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %eax,%eax
jle    1176 <socket_gets+0x56>
movzbl 0x7(%rsp),%eax
mov    %al,(%rbx,%rbp,1)
add    $0x1,%rbp
cmp    $0xa,%al
jne    1150 <socket_gets+0x30>
movslq %ebp,%rax
movb   $0x0,(%rbx,%rax,1)
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i8* @socket_gets(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
%7 = call i8* @calloc(i32 1024, i32 1)
store i8* %7, i8** %4
store i32 0, i32* %6
br label %8
8: ; preds = %23, %1
%9 = load i32, i32* %2
%10 = call i32 @recv(i32 %9, i8* %3, i32 1, i32 0)
store i32 %10, i32* %5
%11 = icmp sgt i32 %10, 0
br i1 %11, label %12, label %24
12: ; preds = %8
%13 = load i8, i8* %3
%14 = load i8*, i8** %4
%15 = load i32, i32* %6
%16 = add nsw i32 %15, 1
store i32 %16, i32* %6
%17 = sext i32 %15 to i64
%18 = getelementptr inbounds i8, i8* %14, i64 %17
store i8 %13, i8* %18
%19 = load i8, i8* %3
%20 = sext i8 %19 to i32
%21 = icmp eq i32 %20, 10
br i1 %21, label %22, label %23
22: ; preds = %12
br label %24
23: ; preds = %12
br label %8
24: ; preds = %22, %8
%25 = load i8*, i8** %4
%26 = load i32, i32* %6
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8, i8* %25, i64 %27
store i8 0, i8* %28
%29 = load i8*, i8** %4
ret i8* %29
}

LLVM IR (-O2):
define noalias i8* @socket_gets(i32 %0) {
%2 = alloca i8
%3 = tail call dereferenceable_or_null(1024) i8* @calloc(i32 1024, i32 1)
br label %4
4: ; preds = %8, %1
%5 = phi i64 [ %10, %8 ], [ 0, %1 ]
%6 = call i32 @recv(i32 %0, i8* %2, i32 1, i32 0)
%7 = icmp sgt i32 %6, 0
br i1 %7, label %8, label %13
8: ; preds = %4
%9 = load i8, i8* %2
%10 = add nuw i64 %5, 1
%11 = getelementptr inbounds i8, i8* %3, i64 %5
store i8 %9, i8* %11
%12 = icmp eq i8 %9, 10
br i1 %12, label %13, label %4
13: ; preds = %8, %4
%14 = phi i64 [ %10, %8 ], [ %5, %4 ]
%15 = shl i64 %14, 32
%16 = ashr exact i64 %15, 32
%17 = getelementptr inbounds i8, i8* %3, i64 %16
store i8 0, i8* %17
ret i8* %3
}