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

/* Variables and functions */
int /*<<< orphan*/ FIONREAD;
int /*<<< orphan*/ SEGSIZE;
int /*<<< orphan*/ ioctl(int, int /*<<< orphan*/, int *);
int /*<<< orphan*/ recvfrom(int, char *, int, int /*<<< orphan*/,
                            struct sockaddr *, int *);
int synchnet(int f) {
  int i, j = 0;
  char rbuf[SEGSIZE + 4];
  struct sockaddr_in from;
  socklen_t fromlen;
  while (1) {
    (void)ioctl(f, FIONREAD, &i);
    if (i) {
      j++;
      fromlen = sizeof from;
      (void)recvfrom(f, rbuf, sizeof(rbuf), 0, (struct sockaddr *)&from,
                     &fromlen);
    } else {
      return (j);
    }
  }
}


x86 assembly (clang, -O0):
<synchnet>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0xc(%rbp)
mov    0x2ea7(%rip),%rax
mov    (%rax),%eax
add    $0x4,%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
jmp    116a <synchnet+0x4a>
mov    -0x4(%rbp),%edi
mov    0x2e74(%rip),%rax
mov    (%rax),%esi
lea    -0x8(%rbp),%rdx
call   1030 <ioctl@plt>
cmpl   $0x0,-0x8(%rbp)
je     11ba <synchnet+0x9a>
mov    -0x38(%rbp),%rsi
mov    -0x40(%rbp),%rax
mov    -0xc(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0xc(%rbp)
movl   $0x4,-0x2c(%rbp)
mov    -0x4(%rbp),%edi
mov    %eax,%edx
lea    -0x28(%rbp),%r8
xor    %ecx,%ecx
lea    -0x2c(%rbp),%r9
call   1040 <recvfrom@plt>
jmp    11c9 <synchnet+0xa9>
mov    -0xc(%rbp),%eax
mov    -0x18(%rbp),%rcx
mov    %rcx,%rsp
mov    %rbp,%rsp
pop    %rbp
ret
jmp    1165 <synchnet+0x45>

x86 assembly (clang, -O2):
<synchnet>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r12d
mov    %rsp,-0x38(%rbp)
mov    0x2ea1(%rip),%rax
mov    (%rax),%r14d
add    $0x4,%r14d
mov    %rsp,%r15
lea    0xf(%r14),%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r15
mov    %r15,%rsp
mov    0x2e8a(%rip),%rax
mov    (%rax),%esi
lea    -0x2c(%rbp),%rdx
call   1030 <ioctl@plt>
cmpl   $0x0,-0x2c(%rbp)
je     11be <synchnet+0x9e>
xor    %ebx,%ebx
lea    -0x2c(%rbp),%r13
cs nopw 0x0(%rax,%rax,1)
nop
add    $0x1,%ebx
movl   $0x4,-0x30(%rbp)
mov    %r12d,%edi
mov    %r15,%rsi
mov    %r14d,%edx
xor    %ecx,%ecx
lea    -0x40(%rbp),%r8
lea    -0x30(%rbp),%r9
call   1040 <recvfrom@plt>
mov    0x2e3f(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
mov    %r13,%rdx
call   1030 <ioctl@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1180 <synchnet+0x60>
jmp    11c0 <synchnet+0xa0>
xor    %ebx,%ebx
mov    -0x38(%rbp),%rsp
mov    %ebx,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@SEGSIZE = global i32 0
@FIONREAD = global i32 0
define i32 @synchnet(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca %struct.sockaddr_in
%8 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %4
%9 = load i32, i32* @SEGSIZE
%10 = add nsw i32 %9, 4
%11 = zext i32 %10 to i64
%12 = call i8* @llvm.stacksave()
store i8* %12, i8** %5
%13 = alloca i8, i64 %11
store i64 %11, i64* %6
br label %14
14: ; preds = %31, %1
br label %15
15: ; preds = %14
%16 = load i32, i32* %2
%17 = load i32, i32* @FIONREAD
%18 = call i32 @ioctl(i32 %16, i32 %17, i32* %3)
%19 = load i32, i32* %3
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %28
21: ; preds = %15
%22 = load i32, i32* %4
%23 = add nsw i32 %22, 1
store i32 %23, i32* %4
store i32 4, i32* %8
%24 = load i32, i32* %2
%25 = trunc i64 %11 to i32
%26 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%27 = call i32 @recvfrom(i32 %24, i8* %13, i32 %25, i32 0, %struct.sockaddr* %26, i32* %8)
br label %31
28: ; preds = %15
%29 = load i32, i32* %4
%30 = load i8*, i8** %5
ret i32 %29
31: ; preds = %21
br label %14
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@SEGSIZE = global i32 0
@FIONREAD = global i32 0
define i32 @synchnet(i32 %0) {
%2 = alloca i32
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast i32* %2 to i8*
%6 = load i32, i32* @SEGSIZE
%7 = add nsw i32 %6, 4
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = bitcast %struct.sockaddr_in* %3 to i8*
%12 = bitcast i32* %4 to i8*
%13 = load i32, i32* @FIONREAD
%14 = call i32 @ioctl(i32 %0, i32 %13, i32* %2)
%15 = load i32, i32* %2
%16 = icmp eq i32 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %1
%18 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
br label %19
19: ; preds = %17, %19
%20 = phi i32 [ 0, %17 ], [ %21, %19 ]
%21 = add nuw nsw i32 %20, 1
store i32 4, i32* %4
%22 = call i32 @recvfrom(i32 %0, i8* %10, i32 %7, i32 0, %struct.sockaddr* %18, i32* %4)
%23 = load i32, i32* @FIONREAD
%24 = call i32 @ioctl(i32 %0, i32 %23, i32* %2)
%25 = load i32, i32* %2
%26 = icmp eq i32 %25, 0
br i1 %26, label %27, label %19
27: ; preds = %19, %1
%28 = phi i32 [ 0, %1 ], [ %21, %19 ]
ret i32 %28
}