#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 */
int EINTR;
int EPIPE;
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
int O_NONBLOCK;
int errno;
int fcntl(int, int /*<<< orphan*/, int);
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_brecvfrom(int s, void *buf, int len, char *hostname, int *port) {
  int flag = 0;
  int oldfl, ret, comp = len;
  int olderrno;
  struct sockaddr_in c;
  int clen = sizeof(struct sockaddr_in);
  oldfl = fcntl(s, F_GETFL, 0);
  if (oldfl & O_NONBLOCK) {
    fcntl(s, F_SETFL, oldfl & (~O_NONBLOCK));
    flag = 1;
  }
  while (comp) {
  brecvfrom_restart:
    if ((ret = recvfrom(s, (char *)buf, comp, 0, (struct sockaddr *)&c,
                        (socklen_t *)&clen)) < 0) {
      if (errno == EINTR)
        goto brecvfrom_restart;
      olderrno = errno;
      if (flag)
        fcntl(s, F_SETFL, oldfl | O_NONBLOCK);
      errno = olderrno;
      return -1;
    }
    if (!ret) {
      if (flag)
        fcntl(s, F_SETFL, oldfl | O_NONBLOCK);
      errno = EPIPE;
      return -1;
    }
    comp -= ret;
    buf += ret;
  }
  if (flag)
    fcntl(s, F_SETFL, oldfl | O_NONBLOCK);
  strcpy(hostname, inet_ntoa(c.sin_addr));
  *port = ntohs(c.sin_port);
  return (len - comp);
}


