#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 */
typedef int ssize_t;

/* Variables and functions */
int read(int, char *, int);
int seed_tcp_read(int sockfd, char *buffer, int length) {
  ssize_t n, t = 0;
  while (t != length) {
    n = read(sockfd, buffer, length - t);
    if (n == 0)
      return t;
    if (n == -1)
      return -1;
    t += n;
    buffer += n;
  }
  return t;
}


x86 assembly (clang, -O0):
<seed_tcp_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
je     1194 <seed_tcp_read+0x84>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
call   1030 <read@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
jne    115f <seed_tcp_read+0x4f>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    119a <seed_tcp_read+0x8a>
cmpl   $0xffffffff,-0x18(%rbp)
jne    1175 <seed_tcp_read+0x65>
movl   $0xffffffff,-0x4(%rbp)
jmp    119a <seed_tcp_read+0x8a>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
mov    -0x18(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1129 <seed_tcp_read+0x19>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<seed_tcp_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
test   %edx,%edx
je     115a <seed_tcp_read+0x4a>
mov    %edx,%r14d
mov    %rsi,%rbp
mov    %edi,%r15d
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
mov    %r14d,%edx
sub    %ebx,%edx
mov    %r15d,%edi
mov    %rbp,%rsi
call   1030 <read@plt>
cmp    $0xffffffff,%eax
je     115e <seed_tcp_read+0x4e>
test   %eax,%eax
je     1163 <seed_tcp_read+0x53>
add    %eax,%ebx
cltq
add    %rax,%rbp
cmp    %r14d,%ebx
jne    1130 <seed_tcp_read+0x20>
mov    %r14d,%ebx
jmp    1163 <seed_tcp_read+0x53>
xor    %ebx,%ebx
jmp    1163 <seed_tcp_read+0x53>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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

LLVM IR (-O2):
define i32 @seed_tcp_read(i32 %0, i8* nocapture %1, i32 %2) {
%4 = icmp eq i32 %2, 0
br i1 %4, label %17, label %5
5: ; preds = %3, %10
%6 = phi i32 [ %11, %10 ], [ 0, %3 ]
%7 = phi i8* [ %13, %10 ], [ %1, %3 ]
%8 = sub nsw i32 %2, %6
%9 = tail call i32 @read(i32 %0, i8* %7, i32 %8)
switch i32 %9, label %10 [
i32 0, label %15
i32 -1, label %17
]
10: ; preds = %5
%11 = add nsw i32 %9, %6
%12 = sext i32 %9 to i64
%13 = getelementptr inbounds i8, i8* %7, i64 %12
%14 = icmp eq i32 %11, %2
br i1 %14, label %15, label %5
15: ; preds = %10, %5
%16 = phi i32 [ %2, %10 ], [ %6, %5 ]
br label %17
17: ; preds = %5, %15, %3
%18 = phi i32 [ 0, %3 ], [ %16, %15 ], [ -1, %5 ]
ret i32 %18
}