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

/* Variables and functions */
int /*<<< orphan*/ handle_msg(int, int /*<<< orphan*/ *, struct sockaddr_in *,
                              int);
int recvfrom(int, int /*<<< orphan*/ *, int, int /*<<< orphan*/,
             struct sockaddr *, int *);
int listen_loop(int sock) {
  int ret = 0;
  int rc;
  for (;;) {
    uint8_t buf[512] = {0};
    struct sockaddr_in other_addr;
    socklen_t addr_len = sizeof(other_addr);
    rc = recvfrom(sock, buf, sizeof(buf), 0, (struct sockaddr *)&other_addr,
                  &addr_len);
    if (rc == -1) {
      ret = -1;
      goto out;
    }
    handle_msg(sock, buf, &other_addr, addr_len);
  }
out:
  return ret;
}


x86 assembly (clang, -O0):
<listen_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x820,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
lea    -0x810(%rbp),%rdi
xor    %esi,%esi
mov    $0x800,%edx
call   1040 <memset@plt>
movl   $0x4,-0x81c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
lea    -0x818(%rbp),%r8
mov    $0x800,%edx
xor    %ecx,%ecx
lea    -0x81c(%rbp),%r9
call   1050 <recvfrom@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    119f <listen_loop+0x6f>
movl   $0xffffffff,-0x8(%rbp)
jmp    11c0 <listen_loop+0x90>
mov    -0x4(%rbp),%edi
lea    -0x810(%rbp),%rsi
mov    -0x81c(%rbp),%ecx
lea    -0x818(%rbp),%rdx
call   1030 <handle_msg@plt>
jmp    1145 <listen_loop+0x15>
mov    -0x8(%rbp),%eax
add    $0x820,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<listen_loop>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x818,%rsp
mov    %edi,%ebp
lea    0x10(%rsp),%rbx
mov    $0x800,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x4,0x4(%rsp)
lea    0x8(%rsp),%r8
lea    0x4(%rsp),%r9
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
call   1050 <recvfrom@plt>
cmp    $0xffffffff,%eax
je     11d4 <listen_loop+0xa4>
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%r14
lea    0x4(%rsp),%r15
nopw   0x0(%rax,%rax,1)
mov    0x4(%rsp),%ecx
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r14,%rdx
call   1030 <handle_msg@plt>
mov    $0x800,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
movl   $0x4,0x4(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x800,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    %r15,%r9
call   1050 <recvfrom@plt>
cmp    $0xffffffff,%eax
jne    1190 <listen_loop+0x60>
mov    $0xffffffff,%eax
add    $0x818,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @listen_loop(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [512 x i32]
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
br label %8
8: ; preds = %17, %1
%9 = bitcast [512 x i32]* %5 to i8*
store i32 4, i32* %7
%10 = load i32, i32* %2
%11 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 0
%12 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%13 = call i32 @recvfrom(i32 %10, i32* %11, i32 2048, i32 0, %struct.sockaddr* %12, i32* %7)
store i32 %13, i32* %4
%14 = load i32, i32* %4
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %8
store i32 -1, i32* %3
br label %22
17: ; preds = %8
%18 = load i32, i32* %2
%19 = getelementptr inbounds [512 x i32], [512 x i32]* %5, i64 0, i64 0
%20 = load i32, i32* %7
%21 = call i32 @handle_msg(i32 %18, i32* %19, %struct.sockaddr_in* %6, i32 %20)
br label %8
22: ; preds = %16
%23 = load i32, i32* %3
ret i32 %23
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
define i32 @listen_loop(i32 %0) {
%2 = alloca [512 x i32]
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = bitcast [512 x i32]* %2 to i8*
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = getelementptr inbounds [512 x i32], [512 x i32]* %2, i64 0, i64 0
%9 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
store i32 4, i32* %4
%10 = call i32 @recvfrom(i32 %0, i32* %8, i32 2048, i32 0, %struct.sockaddr* %9, i32* %4)
%11 = icmp eq i32 %10, -1
br i1 %11, label %17, label %12
12: ; preds = %1, %12
%13 = load i32, i32* %4
%14 = call i32 @handle_msg(i32 %0, i32* %8, %struct.sockaddr_in* %3, i32 %13)
store i32 4, i32* %4
%15 = call i32 @recvfrom(i32 %0, i32* %8, i32 2048, i32 0, %struct.sockaddr* %9, i32* %4)
%16 = icmp eq i32 %15, -1
br i1 %16, label %17, label %12
17: ; preds = %12, %1
ret i32 -1
}