#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 O_APPEND;
int O_CREAT;
int O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ execfilepath(int, char *, char **, char *);
int posstrinarray(char *, char **, int);
int /*<<< orphan*/ redirfd(int, char *, int, int);
void child(int argslen, char *cmd, char **args, char *filepath) {
  int pos, cmdlen;
  cmdlen = argslen;
  if ((pos = posstrinarray("==>", args, argslen)) != -1) {
    cmdlen = pos;
    redirfd(1, args[pos + 1], O_WRONLY | O_CREAT | O_TRUNC, 0664);
  } else if ((pos = posstrinarray("<==", args, argslen)) != -1) {
    cmdlen = pos;
    redirfd(0, args[pos + 1], O_RDONLY, -1);
  } else if ((pos = posstrinarray("-->", args, argslen)) != -1) {
    cmdlen = pos;
    redirfd(1, args[pos + 1], O_WRONLY | O_CREAT | O_APPEND, 0664);
  }
  execfilepath(cmdlen, cmd, args, filepath);
}


x86 assembly (clang, -O0):
<child>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    %rcx,-0x20(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xea5(%rip),%rdi
call   1050 <posstrinarray@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     11b2 <child+0x82>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
mov    0x2e36(%rip),%rax
mov    (%rax),%edx
mov    0x2e65(%rip),%rax
or     (%rax),%edx
mov    0x2e54(%rip),%rax
or     (%rax),%edx
mov    $0x1,%edi
mov    $0x1b4,%ecx
call   1030 <redirfd@plt>
jmp    126c <child+0x13c>
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xe44(%rip),%rdi
call   1050 <posstrinarray@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1202 <child+0xd2>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
mov    0x2de9(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
mov    $0xffffffff,%ecx
call   1030 <redirfd@plt>
jmp    1267 <child+0x137>
mov    -0x18(%rbp),%rsi
mov    -0x4(%rbp),%edx
lea    0xdf8(%rip),%rdi
call   1050 <posstrinarray@plt>
mov    %eax,-0x24(%rbp)
cmp    $0xffffffff,%eax
je     1262 <child+0x132>
mov    -0x24(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%rax
mov    -0x24(%rbp),%ecx
add    $0x1,%ecx
movslq %ecx,%rcx
mov    (%rax,%rcx,8),%rsi
mov    0x2d81(%rip),%rax
mov    (%rax),%edx
mov    0x2db0(%rip),%rax
or     (%rax),%edx
mov    0x2d67(%rip),%rax
or     (%rax),%edx
mov    $0x1,%edi
mov    $0x1b4,%ecx
call   1030 <redirfd@plt>
jmp    1267 <child+0x137>
jmp    126c <child+0x13c>
mov    -0x28(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    -0x18(%rbp),%rdx
mov    -0x20(%rbp),%rcx
call   1040 <execfilepath@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<child>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    %rcx,%r14
mov    %rdx,%r13
mov    %rsi,%r15
mov    %edi,%ebp
lea    0xeb3(%rip),%rdi
mov    %rdx,%rsi
mov    %ebp,%edx
call   1050 <posstrinarray@plt>
cmp    $0xffffffff,%eax
je     118c <child+0x5c>
mov    %eax,%ebx
cltq
mov    0x2e59(%rip),%rcx
mov    0x2e8a(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x8(%r13,%rax,8),%rsi
mov    0x2e72(%rip),%rax
or     (%rax),%edx
mov    $0x1,%edi
mov    $0x1b4,%ecx
jmp    11bb <child+0x8b>
lea    0xe71(%rip),%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1050 <posstrinarray@plt>
cmp    $0xffffffff,%eax
je     11e0 <child+0xb0>
mov    %eax,%ebx
cltq
mov    0x8(%r13,%rax,8),%rsi
mov    0x2e26(%rip),%rax
mov    (%rax),%edx
xor    %edi,%edi
mov    $0xffffffff,%ecx
call   1030 <redirfd@plt>
mov    %ebx,%ebp
mov    %ebp,%edi
mov    %r15,%rsi
mov    %r13,%rdx
mov    %r14,%rcx
add    $0x8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <execfilepath@plt>
lea    0xe21(%rip),%rdi
mov    %r13,%rsi
mov    %ebp,%edx
call   1050 <posstrinarray@plt>
cmp    $0xffffffff,%eax
je     11c2 <child+0x92>
mov    %eax,%r12d
cltq
mov    0x2dbe(%rip),%rcx
mov    0x2def(%rip),%rdx
mov    (%rdx),%edx
or     (%rcx),%edx
mov    0x8(%r13,%rax,8),%rsi
mov    0x2d9f(%rip),%rax
or     (%rax),%edx
mov    $0x1,%edi
mov    $0x1b4,%ecx
call   1030 <redirfd@plt>
mov    %r12d,%ebp
jmp    11c2 <child+0x92>

LLVM IR (-O0):
@.str = [4 x i8] c"==>\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [4 x i8] c"<==\00"
@O_RDONLY = global i32 0
@.str.2 = [4 x i8] c"-->\00"
@O_APPEND = global i32 0
define void @child(i32 %0, i8* %1, i8** %2, i8* %3) {
%5 = alloca i32
%6 = alloca i8*
%7 = alloca i8**
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
store i32 %0, i32* %5
store i8* %1, i8** %6
store i8** %2, i8*** %7
store i8* %3, i8** %8
%11 = load i32, i32* %5
store i32 %11, i32* %10
%12 = load i8**, i8*** %7
%13 = load i32, i32* %5
%14 = call i32 @posstrinarray(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %12, i32 %13)
store i32 %14, i32* %9
%15 = icmp ne i32 %14, -1
br i1 %15, label %16, label %30
16: ; preds = %4
%17 = load i32, i32* %9
store i32 %17, i32* %10
%18 = load i8**, i8*** %7
%19 = load i32, i32* %9
%20 = add nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8*, i8** %18, i64 %21
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_WRONLY
%25 = load i32, i32* @O_CREAT
%26 = or i32 %24, %25
%27 = load i32, i32* @O_TRUNC
%28 = or i32 %26, %27
%29 = call i32 @redirfd(i32 1, i8* %23, i32 %28, i32 436)
br label %66
30: ; preds = %4
%31 = load i8**, i8*** %7
%32 = load i32, i32* %5
%33 = call i32 @posstrinarray(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %31, i32 %32)
store i32 %33, i32* %9
%34 = icmp ne i32 %33, -1
br i1 %34, label %35, label %45
35: ; preds = %30
%36 = load i32, i32* %9
store i32 %36, i32* %10
%37 = load i8**, i8*** %7
%38 = load i32, i32* %9
%39 = add nsw i32 %38, 1
%40 = sext i32 %39 to i64
%41 = getelementptr inbounds i8*, i8** %37, i64 %40
%42 = load i8*, i8** %41
%43 = load i32, i32* @O_RDONLY
%44 = call i32 @redirfd(i32 0, i8* %42, i32 %43, i32 -1)
br label %65
45: ; preds = %30
%46 = load i8**, i8*** %7
%47 = load i32, i32* %5
%48 = call i32 @posstrinarray(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %46, i32 %47)
store i32 %48, i32* %9
%49 = icmp ne i32 %48, -1
br i1 %49, label %50, label %64
50: ; preds = %45
%51 = load i32, i32* %9
store i32 %51, i32* %10
%52 = load i8**, i8*** %7
%53 = load i32, i32* %9
%54 = add nsw i32 %53, 1
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds i8*, i8** %52, i64 %55
%57 = load i8*, i8** %56
%58 = load i32, i32* @O_WRONLY
%59 = load i32, i32* @O_CREAT
%60 = or i32 %58, %59
%61 = load i32, i32* @O_APPEND
%62 = or i32 %60, %61
%63 = call i32 @redirfd(i32 1, i8* %57, i32 %62, i32 436)
br label %64
64: ; preds = %50, %45
br label %65
65: ; preds = %64, %35
br label %66
66: ; preds = %65, %16
%67 = load i32, i32* %10
%68 = load i8*, i8** %6
%69 = load i8**, i8*** %7
%70 = load i8*, i8** %8
%71 = call i32 @execfilepath(i32 %67, i8* %68, i8** %69, i8* %70)
ret void
}

LLVM IR (-O2):
@.str = [4 x i8] c"==>\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [4 x i8] c"<==\00"
@O_RDONLY = global i32 0
@.str.2 = [4 x i8] c"-->\00"
@O_APPEND = global i32 0
define void @child(i32 %0, i8* %1, i8** %2, i8* %3) {
%5 = tail call i32 @posstrinarray(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8** %2, i32 %0)
%6 = icmp eq i32 %5, -1
br i1 %6, label %18, label %7
7: ; preds = %4
%8 = add nuw nsw i32 %5, 1
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds i8*, i8** %2, i64 %9
%11 = load i8*, i8** %10
%12 = load i32, i32* @O_WRONLY
%13 = load i32, i32* @O_CREAT
%14 = or i32 %13, %12
%15 = load i32, i32* @O_TRUNC
%16 = or i32 %14, %15
%17 = tail call i32 @redirfd(i32 1, i8* %11, i32 %16, i32 436)
br label %42
18: ; preds = %4
%19 = tail call i32 @posstrinarray(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8** %2, i32 %0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %28, label %21
21: ; preds = %18
%22 = add nuw nsw i32 %19, 1
%23 = sext i32 %22 to i64
%24 = getelementptr inbounds i8*, i8** %2, i64 %23
%25 = load i8*, i8** %24
%26 = load i32, i32* @O_RDONLY
%27 = tail call i32 @redirfd(i32 0, i8* %25, i32 %26, i32 -1)
br label %42
28: ; preds = %18
%29 = tail call i32 @posstrinarray(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i8** %2, i32 %0)
%30 = icmp eq i32 %29, -1
br i1 %30, label %42, label %31
31: ; preds = %28
%32 = add nuw nsw i32 %29, 1
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds i8*, i8** %2, i64 %33
%35 = load i8*, i8** %34
%36 = load i32, i32* @O_WRONLY
%37 = load i32, i32* @O_CREAT
%38 = or i32 %37, %36
%39 = load i32, i32* @O_APPEND
%40 = or i32 %38, %39
%41 = tail call i32 @redirfd(i32 1, i8* %35, i32 %40, i32 436)
br label %42
42: ; preds = %21, %31, %28, %7
%43 = phi i32 [ %5, %7 ], [ %19, %21 ], [ %29, %31 ], [ %0, %28 ]
%44 = tail call i32 @execfilepath(i32 %43, i8* %1, i8** %2, i8* %3)
ret void
}