#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 int /*<<< orphan*/ va_list;
typedef int /*<<< orphan*/ cmd_buf;

/* Variables and functions */
int /*<<< orphan*/ ENOMEM;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ free(char *);
char *malloc(int);
int system(char *);
int vsnprintf(char *, int, char const *, int /*<<< orphan*/);
int vsystemf(const char *fmt, va_list args) {
  char cmd_buf[1024], *cmd = cmd_buf;
  int rc, retval;
  assert(fmt != NULL);
  if ((rc = vsnprintf(cmd, sizeof(cmd_buf), fmt, args)) >= sizeof(cmd_buf)) {
    if ((cmd = malloc(rc + 1)) != NULL) {
      vsnprintf(cmd, rc + 1, fmt, args);
      cmd[rc] = '\0';
    }
  }
  if (cmd != NULL) {
    retval = system(cmd);
    if (cmd != cmd_buf) {
      free(cmd);
      cmd = NULL;
    }
  } else {
    retval = -1;
    errno = ENOMEM;
  }
  return retval;
}


x86 assembly (clang, -O0):
<vsystemf>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
cmpq   $0x0,-0x8(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1060 <assert@plt>
mov    -0x418(%rbp),%rdi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
mov    $0x400,%esi
call   1040 <vsnprintf@plt>
mov    %eax,-0x41c(%rbp)
cltq
cmp    $0x400,%rax
jb     1200 <vsystemf+0xb0>
mov    -0x41c(%rbp),%edi
add    $0x1,%edi
call   1070 <malloc@plt>
mov    %rax,-0x418(%rbp)
cmp    $0x0,%rax
je     11fb <vsystemf+0xab>
mov    -0x418(%rbp),%rdi
mov    -0x41c(%rbp),%esi
add    $0x1,%esi
mov    -0x8(%rbp),%rdx
mov    -0xc(%rbp),%ecx
call   1040 <vsnprintf@plt>
mov    -0x418(%rbp),%rax
movslq -0x41c(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
jmp    1200 <vsystemf+0xb0>
cmpq   $0x0,-0x418(%rbp)
je     1253 <vsystemf+0x103>
mov    -0x418(%rbp),%rdi
call   1050 <system@plt>
mov    %eax,-0x420(%rbp)
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
cmp    %rcx,%rax
je     124e <vsystemf+0xfe>
mov    -0x418(%rbp),%rdi
call   1030 <free@plt>
movq   $0x0,-0x418(%rbp)
jmp    126f <vsystemf+0x11f>
movl   $0xffffffff,-0x420(%rbp)
ff ff ff
mov    0x2d7c(%rip),%rax
mov    (%rax),%ecx
mov    0x2d6b(%rip),%rax
mov    %ecx,(%rax)
mov    -0x420(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<vsystemf>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %esi,%r14d
mov    %rdi,%rbx
xor    %edi,%edi
test   %rbx,%rbx
setne  %dil
call   1060 <assert@plt>
mov    %rsp,%rdi
mov    $0x400,%esi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1040 <vsnprintf@plt>
cmp    $0x3ff,%eax
jbe    11d2 <vsystemf+0x82>
mov    %eax,%ebp
lea    0x1(%rbp),%r12d
mov    %r12d,%edi
call   1070 <malloc@plt>
test   %rax,%rax
je     11dc <vsystemf+0x8c>
mov    %rax,%r15
movslq %ebp,%rbp
mov    %rax,%rdi
mov    %r12d,%esi
mov    %rbx,%rdx
mov    %r14d,%ecx
call   1040 <vsnprintf@plt>
movb   $0x0,(%r15,%rbp,1)
mov    %r15,%rdi
call   1050 <system@plt>
mov    %eax,%ebx
mov    %r15,%rdi
call   1030 <free@plt>
mov    %ebx,%eax
jmp    11f3 <vsystemf+0xa3>
mov    %rsp,%rdi
call   1050 <system@plt>
jmp    11f3 <vsystemf+0xa3>
mov    0x2dfd(%rip),%rax
mov    (%rax),%eax
mov    0x2dec(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @vsystemf(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca i8*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
store i8* %9, i8** %6
%10 = load i8*, i8** %3
%11 = icmp ne i8* %10, null
%12 = zext i1 %11 to i32
%13 = call i32 @assert(i32 %12)
%14 = load i8*, i8** %6
%15 = load i8*, i8** %3
%16 = load i32, i32* %4
%17 = call i32 @vsnprintf(i8* %14, i32 1024, i8* %15, i32 %16)
store i32 %17, i32* %7
%18 = sext i32 %17 to i64
%19 = icmp uge i64 %18, 1024
br i1 %19, label %20, label %37
20: ; preds = %2
%21 = load i32, i32* %7
%22 = add nsw i32 %21, 1
%23 = call i8* @malloc(i32 %22)
store i8* %23, i8** %6
%24 = icmp ne i8* %23, null
br i1 %24, label %25, label %36
25: ; preds = %20
%26 = load i8*, i8** %6
%27 = load i32, i32* %7
%28 = add nsw i32 %27, 1
%29 = load i8*, i8** %3
%30 = load i32, i32* %4
%31 = call i32 @vsnprintf(i8* %26, i32 %28, i8* %29, i32 %30)
%32 = load i8*, i8** %6
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %32, i64 %34
store i8 0, i8* %35
br label %36
36: ; preds = %25, %20
br label %37
37: ; preds = %36, %2
%38 = load i8*, i8** %6
%39 = icmp ne i8* %38, null
br i1 %39, label %40, label %50
40: ; preds = %37
%41 = load i8*, i8** %6
%42 = call i32 @system(i8* %41)
store i32 %42, i32* %8
%43 = load i8*, i8** %6
%44 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%45 = icmp ne i8* %43, %44
br i1 %45, label %46, label %49
46: ; preds = %40
%47 = load i8*, i8** %6
%48 = call i32 @free(i8* %47)
store i8* null, i8** %6
br label %49
49: ; preds = %46, %40
br label %52
50: ; preds = %37
store i32 -1, i32* %8
%51 = load i32, i32* @ENOMEM
store i32 %51, i32* @errno
br label %52
52: ; preds = %50, %49
%53 = load i32, i32* %8
ret i32 %53
}

LLVM IR (-O2):
@ENOMEM = global i32 0
@errno = global i32 0
define i32 @vsystemf(i8* %0, i32 %1) {
%3 = alloca [1024 x i8]
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%5 = icmp ne i8* %0, null
%6 = zext i1 %5 to i32
%7 = tail call i32 @assert(i32 %6)
%8 = call i32 @vsnprintf(i8* %4, i32 1024, i8* %0, i32 %1)
%9 = sext i32 %8 to i64
%10 = icmp ugt i32 %8, 1023
br i1 %10, label %13, label %11
11: ; preds = %2
%12 = call i32 @system(i8* %4)
br label %24
13: ; preds = %2
%14 = add nsw i32 %8, 1
%15 = tail call i8* @malloc(i32 %14)
%16 = icmp eq i8* %15, null
br i1 %16, label %22, label %17
17: ; preds = %13
%18 = tail call i32 @vsnprintf(i8* %15, i32 %14, i8* %0, i32 %1)
%19 = getelementptr inbounds i8, i8* %15, i64 %9
store i8 0, i8* %19
%20 = tail call i32 @system(i8* %15)
%21 = tail call i32 @free(i8* %15)
br label %24
22: ; preds = %13
%23 = load i32, i32* @ENOMEM
store i32 %23, i32* @errno
br label %24
24: ; preds = %11, %17, %22
%25 = phi i32 [ %20, %17 ], [ -1, %22 ], [ %12, %11 ]
ret i32 %25
}