#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*/ uint32_t;
typedef int /*<<< orphan*/ u32n;
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 /*<<< orphan*/ LOG_WARNING;
int MSG_TRUNC;
int /*<<< orphan*/ log_err(int /*<<< orphan*/, char *);
int /*<<< orphan*/ log_printf(int /*<<< orphan*/, char *);
scalar_t__ memcmp(char *, char *, int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, char *, int);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int /*<<< orphan*/ ntohl(int /*<<< orphan*/);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int strlen(char *);
int client_recv_msg(int sockfd, uint32_t *seq) {
  char msg[512];
  struct msghdr mhdr;
  struct sockaddr_storage from_addr;
  int res;
  struct iovec msg_iovec;
  ssize_t recv_size;
  uint32_t u32n;
  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");
    return (-1);
  }
  if (mhdr.msg_flags & MSG_TRUNC) {
    log_printf(LOG_WARNING, "packet truncated");
  }
  if ((msg_iovec.iov_len < strlen("UDPTest") + sizeof(u32n)) ||
      (memcmp(msg, "UDPTest", strlen("UDPTest")) != 0)) {
    log_printf(LOG_ERR, "received packet is not ours");
    return (-1);
  }
  memcpy(&u32n, msg + strlen("UDPTest"), sizeof(u32n));
  *seq = ntohl(u32n);
  return (0);
}


