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

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(void *);
void *malloc(size_t);
int /*<<< orphan*/ memcpy(void *, void *, size_t);
int expand_buf(struct iovec *iov, size_t size) {
  void *new;
  if (size <= iov->iov_len)
    return 1;
  if (!(new = malloc(size))) {
    errno = ENOMEM;
    return 0;
  }
  if (iov->iov_base) {
    memcpy(new, iov->iov_base, iov->iov_len);
    free(iov->iov_base);
  }
  iov->iov_base = new;
  iov->iov_len = size;
  return 1;
}


x86 assembly (clang, -O0):
<expand_buf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x10(%rbp),%rcx
cmp    (%rcx),%rax
ja     115d <expand_buf+0x2d>
movl   $0x1,-0x4(%rbp)
jmp    11e4 <expand_buf+0xb4>
mov    -0x18(%rbp),%rdi
call   1050 <malloc@plt>
mov    %rax,-0x20(%rbp)
cmp    $0x0,%rax
jne    1192 <expand_buf+0x62>
mov    0x2e65(%rip),%rax
mov    (%rax),%ecx
mov    0x2e54(%rip),%rax
mov    %ecx,(%rax)
movl   $0x0,-0x4(%rbp)
jmp    11e4 <expand_buf+0xb4>
mov    -0x10(%rbp),%rax
cmpq   $0x0,0x8(%rax)
je     11c6 <expand_buf+0x96>
mov    -0x20(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
call   1040 <memcpy@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1030 <free@plt>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,0x8(%rax)
mov    -0x18(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<expand_buf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    (%rdi),%r15
mov    $0x1,%r14d
cmp    %rsi,%r15
jae    1198 <expand_buf+0x68>
mov    %rsi,%rbx
mov    %rdi,%rbp
mov    %rsi,%rdi
call   1050 <malloc@plt>
test   %rax,%rax
je     1183 <expand_buf+0x53>
mov    %rax,%r12
mov    0x8(%rbp),%rsi
test   %rsi,%rsi
je     1179 <expand_buf+0x49>
mov    %r12,%rdi
mov    %r15,%rdx
call   1040 <memcpy@plt>
mov    0x8(%rbp),%rdi
call   1030 <free@plt>
mov    %r12,0x8(%rbp)
mov    %rbx,0x0(%rbp)
jmp    1198 <expand_buf+0x68>
mov    0x2e56(%rip),%rax
mov    (%rax),%eax
mov    0x2e45(%rip),%rcx
mov    %eax,(%rcx)
xor    %r14d,%r14d
mov    %r14d,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i64, i8* }
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @expand_buf(%struct.iovec* %0, i64 %1) {
%3 = alloca i32
%4 = alloca %struct.iovec*
%5 = alloca i64
%6 = alloca i8*
store %struct.iovec* %0, %struct.iovec** %4
store i64 %1, i64* %5
%7 = load i64, i64* %5
%8 = load %struct.iovec*, %struct.iovec** %4
%9 = getelementptr inbounds %struct.iovec, %struct.iovec* %8, i32 0, i32 0
%10 = load i64, i64* %9
%11 = icmp ule i64 %7, %10
br i1 %11, label %12, label %13
12: ; preds = %2
store i32 1, i32* %3
br label %44
13: ; preds = %2
%14 = load i64, i64* %5
%15 = call i8* @malloc(i64 %14)
store i8* %15, i8** %6
%16 = icmp ne i8* %15, null
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = load i32, i32* @ENOMEM
store i32 %18, i32* @errno
store i32 0, i32* %3
br label %44
19: ; preds = %13
%20 = load %struct.iovec*, %struct.iovec** %4
%21 = getelementptr inbounds %struct.iovec, %struct.iovec* %20, i32 0, i32 1
%22 = load i8*, i8** %21
%23 = icmp ne i8* %22, null
br i1 %23, label %24, label %37
24: ; preds = %19
%25 = load i8*, i8** %6
%26 = load %struct.iovec*, %struct.iovec** %4
%27 = getelementptr inbounds %struct.iovec, %struct.iovec* %26, i32 0, i32 1
%28 = load i8*, i8** %27
%29 = load %struct.iovec*, %struct.iovec** %4
%30 = getelementptr inbounds %struct.iovec, %struct.iovec* %29, i32 0, i32 0
%31 = load i64, i64* %30
%32 = call i32 @memcpy(i8* %25, i8* %28, i64 %31)
%33 = load %struct.iovec*, %struct.iovec** %4
%34 = getelementptr inbounds %struct.iovec, %struct.iovec* %33, i32 0, i32 1
%35 = load i8*, i8** %34
%36 = call i32 @free(i8* %35)
br label %37
37: ; preds = %24, %19
%38 = load i8*, i8** %6
%39 = load %struct.iovec*, %struct.iovec** %4
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %39, i32 0, i32 1
store i8* %38, i8** %40
%41 = load i64, i64* %5
%42 = load %struct.iovec*, %struct.iovec** %4
%43 = getelementptr inbounds %struct.iovec, %struct.iovec* %42, i32 0, i32 0
store i64 %41, i64* %43
store i32 1, i32* %3
br label %44
44: ; preds = %37, %17, %12
%45 = load i32, i32* %3
ret i32 %45
}

LLVM IR (-O2):
%struct.iovec = type { i64, i8* }
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @expand_buf(%struct.iovec* nocapture %0, i64 %1) {
%3 = getelementptr inbounds %struct.iovec, %struct.iovec* %0, i64 0, i32 0
%4 = load i64, i64* %3
%5 = icmp ult i64 %4, %1
br i1 %5, label %6, label %20
6: ; preds = %2
%7 = tail call i8* @malloc(i64 %1)
%8 = icmp eq i8* %7, null
br i1 %8, label %9, label %11
9: ; preds = %6
%10 = load i32, i32* @ENOMEM
store i32 %10, i32* @errno
br label %20
11: ; preds = %6
%12 = getelementptr inbounds %struct.iovec, %struct.iovec* %0, i64 0, i32 1
%13 = load i8*, i8** %12
%14 = icmp eq i8* %13, null
br i1 %14, label %19, label %15
15: ; preds = %11
%16 = tail call i32 @memcpy(i8* %7, i8* %13, i64 %4)
%17 = load i8*, i8** %12
%18 = tail call i32 @free(i8* %17)
br label %19
19: ; preds = %15, %11
store i8* %7, i8** %12
store i64 %1, i64* %3
br label %20
20: ; preds = %2, %19, %9
%21 = phi i32 [ 1, %19 ], [ 0, %9 ], [ 1, %2 ]
ret i32 %21
}