#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 timeval {
  int dummy;
};
struct msghdr {
  int msg_iovlen;
  unsigned char *msg_control;
  size_t msg_namelen;
  int msg_controllen;
  int /*<<< orphan*/ msg_name;
  struct iovec *msg_iov;
  int /*<<< orphan*/ member_0;
};
struct iovec {
  unsigned char *iov_base;
  int iov_len;
};
typedef int /*<<< orphan*/ recvbuf;
typedef int /*<<< orphan*/ ctrl;

/* Variables and functions */
int CMSG_SPACE(int);
scalar_t__ EINTR;
int /*<<< orphan*/ calloc(int, size_t);
scalar_t__ errno;
int /*<<< orphan*/ fatal(char *);
int /*<<< orphan*/ proc(unsigned char *, int, struct msghdr *);
int recvmsg(int, struct msghdr *, int /*<<< orphan*/);
int /*<<< orphan*/ sys_error(char *);
void pong(int sock, size_t addrlen) {
  unsigned char ctrl[CMSG_SPACE(sizeof(struct timeval))];
  unsigned char recvbuf[60 + 8 + 56];
  struct msghdr msg = {0};
  struct iovec iov;
  int readb;
  iov.iov_base = recvbuf;
  iov.iov_len = sizeof(recvbuf);
  msg.msg_iov = &iov;
  msg.msg_iovlen = 1;
  msg.msg_control = ctrl;
  msg.msg_name = calloc(1, addrlen);
  if (!msg.msg_name)
    fatal("Out of memory");
  for (;;) {
    msg.msg_namelen = addrlen;
    msg.msg_controllen = sizeof(ctrl);
    readb = recvmsg(sock, &msg, 0);
    if (readb > 0) {
      proc(recvbuf, readb, &msg);
      break;
    } else if (errno != EINTR)
      sys_error("recvmsg");
  }
}


