#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 exec_cmd(char **, int, int, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ free(char **);
char **malloc(int);
int /*<<< orphan*/ sprintf(char *, char *, char *, ...);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ tokenize(char **, int *, char const *, char *);
int exec_cmdline(char const *line) {
  char **cmds = malloc(((63) + 1) * sizeof(char *));
  char **args = malloc(((63) + 1) * sizeof(char *));
  char s[(1023) + 1];
  int failed_cmd = 0, i, j, no_cmds, no_args;
  tokenize(cmds, &no_cmds, line, "|");
  for (i = 0; i < no_cmds; i++) {
    tokenize(args, &no_args, cmds[i], " ");
    if (no_args == 0) {
      failed_cmd = i + 1;
      fprintf(stderr, "exec_cmdline: Empty command\n");
      break;
    }
    if (exec_cmd(args, no_args, i + 1, no_cmds) == -1) {
      failed_cmd = i + 1;
      sprintf(s, "%s", args[0]);
      for (j = 1; j < no_args; j++)
        sprintf(s, "%s %s", s, args[j]);
      fprintf(stderr, "exec_cmdline: Command '%s' failed\n", s);
      break;
    }
  }
  free(cmds);
  free(args);
  return failed_cmd;
}


x86 assembly (clang, -O0):
<exec_cmdline>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x8(%rbp)
mov    $0x200,%edi
call   1050 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0x200,%edi
call   1050 <malloc@plt>
mov    %rax,-0x18(%rbp)
movl   $0x0,-0x424(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    -0x430(%rbp),%rsi
lea    0xe55(%rip),%rcx
call   1070 <tokenize@plt>
movl   $0x0,-0x428(%rbp)
mov    -0x428(%rbp),%eax
cmp    -0x430(%rbp),%eax
jge    1313 <exec_cmdline+0x1b3>
mov    -0x18(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x428(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    -0x434(%rbp),%rsi
lea    0xe15(%rip),%rcx
call   1070 <tokenize@plt>
cmpl   $0x0,-0x434(%rbp)
jne    122a <exec_cmdline+0xca>
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
mov    0x2dcb(%rip),%rax
mov    (%rax),%edi
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1313 <exec_cmdline+0x1b3>
mov    -0x18(%rbp),%rdi
mov    -0x434(%rbp),%esi
mov    -0x428(%rbp),%edx
add    $0x1,%edx
mov    -0x430(%rbp),%ecx
call   1060 <exec_cmd@plt>
cmp    $0xffffffff,%eax
jne    12fa <exec_cmdline+0x19a>
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x424(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x18(%rbp),%rax
mov    (%rax),%rdx
lea    0xdaf(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
movl   $0x1,-0x42c(%rbp)
mov    -0x42c(%rbp),%eax
cmp    -0x434(%rbp),%eax
jge    12d7 <exec_cmdline+0x177>
lea    -0x420(%rbp),%rdi
lea    -0x420(%rbp),%rdx
mov    -0x18(%rbp),%rax
movslq -0x42c(%rbp),%rcx
mov    (%rax,%rcx,8),%rcx
lea    0xd65(%rip),%rsi
mov    $0x0,%al
call   1080 <sprintf@plt>
mov    -0x42c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x42c(%rbp)
jmp    1286 <exec_cmdline+0x126>
mov    0x2d02(%rip),%rax
mov    (%rax),%edi
lea    -0x420(%rbp),%rdx
lea    0xd39(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
jmp    1313 <exec_cmdline+0x1b3>
jmp    12ff <exec_cmdline+0x19f>
mov    -0x428(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x428(%rbp)
jmp    11ba <exec_cmdline+0x5a>
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
mov    -0x18(%rbp),%rdi
call   1030 <free@plt>
mov    -0x424(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_cmdline>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%rbx
mov    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,%r14
mov    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,%r15
lea    0xe5b(%rip),%rcx
lea    0xc(%rsp),%rsi
mov    %r14,%rdi
mov    %rbx,%rdx
call   1080 <tokenize@plt>
xor    %r12d,%r12d
lea    0xe43(%rip),%r13
lea    0x8(%rsp),%rbp
xor    %ebx,%ebx
cs nopw 0x0(%rax,%rax,1)
movslq 0xc(%rsp),%rax
cmp    %rax,%rbx
jge    129d <exec_cmdline+0x12d>
mov    (%r14,%rbx,8),%rdx
mov    %r15,%rdi
mov    %rbp,%rsi
mov    %r13,%rcx
call   1080 <tokenize@plt>
mov    0x8(%rsp),%esi
test   %esi,%esi
je     1280 <exec_cmdline+0x110>
add    $0x1,%rbx
mov    0xc(%rsp),%ecx
mov    %r15,%rdi
mov    %ebx,%edx
call   1070 <exec_cmd@plt>
cmp    $0xffffffff,%eax
jne    11d0 <exec_cmdline+0x60>
mov    (%r15),%rsi
lea    0x10(%rsp),%rdi
call   1040 <strcpy@plt>
cmpl   $0x2,0x8(%rsp)
jl     1262 <exec_cmdline+0xf2>
mov    $0x1,%ebp
lea    0xdee(%rip),%r12
lea    0x10(%rsp),%r13
nopl   0x0(%rax,%rax,1)
mov    (%r15,%rbp,8),%rcx
mov    %r13,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1090 <sprintf@plt>
add    $0x1,%rbp
movslq 0x8(%rsp),%rax
cmp    %rax,%rbp
jl     1240 <exec_cmdline+0xd0>
mov    0x2d77(%rip),%rax
mov    (%rax),%edi
lea    0xdb5(%rip),%rsi
lea    0x10(%rsp),%rdx
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    129a <exec_cmdline+0x12a>
add    $0x1,%ebx
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
lea    0xd71(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
mov    %ebx,%r12d
mov    %r14,%rdi
call   1030 <free@plt>
mov    %r15,%rdi
call   1030 <free@plt>
mov    %r12d,%eax
add    $0x418,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"|\00"
@.str.1 = [2 x i8] c" \00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"exec_cmdline: Empty command\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [6 x i8] c"%s %s\00"
@.str.5 = [35 x i8] c"exec_cmdline: Command '%s' failed\0A\00"
define i32 @exec_cmdline(i8* %0) {
%2 = alloca i8*
%3 = alloca i8**
%4 = alloca i8**
%5 = alloca [1024 x i8]
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i8* %0, i8** %2
%11 = call i8** @malloc(i32 512)
store i8** %11, i8*** %3
%12 = call i8** @malloc(i32 512)
store i8** %12, i8*** %4
store i32 0, i32* %6
%13 = load i8**, i8*** %3
%14 = load i8*, i8** %2
%15 = call i32 @tokenize(i8** %13, i32* %9, i8* %14, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %7
br label %16
16: ; preds = %72, %1
%17 = load i32, i32* %7
%18 = load i32, i32* %9
%19 = icmp slt i32 %17, %18
br i1 %19, label %20, label %75
20: ; preds = %16
%21 = load i8**, i8*** %4
%22 = load i8**, i8*** %3
%23 = load i32, i32* %7
%24 = sext i32 %23 to i64
%25 = getelementptr inbounds i8*, i8** %22, i64 %24
%26 = load i8*, i8** %25
%27 = call i32 @tokenize(i8** %21, i32* %10, i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%28 = load i32, i32* %10
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %35
30: ; preds = %20
%31 = load i32, i32* %7
%32 = add nsw i32 %31, 1
store i32 %32, i32* %6
%33 = load i32, i32* @stderr
%34 = call i32 (i32, i8*, ...) @fprintf(i32 %33, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %75
35: ; preds = %20
%36 = load i8**, i8*** %4
%37 = load i32, i32* %10
%38 = load i32, i32* %7
%39 = add nsw i32 %38, 1
%40 = load i32, i32* %9
%41 = call i32 @exec_cmd(i8** %36, i32 %37, i32 %39, i32 %40)
%42 = icmp eq i32 %41, -1
br i1 %42, label %43, label %71
43: ; preds = %35
%44 = load i32, i32* %7
%45 = add nsw i32 %44, 1
store i32 %45, i32* %6
%46 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%47 = load i8**, i8*** %4
%48 = getelementptr inbounds i8*, i8** %47, i64 0
%49 = load i8*, i8** %48
%50 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %46, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %49)
store i32 1, i32* %8
br label %51
51: ; preds = %64, %43
%52 = load i32, i32* %8
%53 = load i32, i32* %10
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %67
55: ; preds = %51
%56 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%57 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%58 = load i8**, i8*** %4
%59 = load i32, i32* %8
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds i8*, i8** %58, i64 %60
%62 = load i8*, i8** %61
%63 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %56, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %57, i8* %62)
br label %64
64: ; preds = %55
%65 = load i32, i32* %8
%66 = add nsw i32 %65, 1
store i32 %66, i32* %8
br label %51
67: ; preds = %51
%68 = load i32, i32* @stderr
%69 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%70 = call i32 (i32, i8*, ...) @fprintf(i32 %68, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %69)
br label %75
71: ; preds = %35
br label %72
72: ; preds = %71
%73 = load i32, i32* %7
%74 = add nsw i32 %73, 1
store i32 %74, i32* %7
br label %16
75: ; preds = %67, %30, %16
%76 = load i8**, i8*** %3
%77 = call i32 @free(i8** %76)
%78 = load i8**, i8*** %4
%79 = call i32 @free(i8** %78)
%80 = load i32, i32* %6
ret i32 %80
}

LLVM IR (-O2):
@.str = [2 x i8] c"|\00"
@.str.1 = [2 x i8] c" \00"
@stderr = global i32 0
@.str.2 = [29 x i8] c"exec_cmdline: Empty command\0A\00"
@.str.4 = [6 x i8] c"%s %s\00"
@.str.5 = [35 x i8] c"exec_cmdline: Command '%s' failed\0A\00"
define i32 @exec_cmdline(i8* %0) {
%2 = alloca [1024 x i8]
%3 = alloca i32
%4 = alloca i32
%5 = tail call i8** @malloc(i32 512)
%6 = tail call i8** @malloc(i32 512)
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%8 = bitcast i32* %3 to i8*
%9 = bitcast i32* %4 to i8*
%10 = call i32 @tokenize(i8** %5, i32* %3, i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %27, %1
%12 = phi i64 [ %28, %27 ], [ 0, %1 ]
%13 = load i32, i32* %3
%14 = sext i32 %13 to i64
%15 = icmp slt i64 %12, %14
br i1 %15, label %16, label %51
16: ; preds = %11
%17 = getelementptr inbounds i8*, i8** %5, i64 %12
%18 = load i8*, i8** %17
%19 = call i32 @tokenize(i8** %6, i32* %4, i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32, i32* %4
%21 = icmp eq i32 %20, 0
br i1 %21, label %22, label %27
22: ; preds = %16
%23 = trunc i64 %12 to i32
%24 = add nuw nsw i32 %23, 1
%25 = load i32, i32* @stderr
%26 = call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.2, i64 0, i64 0))
br label %51
27: ; preds = %16
%28 = add nuw nsw i64 %12, 1
%29 = load i32, i32* %3
%30 = trunc i64 %28 to i32
%31 = call i32 @exec_cmd(i8** %6, i32 %20, i32 %30, i32 %29)
%32 = icmp eq i32 %31, -1
br i1 %32, label %33, label %11
33: ; preds = %27
%34 = trunc i64 %28 to i32
%35 = load i8*, i8** %6
%36 = call i8* @strcpy(i8* %7, i8* dereferenceable(1) %35)
%37 = load i32, i32* %4
%38 = icmp sgt i32 %37, 1
br i1 %38, label %39, label %48
39: ; preds = %33, %39
%40 = phi i64 [ %44, %39 ], [ 1, %33 ]
%41 = getelementptr inbounds i8*, i8** %6, i64 %40
%42 = load i8*, i8** %41
%43 = call i32 (i8*, i8*, i8*, ...) @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %7, i8* %42)
%44 = add nuw nsw i64 %40, 1
%45 = load i32, i32* %4
%46 = sext i32 %45 to i64
%47 = icmp slt i64 %44, %46
br i1 %47, label %39, label %48
48: ; preds = %39, %33
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str.5, i64 0, i64 0), i8* %7)
br label %51
51: ; preds = %11, %48, %22
%52 = phi i32 [ %24, %22 ], [ %34, %48 ], [ 0, %11 ]
%53 = call i32 @free(i8** %5)
%54 = call i32 @free(i8** %6)
ret i32 %52
}