#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 */
scalar_t__ EINTR;
int /*<<< orphan*/ MSG_WAITALL;
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int);
int recv(int, void *, size_t, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int sock_recv(int sock_fd, size_t size, void *buf) {
  int rc;
retry_after_signal:
  rc = recv(sock_fd, buf, size, MSG_WAITALL);
  if (rc != size) {
    fprintf(stderr, "recv failed: %s, rc=%d\n", strerror(errno), rc);
    if ((errno == EINTR) && (rc != 0))
      goto retry_after_signal;
    if (rc)
      return rc;
    else
      return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<sock_recv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x10(%rbp),%rdx
mov    0x2e6b(%rip),%rax
mov    (%rax),%ecx
call   1030 <recv@plt>
mov    %eax,-0x1c(%rbp)
movslq -0x1c(%rbp),%rax
cmp    -0x10(%rbp),%rax
je     11e7 <sock_recv+0xb7>
mov    0x2e6c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2e58(%rip),%rax
mov    (%rax),%rdi
call   1050 <strerror@plt>
mov    -0x20(%rbp),%edi
mov    %rax,%rdx
mov    -0x1c(%rbp),%ecx
lea    0xe68(%rip),%rsi
call   1040 <fprintf@plt>
mov    0x2e34(%rip),%rax
mov    (%rax),%rax
mov    0x2e22(%rip),%rcx
cmp    (%rcx),%rax
jne    11c6 <sock_recv+0x96>
cmpl   $0x0,-0x1c(%rbp)
je     11c6 <sock_recv+0x96>
jmp    1143 <sock_recv+0x13>
cmpl   $0x0,-0x1c(%rbp)
je     11db <sock_recv+0xab>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    11ee <sock_recv+0xbe>
movl   $0xffffffff,-0x4(%rbp)
jmp    11ee <sock_recv+0xbe>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_recv>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %rsi,%r13
mov    %edi,%r12d
mov    0x2e8d(%rip),%r15
nopl   0x0(%rax,%rax,1)
mov    0x2e69(%rip),%rax
mov    (%rax),%ecx
mov    %r12d,%edi
mov    %r14,%rsi
mov    %r13,%rdx
call   1030 <recv@plt>
mov    %eax,%ebp
cltq
cmp    %r13,%rax
je     11b0 <sock_recv+0x80>
mov    0x2e69(%rip),%rax
mov    (%rax),%ebx
mov    (%r15),%rdi
call   1050 <strerror@plt>
mov    %ebx,%edi
lea    0xe76(%rip),%rsi
mov    %rax,%rdx
mov    %ebp,%ecx
call   1040 <fprintf@plt>
mov    (%r15),%rax
mov    0x2e32(%rip),%rcx
cmp    (%rcx),%rax
jne    11a7 <sock_recv+0x77>
test   %ebp,%ebp
jne    1150 <sock_recv+0x20>
cmp    $0x1,%ebp
sbb    %eax,%eax
or     %ebp,%eax
jmp    11b2 <sock_recv+0x82>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@MSG_WAITALL = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"recv failed: %s, rc=%d\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_recv(i32 %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
store i32 %0, i32* %5
store i64 %1, i64* %6
store i8* %2, i8** %7
br label %9
9: ; preds = %31, %3
%10 = load i32, i32* %5
%11 = load i8*, i8** %7
%12 = load i64, i64* %6
%13 = load i32, i32* @MSG_WAITALL
%14 = call i32 @recv(i32 %10, i8* %11, i64 %12, i32 %13)
store i32 %14, i32* %8
%15 = load i32, i32* %8
%16 = sext i32 %15 to i64
%17 = load i64, i64* %6
%18 = icmp ne i64 %16, %17
br i1 %18, label %19, label %38
19: ; preds = %9
%20 = load i32, i32* @stderr
%21 = load i64, i64* @errno
%22 = call i8* @strerror(i64 %21)
%23 = load i32, i32* %8
%24 = call i32 @fprintf(i32 %20, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %22, i32 %23)
%25 = load i64, i64* @errno
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %19
%29 = load i32, i32* %8
%30 = icmp ne i32 %29, 0
br i1 %30, label %31, label %32
31: ; preds = %28
br label %9
32: ; preds = %28, %19
%33 = load i32, i32* %8
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = load i32, i32* %8
store i32 %36, i32* %4
br label %39
37: ; preds = %32
store i32 -1, i32* %4
br label %39
38: ; preds = %9
store i32 0, i32* %4
br label %39
39: ; preds = %38, %37, %35
%40 = load i32, i32* %4
ret i32 %40
}

LLVM IR (-O2):
@MSG_WAITALL = global i32 0
@stderr = global i32 0
@.str = [24 x i8] c"recv failed: %s, rc=%d\0A\00"
@errno = global i64 0
@EINTR = global i64 0
define i32 @sock_recv(i32 %0, i64 %1, i8* %2) {
br label %4
4: ; preds = %9, %3
%5 = load i32, i32* @MSG_WAITALL
%6 = tail call i32 @recv(i32 %0, i8* %2, i64 %1, i32 %5)
%7 = sext i32 %6 to i64
%8 = icmp eq i64 %7, %1
br i1 %8, label %21, label %9
9: ; preds = %4
%10 = load i32, i32* @stderr
%11 = load i64, i64* @errno
%12 = tail call i8* @strerror(i64 %11)
%13 = tail call i32 @fprintf(i32 %10, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %12, i32 %6)
%14 = load i64, i64* @errno
%15 = load i64, i64* @EINTR
%16 = icmp eq i64 %14, %15
%17 = icmp ne i32 %6, 0
%18 = select i1 %16, i1 %17, i1 false
br i1 %18, label %4, label %19
19: ; preds = %9
%20 = select i1 %17, i32 %6, i32 -1
br label %21
21: ; preds = %4, %19
%22 = phi i32 [ %20, %19 ], [ 0, %4 ]
ret i32 %22
}