x86 assembly (clang, -O0):
<pong>:
push   %rbp
mov    %rsp,%rbp
sub    $0x100,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    $0x4,%edi
call   1060 <CMSG_SPACE@plt>
mov    %eax,%eax
mov    %rax,-0xf8(%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,-0xf0(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
lea    -0xd0(%rbp),%rdi
xor    %esi,%esi
mov    $0x30,%edx
call   1030 <memset@plt>
mov    -0xf0(%rbp),%rax
lea    -0xa0(%rbp),%rcx
mov    %rcx,-0xe0(%rbp)
movl   $0x7c,-0xd8(%rbp)
lea    -0xe0(%rbp),%rcx
mov    %rcx,-0xb0(%rbp)
movl   $0x1,-0xd0(%rbp)
mov    %rax,-0xc8(%rbp)
mov    -0x10(%rbp),%rsi
mov    $0x1,%edi
call   1050 <calloc@plt>
mov    %eax,-0xb4(%rbp)
cmpl   $0x0,-0xb4(%rbp)
jne    1239 <pong+0xc9>
lea    0xdcc(%rip),%rdi
call   1090 <fatal@plt>
jmp    123e <pong+0xce>
mov    -0xf8(%rbp),%rax
mov    -0x10(%rbp),%rcx
mov    %rcx,-0xc0(%rbp)
mov    %eax,-0xb8(%rbp)
mov    -0x4(%rbp),%edi
lea    -0xd0(%rbp),%rsi
xor    %edx,%edx
call   1080 <recvmsg@plt>
mov    %eax,-0xe4(%rbp)
cmpl   $0x0,-0xe4(%rbp)
jle    1298 <pong+0x128>
lea    -0xa0(%rbp),%rdi
mov    -0xe4(%rbp),%esi
lea    -0xd0(%rbp),%rdx
call   1040 <proc@plt>
jmp    12c8 <pong+0x158>
mov    0x2d41(%rip),%rax
mov    (%rax),%rax
mov    0x2d2f(%rip),%rcx
cmp    (%rcx),%rax
je     12be <pong+0x14e>
lea    0xd55(%rip),%rdi
call   1070 <sys_error@plt>
jmp    12c3 <pong+0x153>
jmp    123e <pong+0xce>
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pong>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0xc8,%rsp
mov    %rsi,%r15
mov    %edi,%r14d
mov    $0x4,%edi
call   1050 <CMSG_SPACE@plt>
mov    %eax,%ebx
mov    %eax,%eax
mov    %rsp,%rcx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rcx
mov    %rcx,%rsp
xorps  %xmm0,%xmm0
movaps %xmm0,-0x40(%rbp)
movaps %xmm0,-0x50(%rbp)
movaps %xmm0,-0x60(%rbp)
lea    -0xf0(%rbp),%rax
mov    %rax,-0x70(%rbp)
movl   $0x7c,-0x68(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x40(%rbp)
movl   $0x1,-0x60(%rbp)
mov    %rcx,-0x58(%rbp)
mov    $0x1,%edi
mov    %r15,%rsi
call   1040 <calloc@plt>
mov    %eax,-0x44(%rbp)
test   %eax,%eax
jne    11ed <pong+0x8d>
lea    0xe18(%rip),%rdi
call   1080 <fatal@plt>
mov    %r15,-0x50(%rbp)
mov    %ebx,-0x48(%rbp)
lea    -0x60(%rbp),%rsi
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <recvmsg@plt>
test   %eax,%eax
jle    1227 <pong+0xc7>
lea    -0xf0(%rbp),%rdi
lea    -0x60(%rbp),%rdx
mov    %eax,%esi
call   1030 <proc@plt>
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2daa(%rip),%r13
lea    -0x60(%rbp),%r12
jmp    1258 <pong+0xf8>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,-0x50(%rbp)
mov    %ebx,-0x48(%rbp)
mov    %r14d,%edi
mov    %r12,%rsi
xor    %edx,%edx
call   1070 <recvmsg@plt>
test   %eax,%eax
jg     1206 <pong+0xa6>
mov    0x2d81(%rip),%rax
mov    (%rax),%rax
cmp    0x0(%r13),%rax
je     1240 <pong+0xe0>
lea    0xd9f(%rip),%rdi
call   1060 <sys_error@plt>
jmp    1240 <pong+0xe0>

LLVM IR (-O0):
%struct.msghdr = type { i32, i8*, i64, i32, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
@.str = [14 x i8] c"Out of memory\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"recvmsg\00"
define void @pong(i32 %0, i64 %1) {
%3 = alloca i32
%4 = alloca i64
%5 = alloca i8*
%6 = alloca i64
%7 = alloca [124 x i8]
%8 = alloca %struct.msghdr
%9 = alloca %struct.iovec
%10 = alloca i32
store i32 %0, i32* %3
store i64 %1, i64* %4
%11 = call i32 @CMSG_SPACE(i32 4)
%12 = zext i32 %11 to i64
%13 = call i8* @llvm.stacksave()
store i8* %13, i8** %5
%14 = alloca i8, i64 %12
store i64 %12, i64* %6
%15 = bitcast %struct.msghdr* %8 to i8*
%16 = getelementptr inbounds [124 x i8], [124 x i8]* %7, i64 0, i64 0
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 0
store i8* %16, i8** %17
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %9, i32 0, i32 1
store i32 124, i32* %18
%19 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 5
store %struct.iovec* %9, %struct.iovec** %19
%20 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 0
store i32 1, i32* %20
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 1
store i8* %14, i8** %21
%22 = load i64, i64* %4
%23 = call i32 @calloc(i32 1, i64 %22)
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 4
store i32 %23, i32* %24
%25 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 4
%26 = load i32, i32* %25
%27 = icmp ne i32 %26, 0
br i1 %27, label %30, label %28
28: ; preds = %2
%29 = call i32 @fatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %30
30: ; preds = %28, %2
br label %31
31: ; preds = %51, %30
%32 = load i64, i64* %4
%33 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 2
store i64 %32, i64* %33
%34 = trunc i64 %12 to i32
%35 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %8, i32 0, i32 3
store i32 %34, i32* %35
%36 = load i32, i32* %3
%37 = call i32 @recvmsg(i32 %36, %struct.msghdr* %8, i32 0)
store i32 %37, i32* %10
%38 = load i32, i32* %10
%39 = icmp sgt i32 %38, 0
br i1 %39, label %40, label %44
40: ; preds = %31
%41 = getelementptr inbounds [124 x i8], [124 x i8]* %7, i64 0, i64 0
%42 = load i32, i32* %10
%43 = call i32 @proc(i8* %41, i32 %42, %struct.msghdr* %8)
br label %52
44: ; preds = %31
%45 = load i64, i64* @errno
%46 = load i64, i64* @EINTR
%47 = icmp ne i64 %45, %46
br i1 %47, label %48, label %50
48: ; preds = %44
%49 = call i32 @sys_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %50
50: ; preds = %48, %44
br label %51
51: ; preds = %50
br label %31
52: ; preds = %40
%53 = load i8*, i8** %5
ret void
}

LLVM IR (-O2):
%struct.msghdr = type { i32, i8*, i64, i32, i32, %struct.iovec*, i32 }
%struct.iovec = type { i8*, i32 }
@.str = [14 x i8] c"Out of memory\00"
@errno = global i64 0
@EINTR = global i64 0
@.str.1 = [8 x i8] c"recvmsg\00"
define void @pong(i32 %0, i64 %1) {
%3 = alloca [124 x i8]
%4 = alloca %struct.msghdr
%5 = alloca %struct.iovec
%6 = tail call i32 @CMSG_SPACE(i32 4)
%7 = zext i32 %6 to i64
%8 = alloca i8, i64 %7
%9 = getelementptr inbounds [124 x i8], [124 x i8]* %3, i64 0, i64 0
%10 = bitcast %struct.msghdr* %4 to i8*
%11 = bitcast %struct.iovec* %5 to i8*
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 0
store i8* %9, i8** %12
%13 = getelementptr inbounds %struct.iovec, %struct.iovec* %5, i64 0, i32 1
store i32 124, i32* %13
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 5
store %struct.iovec* %5, %struct.iovec** %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 0
store i32 1, i32* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 1
store i8* %8, i8** %16
%17 = call i32 @calloc(i32 1, i64 %1)
%18 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 4
store i32 %17, i32* %18
%19 = icmp eq i32 %17, 0
br i1 %19, label %20, label %22
20: ; preds = %2
%21 = call i32 @fatal(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %22
22: ; preds = %20, %2
%23 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 2
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %4, i64 0, i32 3
store i64 %1, i64* %23
store i32 %6, i32* %24
%25 = call i32 @recvmsg(i32 %0, %struct.msghdr* %4, i32 0)
%26 = icmp sgt i32 %25, 0
br i1 %26, label %27, label %30
27: ; preds = %36, %22
%28 = phi i32 [ %25, %22 ], [ %37, %36 ]
%29 = call i32 @proc(i8* %9, i32 %28, %struct.msghdr* %4)
ret void
30: ; preds = %22, %36
%31 = load i64, i64* @errno
%32 = load i64, i64* @EINTR
%33 = icmp eq i64 %31, %32
br i1 %33, label %36, label %34
34: ; preds = %30
%35 = call i32 @sys_error(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
br label %36
36: ; preds = %30, %34
store i64 %1, i64* %23
store i32 %6, i32* %24
%37 = call i32 @recvmsg(i32 %0, %struct.msghdr* %4, i32 0)
%38 = icmp sgt i32 %37, 0
br i1 %38, label %27, label %30
}