x86 assembly (clang, -O0):
<client_recv_msg>:
push   %rbp
mov    %rsp,%rbp
sub    $0x290,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%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    -0x238(%rbp),%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    -0x240(%rbp),%rax
mov    %rax,-0x218(%rbp)
movl   $0x4,-0x22c(%rbp)
lea    -0x270(%rbp),%rax
mov    %rax,-0x220(%rbp)
movl   $0x1,-0x228(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x238(%rbp),%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
mov    %eax,-0x274(%rbp)
cmpl   $0xffffffff,-0x274(%rbp)
jne    1245 <client_recv_msg+0xc5>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdcc(%rip),%rsi
call   10a0 <log_err@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1350 <client_recv_msg+0x1d0>
mov    -0x224(%rbp),%eax
mov    0x2d96(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     1272 <client_recv_msg+0xf2>
mov    0x2d7c(%rip),%rax
mov    (%rax),%edi
lea    0xda2(%rip),%rsi
call   1060 <log_printf@plt>
movslq -0x268(%rbp),%rax
mov    %rax,-0x280(%rbp)
lea    0xd99(%rip),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x280(%rbp),%rax
movslq %ecx,%rcx
add    $0x4,%rcx
cmp    %rcx,%rax
jb     12de <client_recv_msg+0x15e>
lea    -0x210(%rbp),%rax
mov    %rax,-0x288(%rbp)
lea    0xd66(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x288(%rbp),%rdi
mov    %eax,%edx
lea    0xd51(%rip),%rsi
call   1050 <memcmp@plt>
cmp    $0x0,%rax
je     12ff <client_recv_msg+0x17f>
mov    0x2ce3(%rip),%rax
mov    (%rax),%edi
lea    0xd3a(%rip),%rsi
call   1060 <log_printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1350 <client_recv_msg+0x1d0>
lea    -0x210(%rbp),%rax
mov    %rax,-0x290(%rbp)
lea    0xd0c(%rip),%rdi
call   1030 <strlen@plt>
mov    -0x290(%rbp),%rsi
cltq
add    %rax,%rsi
lea    -0x278(%rbp),%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    -0x278(%rbp),%edi
call   1090 <ntohl@plt>
mov    %eax,%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x290,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<client_recv_msg>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x268,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
lea    0x38(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    0x60(%rsp),%rax
mov    %rax,0x38(%rsp)
movl   $0x200,0x40(%rsp)
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x28,%edx
call   1040 <memset@plt>
lea    0x8(%rsp),%rax
mov    %rax,0x30(%rsp)
movabs $0x100000004,%rax
mov    %rax,0x1c(%rsp)
mov    %r15,0x28(%rsp)
mov    %ebp,%edi
mov    %rbx,%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
cmp    $0xffffffff,%eax
je     127a <client_recv_msg+0xfa>
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
test   %eax,0x24(%rsp)
je     121f <client_recv_msg+0x9f>
mov    0x2dcf(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1060 <log_printf@plt>
movslq 0x40(%rsp),%rbx
lea    0xdf5(%rip),%rdi
call   1030 <strlen@plt>
cltq
add    $0x4,%rax
cmp    %rbx,%rax
ja     125e <client_recv_msg+0xde>
lea    0xdde(%rip),%rbx
mov    %rbx,%rdi
call   1030 <strlen@plt>
lea    0x60(%rsp),%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1050 <memcmp@plt>
test   %rax,%rax
je     1296 <client_recv_msg+0x116>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
lea    0xdba(%rip),%rsi
call   1060 <log_printf@plt>
mov    $0xffffffff,%eax
jmp    12c9 <client_recv_msg+0x149>
mov    0x2d47(%rip),%rax
mov    (%rax),%edi
lea    0xd76(%rip),%rsi
call   10a0 <log_err@plt>
mov    $0xffffffff,%eax
jmp    12c9 <client_recv_msg+0x149>
lea    0xd83(%rip),%rdi
call   1030 <strlen@plt>
cltq
lea    (%rsp,%rax,1),%rsi
add    $0x60,%rsi
lea    0x4(%rsp),%rdi
mov    $0x4,%edx
call   1070 <memcpy@plt>
mov    0x4(%rsp),%edi
call   1090 <ntohl@plt>
mov    %eax,(%r14)
xor    %eax,%eax
add    $0x268,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

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
@LOG_WARNING = global i32 0
@.str.1 = [17 x i8] c"packet truncated\00"
@.str.2 = [8 x i8] c"UDPTest\00"
@.str.3 = [28 x i8] c"received packet is not ours\00"
define i32 @client_recv_msg(i32 %0, i32* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [512 x i8]
%7 = alloca %struct.msghdr
%8 = alloca %struct.sockaddr_storage
%9 = alloca i32
%10 = alloca %struct.iovec
%11 = alloca i32
%12 = alloca i32
store i32 %0, i32* %4
store i32* %1, i32** %5
%13 = bitcast %struct.iovec* %10 to %struct.msghdr*
%14 = call i32 @memset(%struct.msghdr* %13, i32 0, i32 40)
%15 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 0
store i8* %15, i8** %16
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 1
store i32 512, i32* %17
%18 = call i32 @memset(%struct.msghdr* %7, i32 0, i32 40)
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 6
store %struct.sockaddr_storage* %8, %struct.sockaddr_storage** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 2
store i32 4, i32* %20
%21 = bitcast %struct.iovec* %10 to %struct.msghdr*
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 5
store %struct.msghdr* %21, %struct.msghdr** %22
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 3
store i32 1, i32* %23
%24 = load i32, i32* %4
%25 = call i32 @recvmsg(i32 %24, %struct.msghdr* %7, i32 0)
store i32 %25, i32* %11
%26 = load i32, i32* %11
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %2
%29 = load i32, i32* @LOG_ERR
%30 = call i32 @log_err(i32 %29, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
31: ; preds = %2
%32 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i32 0, i32 4
%33 = load i32, i32* %32
%34 = load i32, i32* @MSG_TRUNC
%35 = and i32 %33, %34
%36 = icmp ne i32 %35, 0
br i1 %36, label %37, label %40
37: ; preds = %31
%38 = load i32, i32* @LOG_WARNING
%39 = call i32 @log_printf(i32 %38, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %40
40: ; preds = %37, %31
%41 = getelementptr inbounds %struct.iovec, %struct.iovec* %10, i32 0, i32 1
%42 = load i32, i32* %41
%43 = sext i32 %42 to i64
%44 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%45 = sext i32 %44 to i64
%46 = add i64 %45, 4
%47 = icmp ult i64 %43, %46
br i1 %47, label %53, label %48
48: ; preds = %40
%49 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%50 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%51 = call i64 @memcmp(i8* %49, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %50)
%52 = icmp ne i64 %51, 0
br i1 %52, label %53, label %56
53: ; preds = %48, %40
%54 = load i32, i32* @LOG_ERR
%55 = call i32 @log_printf(i32 %54, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %65
56: ; preds = %48
%57 = getelementptr inbounds [512 x i8], [512 x i8]* %6, i64 0, i64 0
%58 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%59 = sext i32 %58 to i64
%60 = getelementptr inbounds i8, i8* %57, i64 %59
%61 = call i32 @memcpy(i32* %12, i8* %60, i32 4)
%62 = load i32, i32* %12
%63 = call i32 @ntohl(i32 %62)
%64 = load i32*, i32** %5
store i32 %63, i32* %64
store i32 0, i32* %3
br label %65
65: ; preds = %56, %53, %28
%66 = load i32, i32* %3
ret i32 %66
}

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
@LOG_WARNING = global i32 0
@.str.1 = [17 x i8] c"packet truncated\00"
@.str.2 = [8 x i8] c"UDPTest\00"
@.str.3 = [28 x i8] c"received packet is not ours\00"
define i32 @client_recv_msg(i32 %0, i32* nocapture writeonly %1) {
%3 = alloca [512 x i8]
%4 = alloca %struct.msghdr
%5 = alloca %struct.sockaddr_storage
%6 = alloca %struct.iovec
%7 = alloca i32
%8 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%9 = bitcast %struct.msghdr* %4 to i8*
%10 = bitcast %struct.sockaddr_storage* %5 to i8*
%11 = bitcast %struct.iovec* %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = bitcast %struct.iovec* %6 to %struct.msghdr*
%14 = call i32 @memset(%struct.msghdr* %13, i32 0, i32 40)
%15 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 0
store i8* %8, i8** %15
%16 = getelementptr inbounds %struct.iovec, %struct.iovec* %6, i64 0, i32 1
store i32 512, i32* %16
%17 = call i32 @memset(%struct.msghdr* %4, i32 0, i32 40)
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 6
store %struct.sockaddr_storage* %5, %struct.sockaddr_storage** %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
store i32 4, i32* %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 5
%21 = bitcast %struct.msghdr** %20 to %struct.iovec**
store %struct.iovec* %6, %struct.iovec** %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
store i32 1, i32* %22
%23 = call i32 @recvmsg(i32 %0, %struct.msghdr* %4, i32 0)
%24 = icmp eq i32 %23, -1
br i1 %24, label %25, label %28
25: ; preds = %2
%26 = load i32, i32* @LOG_ERR
%27 = call i32 @log_err(i32 %26, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str, i64 0, i64 0))
br label %58
28: ; preds = %2
%29 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 4
%30 = load i32, i32* %29
%31 = load i32, i32* @MSG_TRUNC
%32 = and i32 %31, %30
%33 = icmp eq i32 %32, 0
br i1 %33, label %37, label %34
34: ; preds = %28
%35 = load i32, i32* @LOG_WARNING
%36 = call i32 @log_printf(i32 %35, i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.1, i64 0, i64 0))
br label %37
37: ; preds = %34, %28
%38 = load i32, i32* %16
%39 = sext i32 %38 to i64
%40 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%41 = sext i32 %40 to i64
%42 = add nsw i64 %41, 4
%43 = icmp ugt i64 %42, %39
br i1 %43, label %48, label %44
44: ; preds = %37
%45 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%46 = call i64 @memcmp(i8* %8, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %45)
%47 = icmp eq i64 %46, 0
br i1 %47, label %51, label %48
48: ; preds = %44, %37
%49 = load i32, i32* @LOG_ERR
%50 = call i32 @log_printf(i32 %49, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0))
br label %58
51: ; preds = %44
%52 = call i32 @strlen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 %53
%55 = call i32 @memcpy(i32* %7, i8* %54, i32 4)
%56 = load i32, i32* %7
%57 = call i32 @ntohl(i32 %56)
store i32 %57, i32* %1
br label %58
58: ; preds = %51, %48, %25
%59 = phi i32 [ -1, %25 ], [ -1, %48 ], [ 0, %51 ]
ret i32 %59
}