#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
typedef int /*<<< orphan*/ uint32_t;
typedef int /*<<< orphan*/ uint16_t;
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct msghdr {
  int msg_namelen;
  size_t msg_iovlen;
  struct iovec *msg_iov;
  struct sockaddr_in *msg_name;
};
struct iovec {
  int dummy;
};
typedef int ssize_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int teredo_recverr(int);
int teredo_sendv(int fd, const struct iovec *iov, size_t count,
                 uint32_t dest_ip, uint16_t dest_port) {
  struct sockaddr_in addr = {
      .sin_family = AF_INET, .sin_port = dest_port, .sin_addr.s_addr = dest_ip};
  struct msghdr msg = {.msg_name = &addr,
                       .msg_namelen = sizeof(addr),
                       .msg_iov = (struct iovec *)iov,
                       .msg_iovlen = count};
  ssize_t res;
  do
    res = sendmsg(fd, &msg, 0);
  while ((res == -1) && (teredo_recverr(fd) != -1));
  return res;
}


x86 assembly (clang, -O0):
<teredo_sendv>:
push   %rbp
mov    %rsp,%rbp
sub    $0x60,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    -0x1c(%rbp),%eax
mov    %eax,-0x30(%rbp)
mov    -0x20(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    0x2e9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
movl   $0xc,-0x50(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
lea    -0x30(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x50(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,-0x54(%rbp)
xor    %eax,%eax
cmpl   $0xffffffff,-0x54(%rbp)
mov    %al,-0x55(%rbp)
jne    11a2 <teredo_sendv+0x82>
mov    -0x4(%rbp),%edi
call   1040 <teredo_recverr@plt>
cmp    $0xffffffff,%eax
setne  %al
mov    %al,-0x55(%rbp)
mov    -0x55(%rbp),%al
test   $0x1,%al
jne    1171 <teredo_sendv+0x51>
mov    -0x54(%rbp),%eax
add    $0x60,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<teredo_sendv>:
push   %rbp
push   %r14
push   %rbx
sub    $0x30,%rsp
mov    %edi,%ebx
mov    %ecx,(%rsp)
mov    %r8d,0x4(%rsp)
mov    0x2eaf(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
movl   $0xc,0x10(%rsp)
mov    %rdx,0x18(%rsp)
mov    %rsi,0x20(%rsp)
mov    %rsp,%rax
mov    %rax,0x28(%rsp)
lea    0x10(%rsp),%r14
xchg   %ax,%ax
mov    %ebx,%edi
mov    %r14,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    117f <teredo_sendv+0x5f>
mov    %ebx,%edi
call   1040 <teredo_recverr@plt>
cmp    $0xffffffff,%eax
jne    1160 <teredo_sendv+0x40>
mov    %ebp,%eax
add    $0x30,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.msghdr = type { i32, i64, %struct.iovec*, %struct.sockaddr_in* }
@AF_INET = global i32 0
define i32 @teredo_sendv(i32 %0, %struct.iovec* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca %struct.iovec*
%8 = alloca i64
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.msghdr
%13 = alloca i32
store i32 %0, i32* %6
store %struct.iovec* %1, %struct.iovec** %7
store i64 %2, i64* %8
store i32 %3, i32* %9
store i32 %4, i32* %10
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
%16 = load i32, i32* %9
store i32 %16, i32* %15
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%18 = load i32, i32* %10
store i32 %18, i32* %17
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
%20 = load i32, i32* @AF_INET
store i32 %20, i32* %19
%21 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 0
store i32 12, i32* %21
%22 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 1
%23 = load i64, i64* %8
store i64 %23, i64* %22
%24 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 2
%25 = load %struct.iovec*, %struct.iovec** %7
store %struct.iovec* %25, %struct.iovec** %24
%26 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 3
store %struct.sockaddr_in* %11, %struct.sockaddr_in** %26
br label %27
27: ; preds = %37, %5
%28 = load i32, i32* %6
%29 = call i32 @sendmsg(i32 %28, %struct.msghdr* %12, i32 0)
store i32 %29, i32* %13
br label %30
30: ; preds = %27
%31 = load i32, i32* %13
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %37
33: ; preds = %30
%34 = load i32, i32* %6
%35 = call i32 @teredo_recverr(i32 %34)
%36 = icmp ne i32 %35, -1
br label %37
37: ; preds = %33, %30
%38 = phi i1 [ false, %30 ], [ %36, %33 ]
br i1 %38, label %27, label %39
39: ; preds = %37
%40 = load i32, i32* %13
ret i32 %40
}

LLVM IR (-O2):
%struct.iovec = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.msghdr = type { i32, i64, %struct.iovec*, %struct.sockaddr_in* }
@AF_INET = global i32 0
define i32 @teredo_sendv(i32 %0, %struct.iovec* %1, i64 %2, i32 %3, i32 %4) {
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.msghdr
%8 = bitcast %struct.sockaddr_in* %6 to i8*
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 0, i32 0
store i32 %3, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 1
store i32 %4, i32* %10
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i64 0, i32 2
%12 = load i32, i32* @AF_INET
store i32 %12, i32* %11
%13 = bitcast %struct.msghdr* %7 to i8*
%14 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 0
store i32 12, i32* %14
%15 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 1
store i64 %2, i64* %15
%16 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 2
store %struct.iovec* %1, %struct.iovec** %16
%17 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 3
store %struct.sockaddr_in* %6, %struct.sockaddr_in** %17
br label %18
18: ; preds = %21, %5
%19 = call i32 @sendmsg(i32 %0, %struct.msghdr* %7, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %24
21: ; preds = %18
%22 = call i32 @teredo_recverr(i32 %0)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %18
24: ; preds = %18, %21
ret i32 %19
}