#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*/ read(int, char *, int);
scalar_t__ recv(int, char *, size_t, int /*<<< orphan*/);
size_t socket_read_answer_size(int fd) {
  size_t result = 0;
  size_t len = 7;
  char t[len];
  char digit = 0;
  if (recv(fd, t, len, 0) < 0)
    return -1;
  while (1) {
    read(fd, &digit, 1);
    if (digit == '\n')
      break;
    result = result * 10 + (digit - '0');
  }
  return result;
}


x86 assembly (clang, -O0):
<socket_read_answer_size>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0xc(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x7,-0x20(%rbp)
mov    -0x20(%rbp),%rax
mov    %rsp,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,%rsp
mov    %rax,-0x30(%rbp)
movb   $0x0,-0x31(%rbp)
mov    -0xc(%rbp),%edi
mov    -0x20(%rbp),%rdx
xor    %ecx,%ecx
call   1030 <recv@plt>
cmp    $0x0,%rax
jge    118e <socket_read_answer_size+0x6e>
movq   $0xffffffffffffffff,-0x8(%rbp)
ff
movl   $0x1,-0x38(%rbp)
jmp    11e5 <socket_read_answer_size+0xc5>
jmp    1193 <socket_read_answer_size+0x73>
jmp    1198 <socket_read_answer_size+0x78>
mov    -0xc(%rbp),%edi
lea    -0x31(%rbp),%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movsbl -0x31(%rbp),%eax
cmp    $0xa,%eax
jne    11bb <socket_read_answer_size+0x9b>
jmp    11d6 <socket_read_answer_size+0xb6>
imul   $0xa,-0x18(%rbp),%rax
movsbl -0x31(%rbp),%ecx
sub    $0x30,%ecx
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x18(%rbp)
jmp    1193 <socket_read_answer_size+0x73>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
movl   $0x1,-0x38(%rbp)
mov    -0x28(%rbp),%rax
mov    %rax,%rsp
mov    -0x8(%rbp),%rax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_read_answer_size>:
push   %rbp
push   %r14
push   %rbx
sub    $0x20,%rsp
mov    %edi,%ebp
movb   $0x0,0xf(%rsp)
lea    0x10(%rsp),%rsi
mov    $0x7,%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
test   %rax,%rax
js     119a <socket_read_answer_size+0x7a>
lea    0xf(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1040 <read@plt>
mov    0xf(%rsp),%al
xor    %ebx,%ebx
cmp    $0xa,%al
je     11a1 <socket_read_answer_size+0x81>
lea    0xf(%rsp),%r14
cs nopw 0x0(%rax,%rax,1)
nop
movsbq %al,%rax
lea    (%rbx,%rbx,4),%rcx
lea    (%rax,%rcx,2),%rbx
add    $0xffffffffffffffd0,%rbx
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1040 <read@plt>
movzbl 0xf(%rsp),%eax
cmp    $0xa,%al
jne    1170 <socket_read_answer_size+0x50>
jmp    11a1 <socket_read_answer_size+0x81>
mov    $0xffffffffffffffff,%rbx
mov    %rbx,%rax
add    $0x20,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i64 @socket_read_answer_size(i32 %0) {
%2 = alloca i64
%3 = alloca i32
%4 = alloca i64
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i64
%8 = alloca i8
%9 = alloca i32
store i32 %0, i32* %3
store i64 0, i64* %4
store i64 7, i64* %5
%10 = load i64, i64* %5
%11 = call i8* @llvm.stacksave()
store i8* %11, i8** %6
%12 = alloca i8, i64 %10
store i64 %10, i64* %7
store i8 0, i8* %8
%13 = load i32, i32* %3
%14 = load i64, i64* %5
%15 = call i64 @recv(i32 %13, i8* %12, i64 %14, i32 0)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %1
store i64 -1, i64* %2
store i32 1, i32* %9
br label %37
18: ; preds = %1
br label %19
19: ; preds = %27, %18
br label %20
20: ; preds = %19
%21 = load i32, i32* %3
%22 = call i32 @read(i32 %21, i8* %8, i32 1)
%23 = load i8, i8* %8
%24 = sext i8 %23 to i32
%25 = icmp eq i32 %24, 10
br i1 %25, label %26, label %27
26: ; preds = %20
br label %35
27: ; preds = %20
%28 = load i64, i64* %4
%29 = mul i64 %28, 10
%30 = load i8, i8* %8
%31 = sext i8 %30 to i32
%32 = sub nsw i32 %31, 48
%33 = sext i32 %32 to i64
%34 = add i64 %29, %33
store i64 %34, i64* %4
br label %19
35: ; preds = %26
%36 = load i64, i64* %4
store i64 %36, i64* %2
store i32 1, i32* %9
br label %37
37: ; preds = %35, %17
%38 = load i8*, i8** %6
%39 = load i64, i64* %2
ret i64 %39
}

LLVM IR (-O2):
define i64 @socket_read_answer_size(i32 %0) {
%2 = alloca i8
%3 = alloca [7 x i8]
%4 = getelementptr inbounds [7 x i8], [7 x i8]* %3, i64 0, i64 0
store i8 0, i8* %2
%5 = call i64 @recv(i32 %0, i8* %4, i64 7, i32 0)
%6 = icmp slt i64 %5, 0
br i1 %6, label %21, label %7
7: ; preds = %1
%8 = call i32 @read(i32 %0, i8* %2, i32 1)
%9 = load i8, i8* %2
%10 = icmp eq i8 %9, 10
br i1 %10, label %21, label %11
11: ; preds = %7, %11
%12 = phi i8 [ %19, %11 ], [ %9, %7 ]
%13 = phi i64 [ %17, %11 ], [ 0, %7 ]
%14 = sext i8 %12 to i64
%15 = mul i64 %13, 10
%16 = add i64 %15, -48
%17 = add i64 %16, %14
%18 = call i32 @read(i32 %0, i8* %2, i32 1)
%19 = load i8, i8* %2
%20 = icmp eq i8 %19, 10
br i1 %20, label %21, label %11
21: ; preds = %11, %7, %1
%22 = phi i64 [ -1, %1 ], [ 0, %7 ], [ %17, %11 ]
ret i64 %22
}