#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 msghdr {
  int msg_iovlen;
  struct iovec *msg_iov;
};
struct ip {
  scalar_t__ ip_p;
  int ip_hl;
  unsigned short ip_id;
};
struct iovec {
  char *iov_base;
  int iov_len;
};
typedef int /*<<< orphan*/ mh;
typedef int /*<<< orphan*/ iov;

/* Variables and functions */
unsigned short htons(int);
int /*<<< orphan*/ memcpy(char *, unsigned short *, int);
int /*<<< orphan*/ memset(struct msghdr *, int /*<<< orphan*/, int);
int ntohs(unsigned short);
int /*<<< orphan*/ printf(char *, int, int);
int sendmsg(int, struct msghdr *, int /*<<< orphan*/);
int handle_data(int dude, char *buf, int len) {
  struct ip *ih;
  unsigned short id;
  char tmp[4];
  struct iovec iov[2];
  struct msghdr mh;
  ih = (struct ip *)buf;
  if (ih->ip_p != 0)
    return 0;
  if (ih->ip_hl != 5)
    return 0;
  id = ih->ip_id;
  len -= 20;
  buf += 20;
  printf("Got %d bytes [%d]\n", len, ntohs(id));
  memcpy(tmp, &id, 2);
  id = htons(len);
  memcpy(&tmp[2], &id, 2);
  iov[0].iov_base = tmp;
  iov[0].iov_len = 4;
  iov[1].iov_base = buf;
  iov[1].iov_len = len;
  memset(&mh, 0, sizeof(mh));
  mh.msg_iov = iov;
  mh.msg_iovlen = sizeof(iov) / sizeof(struct iovec);
  if (sendmsg(dude, &mh, 0) != (4 + len))
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<handle_data>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rax
cmpq   $0x0,(%rax)
je     1194 <handle_data+0x34>
movl   $0x0,-0x4(%rbp)
jmp    128e <handle_data+0x12e>
mov    -0x20(%rbp),%rax
cmpl   $0x5,0x8(%rax)
je     11ae <handle_data+0x4e>
movl   $0x0,-0x4(%rbp)
jmp    128e <handle_data+0x12e>
mov    -0x20(%rbp),%rax
mov    0xc(%rax),%ax
mov    %ax,-0x22(%rbp)
mov    -0x14(%rbp),%eax
sub    $0x14,%eax
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
add    $0x14,%rax
mov    %rax,-0x10(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x64(%rbp)
movzwl -0x22(%rbp),%edi
call   1080 <ntohs@plt>
mov    -0x64(%rbp),%esi
mov    %eax,%edx
lea    0xe16(%rip),%rdi
call   1050 <printf@plt>
lea    -0x26(%rbp),%rdi
lea    -0x22(%rbp),%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %ax,-0x22(%rbp)
lea    -0x26(%rbp),%rdi
add    $0x2,%rdi
lea    -0x22(%rbp),%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
lea    -0x26(%rbp),%rax
mov    %rax,-0x50(%rbp)
movl   $0x4,-0x48(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x38(%rbp)
lea    -0x60(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    -0x50(%rbp),%rax
mov    %rax,-0x58(%rbp)
movl   $0x2,-0x60(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x60(%rbp),%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    -0x14(%rbp),%ecx
add    $0x4,%ecx
cmp    %ecx,%eax
je     1287 <handle_data+0x127>
movl   $0xffffffff,-0x4(%rbp)
jmp    128e <handle_data+0x12e>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<handle_data>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x48,%rsp
xor    %eax,%eax
cmpq   $0x0,(%rsi)
jne    1244 <handle_data+0xe4>
mov    %rsi,%rbp
cmpl   $0x5,0x8(%rsi)
jne    1244 <handle_data+0xe4>
mov    %edx,%ebx
mov    %edi,%r14d
movzwl 0xc(%rbp),%edi
mov    %di,0xa(%rsp)
lea    -0x14(%rbx),%r15d
add    $0x14,%rbp
call   1080 <ntohs@plt>
lea    0xe57(%rip),%rdi
mov    %r15d,%esi
mov    %eax,%edx
call   1050 <printf@plt>
lea    0xc(%rsp),%r12
lea    0xa(%rsp),%r13
mov    %r12,%rdi
mov    %r13,%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
mov    %r15d,%edi
call   1040 <htons@plt>
mov    %ax,0xa(%rsp)
lea    0xe(%rsp),%rdi
mov    %r13,%rsi
mov    $0x2,%edx
call   1070 <memcpy@plt>
mov    %r12,0x20(%rsp)
movl   $0x4,0x28(%rsp)
mov    %rbp,0x30(%rsp)
mov    %r15d,0x38(%rsp)
lea    0x10(%rsp),%rbp
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
lea    0x20(%rsp),%rax
mov    %rax,0x18(%rsp)
movl   $0x2,0x10(%rsp)
mov    %r14d,%edi
mov    %rbp,%rsi
xor    %edx,%edx
call   1030 <sendmsg@plt>
mov    %eax,%ecx
add    $0xfffffff0,%ebx
xor    %eax,%eax
cmp    %ebx,%ecx
setne  %al
neg    %eax
add    $0x48,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.ip = type { i64, i32, i16 }
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, %struct.iovec* }
@.str = [19 x i8] c"Got %d bytes [%d]\0A\00"
define i32 @handle_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i32
%8 = alloca %struct.ip*
%9 = alloca i16
%10 = alloca [4 x i8]
%11 = alloca [2 x %struct.iovec]
%12 = alloca %struct.msghdr
store i32 %0, i32* %5
store i8* %1, i8** %6
store i32 %2, i32* %7
%13 = load i8*, i8** %6
%14 = bitcast i8* %13 to %struct.ip*
store %struct.ip* %14, %struct.ip** %8
%15 = load %struct.ip*, %struct.ip** %8
%16 = getelementptr inbounds %struct.ip, %struct.ip* %15, i32 0, i32 0
%17 = load i64, i64* %16
%18 = icmp ne i64 %17, 0
br i1 %18, label %19, label %20
19: ; preds = %3
store i32 0, i32* %4
br label %66
20: ; preds = %3
%21 = load %struct.ip*, %struct.ip** %8
%22 = getelementptr inbounds %struct.ip, %struct.ip* %21, i32 0, i32 1
%23 = load i32, i32* %22
%24 = icmp ne i32 %23, 5
br i1 %24, label %25, label %26
25: ; preds = %20
store i32 0, i32* %4
br label %66
26: ; preds = %20
%27 = load %struct.ip*, %struct.ip** %8
%28 = getelementptr inbounds %struct.ip, %struct.ip* %27, i32 0, i32 2
%29 = load i16, i16* %28
store i16 %29, i16* %9
%30 = load i32, i32* %7
%31 = sub nsw i32 %30, 20
store i32 %31, i32* %7
%32 = load i8*, i8** %6
%33 = getelementptr inbounds i8, i8* %32, i64 20
store i8* %33, i8** %6
%34 = load i32, i32* %7
%35 = load i16, i16* %9
%36 = call i32 @ntohs(i16 zeroext %35)
%37 = call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %34, i32 %36)
%38 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%39 = call i32 @memcpy(i8* %38, i16* %9, i32 2)
%40 = load i32, i32* %7
%41 = call zeroext i16 @htons(i32 %40)
store i16 %41, i16* %9
%42 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 2
%43 = call i32 @memcpy(i8* %42, i16* %9, i32 2)
%44 = getelementptr inbounds [4 x i8], [4 x i8]* %10, i64 0, i64 0
%45 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 0
%46 = getelementptr inbounds %struct.iovec, %struct.iovec* %45, i32 0, i32 0
store i8* %44, i8** %46
%47 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 0
%48 = getelementptr inbounds %struct.iovec, %struct.iovec* %47, i32 0, i32 1
store i32 4, i32* %48
%49 = load i8*, i8** %6
%50 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 1
%51 = getelementptr inbounds %struct.iovec, %struct.iovec* %50, i32 0, i32 0
store i8* %49, i8** %51
%52 = load i32, i32* %7
%53 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 1
%54 = getelementptr inbounds %struct.iovec, %struct.iovec* %53, i32 0, i32 1
store i32 %52, i32* %54
%55 = call i32 @memset(%struct.msghdr* %12, i32 0, i32 16)
%56 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %11, i64 0, i64 0
%57 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 1
store %struct.iovec* %56, %struct.iovec** %57
%58 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %12, i32 0, i32 0
store i32 2, i32* %58
%59 = load i32, i32* %5
%60 = call i32 @sendmsg(i32 %59, %struct.msghdr* %12, i32 0)
%61 = load i32, i32* %7
%62 = add nsw i32 4, %61
%63 = icmp ne i32 %60, %62
br i1 %63, label %64, label %65
64: ; preds = %26
store i32 -1, i32* %4
br label %66
65: ; preds = %26
store i32 0, i32* %4
br label %66
66: ; preds = %65, %64, %25, %19
%67 = load i32, i32* %4
ret i32 %67
}

LLVM IR (-O2):
%struct.iovec = type { i8*, i32 }
%struct.msghdr = type { i32, %struct.iovec* }
@.str = [19 x i8] c"Got %d bytes [%d]\0A\00"
define i32 @handle_data(i32 %0, i8* %1, i32 %2) {
%4 = alloca i16
%5 = alloca [4 x i8]
%6 = alloca [2 x %struct.iovec]
%7 = alloca %struct.msghdr
%8 = bitcast i16* %4 to i8*
%9 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 0
%10 = bitcast [2 x %struct.iovec]* %6 to i8*
%11 = bitcast %struct.msghdr* %7 to i8*
%12 = bitcast i8* %1 to i64*
%13 = load i64, i64* %12
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %44
15: ; preds = %3
%16 = getelementptr inbounds i8, i8* %1, i64 8
%17 = bitcast i8* %16 to i32*
%18 = load i32, i32* %17
%19 = icmp eq i32 %18, 5
br i1 %19, label %20, label %44
20: ; preds = %15
%21 = getelementptr inbounds i8, i8* %1, i64 12
%22 = bitcast i8* %21 to i16*
%23 = load i16, i16* %22
store i16 %23, i16* %4
%24 = add nsw i32 %2, -20
%25 = getelementptr inbounds i8, i8* %1, i64 20
%26 = tail call i32 @ntohs(i16 zeroext %23)
%27 = tail call i32 @printf(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i32 %24, i32 %26)
%28 = call i32 @memcpy(i8* %9, i16* %4, i32 2)
%29 = call zeroext i16 @htons(i32 %24)
store i16 %29, i16* %4
%30 = getelementptr inbounds [4 x i8], [4 x i8]* %5, i64 0, i64 2
%31 = call i32 @memcpy(i8* %30, i16* %4, i32 2)
%32 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 0
%33 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 0, i32 0
store i8* %9, i8** %33
%34 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 0, i32 1
store i32 4, i32* %34
%35 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 1, i32 0
store i8* %25, i8** %35
%36 = getelementptr inbounds [2 x %struct.iovec], [2 x %struct.iovec]* %6, i64 0, i64 1, i32 1
store i32 %24, i32* %36
%37 = call i32 @memset(%struct.msghdr* %7, i32 0, i32 16)
%38 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 1
store %struct.iovec* %32, %struct.iovec** %38
%39 = getelementptr inbounds %struct.msghdr, %struct.msghdr* %7, i64 0, i32 0
store i32 2, i32* %39
%40 = call i32 @sendmsg(i32 %0, %struct.msghdr* %7, i32 0)
%41 = add nsw i32 %2, -16
%42 = icmp ne i32 %40, %41
%43 = sext i1 %42 to i32
br label %44
44: ; preds = %20, %15, %3
%45 = phi i32 [ 0, %3 ], [ 0, %15 ], [ %43, %20 ]
ret i32 %45
}