#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 */

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memcpy(char *, char *, int);
int _sarg_buf_resize(char **buf, int *len) {
  char *buf_tmp = *buf;
  int old_len = *len;
  *buf = (char *)malloc(old_len * 2);
  if (!*buf) {
    *buf = buf_tmp;
    return -6;
  }
  memcpy(*buf, buf_tmp, old_len);
  *len = 2 * old_len;
  free(buf_tmp);
  return 0;
}


x86 assembly (clang, -O0):
<_sarg_buf_resize>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
mov    -0x18(%rbp),%rax
mov    (%rax),%eax
mov    %eax,-0x24(%rbp)
mov    -0x24(%rbp),%edi
shl    $0x1,%edi
call   1050 <malloc@plt>
mov    %rax,%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
mov    -0x10(%rbp),%rax
cmpq   $0x0,(%rax)
jne    118e <_sarg_buf_resize+0x5e>
mov    -0x20(%rbp),%rcx
mov    -0x10(%rbp),%rax
mov    %rcx,(%rax)
movl   $0xfffffffa,-0x4(%rbp)
jmp    11bd <_sarg_buf_resize+0x8d>
mov    -0x10(%rbp),%rax
mov    (%rax),%rdi
mov    -0x20(%rbp),%rsi
mov    -0x24(%rbp),%edx
call   1040 <memcpy@plt>
mov    -0x24(%rbp),%ecx
shl    $0x1,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x20(%rbp),%rdi
call   1030 <free@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_sarg_buf_resize>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r15
mov    %rdi,%rbp
mov    (%rdi),%r14
mov    (%rsi),%ebx
lea    (%rbx,%rbx,1),%r12d
mov    %r12d,%edi
call   1050 <malloc@plt>
mov    %rax,0x0(%rbp)
test   %rax,%rax
je     1174 <_sarg_buf_resize+0x44>
mov    %rax,%rdi
mov    %r14,%rsi
mov    %ebx,%edx
call   1040 <memcpy@plt>
mov    %r12d,(%r15)
mov    %r14,%rdi
call   1030 <free@plt>
xor    %eax,%eax
jmp    117d <_sarg_buf_resize+0x4d>
mov    %r14,0x0(%rbp)
mov    $0xfffffffa,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
define i32 @_sarg_buf_resize(i8** %0, i32* %1) {
%3 = alloca i32
%4 = alloca i8**
%5 = alloca i32*
%6 = alloca i8*
%7 = alloca i32
store i8** %0, i8*** %4
store i32* %1, i32** %5
%8 = load i8**, i8*** %4
%9 = load i8*, i8** %8
store i8* %9, i8** %6
%10 = load i32*, i32** %5
%11 = load i32, i32* %10
store i32 %11, i32* %7
%12 = load i32, i32* %7
%13 = mul nsw i32 %12, 2
%14 = call i64 @malloc(i32 %13)
%15 = inttoptr i64 %14 to i8*
%16 = load i8**, i8*** %4
store i8* %15, i8** %16
%17 = load i8**, i8*** %4
%18 = load i8*, i8** %17
%19 = icmp ne i8* %18, null
br i1 %19, label %23, label %20
20: ; preds = %2
%21 = load i8*, i8** %6
%22 = load i8**, i8*** %4
store i8* %21, i8** %22
store i32 -6, i32* %3
br label %34
23: ; preds = %2
%24 = load i8**, i8*** %4
%25 = load i8*, i8** %24
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = call i32 @memcpy(i8* %25, i8* %26, i32 %27)
%29 = load i32, i32* %7
%30 = mul nsw i32 2, %29
%31 = load i32*, i32** %5
store i32 %30, i32* %31
%32 = load i8*, i8** %6
%33 = call i32 @free(i8* %32)
store i32 0, i32* %3
br label %34
34: ; preds = %23, %20
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
define i32 @_sarg_buf_resize(i8** nocapture %0, i32* nocapture %1) {
%3 = load i8*, i8** %0
%4 = load i32, i32* %1
%5 = shl nsw i32 %4, 1
%6 = tail call i64 @malloc(i32 %5)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %0
%8 = icmp eq i64 %6, 0
br i1 %8, label %9, label %10
9: ; preds = %2
store i8* %3, i8** %0
br label %13
10: ; preds = %2
%11 = tail call i32 @memcpy(i8* %7, i8* %3, i32 %4)
store i32 %5, i32* %1
%12 = tail call i32 @free(i8* %3)
br label %13
13: ; preds = %10, %9
%14 = phi i32 [ 0, %10 ], [ -6, %9 ]
ret i32 %14
}