#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*/ EINVAL;
int /*<<< orphan*/ errno;
int execvp(char *, char **);
int chain_command(int shift, int argc, char *argv[]) {
  if (argc - shift <= 0) {
    errno = EINVAL;
    return -1;
  }
  char *new_arg[argc - shift + 1];
  for (int i = shift; i < argc; ++i)
    new_arg[i - shift] = argv[i];
  new_arg[argc - shift] = 0;
  return execvp(new_arg[0], new_arg);
}


x86 assembly (clang, -O0):
<chain_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
cmp    $0x0,%eax
jg     114f <chain_command+0x3f>
mov    0x2ea0(%rip),%rax
mov    (%rax),%ecx
mov    0x2e9f(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <chain_command+0xd1>
mov    -0xc(%rbp),%eax
mov    -0x8(%rbp),%ecx
sub    %ecx,%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
lea    0xf(,%rax,8),%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
cmp    -0xc(%rbp),%eax
jge    11bb <chain_command+0xab>
mov    -0x38(%rbp),%rax
mov    -0x18(%rbp),%rcx
movslq -0x2c(%rbp),%rdx
mov    (%rcx,%rdx,8),%rdx
mov    -0x2c(%rbp),%ecx
sub    -0x8(%rbp),%ecx
movslq %ecx,%rcx
mov    %rdx,(%rax,%rcx,8)
mov    -0x2c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x2c(%rbp)
jmp    1184 <chain_command+0x74>
mov    -0x38(%rbp),%rsi
mov    -0xc(%rbp),%eax
sub    -0x8(%rbp),%eax
cltq
movq   $0x0,(%rsi,%rax,8)
mov    (%rsi),%rdi
call   1030 <execvp@plt>
mov    %eax,-0x4(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<chain_command>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %esi,%eax
sub    %edi,%eax
jle    118d <chain_command+0x6d>
mov    %rsp,%r14
mov    %rsp,%r15
mov    %eax,%r12d
lea    0x17(,%r12,8),%rbx
and    $0xfffffffffffffff0,%rbx
mov    %r15,%r13
sub    %rbx,%r13
mov    %r13,%rsp
neg    %rbx
movslq %edi,%rax
lea    (%rdx,%rax,8),%rax
not    %edi
add    %esi,%edi
lea    0x8(,%rdi,8),%rdx
mov    %r13,%rdi
mov    %rax,%rsi
call   1030 <memcpy@plt>
mov    (%r15,%rbx,1),%rdi
movq   $0x0,0x0(%r13,%r12,8)
mov    %r13,%rsi
call   1040 <execvp@plt>
mov    %r14,%rsp
jmp    11a4 <chain_command+0x84>
mov    0x2e44(%rip),%rax
mov    (%rax),%eax
mov    0x2e43(%rip),%rcx
mov    %eax,(%rcx)
mov    $0xffffffff,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @chain_command(i32 %0, i32 %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %5
store i32 %1, i32* %6
store i8** %2, i8*** %7
%11 = load i32, i32* %6
%12 = load i32, i32* %5
%13 = sub nsw i32 %11, %12
%14 = icmp sle i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %3
%16 = load i32, i32* @EINVAL
store i32 %16, i32* @errno
store i32 -1, i32* %4
br label %54
17: ; preds = %3
%18 = load i32, i32* %6
%19 = load i32, i32* %5
%20 = sub nsw i32 %18, %19
%21 = add nsw i32 %20, 1
%22 = zext i32 %21 to i64
%23 = call i8* @llvm.stacksave()
store i8* %23, i8** %8
%24 = alloca i8*, i64 %22
store i64 %22, i64* %9
%25 = load i32, i32* %5
store i32 %25, i32* %10
br label %26
26: ; preds = %41, %17
%27 = load i32, i32* %10
%28 = load i32, i32* %6
%29 = icmp slt i32 %27, %28
br i1 %29, label %30, label %44
30: ; preds = %26
%31 = load i8**, i8*** %7
%32 = load i32, i32* %10
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %31, i64 %33
%35 = load i8*, i8** %34
%36 = load i32, i32* %10
%37 = load i32, i32* %5
%38 = sub nsw i32 %36, %37
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8*, i8** %24, i64 %39
store i8* %35, i8** %40
br label %41
41: ; preds = %30
%42 = load i32, i32* %10
%43 = add nsw i32 %42, 1
store i32 %43, i32* %10
br label %26
44: ; preds = %26
%45 = load i32, i32* %6
%46 = load i32, i32* %5
%47 = sub nsw i32 %45, %46
%48 = sext i32 %47 to i64
%49 = getelementptr inbounds i8*, i8** %24, i64 %48
store i8* null, i8** %49
%50 = getelementptr inbounds i8*, i8** %24, i64 0
%51 = load i8*, i8** %50
%52 = call i32 @execvp(i8* %51, i8** %24)
store i32 %52, i32* %4
%53 = load i8*, i8** %8
br label %54
54: ; preds = %44, %15
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@EINVAL = global i32 0
@errno = global i32 0
define i32 @chain_command(i32 %0, i32 %1, i8** nocapture %2) {
%4 = sub nsw i32 %1, %0
%5 = icmp slt i32 %4, 1
br i1 %5, label %6, label %8
6: ; preds = %3
%7 = load i32, i32* @EINVAL
store i32 %7, i32* @errno
br label %27
8: ; preds = %3
%9 = add nuw nsw i32 %4, 1
%10 = zext i32 %9 to i64
%11 = tail call i8* @llvm.stacksave()
%12 = alloca i8*, i64 %10
%13 = icmp sgt i32 %1, %0
%14 = bitcast i8** %12 to i8*
%15 = sext i32 %0 to i64
%16 = getelementptr i8*, i8** %2, i64 %15
%17 = bitcast i8** %16 to i8*
%18 = xor i32 %0, -1
%19 = add i32 %18, %1
%20 = zext i32 %19 to i64
%21 = shl nuw nsw i64 %20, 3
%22 = add nuw nsw i64 %21, 8
%23 = load i8*, i8** %12
%24 = zext i32 %4 to i64
%25 = getelementptr inbounds i8*, i8** %12, i64 %24
store i8* null, i8** %25
%26 = call i32 @execvp(i8* %23, i8** %12)
br label %27
27: ; preds = %8, %6
%28 = phi i32 [ -1, %6 ], [ %26, %8 ]
ret i32 %28
}