x86 assembly (clang, -O0):
<sock_brecvfrom>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
movl   $0x0,-0x2c(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
movl   $0x8,-0x4c(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e38(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,-0x30(%rbp)
mov    -0x30(%rbp),%eax
mov    0x2e4a(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     11d2 <sock_brecvfrom+0x82>
mov    -0x8(%rbp),%edi
mov    0x2e1d(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%edx
mov    0x2e29(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    %eax,%edx
call   1040 <fcntl@plt>
movl   $0x1,-0x2c(%rbp)
jmp    11d7 <sock_brecvfrom+0x87>
cmpl   $0x0,-0x38(%rbp)
je     12e3 <sock_brecvfrom+0x193>
jmp    11e6 <sock_brecvfrom+0x96>
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x38(%rbp),%edx
lea    -0x48(%rbp),%r8
xor    %ecx,%ecx
lea    -0x4c(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x34(%rbp)
cmp    $0x0,%eax
jge    1273 <sock_brecvfrom+0x123>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    0x2d9d(%rip),%rcx
cmp    (%rcx),%eax
jne    1228 <sock_brecvfrom+0xd8>
jmp    11e6 <sock_brecvfrom+0x96>
mov    0x2d99(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     125b <sock_brecvfrom+0x10b>
mov    -0x8(%rbp),%edi
mov    0x2d88(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%edx
mov    0x2d94(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x3c(%rbp),%ecx
mov    0x2d63(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <sock_brecvfrom+0x1ee>
cmpl   $0x0,-0x34(%rbp)
jne    12c2 <sock_brecvfrom+0x172>
cmpl   $0x0,-0x2c(%rbp)
je     12a4 <sock_brecvfrom+0x154>
mov    -0x8(%rbp),%edi
mov    0x2d3f(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%edx
mov    0x2d4b(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    0x2d2d(%rip),%rax
mov    (%rax),%ecx
mov    0x2d14(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    133e <sock_brecvfrom+0x1ee>
mov    -0x34(%rbp),%ecx
mov    -0x38(%rbp),%eax
sub    %ecx,%eax
mov    %eax,-0x38(%rbp)
mov    -0x34(%rbp),%ecx
mov    -0x10(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x10(%rbp)
jmp    11d7 <sock_brecvfrom+0x87>
cmpl   $0x0,-0x2c(%rbp)
je     130a <sock_brecvfrom+0x1ba>
mov    -0x8(%rbp),%edi
mov    0x2cd9(%rip),%rax
mov    (%rax),%esi
mov    -0x30(%rbp),%edx
mov    0x2ce5(%rip),%rax
or     (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x20(%rbp),%rax
mov    %rax,-0x58(%rbp)
mov    -0x44(%rbp),%edi
call   1050 <inet_ntoa@plt>
mov    -0x58(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    -0x48(%rbp),%edi
call   1070 <ntohs@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x14(%rbp),%eax
sub    -0x38(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_brecvfrom>:
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,%r14d
mov    %rsi,%rbx
mov    %edi,%ebp
movl   $0x8,0xc(%rsp)
mov    0x2e41(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    %eax,%r15d
mov    0x2e56(%rip),%rax
mov    (%rax),%r12d
test   %r15d,%r12d
je     11b2 <sock_brecvfrom+0x62>
mov    0x2e2f(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edx
not    %edx
and    %r15d,%edx
mov    %ebp,%edi
call   1040 <fcntl@plt>
mov    %r12d,(%rsp)
mov    %r15d,0x4(%rsp)
test   %r14d,%r14d
je     1222 <sock_brecvfrom+0xd2>
lea    0xc(%rsp),%r15
mov    0x2dec(%rip),%r13
mov    %r14d,%r12d
mov    %r14d,0x8(%rsp)
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12d,%edx
xor    %ecx,%ecx
lea    0x10(%rsp),%r8
mov    %r15,%r9
call   1060 <recvfrom@plt>
test   %eax,%eax
jns    120d <sock_brecvfrom+0xbd>
mov    0x2dc6(%rip),%rax
mov    (%rax),%r14d
cmp    0x0(%r13),%r14d
je     11e0 <sock_brecvfrom+0x90>
jmp    126d <sock_brecvfrom+0x11d>
mov    0x8(%rsp),%r14d
je     12b3 <sock_brecvfrom+0x163>
mov    %eax,%ecx
add    %rcx,%rbx
sub    %eax,%r12d
jne    11e0 <sock_brecvfrom+0x90>
mov    0x4(%rsp),%ecx
test   %ecx,(%rsp)
je     1246 <sock_brecvfrom+0xf6>
mov    0x2d9e(%rip),%rax
mov    (%rax),%esi
mov    0x2dad(%rip),%rax
mov    (%rax),%edx
or     %ecx,%edx
mov    %ebp,%edi
call   1040 <fcntl@plt>
mov    0x14(%rsp),%edi
call   1050 <inet_ntoa@plt>
mov    0x18(%rsp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
mov    0x10(%rsp),%edi
call   1070 <ntohs@plt>
mov    0x20(%rsp),%rcx
mov    %eax,(%rcx)
jmp    12a1 <sock_brecvfrom+0x151>
mov    0x4(%rsp),%ecx
test   %ecx,(%rsp)
je     1291 <sock_brecvfrom+0x141>
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    0x2d62(%rip),%rax
mov    (%rax),%edx
or     %ecx,%edx
mov    %ebp,%edi
call   1040 <fcntl@plt>
mov    0x2d30(%rip),%rax
mov    %r14d,(%rax)
mov    $0xffffffff,%r14d
mov    %r14d,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x4(%rsp),%ecx
test   %ecx,(%rsp)
je     12d7 <sock_brecvfrom+0x187>
mov    0x2d0d(%rip),%rax
mov    (%rax),%esi
mov    0x2d1c(%rip),%rax
mov    (%rax),%edx
or     %ecx,%edx
mov    %ebp,%edi
call   1040 <fcntl@plt>
mov    0x2cfa(%rip),%rax
mov    (%rax),%eax
mov    0x2ce1(%rip),%rcx
mov    %eax,(%rcx)
jmp    129b <sock_brecvfrom+0x14b>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EPIPE = global i32 0
define i32 @sock_brecvfrom(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 i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca %struct.sockaddr_in
%18 = 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
store i32 0, i32* %12
%19 = load i32, i32* %9
store i32 %19, i32* %15
store i32 8, i32* %18
%20 = load i32, i32* %7
%21 = load i32, i32* @F_GETFL
%22 = call i32 @fcntl(i32 %20, i32 %21, i32 0)
store i32 %22, i32* %13
%23 = load i32, i32* %13
%24 = load i32, i32* @O_NONBLOCK
%25 = and i32 %23, %24
%26 = icmp ne i32 %25, 0
br i1 %26, label %27, label %35
27: ; preds = %5
%28 = load i32, i32* %7
%29 = load i32, i32* @F_SETFL
%30 = load i32, i32* %13
%31 = load i32, i32* @O_NONBLOCK
%32 = xor i32 %31, -1
%33 = and i32 %30, %32
%34 = call i32 @fcntl(i32 %28, i32 %29, i32 %33)
store i32 1, i32* %12
br label %35
35: ; preds = %27, %5
br label %36
36: ; preds = %80, %35
%37 = load i32, i32* %15
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %88
39: ; preds = %36
br label %40
40: ; preds = %51, %39
%41 = load i32, i32* %7
%42 = load i8*, i8** %8
%43 = load i32, i32* %15
%44 = bitcast %struct.sockaddr_in* %17 to %struct.sockaddr*
%45 = call i32 @recvfrom(i32 %41, i8* %42, i32 %43, i32 0, %struct.sockaddr* %44, i32* %18)
store i32 %45, i32* %14
%46 = icmp slt i32 %45, 0
br i1 %46, label %47, label %65
47: ; preds = %40
%48 = load i32, i32* @errno
%49 = load i32, i32* @EINTR
%50 = icmp eq i32 %48, %49
br i1 %50, label %51, label %52
51: ; preds = %47
br label %40
52: ; preds = %47
%53 = load i32, i32* @errno
store i32 %53, i32* %16
%54 = load i32, i32* %12
%55 = icmp ne i32 %54, 0
br i1 %55, label %56, label %63
56: ; preds = %52
%57 = load i32, i32* %7
%58 = load i32, i32* @F_SETFL
%59 = load i32, i32* %13
%60 = load i32, i32* @O_NONBLOCK
%61 = or i32 %59, %60
%62 = call i32 @fcntl(i32 %57, i32 %58, i32 %61)
br label %63
63: ; preds = %56, %52
%64 = load i32, i32* %16
store i32 %64, i32* @errno
store i32 -1, i32* %6
br label %111
65: ; preds = %40
%66 = load i32, i32* %14
%67 = icmp ne i32 %66, 0
br i1 %67, label %80, label %68
68: ; preds = %65
%69 = load i32, i32* %12
%70 = icmp ne i32 %69, 0
br i1 %70, label %71, label %78
71: ; preds = %68
%72 = load i32, i32* %7
%73 = load i32, i32* @F_SETFL
%74 = load i32, i32* %13
%75 = load i32, i32* @O_NONBLOCK
%76 = or i32 %74, %75
%77 = call i32 @fcntl(i32 %72, i32 %73, i32 %76)
br label %78
78: ; preds = %71, %68
%79 = load i32, i32* @EPIPE
store i32 %79, i32* @errno
store i32 -1, i32* %6
br label %111
80: ; preds = %65
%81 = load i32, i32* %14
%82 = load i32, i32* %15
%83 = sub nsw i32 %82, %81
store i32 %83, i32* %15
%84 = load i32, i32* %14
%85 = load i8*, i8** %8
%86 = sext i32 %84 to i64
%87 = getelementptr i8, i8* %85, i64 %86
store i8* %87, i8** %8
br label %36
88: ; preds = %36
%89 = load i32, i32* %12
%90 = icmp ne i32 %89, 0
br i1 %90, label %91, label %98
91: ; preds = %88
%92 = load i32, i32* %7
%93 = load i32, i32* @F_SETFL
%94 = load i32, i32* %13
%95 = load i32, i32* @O_NONBLOCK
%96 = or i32 %94, %95
%97 = call i32 @fcntl(i32 %92, i32 %93, i32 %96)
br label %98
98: ; preds = %91, %88
%99 = load i8*, i8** %10
%100 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 1
%101 = load i32, i32* %100
%102 = call i32 @inet_ntoa(i32 %101)
%103 = call i32 @strcpy(i8* %99, i32 %102)
%104 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %17, i32 0, i32 0
%105 = load i32, i32* %104
%106 = call i32 @ntohs(i32 %105)
%107 = load i32*, i32** %11
store i32 %106, i32* %107
%108 = load i32, i32* %9
%109 = load i32, i32* %15
%110 = sub nsw i32 %108, %109
store i32 %110, i32* %6
br label %111
111: ; preds = %98, %78, %63
%112 = load i32, i32* %6
ret i32 %112
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32 }
%struct.sockaddr = type { i32 }
@F_GETFL = global i32 0
@O_NONBLOCK = global i32 0
@F_SETFL = global i32 0
@errno = global i32 0
@EINTR = global i32 0
@EPIPE = global i32 0
define i32 @sock_brecvfrom(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 = load i32, i32* @F_GETFL
%11 = tail call i32 @fcntl(i32 %0, i32 %10, i32 0)
%12 = load i32, i32* @O_NONBLOCK
%13 = and i32 %12, %11
%14 = icmp eq i32 %13, 0
br i1 %14, label %20, label %15
15: ; preds = %5
%16 = load i32, i32* @F_SETFL
%17 = xor i32 %12, -1
%18 = and i32 %11, %17
%19 = tail call i32 @fcntl(i32 %0, i32 %16, i32 %18)
br label %20
20: ; preds = %15, %5
%21 = icmp eq i32 %2, 0
br i1 %21, label %56, label %22
22: ; preds = %20
%23 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
br label %24
24: ; preds = %22, %51
%25 = phi i32 [ %2, %22 ], [ %52, %51 ]
%26 = phi i8* [ %1, %22 ], [ %54, %51 ]
br label %27
27: ; preds = %24, %30
%28 = call i32 @recvfrom(i32 %0, i8* %26, i32 %25, i32 0, %struct.sockaddr* %23, i32* %7)
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %41
30: ; preds = %27
%31 = load i32, i32* @errno
%32 = load i32, i32* @EINTR
%33 = icmp eq i32 %31, %32
br i1 %33, label %27, label %34
34: ; preds = %30
br i1 %14, label %40, label %35
35: ; preds = %34
%36 = load i32, i32* @F_SETFL
%37 = load i32, i32* @O_NONBLOCK
%38 = or i32 %37, %11
%39 = call i32 @fcntl(i32 %0, i32 %36, i32 %38)
br label %40
40: ; preds = %35, %34
store i32 %31, i32* @errno
br label %70
41: ; preds = %27
%42 = icmp eq i32 %28, 0
br i1 %42, label %43, label %51
43: ; preds = %41
br i1 %14, label %49, label %44
44: ; preds = %43
%45 = load i32, i32* @F_SETFL
%46 = load i32, i32* @O_NONBLOCK
%47 = or i32 %46, %11
%48 = call i32 @fcntl(i32 %0, i32 %45, i32 %47)
br label %49
49: ; preds = %44, %43
%50 = load i32, i32* @EPIPE
store i32 %50, i32* @errno
br label %70
51: ; preds = %41
%52 = sub nsw i32 %25, %28
%53 = zext i32 %28 to i64
%54 = getelementptr i8, i8* %26, i64 %53
%55 = icmp eq i32 %52, 0
br i1 %55, label %56, label %24
56: ; preds = %51, %20
br i1 %14, label %62, label %57
57: ; preds = %56
%58 = load i32, i32* @F_SETFL
%59 = load i32, i32* @O_NONBLOCK
%60 = or i32 %59, %11
%61 = call i32 @fcntl(i32 %0, i32 %58, i32 %60)
br label %62
62: ; preds = %57, %56
%63 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
%64 = load i32, i32* %63
%65 = call i32 @inet_ntoa(i32 %64)
%66 = call i32 @strcpy(i8* %3, i32 %65)
%67 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0
%68 = load i32, i32* %67
%69 = call i32 @ntohs(i32 %68)
store i32 %69, i32* %4
br label %70
70: ; preds = %62, %49, %40
%71 = phi i32 [ -1, %40 ], [ -1, %49 ], [ %2, %62 ]
ret i32 %71
}