#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 char uint8_t;
typedef size_t u_int32_t;
struct iovec {
  size_t iov_len;
  scalar_t__ iov_base;
};

/* Variables and functions */
char *xmalloc(size_t);
char *rtkl_pkt_copy(struct iovec *iov, size_t len) {
  u_int32_t i, j, count;
  char *p = xmalloc(len);
  for (i = 0, count = 0; count < len; ++i) {
    for (j = 0; count < len && j < iov[i].iov_len; ++j, ++count) {
      p[count] = ((uint8_t *)iov[i].iov_base)[j];
    }
  }
  return p;
}


x86 assembly (clang, -O0):
<rtkl_pkt_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <xmalloc@plt>
mov    %rax,-0x30(%rbp)
movq   $0x0,-0x18(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
cmp    -0x10(%rbp),%rax
jae    11ea <rtkl_pkt_copy+0xda>
movq   $0x0,-0x20(%rbp)
mov    -0x28(%rbp),%rcx
xor    %eax,%eax
cmp    -0x10(%rbp),%rcx
mov    %al,-0x31(%rbp)
jae    1182 <rtkl_pkt_copy+0x72>
mov    -0x20(%rbp),%rax
mov    -0x8(%rbp),%rcx
mov    -0x18(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rcx
cmp    (%rcx),%rax
setb   %al
mov    %al,-0x31(%rbp)
mov    -0x31(%rbp),%al
test   $0x1,%al
jne    1192 <rtkl_pkt_copy+0x82>
jmp    11d4 <rtkl_pkt_copy+0xc4>
mov    -0x8(%rbp),%rax
mov    -0x18(%rbp),%rcx
shl    $0x4,%rcx
add    %rcx,%rax
mov    0x8(%rax),%rax
mov    -0x20(%rbp),%rcx
mov    (%rax,%rcx,1),%dl
mov    -0x30(%rbp),%rax
mov    -0x28(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x20(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    1153 <rtkl_pkt_copy+0x43>
jmp    11d9 <rtkl_pkt_copy+0xc9>
mov    -0x18(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x18(%rbp)
jmp    113d <rtkl_pkt_copy+0x2d>
mov    -0x30(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<rtkl_pkt_copy>:
push   %r15
push   %r14
push   %rbx
mov    %rsi,%r15
mov    %rdi,%r14
mov    %rsi,%rdi
call   1030 <xmalloc@plt>
test   %r15,%r15
je     118b <rtkl_pkt_copy+0x7b>
mov    %r15,%r8
neg    %r8
xor    %r10d,%r10d
xor    %r9d,%r9d
jmp    114c <rtkl_pkt_copy+0x3c>
cs nopw 0x0(%rax,%rax,1)
add    %rsi,%r9
add    $0x1,%r10
cmp    %r15,%r9
jae    118b <rtkl_pkt_copy+0x7b>
cmp    %r15,%r9
jae    1143 <rtkl_pkt_copy+0x33>
mov    %r10,%rcx
shl    $0x4,%rcx
lea    (%r14,%rcx,1),%rdi
lea    (%r14,%rcx,1),%r11
add    $0x8,%r11
lea    (%rax,%r9,1),%rbx
lea    (%r8,%r9,1),%rdx
xor    %esi,%esi
xchg   %ax,%ax
cmp    (%rdi),%rsi
jae    1140 <rtkl_pkt_copy+0x30>
mov    (%r11),%rcx
movzbl (%rcx,%rsi,1),%ecx
mov    %cl,(%rbx,%rsi,1)
add    $0x1,%rsi
mov    %rdx,%rcx
add    %rsi,%rcx
jne    1170 <rtkl_pkt_copy+0x60>
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
%struct.iovec = type { i64, i64 }
define i8* @rtkl_pkt_copy(%struct.iovec* %0, i64 %1) {
%3 = alloca %struct.iovec*
%4 = alloca i64
%5 = alloca i64
%6 = alloca i64
%7 = alloca i64
%8 = alloca i8*
store %struct.iovec* %0, %struct.iovec** %3
store i64 %1, i64* %4
%9 = load i64, i64* %4
%10 = call i8* @xmalloc(i64 %9)
store i8* %10, i8** %8
store i64 0, i64* %5
store i64 0, i64* %7
br label %11
11: ; preds = %49, %2
%12 = load i64, i64* %7
%13 = load i64, i64* %4
%14 = icmp ult i64 %12, %13
br i1 %14, label %15, label %52
15: ; preds = %11
store i64 0, i64* %6
br label %16
16: ; preds = %43, %15
%17 = load i64, i64* %7
%18 = load i64, i64* %4
%19 = icmp ult i64 %17, %18
br i1 %19, label %20, label %28
20: ; preds = %16
%21 = load i64, i64* %6
%22 = load %struct.iovec*, %struct.iovec** %3
%23 = load i64, i64* %5
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %22, i64 %23
%25 = getelementptr inbounds %struct.iovec, %struct.iovec* %24, i32 0, i32 0
%26 = load i64, i64* %25
%27 = icmp ult i64 %21, %26
br label %28
28: ; preds = %20, %16
%29 = phi i1 [ false, %16 ], [ %27, %20 ]
br i1 %29, label %30, label %48
30: ; preds = %28
%31 = load %struct.iovec*, %struct.iovec** %3
%32 = load i64, i64* %5
%33 = getelementptr inbounds %struct.iovec, %struct.iovec* %31, i64 %32
%34 = getelementptr inbounds %struct.iovec, %struct.iovec* %33, i32 0, i32 1
%35 = load i64, i64* %34
%36 = inttoptr i64 %35 to i8*
%37 = load i64, i64* %6
%38 = getelementptr inbounds i8, i8* %36, i64 %37
%39 = load i8, i8* %38
%40 = load i8*, i8** %8
%41 = load i64, i64* %7
%42 = getelementptr inbounds i8, i8* %40, i64 %41
store i8 %39, i8* %42
br label %43
43: ; preds = %30
%44 = load i64, i64* %6
%45 = add i64 %44, 1
store i64 %45, i64* %6
%46 = load i64, i64* %7
%47 = add i64 %46, 1
store i64 %47, i64* %7
br label %16
48: ; preds = %28
br label %49
49: ; preds = %48
%50 = load i64, i64* %5
%51 = add i64 %50, 1
store i64 %51, i64* %5
br label %11
52: ; preds = %11
%53 = load i8*, i8** %8
ret i8* %53
}

LLVM IR (-O2):
%struct.iovec = type { i64, i64 }
define i8* @rtkl_pkt_copy(%struct.iovec* nocapture %0, i64 %1) {
%3 = tail call i8* @xmalloc(i64 %1)
%4 = icmp eq i64 %1, 0
br i1 %4, label %31, label %5
5: ; preds = %2, %27
%6 = phi i64 [ %29, %27 ], [ 0, %2 ]
%7 = phi i64 [ %28, %27 ], [ 0, %2 ]
%8 = getelementptr inbounds %struct.iovec, %struct.iovec* %0, i64 %6, i32 0
%9 = icmp ult i64 %7, %1
br i1 %9, label %10, label %27
10: ; preds = %5
%11 = getelementptr inbounds %struct.iovec, %struct.iovec* %0, i64 %6, i32 1
%12 = sub i64 %1, %7
br label %13
13: ; preds = %10, %18
%14 = phi i64 [ %7, %10 ], [ %25, %18 ]
%15 = phi i64 [ 0, %10 ], [ %24, %18 ]
%16 = load i64, i64* %8
%17 = icmp ult i64 %15, %16
br i1 %17, label %18, label %27
18: ; preds = %13
%19 = load i64, i64* %11
%20 = inttoptr i64 %19 to i8*
%21 = getelementptr inbounds i8, i8* %20, i64 %15
%22 = load i8, i8* %21
%23 = getelementptr inbounds i8, i8* %3, i64 %14
store i8 %22, i8* %23
%24 = add nuw i64 %15, 1
%25 = add nuw i64 %14, 1
%26 = icmp eq i64 %24, %12
br i1 %26, label %31, label %13
27: ; preds = %13, %5
%28 = phi i64 [ %7, %5 ], [ %14, %13 ]
%29 = add i64 %6, 1
%30 = icmp ult i64 %28, %1
br i1 %30, label %5, label %31
31: ; preds = %27, %18, %2
ret i8* %3
}