#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 read(int, char *, int);
int /*<<< orphan*/ *strstr(char *, char *);
int read_socket(int fd, char *buffer, int size) {
  int bytes_recvd = 0;
  int retries = 0;
  int total_recvd = 0;
  while (retries < 1 && size > 0 && strstr(buffer, ">") == NULL) {
    bytes_recvd = read(fd, buffer, size);
    if (bytes_recvd > 0) {
      buffer += bytes_recvd;
      size -= bytes_recvd;
      total_recvd += bytes_recvd;
    } else {
      retries++;
    }
  }
  if (bytes_recvd >= 0) {
    return total_recvd;
  }
  return -1;
}


x86 assembly (clang, -O0):
<read_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
movl   $0x0,-0x20(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x1c(%rbp)
mov    %al,-0x21(%rbp)
jge    117f <read_socket+0x5f>
xor    %eax,%eax
cmpl   $0x0,-0x14(%rbp)
mov    %al,-0x21(%rbp)
jle    117f <read_socket+0x5f>
mov    -0x10(%rbp),%rdi
lea    0xe90(%rip),%rsi
call   1040 <strstr@plt>
cmp    $0x0,%rax
sete   %al
mov    %al,-0x21(%rbp)
mov    -0x21(%rbp),%al
test   $0x1,%al
jne    118f <read_socket+0x6f>
jmp    11e3 <read_socket+0xc3>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jle    11d5 <read_socket+0xb5>
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x14(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x14(%rbp)
mov    -0x18(%rbp),%eax
add    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
jmp    11de <read_socket+0xbe>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    1147 <read_socket+0x27>
cmpl   $0x0,-0x18(%rbp)
jl     11f8 <read_socket+0xd8>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ff <read_socket+0xdf>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
jle    1171 <read_socket+0x51>
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %edi,%r15d
xor    %r14d,%r14d
cs nopw 0x0(%rax,%rax,1)
mov    %rbx,%rdi
mov    $0x3e,%esi
call   1030 <strchr@plt>
test   %rax,%rax
jne    117e <read_socket+0x5e>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    %ebp,%edx
call   1040 <read@plt>
test   %eax,%eax
jle    1176 <read_socket+0x56>
mov    %eax,%ecx
add    %rcx,%rbx
add    %eax,%r14d
sub    %eax,%ebp
jg     1140 <read_socket+0x20>
jmp    117e <read_socket+0x5e>
xor    %r14d,%r14d
jmp    117e <read_socket+0x5e>
jns    117e <read_socket+0x5e>
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c">\00"
define i32 @read_socket(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
br label %11
11: ; preds = %44, %3
%12 = load i32, i32* %9
%13 = icmp slt i32 %12, 1
br i1 %13, label %14, label %21
14: ; preds = %11
%15 = load i32, i32* %7
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %14
%18 = load i8*, i8** %6
%19 = call i32* @strstr(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%20 = icmp eq i32* %19, null
br label %21
21: ; preds = %17, %14, %11
%22 = phi i1 [ false, %14 ], [ false, %11 ], [ %20, %17 ]
br i1 %22, label %23, label %45
23: ; preds = %21
%24 = load i32, i32* %5
%25 = load i8*, i8** %6
%26 = load i32, i32* %7
%27 = call i32 @read(i32 %24, i8* %25, i32 %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = icmp sgt i32 %28, 0
br i1 %29, label %30, label %41
30: ; preds = %23
%31 = load i32, i32* %8
%32 = load i8*, i8** %6
%33 = sext i32 %31 to i64
%34 = getelementptr inbounds i8, i8* %32, i64 %33
store i8* %34, i8** %6
%35 = load i32, i32* %8
%36 = load i32, i32* %7
%37 = sub nsw i32 %36, %35
store i32 %37, i32* %7
%38 = load i32, i32* %8
%39 = load i32, i32* %10
%40 = add nsw i32 %39, %38
store i32 %40, i32* %10
br label %44
41: ; preds = %23
%42 = load i32, i32* %9
%43 = add nsw i32 %42, 1
store i32 %43, i32* %9
br label %44
44: ; preds = %41, %30
br label %11
45: ; preds = %21
%46 = load i32, i32* %8
%47 = icmp sge i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = load i32, i32* %10
store i32 %49, i32* %4
br label %51
50: ; preds = %45
store i32 -1, i32* %4
br label %51
51: ; preds = %50, %48
%52 = load i32, i32* %4
ret i32 %52
}

LLVM IR (-O2):
define i32 @read_socket(i32 %0, i8* %1, i32 %2) {
%4 = icmp sgt i32 %2, 0
br i1 %4, label %5, label %22
5: ; preds = %3, %14
%6 = phi i32 [ %18, %14 ], [ 0, %3 ]
%7 = phi i32 [ %17, %14 ], [ %2, %3 ]
%8 = phi i8* [ %16, %14 ], [ %1, %3 ]
%9 = tail call i8* @strchr(i8* dereferenceable(1) %8, i32 62)
%10 = icmp eq i8* %9, null
br i1 %10, label %11, label %22
11: ; preds = %5
%12 = tail call i32 @read(i32 %0, i8* %8, i32 %7)
%13 = icmp sgt i32 %12, 0
br i1 %13, label %14, label %20
14: ; preds = %11
%15 = zext i32 %12 to i64
%16 = getelementptr inbounds i8, i8* %8, i64 %15
%17 = sub nsw i32 %7, %12
%18 = add nsw i32 %12, %6
%19 = icmp sgt i32 %17, 0
br i1 %19, label %5, label %22
20: ; preds = %11
%21 = icmp sgt i32 %12, -1
br i1 %21, label %22, label %24
22: ; preds = %14, %5, %3, %20
%23 = phi i32 [ %6, %20 ], [ 0, %3 ], [ %6, %5 ], [ %18, %14 ]
br label %24
24: ; preds = %20, %22
%25 = phi i32 [ %23, %22 ], [ -1, %20 ]
ret i32 %25
}