#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 iovec {
  int iov_len;
  void *iov_base;
};

/* Variables and functions */
struct iovec *realloc(struct iovec *, int);
void *strdup(char const *);
int strlen(void *);
void build_iovec(struct iovec **iov, int *iovlen, const char *name, void *val,
                 size_t len) {
  int i;
  if (*iovlen < 0)
    return;
  i = *iovlen;
  *iov = realloc(*iov, sizeof **iov * (i + 2));
  if (*iov == NULL) {
    *iovlen = -1;
    return;
  }
  (*iov)[i].iov_base = strdup(name);
  (*iov)[i].iov_len = strlen(name) + 1;
  i++;
  (*iov)[i].iov_base = val;
  if (len == (size_t)-1) {
    if (val != NULL)
      len = strlen(val) + 1;
    else
      len = 0;
  }
  (*iov)[i].iov_len = (int)len;
  *iovlen = ++i;
}


x86 assembly (clang, -O0):
<build_iovec>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    %r8,-0x28(%rbp)
mov    -0x10(%rbp),%rax
cmpl   $0x0,(%rax)
jge    115e <build_iovec+0x2e>
jmp    1272 <build_iovec+0x142>
mov    -0x10(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x8(%rbp),%rax
mov    (%rax),%rdi
mov    -0x2c(%rbp),%eax
add    $0x2,%eax
cltq
shl    $0x4,%rax
mov    %eax,%esi
call   1040 <realloc@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x8(%rbp),%rax
cmpq   $0x0,(%rax)
jne    11a8 <build_iovec+0x78>
mov    -0x10(%rbp),%rax
movl   $0xffffffff,(%rax)
jmp    1272 <build_iovec+0x142>
mov    -0x18(%rbp),%rdi
call   1050 <strdup@plt>
mov    %rax,%rcx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x2c(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
add    $0x1,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x2c(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
mov    -0x20(%rbp),%rcx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x2c(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %rcx,0x8(%rax)
cmpq   $0xffffffffffffffff,-0x28(%rbp)
jne    1249 <build_iovec+0x119>
cmpq   $0x0,-0x20(%rbp)
je     123c <build_iovec+0x10c>
mov    -0x20(%rbp),%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
cltq
mov    %rax,-0x28(%rbp)
jmp    1244 <build_iovec+0x114>
movq   $0x0,-0x28(%rbp)
jmp    1249 <build_iovec+0x119>
mov    -0x28(%rbp),%rax
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
movslq -0x2c(%rbp),%rdx
shl    $0x4,%rdx
add    %rdx,%rax
mov    %ecx,(%rax)
mov    -0x2c(%rbp),%ecx
add    $0x1,%ecx
mov    %ecx,-0x2c(%rbp)
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<build_iovec>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    (%rsi),%r15d
test   %r15d,%r15d
js     11e5 <build_iovec+0xb5>
mov    %r8,%r12
mov    %rdx,%r13
mov    %rsi,%r14
mov    %rdi,%rbx
mov    %rcx,0x10(%rsp)
mov    (%rdi),%rdi
mov    %r15d,%esi
shl    $0x4,%esi
add    $0x20,%esi
call   1040 <realloc@plt>
mov    %rax,(%rbx)
test   %rax,%rax
je     11ce <build_iovec+0x9e>
mov    %rax,%rbp
mov    %r13,%rdi
call   1050 <strdup@plt>
mov    %r12,0x8(%rsp)
mov    %r15,%r12
shl    $0x4,%r12
mov    %rax,0x8(%rbp,%r12,1)
mov    %r13,%rdi
call   1030 <strlen@plt>
add    $0x1,%eax
mov    (%rbx),%rcx
mov    %eax,(%rcx,%r12,1)
mov    0x8(%rsp),%rax
lea    0x1(%r15),%rbp
shl    $0x4,%rbp
mov    0x10(%rsp),%rdi
mov    %rdi,0x8(%rcx,%rbp,1)
cmp    $0xffffffffffffffff,%rax
jne    11d8 <build_iovec+0xa8>
test   %rdi,%rdi
je     11d6 <build_iovec+0xa6>
call   1030 <strlen@plt>
add    $0x1,%eax
jmp    11d8 <build_iovec+0xa8>
mov    $0xffffffff,%r15d
jmp    11e2 <build_iovec+0xb2>
xor    %eax,%eax
mov    (%rbx),%rcx
mov    %eax,(%rcx,%rbp,1)
add    $0x2,%r15d
mov    %r15d,(%r14)
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32, i8* }
define void @build_iovec(%struct.iovec** %0, i32* %1, i8* %2, i8* %3, i64 %4) {
%6 = alloca %struct.iovec**
%7 = alloca i32*
%8 = alloca i8*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
store %struct.iovec** %0, %struct.iovec*** %6
store i32* %1, i32** %7
store i8* %2, i8** %8
store i8* %3, i8** %9
store i64 %4, i64* %10
%12 = load i32*, i32** %7
%13 = load i32, i32* %12
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %16
15: ; preds = %5
br label %84
16: ; preds = %5
%17 = load i32*, i32** %7
%18 = load i32, i32* %17
store i32 %18, i32* %11
%19 = load %struct.iovec**, %struct.iovec*** %6
%20 = load %struct.iovec*, %struct.iovec** %19
%21 = load i32, i32* %11
%22 = add nsw i32 %21, 2
%23 = sext i32 %22 to i64
%24 = mul i64 16, %23
%25 = trunc i64 %24 to i32
%26 = call %struct.iovec* @realloc(%struct.iovec* %20, i32 %25)
%27 = load %struct.iovec**, %struct.iovec*** %6
store %struct.iovec* %26, %struct.iovec** %27
%28 = load %struct.iovec**, %struct.iovec*** %6
%29 = load %struct.iovec*, %struct.iovec** %28
%30 = icmp eq %struct.iovec* %29, null
br i1 %30, label %31, label %33
31: ; preds = %16
%32 = load i32*, i32** %7
store i32 -1, i32* %32
br label %84
33: ; preds = %16
%34 = load i8*, i8** %8
%35 = call i8* @strdup(i8* %34)
%36 = load %struct.iovec**, %struct.iovec*** %6
%37 = load %struct.iovec*, %struct.iovec** %36
%38 = load i32, i32* %11
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %37, i64 %39
%41 = getelementptr inbounds %struct.iovec, %struct.iovec* %40, i32 0, i32 1
store i8* %35, i8** %41
%42 = load i8*, i8** %8
%43 = call i32 @strlen(i8* %42)
%44 = add nsw i32 %43, 1
%45 = load %struct.iovec**, %struct.iovec*** %6
%46 = load %struct.iovec*, %struct.iovec** %45
%47 = load i32, i32* %11
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds %struct.iovec, %struct.iovec* %46, i64 %48
%50 = getelementptr inbounds %struct.iovec, %struct.iovec* %49, i32 0, i32 0
store i32 %44, i32* %50
%51 = load i32, i32* %11
%52 = add nsw i32 %51, 1
store i32 %52, i32* %11
%53 = load i8*, i8** %9
%54 = load %struct.iovec**, %struct.iovec*** %6
%55 = load %struct.iovec*, %struct.iovec** %54
%56 = load i32, i32* %11
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds %struct.iovec, %struct.iovec* %55, i64 %57
%59 = getelementptr inbounds %struct.iovec, %struct.iovec* %58, i32 0, i32 1
store i8* %53, i8** %59
%60 = load i64, i64* %10
%61 = icmp eq i64 %60, -1
br i1 %61, label %62, label %72
62: ; preds = %33
%63 = load i8*, i8** %9
%64 = icmp ne i8* %63, null
br i1 %64, label %65, label %70
65: ; preds = %62
%66 = load i8*, i8** %9
%67 = call i32 @strlen(i8* %66)
%68 = add nsw i32 %67, 1
%69 = sext i32 %68 to i64
store i64 %69, i64* %10
br label %71
70: ; preds = %62
store i64 0, i64* %10
br label %71
71: ; preds = %70, %65
br label %72
72: ; preds = %71, %33
%73 = load i64, i64* %10
%74 = trunc i64 %73 to i32
%75 = load %struct.iovec**, %struct.iovec*** %6
%76 = load %struct.iovec*, %struct.iovec** %75
%77 = load i32, i32* %11
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds %struct.iovec, %struct.iovec* %76, i64 %78
%80 = getelementptr inbounds %struct.iovec, %struct.iovec* %79, i32 0, i32 0
store i32 %74, i32* %80
%81 = load i32, i32* %11
%82 = add nsw i32 %81, 1
store i32 %82, i32* %11
%83 = load i32*, i32** %7
store i32 %82, i32* %83
br label %84
84: ; preds = %72, %31, %15
ret void
}

LLVM IR (-O2):
%struct.iovec = type { i32, i8* }
define void @build_iovec(%struct.iovec** nocapture %0, i32* nocapture %1, i8* %2, i8* %3, i64 %4) {
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 0
br i1 %7, label %40, label %8
8: ; preds = %5
%9 = load %struct.iovec*, %struct.iovec** %0
%10 = shl i32 %6, 4
%11 = add i32 %10, 32
%12 = tail call %struct.iovec* @realloc(%struct.iovec* %9, i32 %11)
store %struct.iovec* %12, %struct.iovec** %0
%13 = icmp eq %struct.iovec* %12, null
br i1 %13, label %38, label %14
14: ; preds = %8
%15 = tail call i8* @strdup(i8* %2)
%16 = zext i32 %6 to i64
%17 = getelementptr inbounds %struct.iovec, %struct.iovec* %12, i64 %16, i32 1
store i8* %15, i8** %17
%18 = tail call i32 @strlen(i8* %2)
%19 = add nsw i32 %18, 1
%20 = load %struct.iovec*, %struct.iovec** %0
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i64 %16, i32 0
store i32 %19, i32* %21
%22 = add nuw nsw i32 %6, 1
%23 = zext i32 %22 to i64
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i64 %23, i32 1
store i8* %3, i8** %24
%25 = icmp eq i64 %4, -1
br i1 %25, label %26, label %32
26: ; preds = %14
%27 = icmp eq i8* %3, null
br i1 %27, label %32, label %28
28: ; preds = %26
%29 = tail call i32 @strlen(i8* %3)
%30 = add nsw i32 %29, 1
%31 = zext i32 %30 to i64
br label %32
32: ; preds = %26, %28, %14
%33 = phi i64 [ %31, %28 ], [ %4, %14 ], [ 0, %26 ]
%34 = trunc i64 %33 to i32
%35 = load %struct.iovec*, %struct.iovec** %0
%36 = getelementptr inbounds %struct.iovec, %struct.iovec* %35, i64 %23, i32 0
store i32 %34, i32* %36
%37 = add nuw nsw i32 %6, 2
br label %38
38: ; preds = %8, %32
%39 = phi i32 [ %37, %32 ], [ -1, %8 ]
store i32 %39, i32* %1
br label %40
40: ; preds = %38, %5
ret void
}