#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_storage {
  int dummy;
};
struct msghdr {
  char *iov_base;
  int iov_len;
  int msg_namelen;
  int msg_iovlen;
  int msg_flags;
  struct msghdr *msg_iov;
  struct sockaddr_storage *msg_name;
};
struct iovec {
  char *iov_base;
  int iov_len;
  int msg_namelen;
  int msg_iovlen;
  int msg_flags;
  struct iovec *msg_iov;
  struct sockaddr_storage *msg_name;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ msg_iovec;
typedef int /*<<< orphan*/ mhdr;

/* Variables and functions */
int /*<<< orphan*/ LOG_ERR;
int MSG_TRUNC;
int /*<<< orphan*/ log_err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ log_printf(int /*<<< orphan*/, char *);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
void server_loop(int sockfd) {
  char msg[512];
  int res;
  struct msghdr mhdr;
  struct sockaddr_storage from_addr;
  struct iovec msg_iovec;
  ssize_t recv_size;
  ssize_t send_size;
  while (1) {
    memset(&msg_iovec, 0, sizeof(msg_iovec));
    msg_iovec.iov_base = msg;
    msg_iovec.iov_len = 512;
    memset(&mhdr, 0, sizeof(mhdr));
    mhdr.msg_name = &from_addr;
    mhdr.msg_namelen = sizeof(struct sockaddr_storage);
    mhdr.msg_iov = &msg_iovec;
    mhdr.msg_iovlen = 1;
    recv_size = recvmsg(sockfd, &mhdr, 0);
    if (recv_size == -1) {
      log_err(LOG_ERR, "recvmsg failed");
      continue;
    }
    if (mhdr.msg_flags & MSG_TRUNC) {
      log_printf(LOG_ERR, "packet truncated");
      continue;
    }
    msg_iovec.iov_len = recv_size;
    send_size = sendmsg(sockfd, &mhdr, 0);
    if (send_size == -1) {
      log_err(LOG_ERR, "sendmsg failed");
      continue;
    }
  }
}


x86 assembly (clang, -O0):
<server_loop>:
push   %rbp
mov    %rsp,%rbp
sub    $0x280,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x270(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    -0x210(%rbp),%rax
mov    %rax,-0x270(%rbp)
movl   $0x200,-0x268(%rbp)
lea    -0x240(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    -0x248(%rbp),%rax
mov    %rax,-0x220(%rbp)
movl   $0x4,-0x234(%rbp)
lea    -0x270(%rbp),%rax
mov    %rax,-0x228(%rbp)
movl   $0x1,-0x230(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x240(%rbp),%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
mov    %eax,-0x274(%rbp)
cmpl   $0xffffffff,-0x274(%rbp)
jne    120a <server_loop+0xba>
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1070 <log_err@plt>
jmp    115e <server_loop+0xe>
mov    -0x22c(%rbp),%eax
mov    0x2dd1(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     123c <server_loop+0xec>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xddd(%rip),%rsi
call   1050 <log_printf@plt>
jmp    115e <server_loop+0xe>
mov    -0x274(%rbp),%eax
mov    %eax,-0x268(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x240(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,-0x278(%rbp)
cmpl   $0xffffffff,-0x278(%rbp)
jne    1286 <server_loop+0x136>
mov    0x2d5d(%rip),%rax
mov    (%rax),%edi
lea    0xda4(%rip),%rsi
call   1070 <log_err@plt>
jmp    115e <server_loop+0xe>
jmp    115e <server_loop+0xe>

x86 assembly (clang, -O2):
<server_loop>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x268,%rsp
mov    %edi,%ebx
lea    0x38(%rsp),%r14
lea    0x60(%rsp),%r13
lea    0x10(%rsp),%r15
lea    0x8(%rsp),%r12
movabs $0x100000004,%rbp
jmp    11a5 <server_loop+0x55>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
mov    0x2e39(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
call   1070 <log_err@plt>
mov    %r14,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    %r13,0x38(%rsp)
movl   $0x200,0x40(%rsp)
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
mov    %r12,0x30(%rsp)
mov    %rbp,0x1c(%rsp)
mov    %r14,0x28(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1060 <recvmsg@plt>
cmp    $0xffffffff,%eax
je     1190 <server_loop+0x40>
mov    0x2df1(%rip),%rcx
mov    (%rcx),%ecx
test   %ecx,0x24(%rsp)
je     1220 <server_loop+0xd0>
mov    0x2dca(%rip),%rax
mov    (%rax),%edi
lea    0xe00(%rip),%rsi
call   1050 <log_printf@plt>
jmp    11a5 <server_loop+0x55>
cs nopw 0x0(%rax,%rax,1)
mov    %eax,0x40(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
cmp    $0xffffffff,%eax
jne    11a5 <server_loop+0x55>
mov    0x2d90(%rip),%rax
mov    (%rax),%edi
lea    0xdd7(%rip),%rsi
call   1070 <log_err@plt>
jmp    11a5 <server_loop+0x55>

LLVM IR (-O0):
%struct.msghdr = type { i8*, i32, i32, i32, i32, %struct.msghdr*, %struct.sockaddr_storage* }
%struct.sockaddr_storage = type { i32 }
%struct.iovec = type { i8*, i32, i32, i32, i32, %struct.iovec*, %struct.sockaddr_storage* }
@LOG_ERR = global i32 0
@.str = [15 x i8] c"recvmsg failed\00"
@MSG_TRUNC = global i32 0
@.str.1 = [17 x i8] c"packet truncated\00"
@.str.2 = [15 x i8] c"sendmsg failed\00"
define void @server_loop(i32 %0) {
%2 = alloca i32
%3 = alloca [512 x i8]
%4 = alloca i32
%5 = alloca %struct.msghdr
%6 = alloca %struct.sockaddr_storage
%7 = alloca %struct.iovec
%8 = alloca i32
%9 = alloca i32
store i32 %0, i32* %2
br label %10
10: ; preds = %1, %26, %35, %45, %48
%11 = bitcast %struct.iovec* %7 to %struct.msghdr*
%12 = call i32 @memset(%struct.msghdr* %11, i32 0, i32 40)
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%14 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 0
store i8* %13, i8** %14
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
store i32 512, i32* %15
%16 = call i32 @memset(%struct.msghdr* %5, i32 0, i32 40)
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 6
store %struct.sockaddr_storage* %6, %struct.sockaddr_storage** %17
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 2
store i32 4, i32* %18
%19 = bitcast %struct.iovec* %7 to %struct.msghdr*
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 5
store %struct.msghdr* %19, %struct.msghdr** %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 3
store i32 1, i32* %21
%22 = load i32, i32* %2
%23 = call i32 @recvmsg(i32 %22, %struct.msghdr* %5, i32 0)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %10
%27 = load i32, i32* @LOG_ERR
%28 = call i32 @log_err(i32 %27, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %10
29: ; preds = %10
%30 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %5, i32 0, i32 4
%31 = load i32, i32* %30
%32 = load i32, i32* @MSG_TRUNC
%33 = and i32 %31, %32
%34 = icmp ne i32 %33, 0
br i1 %34, label %35, label %38
35: ; preds = %29
%36 = load i32, i32* @LOG_ERR
%37 = call i32 @log_printf(i32 %36, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %10
38: ; preds = %29
%39 = load i32, i32* %8
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %7, i32 0, i32 1
store i32 %39, i32* %40
%41 = load i32, i32* %2
%42 = call i32 @sendmsg(i32 %41, %struct.msghdr* %5, i32 0)
store i32 %42, i32* %9
%43 = load i32, i32* %9
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %48
45: ; preds = %38
%46 = load i32, i32* @LOG_ERR
%47 = call i32 @log_err(i32 %46, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %10
48: ; preds = %38
br label %10
}

LLVM IR (-O2):
%struct.msghdr = type { i8*, i32, i32, i32, i32, %struct.msghdr*, %struct.sockaddr_storage* }
%struct.sockaddr_storage = type { i32 }
%struct.iovec = type { i8*, i32, i32, i32, i32, %struct.iovec*, %struct.sockaddr_storage* }
@LOG_ERR = global i32 0
@.str = [15 x i8] c"recvmsg failed\00"
@MSG_TRUNC = global i32 0
@.str.1 = [17 x i8] c"packet truncated\00"
@.str.2 = [15 x i8] c"sendmsg failed\00"
define void @server_loop(i32 %0) {
%2 = alloca [512 x i8]
%3 = alloca %struct.msghdr
%4 = alloca %struct.sockaddr_storage
%5 = alloca %struct.iovec
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%7 = bitcast %struct.msghdr* %3 to i8*
%8 = bitcast %struct.sockaddr_storage* %4 to i8*
%9 = bitcast %struct.iovec* %5 to i8*
%10 = bitcast %struct.iovec* %5 to %struct.msghdr*
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
%13 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 6
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 2
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 5
%16 = bitcast %struct.msghdr** %15 to %struct.iovec**
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 3
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %3, i64 0, i32 4
br label %19
19: ; preds = %27, %1
%20 = call i32 @memset(%struct.msghdr* %10, i32 0, i32 40)
store i8* %6, i8** %11
store i32 512, i32* %12
%21 = call i32 @memset(%struct.msghdr* %3, i32 0, i32 40)
store %struct.sockaddr_storage* %4, %struct.sockaddr_storage** %13
store i32 4, i32* %14
store %struct.iovec* %5, %struct.iovec** %16
store i32 1, i32* %17
%22 = call i32 @recvmsg(i32 %0, %struct.msghdr* %3, i32 0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %28
24: ; preds = %19
%25 = load i32, i32* @LOG_ERR
%26 = call i32 @log_err(i32 %25, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %27
27: ; preds = %24, %33, %39, %36
br label %19
28: ; preds = %19
%29 = load i32, i32* %18
%30 = load i32, i32* @MSG_TRUNC
%31 = and i32 %30, %29
%32 = icmp eq i32 %31, 0
br i1 %32, label %36, label %33
33: ; preds = %28
%34 = load i32, i32* @LOG_ERR
%35 = call i32 @log_printf(i32 %34, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %27
36: ; preds = %28
store i32 %22, i32* %12
%37 = call i32 @sendmsg(i32 %0, %struct.msghdr* %3, i32 0)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %27
39: ; preds = %36
%40 = load i32, i32* @LOG_ERR
%41 = call i32 @log_err(i32 %40, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.2, i64 0, i64 0))
br label %27
}