#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 */
struct sockaddr_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ socklen_t;

/* Variables and functions */
scalar_t__ EINTR;
scalar_t__ EPIPE;
scalar_t__ EWOULDBLOCK;
scalar_t__ errno;
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int sock_nbrecvfrom(int s, void *buf, int len, char *hostname, int *port) {
  int ret, comp = len;
  struct sockaddr_in c;
  int clen = sizeof(struct sockaddr_in);
  while (comp) {
  nbrecvfrom_restart:
    ret = recvfrom(s, (char *)buf, comp, 0, (struct sockaddr *)&c,
                   (socklen_t *)&clen);
    if (!ret) {
      errno = EPIPE;
      return -1;
    }
    if (ret == -1 && errno == EWOULDBLOCK)
      return (len - comp);
    if (ret == -1 && errno == EINTR)
      goto nbrecvfrom_restart;
    else if (ret == -1)
      return -1;
    comp -= ret;
    buf += ret;
  }
  strcpy(hostname, inet_ntoa(c.sin_addr));
  *port = ntohs(c.sin_port);
  return (len - comp);
}


x86 assembly (clang, -O0):
<sock_nbrecvfrom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x30(%rbp)
movl   $0x8,-0x3c(%rbp)
cmpl   $0x0,-0x30(%rbp)
je     1253 <sock_nbrecvfrom+0x113>
jmp    1176 <sock_nbrecvfrom+0x36>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
lea    -0x38(%rbp),%r8
xor    %ecx,%ecx
lea    -0x3c(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11bc <sock_nbrecvfrom+0x7c>
mov    0x2e3d(%rip),%rax
mov    (%rax),%rcx
mov    0x2e2b(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <sock_nbrecvfrom+0x147>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11ee <sock_nbrecvfrom+0xae>
mov    0x2e0b(%rip),%rax
mov    (%rax),%rax
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
jne    11ee <sock_nbrecvfrom+0xae>
mov    -0x14(%rbp),%eax
sub    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1287 <sock_nbrecvfrom+0x147>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1217 <sock_nbrecvfrom+0xd7>
mov    0x2dd9(%rip),%rax
mov    (%rax),%rax
mov    0x2dbf(%rip),%rcx
cmp    (%rcx),%rax
jne    1217 <sock_nbrecvfrom+0xd7>
jmp    1176 <sock_nbrecvfrom+0x36>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    122d <sock_nbrecvfrom+0xed>
movl   $0xffffffff,-0x4(%rbp)
jmp    1287 <sock_nbrecvfrom+0x147>
jmp    1232 <sock_nbrecvfrom+0xf2>
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
mov    -0x2c(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    1167 <sock_nbrecvfrom+0x27>
mov    -0x20(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x34(%rbp),%edi
call   1040 <inet_ntoa@plt>
mov    -0x48(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    -0x38(%rbp),%edi
call   1060 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
sub    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_nbrecvfrom>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,0x20(%rsp)
mov    %rcx,0x18(%rsp)
mov    %edx,%r12d
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x8,0xc(%rsp)
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%r14
mov    0x2e57(%rip),%r15
mov    %edx,0x8(%rsp)
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
mov    %r13,%r8
mov    %r14,%r9
call   1050 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    11b7 <sock_nbrecvfrom+0x77>
mov    0x2e37(%rip),%rax
mov    (%rax),%rax
cmp    (%r15),%rax
je     120c <sock_nbrecvfrom+0xcc>
mov    0x2e18(%rip),%rcx
cmp    (%rcx),%rax
je     1180 <sock_nbrecvfrom+0x40>
jmp    1205 <sock_nbrecvfrom+0xc5>
test   %eax,%eax
je     11f1 <sock_nbrecvfrom+0xb1>
movslq %eax,%rcx
add    %rcx,%rbx
sub    %eax,%r12d
jne    1180 <sock_nbrecvfrom+0x40>
mov    0x14(%rsp),%edi
call   1040 <inet_ntoa@plt>
mov    0x18(%rsp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    0x10(%rsp),%edi
call   1060 <ntohs@plt>
mov    0x20(%rsp),%rcx
mov    %eax,(%rcx)
mov    0x8(%rsp),%eax
jmp    1213 <sock_nbrecvfrom+0xd3>
mov    0x2de8(%rip),%rax
mov    (%rax),%rax
mov    0x2dd6(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%eax
jmp    1213 <sock_nbrecvfrom+0xd3>
mov    0x8(%rsp),%eax
sub    %r12d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@EPIPE = global i64 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
define i32 @sock_nbrecvfrom(i32 %0, i8* %1, i32 %2, i8* %3, i32* %4) {
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
%14 = alloca %struct.sockaddr_in
%15 = alloca i32
store i32 %0, i32* %7
store i8* %1, i8** %8
store i32 %2, i32* %9
store i8* %3, i8** %10
store i32* %4, i32** %11
%16 = load i32, i32* %9
store i32 %16, i32* %13
store i32 8, i32* %15
br label %17
17: ; preds = %55, %5
%18 = load i32, i32* %13
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %63
20: ; preds = %17
br label %21
21: ; preds = %49, %20
%22 = load i32, i32* %7
%23 = load i8*, i8** %8
%24 = load i32, i32* %13
%25 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%26 = call i32 @recvfrom(i32 %22, i8* %23, i32 %24, i32 0, %struct.sockaddr* %25, i32* %15)
store i32 %26, i32* %12
%27 = load i32, i32* %12
%28 = icmp ne i32 %27, 0
br i1 %28, label %31, label %29
29: ; preds = %21
%30 = load i64, i64* @EPIPE
store i64 %30, i64* @errno
store i32 -1, i32* %6
br label %76
31: ; preds = %21
%32 = load i32, i32* %12
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %42
34: ; preds = %31
%35 = load i64, i64* @errno
%36 = load i64, i64* @EWOULDBLOCK
%37 = icmp eq i64 %35, %36
br i1 %37, label %38, label %42
38: ; preds = %34
%39 = load i32, i32* %9
%40 = load i32, i32* %13
%41 = sub nsw i32 %39, %40
store i32 %41, i32* %6
br label %76
42: ; preds = %34, %31
%43 = load i32, i32* %12
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = load i64, i64* @errno
%47 = load i64, i64* @EINTR
%48 = icmp eq i64 %46, %47
br i1 %48, label %49, label %50
49: ; preds = %45
br label %21
50: ; preds = %45, %42
%51 = load i32, i32* %12
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %54
53: ; preds = %50
store i32 -1, i32* %6
br label %76
54: ; preds = %50
br label %55
55: ; preds = %54
%56 = load i32, i32* %12
%57 = load i32, i32* %13
%58 = sub nsw i32 %57, %56
store i32 %58, i32* %13
%59 = load i32, i32* %12
%60 = load i8*, i8** %8
%61 = sext i32 %59 to i64
%62 = getelementptr i8, i8* %60, i64 %61
store i8* %62, i8** %8
br label %17
63: ; preds = %17
%64 = load i8*, i8** %10
%65 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
%66 = load i32, i32* %65
%67 = call i32 @inet_ntoa(i32 %66)
%68 = call i32 @strcpy(i8* %64, i32 %67)
%69 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%70 = load i32, i32* %69
%71 = call i32 @ntohs(i32 %70)
%72 = load i32*, i32** %11
store i32 %71, i32* %72
%73 = load i32, i32* %9
%74 = load i32, i32* %13
%75 = sub nsw i32 %73, %74
store i32 %75, i32* %6
br label %76
76: ; preds = %63, %53, %38, %29
%77 = load i32, i32* %6
ret i32 %77
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@EPIPE = global i64 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
define i32 @sock_nbrecvfrom(i32 %0, i8* %1, i32 %2, i8* %3, i32* nocapture writeonly %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = bitcast %struct.sockaddr_in* %6 to i8*
%9 = bitcast i32* %7 to i8*
store i32 8, i32* %7
%10 = icmp ne i32 %2, 0
%11 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
br label %12
12: ; preds = %5, %28
%13 = phi i32 [ %2, %5 ], [ %29, %28 ]
%14 = phi i8* [ %1, %5 ], [ %31, %28 ]
br label %15
15: ; preds = %12, %25
%16 = call i32 @recvfrom(i32 %0, i8* %14, i32 %13, i32 0, %struct.sockaddr* %11, i32* %7)
switch i32 %16, label %28 [
i32 0, label %17
i32 -1, label %19
]
17: ; preds = %15
%18 = load i64, i64* @EPIPE
store i64 %18, i64* @errno
br label %41
19: ; preds = %15
%20 = load i64, i64* @errno
%21 = load i64, i64* @EWOULDBLOCK
%22 = icmp eq i64 %20, %21
br i1 %22, label %23, label %25
23: ; preds = %19
%24 = sub nsw i32 %2, %13
br label %41
25: ; preds = %19
%26 = load i64, i64* @EINTR
%27 = icmp eq i64 %20, %26
br i1 %27, label %15, label %41
28: ; preds = %15
%29 = sub nsw i32 %13, %16
%30 = sext i32 %16 to i64
%31 = getelementptr i8, i8* %14, i64 %30
%32 = icmp eq i32 %29, 0
br i1 %32, label %33, label %12
33: ; preds = %28
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%35 = load i32, i32* %34
%36 = call i32 @inet_ntoa(i32 %35)
%37 = call i32 @strcpy(i8* %3, i32 %36)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%39 = load i32, i32* %38
%40 = call i32 @ntohs(i32 %39)
store i32 %40, i32* %4
br label %41
41: ; preds = %25, %33, %23, %17
%42 = phi i32 [ %24, %23 ], [ -1, %17 ], [ %2, %33 ], [ -1, %25 ]
ret i32 %42
}