#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*/ hot_sys_Panic(char *);
int /*<<< orphan*/ perror(char *);
int recv(int, char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ trace(char *, int, ...);
int tcp_recv(int s, void *buf_void, int len) {
  int result, ofs;
  char *buf = buf_void;
  trace("tcprecv: %d 0x%x %d", s, buf, len);
  for (ofs = 0; ofs < len; ofs += result) {
    result = recv(s, buf + ofs, len - ofs, 0);
    trace("tcprecv: got %d bytes", result);
    if (result == 0) {
      hot_sys_Panic("lost connection to outboard");
    }
    if (result == -1) {
      perror("recv");
      hot_sys_Panic("lost connection to outboard");
    }
  }
  return len;
}


x86 assembly (clang, -O0):
<tcp_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x4(%rbp),%esi
mov    -0x28(%rbp),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1050 <trace@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x14(%rbp),%eax
jge    11ff <tcp_recv+0xbf>
mov    -0x4(%rbp),%edi
mov    -0x28(%rbp),%rsi
movslq -0x1c(%rbp),%rax
add    %rax,%rsi
mov    -0x14(%rbp),%edx
sub    -0x1c(%rbp),%edx
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%esi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1050 <trace@plt>
cmpl   $0x0,-0x18(%rbp)
jne    11ca <tcp_recv+0x8a>
lea    0xe65(%rip),%rdi
call   1040 <hot_sys_Panic@plt>
cmpl   $0xffffffff,-0x18(%rbp)
jne    11ec <tcp_recv+0xac>
lea    0xe6b(%rip),%rdi
call   1060 <perror@plt>
lea    0xe43(%rip),%rdi
call   1040 <hot_sys_Panic@plt>
jmp    11f1 <tcp_recv+0xb1>
mov    -0x18(%rbp),%eax
add    -0x1c(%rbp),%eax
mov    %eax,-0x1c(%rbp)
jmp    1179 <tcp_recv+0x39>
mov    -0x14(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<tcp_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %edx,%ebx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xea7(%rip),%rdi
mov    %ebp,%esi
mov    %r14,%rdx
mov    %ebx,%ecx
xor    %eax,%eax
call   1050 <trace@plt>
test   %ebx,%ebx
jle    11d4 <tcp_recv+0x94>
lea    0xea2(%rip),%r15
xor    %r12d,%r12d
jmp    11a4 <tcp_recv+0x64>
lea    0xec8(%rip),%rdi
call   1060 <perror@plt>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
lea    0xe93(%rip),%rdi
call   1040 <hot_sys_Panic@plt>
add    %r13d,%r12d
cmp    %ebx,%r12d
jge    11d4 <tcp_recv+0x94>
movslq %r12d,%rax
lea    (%r14,%rax,1),%rsi
mov    %ebx,%edx
sub    %eax,%edx
mov    %ebp,%edi
xor    %ecx,%ecx
call   1030 <recv@plt>
mov    %eax,%r13d
mov    %r15,%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1050 <trace@plt>
test   %r13d,%r13d
je     1190 <tcp_recv+0x50>
cmp    $0xffffffff,%r13d
jne    119c <tcp_recv+0x5c>
jmp    1177 <tcp_recv+0x37>
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [20 x i8] c"tcprecv: %d 0x%x %d\00"
@.str.1 = [22 x i8] c"tcprecv: got %d bytes\00"
@.str.2 = [28 x i8] c"lost connection to outboard\00"
@.str.3 = [5 x i8] c"recv\00"
define i32 @tcp_recv(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%10 = load i8*, i8** %5
store i8* %10, i8** %9
%11 = load i32, i32* %4
%12 = load i8*, i8** %9
%13 = load i32, i32* %6
%14 = call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %11, i8* %12, i32 %13)
store i32 0, i32* %8
br label %15
15: ; preds = %42, %3
%16 = load i32, i32* %8
%17 = load i32, i32* %6
%18 = icmp slt i32 %16, %17
br i1 %18, label %19, label %46
19: ; preds = %15
%20 = load i32, i32* %4
%21 = load i8*, i8** %9
%22 = load i32, i32* %8
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8, i8* %21, i64 %23
%25 = load i32, i32* %6
%26 = load i32, i32* %8
%27 = sub nsw i32 %25, %26
%28 = call i32 @recv(i32 %20, i8* %24, i32 %27, i32 0)
store i32 %28, i32* %7
%29 = load i32, i32* %7
%30 = call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %29)
%31 = load i32, i32* %7
%32 = icmp eq i32 %31, 0
br i1 %32, label %33, label %35
33: ; preds = %19
%34 = call i32 @hot_sys_Panic(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %35
35: ; preds = %33, %19
%36 = load i32, i32* %7
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %41
38: ; preds = %35
%39 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
%40 = call i32 @hot_sys_Panic(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %41
41: ; preds = %38, %35
br label %42
42: ; preds = %41
%43 = load i32, i32* %7
%44 = load i32, i32* %8
%45 = add nsw i32 %44, %43
store i32 %45, i32* %8
br label %15
46: ; preds = %15
%47 = load i32, i32* %6
ret i32 %47
}

LLVM IR (-O2):
@.str = [20 x i8] c"tcprecv: %d 0x%x %d\00"
@.str.1 = [22 x i8] c"tcprecv: got %d bytes\00"
@.str.2 = [28 x i8] c"lost connection to outboard\00"
@.str.3 = [5 x i8] c"recv\00"
define i32 @tcp_recv(i32 %0, i8* %1, i32 returned %2) {
%4 = tail call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str, i64 0, i64 0), i32 %0, i8* %1, i32 %2)
%5 = icmp sgt i32 %2, 0
br i1 %5, label %6, label %20
6: ; preds = %3, %17
%7 = phi i32 [ %18, %17 ], [ 0, %3 ]
%8 = sext i32 %7 to i64
%9 = getelementptr inbounds i8, i8* %1, i64 %8
%10 = sub nsw i32 %2, %7
%11 = tail call i32 @recv(i32 %0, i8* %9, i32 %10, i32 0)
%12 = tail call i32 (i8*, i32, ...) @trace(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i32 %11)
switch i32 %11, label %17 [
i32 0, label %15
i32 -1, label %13
]
13: ; preds = %6
%14 = tail call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.3, i64 0, i64 0))
br label %15
15: ; preds = %6, %13
%16 = tail call i32 @hot_sys_Panic(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0))
br label %17
17: ; preds = %15, %6
%18 = add nsw i32 %11, %7
%19 = icmp slt i32 %18, %2
br i1 %19, label %6, label %20
20: ; preds = %17, %3
ret i32 %2
}