#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;
int /*<<< orphan*/ MSG_PEEK;
scalar_t__ errno;
int /*<<< orphan*/ inet_ntoa(int /*<<< orphan*/);
int ntohs(int /*<<< orphan*/);
int recvfrom(int, void *, int, int /*<<< orphan*/, struct sockaddr *,
             int /*<<< orphan*/ *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
int sock_nbpeek(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) {
  nbpeeka_restart:
    ret = recvfrom(s, buf, comp, MSG_PEEK, (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 nbpeeka_restart;
    else if (ret == -1)
      return -1;
    comp -= ret;
  }
  if (hostname != NULL && port != NULL) {
    strcpy(hostname, inet_ntoa(c.sin_addr));
    *port = ntohs(c.sin_port);
  }
  return (len - comp);
}


x86 assembly (clang, -O0):
<sock_nbpeek>:
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     1249 <sock_nbpeek+0x109>
jmp    1176 <sock_nbpeek+0x36>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x30(%rbp),%edx
mov    0x2e61(%rip),%rax
mov    (%rax),%ecx
lea    -0x38(%rbp),%r8
lea    -0x3c(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
jne    11c3 <sock_nbpeek+0x83>
mov    0x2e2e(%rip),%rax
mov    (%rax),%rcx
mov    0x2e1c(%rip),%rax
mov    %rcx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <sock_nbpeek+0x153>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11f5 <sock_nbpeek+0xb5>
mov    0x2dfc(%rip),%rax
mov    (%rax),%rax
mov    0x2dea(%rip),%rcx
cmp    (%rcx),%rax
jne    11f5 <sock_nbpeek+0xb5>
mov    -0x14(%rbp),%eax
sub    -0x30(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1293 <sock_nbpeek+0x153>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    121e <sock_nbpeek+0xde>
mov    0x2dca(%rip),%rax
mov    (%rax),%rax
mov    0x2db0(%rip),%rcx
cmp    (%rcx),%rax
jne    121e <sock_nbpeek+0xde>
jmp    1176 <sock_nbpeek+0x36>
cmpl   $0xffffffff,-0x2c(%rbp)
jne    1234 <sock_nbpeek+0xf4>
movl   $0xffffffff,-0x4(%rbp)
jmp    1293 <sock_nbpeek+0x153>
jmp    1239 <sock_nbpeek+0xf9>
mov    -0x2c(%rbp),%ecx
mov    -0x30(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x30(%rbp)
jmp    1167 <sock_nbpeek+0x27>
cmpq   $0x0,-0x20(%rbp)
je     128a <sock_nbpeek+0x14a>
cmpq   $0x0,-0x28(%rbp)
je     128a <sock_nbpeek+0x14a>
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_nbpeek>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %r8,0x18(%rsp)
mov    %rcx,0x20(%rsp)
mov    %edx,%eax
movl   $0x8,0xc(%rsp)
test   %edx,%edx
je     11db <sock_nbpeek+0x9b>
mov    %rsi,%rbx
mov    %edi,%ebp
lea    0x10(%rsp),%r13
lea    0xc(%rsp),%r14
mov    0x2e4c(%rip),%r15
mov    %eax,%r12d
mov    %eax,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x2e51(%rip),%rax
mov    (%rax),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
mov    %r13,%r8
mov    %r14,%r9
call   1050 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    11ce <sock_nbpeek+0x8e>
mov    0x2e18(%rip),%rax
mov    (%rax),%rax
cmp    (%r15),%rax
je     1216 <sock_nbpeek+0xd6>
mov    0x2df9(%rip),%rcx
cmp    (%rcx),%rax
je     1190 <sock_nbpeek+0x50>
jmp    1233 <sock_nbpeek+0xf3>
test   %eax,%eax
je     121f <sock_nbpeek+0xdf>
sub    %eax,%r12d
mov    0x8(%rsp),%eax
jne    1190 <sock_nbpeek+0x50>
mov    0x20(%rsp),%rbx
test   %rbx,%rbx
je     1238 <sock_nbpeek+0xf8>
mov    0x18(%rsp),%rbp
test   %rbp,%rbp
je     1238 <sock_nbpeek+0xf8>
mov    0x14(%rsp),%edi
mov    %eax,%r14d
call   1040 <inet_ntoa@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    0x10(%rsp),%edi
call   1060 <ntohs@plt>
mov    %eax,0x0(%rbp)
mov    %r14d,%eax
jmp    1238 <sock_nbpeek+0xf8>
mov    0x8(%rsp),%eax
sub    %r12d,%eax
jmp    1238 <sock_nbpeek+0xf8>
mov    0x2db2(%rip),%rax
mov    (%rax),%rax
mov    0x2da0(%rip),%rcx
mov    %rax,(%rcx)
mov    $0xffffffff,%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 }
@MSG_PEEK = global i32 0
@EPIPE = global i64 0
@errno = global i64 0
@EWOULDBLOCK = global i64 0
@EINTR = global i64 0
define i32 @sock_nbpeek(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 = %56, %5
%18 = load i32, i32* %13
%19 = icmp ne i32 %18, 0
br i1 %19, label %20, label %60
20: ; preds = %17
br label %21
21: ; preds = %50, %20
%22 = load i32, i32* %7
%23 = load i8*, i8** %8
%24 = load i32, i32* %13
%25 = load i32, i32* @MSG_PEEK
%26 = bitcast %struct.sockaddr_in* %14 to %struct.sockaddr*
%27 = call i32 @recvfrom(i32 %22, i8* %23, i32 %24, i32 %25, %struct.sockaddr* %26, i32* %15)
store i32 %27, i32* %12
%28 = load i32, i32* %12
%29 = icmp ne i32 %28, 0
br i1 %29, label %32, label %30
30: ; preds = %21
%31 = load i64, i64* @EPIPE
store i64 %31, i64* @errno
store i32 -1, i32* %6
br label %80
32: ; preds = %21
%33 = load i32, i32* %12
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %43
35: ; preds = %32
%36 = load i64, i64* @errno
%37 = load i64, i64* @EWOULDBLOCK
%38 = icmp eq i64 %36, %37
br i1 %38, label %39, label %43
39: ; preds = %35
%40 = load i32, i32* %9
%41 = load i32, i32* %13
%42 = sub nsw i32 %40, %41
store i32 %42, i32* %6
br label %80
43: ; preds = %35, %32
%44 = load i32, i32* %12
%45 = icmp eq i32 %44, -1
br i1 %45, label %46, label %51
46: ; preds = %43
%47 = load i64, i64* @errno
%48 = load i64, i64* @EINTR
%49 = icmp eq i64 %47, %48
br i1 %49, label %50, label %51
50: ; preds = %46
br label %21
51: ; preds = %46, %43
%52 = load i32, i32* %12
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %55
54: ; preds = %51
store i32 -1, i32* %6
br label %80
55: ; preds = %51
br label %56
56: ; preds = %55
%57 = load i32, i32* %12
%58 = load i32, i32* %13
%59 = sub nsw i32 %58, %57
store i32 %59, i32* %13
br label %17
60: ; preds = %17
%61 = load i8*, i8** %10
%62 = icmp ne i8* %61, null
br i1 %62, label %63, label %76
63: ; preds = %60
%64 = load i32*, i32** %11
%65 = icmp ne i32* %64, null
br i1 %65, label %66, label %76
66: ; preds = %63
%67 = load i8*, i8** %10
%68 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 1
%69 = load i32, i32* %68
%70 = call i32 @inet_ntoa(i32 %69)
%71 = call i32 @strcpy(i8* %67, i32 %70)
%72 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %14, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i32 @ntohs(i32 %73)
%75 = load i32*, i32** %11
store i32 %74, i32* %75
br label %76
76: ; preds = %66, %63, %60
%77 = load i32, i32* %9
%78 = load i32, i32* %13
%79 = sub nsw i32 %77, %78
store i32 %79, i32* %6
br label %80
80: ; preds = %76, %54, %39, %30
%81 = load i32, i32* %6
ret i32 %81
}

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