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

/* Variables and functions */
int execve(char *, char **, int /*<<< orphan*/ *);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ free(char *);
scalar_t__ malloc(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int strlen(char *);
int kattach_execbk(char *cmd, char *kargv) {
  int fpid;
  char *pargv[128];
  char *buf;
  char c = '\0';
  int i = 0, x = 0, n = 1;
  fpid = fork();
  if (fpid < 0) {
    printf("\n\n*** DEBUG *** Fork failed %s %s\n\n", cmd, kargv);
    return 0;
  }
  if (fpid == 0) {
    pargv[0] = cmd;
    buf = (char *)malloc(strlen(kargv) + 1);
    for (i = 0; i <= strlen(kargv); i++) {
      c = kargv[i];
      if ((c != ' ') && (c != '\0')) {
        buf[x] = c;
        x++;
      } else {
        if (x == 0)
          continue;
        buf[x] = '\0';
        pargv[n] = (char *)malloc(strlen(buf) + 1);
        sprintf(pargv[n], "%s", buf);
        n++;
        x = 0;
        memset(buf, 0, sizeof(buf));
      }
    }
    free(buf);
    pargv[n] = NULL;
    x = execve(cmd, pargv, NULL);
    printf("\n\n*** DEBUG *** Exec failed %s %s\n\n", cmd, kargv);
    for (i = 0; i <= n; i++) {
      printf(" arg %u is %s\n", i, pargv[i]);
    }
    exit(1);
  }
  return fpid;
}


x86 assembly (clang, -O0):
<kattach_execbk>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
movb   $0x0,-0x429(%rbp)
movl   $0x0,-0x430(%rbp)
movl   $0x0,-0x434(%rbp)
movl   $0x1,-0x438(%rbp)
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11fe <kattach_execbk+0x6e>
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x4(%rbp)
jmp    1420 <kattach_execbk+0x290>
cmpl   $0x0,-0x1c(%rbp)
jne    141a <kattach_execbk+0x28a>
mov    -0x10(%rbp),%rax
mov    %rax,-0x420(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,-0x428(%rbp)
movl   $0x0,-0x430(%rbp)
mov    -0x430(%rbp),%eax
mov    %eax,-0x43c(%rbp)
mov    -0x18(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%ecx
mov    -0x43c(%rbp),%eax
cmp    %ecx,%eax
jg     136e <kattach_execbk+0x1de>
mov    -0x18(%rbp),%rax
movslq -0x430(%rbp),%rcx
mov    (%rax,%rcx,1),%al
mov    %al,-0x429(%rbp)
movsbl -0x429(%rbp),%eax
cmp    $0x20,%eax
je     12bb <kattach_execbk+0x12b>
movsbl -0x429(%rbp),%eax
cmp    $0x0,%eax
je     12bb <kattach_execbk+0x12b>
mov    -0x429(%rbp),%dl
mov    -0x428(%rbp),%rax
movslq -0x434(%rbp),%rcx
mov    %dl,(%rax,%rcx,1)
mov    -0x434(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x434(%rbp)
jmp    1355 <kattach_execbk+0x1c5>
cmpl   $0x0,-0x434(%rbp)
jne    12cd <kattach_execbk+0x13d>
jmp    135a <kattach_execbk+0x1ca>
mov    -0x428(%rbp),%rax
movslq -0x434(%rbp),%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x428(%rbp),%rdi
call   1040 <strlen@plt>
mov    %eax,%edi
add    $0x1,%edi
call   1080 <malloc@plt>
mov    %rax,%rcx
movslq -0x438(%rbp),%rax
mov    %rcx,-0x420(%rbp,%rax,8)
ff
movslq -0x438(%rbp),%rax
mov    -0x420(%rbp,%rax,8),%rdi
ff
mov    -0x428(%rbp),%rdx
lea    0xd00(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x438(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x438(%rbp)
movl   $0x0,-0x434(%rbp)
mov    -0x428(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1060 <memset@plt>
jmp    135a <kattach_execbk+0x1ca>
mov    -0x430(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x430(%rbp)
jmp    1237 <kattach_execbk+0xa7>
mov    -0x428(%rbp),%rdi
call   1030 <free@plt>
movslq -0x438(%rbp),%rax
movq   $0x0,-0x420(%rbp,%rax,8)
mov    -0x10(%rbp),%rdi
lea    -0x420(%rbp),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1070 <execve@plt>
mov    %eax,-0x434(%rbp)
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
lea    0xc71(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x0,-0x430(%rbp)
mov    -0x430(%rbp),%eax
cmp    -0x438(%rbp),%eax
jg     1410 <kattach_execbk+0x280>
mov    -0x430(%rbp),%esi
movslq -0x430(%rbp),%rax
mov    -0x420(%rbp,%rax,8),%rdx
ff
lea    0xc56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x430(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x430(%rbp)
jmp    13c7 <kattach_execbk+0x237>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<kattach_execbk>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
mov    %rsi,%r13
mov    %rdi,%r14
xor    %ebp,%ebp
xor    %eax,%eax
call   10b0 <fork@plt>
test   %eax,%eax
js     1263 <kattach_execbk+0xd3>
mov    %eax,%ebp
jne    1277 <kattach_execbk+0xe7>
mov    %r14,(%rsp)
mov    %r13,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
mov    %rax,%r15
mov    %r13,%rdi
call   1050 <strlen@plt>
mov    $0x1,%r12d
test   %eax,%eax
jns    128b <kattach_execbk+0xfb>
mov    %r15,%rdi
call   1030 <free@plt>
movslq %r12d,%rbx
movq   $0x0,(%rsp,%rbx,8)
mov    %rsp,%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   1080 <execve@plt>
lea    0xe10(%rip),%rdi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
test   %ebx,%ebx
js     1259 <kattach_execbk+0xc9>
add    $0x1,%r12d
lea    0xe18(%rip),%rbx
xor    %ebp,%ebp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rsp,%rbp,8),%rdx
mov    %rbx,%rdi
mov    %ebp,%esi
xor    %eax,%eax
call   1060 <printf@plt>
add    $0x1,%rbp
cmp    %rbp,%r12
jne    1240 <kattach_execbk+0xb0>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xd96(%rip),%rdi
mov    %r14,%rsi
mov    %r13,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %ebp,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
mov    $0x1,%r12d
mov    $0xffffffffffffffff,%rbp
jmp    12c0 <kattach_execbk+0x130>
movslq %eax,%rbx
mov    %cl,(%r15,%rbx,1)
add    $0x1,%ebx
mov    %r13,%rdi
call   1050 <strlen@plt>
movslq %eax,%rcx
add    $0x1,%rbp
mov    %ebx,%eax
cmp    %rcx,%rbp
jge    11ed <kattach_execbk+0x5d>
movzbl 0x1(%r13,%rbp,1),%ecx
test   $0xdf,%cl
jne    129c <kattach_execbk+0x10c>
xor    %ebx,%ebx
test   %eax,%eax
je     12a6 <kattach_execbk+0x116>
cltq
movb   $0x0,(%r15,%rax,1)
mov    %r15,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%edi
call   1090 <malloc@plt>
movslq %r12d,%r12
mov    %rax,(%rsp,%r12,8)
mov    %rax,%rdi
mov    %r15,%rsi
call   1040 <strcpy@plt>
add    $0x1,%r12d
xor    %ebx,%ebx
mov    %r15,%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1070 <memset@plt>
jmp    12a6 <kattach_execbk+0x116>

LLVM IR (-O0):
@.str = [36 x i8] c"\0A\0A*** DEBUG *** Fork failed %s %s\0A\0A\00"
@.str.1 = [3 x i8] c"%s\00"
@.str.2 = [36 x i8] c"\0A\0A*** DEBUG *** Exec failed %s %s\0A\0A\00"
@.str.3 = [15 x i8] c" arg %u is %s\0A\00"
define i32 @kattach_execbk(i8* %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca [128 x i8*]
%8 = alloca i8*
%9 = alloca i8
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
store i8* %0, i8** %4
store i8* %1, i8** %5
store i8 0, i8* %9
store i32 0, i32* %10
store i32 0, i32* %11
store i32 1, i32* %12
%13 = call i32 (...) @fork()
store i32 %13, i32* %6
%14 = load i32, i32* %6
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %20
16: ; preds = %2
%17 = load i8*, i8** %4
%18 = load i8*, i8** %5
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %18)
store i32 0, i32* %3
br label %118
20: ; preds = %2
%21 = load i32, i32* %6
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %116
23: ; preds = %20
%24 = load i8*, i8** %4
%25 = getelementptr inbounds [128 x i8*], [128 x i8*]* %7, i64 0, i64 0
store i8* %24, i8** %25
%26 = load i8*, i8** %5
%27 = call i32 @strlen(i8* %26)
%28 = add nsw i32 %27, 1
%29 = call i64 @malloc(i32 %28)
%30 = inttoptr i64 %29 to i8*
store i8* %30, i8** %8
store i32 0, i32* %10
br label %31
31: ; preds = %85, %23
%32 = load i32, i32* %10
%33 = load i8*, i8** %5
%34 = call i32 @strlen(i8* %33)
%35 = icmp sle i32 %32, %34
br i1 %35, label %36, label %88
36: ; preds = %31
%37 = load i8*, i8** %5
%38 = load i32, i32* %10
%39 = sext i32 %38 to i64
%40 = getelementptr inbounds i8, i8* %37, i64 %39
%41 = load i8, i8* %40
store i8 %41, i8* %9
%42 = load i8, i8* %9
%43 = sext i8 %42 to i32
%44 = icmp ne i32 %43, 32
br i1 %44, label %45, label %57
45: ; preds = %36
%46 = load i8, i8* %9
%47 = sext i8 %46 to i32
%48 = icmp ne i32 %47, 0
br i1 %48, label %49, label %57
49: ; preds = %45
%50 = load i8, i8* %9
%51 = load i8*, i8** %8
%52 = load i32, i32* %11
%53 = sext i32 %52 to i64
%54 = getelementptr inbounds i8, i8* %51, i64 %53
store i8 %50, i8* %54
%55 = load i32, i32* %11
%56 = add nsw i32 %55, 1
store i32 %56, i32* %11
br label %84
57: ; preds = %45, %36
%58 = load i32, i32* %11
%59 = icmp eq i32 %58, 0
br i1 %59, label %60, label %61
60: ; preds = %57
br label %85
61: ; preds = %57
%62 = load i8*, i8** %8
%63 = load i32, i32* %11
%64 = sext i32 %63 to i64
%65 = getelementptr inbounds i8, i8* %62, i64 %64
store i8 0, i8* %65
%66 = load i8*, i8** %8
%67 = call i32 @strlen(i8* %66)
%68 = add nsw i32 %67, 1
%69 = call i64 @malloc(i32 %68)
%70 = inttoptr i64 %69 to i8*
%71 = load i32, i32* %12
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [128 x i8*], [128 x i8*]* %7, i64 0, i64 %72
store i8* %70, i8** %73
%74 = load i32, i32* %12
%75 = sext i32 %74 to i64
%76 = getelementptr inbounds [128 x i8*], [128 x i8*]* %7, i64 0, i64 %75
%77 = load i8*, i8** %76
%78 = load i8*, i8** %8
%79 = call i32 @sprintf(i8* %77, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.1, i64 0, i64 0), i8* %78)
%80 = load i32, i32* %12
%81 = add nsw i32 %80, 1
store i32 %81, i32* %12
store i32 0, i32* %11
%82 = load i8*, i8** %8
%83 = call i32 @memset(i8* %82, i32 0, i32 8)
br label %84
84: ; preds = %61, %49
br label %85
85: ; preds = %84, %60
%86 = load i32, i32* %10
%87 = add nsw i32 %86, 1
store i32 %87, i32* %10
br label %31
88: ; preds = %31
%89 = load i8*, i8** %8
%90 = call i32 @free(i8* %89)
%91 = load i32, i32* %12
%92 = sext i32 %91 to i64
%93 = getelementptr inbounds [128 x i8*], [128 x i8*]* %7, i64 0, i64 %92
store i8* null, i8** %93
%94 = load i8*, i8** %4
%95 = getelementptr inbounds [128 x i8*], [128 x i8*]* %7, i64 0, i64 0
%96 = call i32 @execve(i8* %94, i8** %95, i32* null)
store i32 %96, i32* %11
%97 = load i8*, i8** %4
%98 = load i8*, i8** %5
%99 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %97, i8* %98)
store i32 0, i32* %10
br label %100
100: ; preds = %111, %88
%101 = load i32, i32* %10
%102 = load i32, i32* %12
%103 = icmp sle i32 %101, %102
br i1 %103, label %104, label %114
104: ; preds = %100
%105 = load i32, i32* %10
%106 = load i32, i32* %10
%107 = sext i32 %106 to i64
%108 = getelementptr inbounds [128 x i8*], [128 x i8*]* %7, i64 0, i64 %107
%109 = load i8*, i8** %108
%110 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %105, i8* %109)
br label %111
111: ; preds = %104
%112 = load i32, i32* %10
%113 = add nsw i32 %112, 1
store i32 %113, i32* %10
br label %100
114: ; preds = %100
%115 = call i32 @exit(i32 1)
unreachable
116: ; preds = %20
%117 = load i32, i32* %6
store i32 %117, i32* %3
br label %118
118: ; preds = %116, %16
%119 = load i32, i32* %3
ret i32 %119
}

LLVM IR (-O2):
@.str = [36 x i8] c"\0A\0A*** DEBUG *** Fork failed %s %s\0A\0A\00"
@.str.2 = [36 x i8] c"\0A\0A*** DEBUG *** Exec failed %s %s\0A\0A\00"
@.str.3 = [15 x i8] c" arg %u is %s\0A\00"
define i32 @kattach_execbk(i8* %0, i8* %1) {
%3 = alloca [128 x i8*]
%4 = bitcast [128 x i8*]* %3 to i8*
%5 = tail call i32 (...) @fork()
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %9
7: ; preds = %2
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %1)
br label %73
9: ; preds = %2
%10 = icmp eq i32 %5, 0
br i1 %10, label %11, label %73
11: ; preds = %9
%12 = getelementptr inbounds [128 x i8*], [128 x i8*]* %3, i64 0, i64 0
store i8* %0, i8** %12
%13 = tail call i32 @strlen(i8* %1)
%14 = add nsw i32 %13, 1
%15 = tail call i64 @malloc(i32 %14)
%16 = inttoptr i64 %15 to i8*
%17 = tail call i32 @strlen(i8* %1)
%18 = icmp slt i32 %17, 0
br i1 %18, label %52, label %19
19: ; preds = %11, %45
%20 = phi i64 [ %48, %45 ], [ 0, %11 ]
%21 = phi i32 [ %47, %45 ], [ 1, %11 ]
%22 = phi i32 [ %46, %45 ], [ 0, %11 ]
%23 = getelementptr inbounds i8, i8* %1, i64 %20
%24 = load i8, i8* %23
%25 = and i8 %24, -33
%26 = icmp eq i8 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %19
%28 = sext i32 %22 to i64
%29 = getelementptr inbounds i8, i8* %16, i64 %28
store i8 %24, i8* %29
%30 = add nsw i32 %22, 1
br label %45
31: ; preds = %19
%32 = icmp eq i32 %22, 0
br i1 %32, label %45, label %33
33: ; preds = %31
%34 = sext i32 %22 to i64
%35 = getelementptr inbounds i8, i8* %16, i64 %34
store i8 0, i8* %35
%36 = tail call i32 @strlen(i8* %16)
%37 = add nsw i32 %36, 1
%38 = tail call i64 @malloc(i32 %37)
%39 = inttoptr i64 %38 to i8*
%40 = sext i32 %21 to i64
%41 = getelementptr inbounds [128 x i8*], [128 x i8*]* %3, i64 0, i64 %40
store i8* %39, i8** %41
%42 = tail call i8* @strcpy(i8* dereferenceable(1) %39, i8* dereferenceable(1) %16)
%43 = add nsw i32 %21, 1
%44 = tail call i32 @memset(i8* %16, i32 0, i32 8)
br label %45
45: ; preds = %27, %33, %31
%46 = phi i32 [ %30, %27 ], [ 0, %31 ], [ 0, %33 ]
%47 = phi i32 [ %21, %27 ], [ %21, %31 ], [ %43, %33 ]
%48 = add nuw nsw i64 %20, 1
%49 = tail call i32 @strlen(i8* %1)
%50 = sext i32 %49 to i64
%51 = icmp slt i64 %20, %50
br i1 %51, label %19, label %52
52: ; preds = %45, %11
%53 = phi i32 [ 1, %11 ], [ %47, %45 ]
%54 = tail call i32 @free(i8* %16)
%55 = sext i32 %53 to i64
%56 = getelementptr inbounds [128 x i8*], [128 x i8*]* %3, i64 0, i64 %55
store i8* null, i8** %56
%57 = call i32 @execve(i8* %0, i8** %12, i32* null)
%58 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %1)
%59 = icmp slt i32 %53, 0
br i1 %59, label %71, label %60
60: ; preds = %52
%61 = add nuw i32 %53, 1
%62 = zext i32 %61 to i64
br label %63
63: ; preds = %60, %63
%64 = phi i64 [ 0, %60 ], [ %69, %63 ]
%65 = getelementptr inbounds [128 x i8*], [128 x i8*]* %3, i64 0, i64 %64
%66 = load i8*, i8** %65
%67 = trunc i64 %64 to i32
%68 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0), i32 %67, i8* %66)
%69 = add nuw nsw i64 %64, 1
%70 = icmp eq i64 %69, %62
br i1 %70, label %71, label %63
71: ; preds = %63, %52
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %9, %7
%74 = phi i32 [ 0, %7 ], [ %5, %9 ]
ret i32 %74
}