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

/* Variables and functions */
int EXIT_FAILURE;
int WEXITSTATUS(int);
int /*<<< orphan*/ WIFEXITED(int);
int /*<<< orphan*/ fputs(char const *, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char const *, char *);
int execute(const char *command, const char *inputs[]) {
  FILE *fp = popen(command, (inputs ? "w" : "r"));
  if (NULL == fp) {
    perror("popen");
    return EXIT_FAILURE;
  }
  if (inputs) {
    for (int i = 0; inputs[i]; ++i) {
      fputs(inputs[i], fp);
      fputs("\n", fp);
    }
  }
  int status = pclose(fp);
  status = ((status == -1 || !WIFEXITED(status)) ? EXIT_FAILURE
                                                 : WEXITSTATUS(status));
  return status;
}


x86 assembly (clang, -O0):
<execute>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%rcx
lea    0xe83(%rip),%rsi
lea    0xe7a(%rip),%rax
cmp    $0x0,%rcx
cmovne %rax,%rsi
call   1060 <popen@plt>
mov    %rax,-0x20(%rbp)
xor    %eax,%eax
cmp    -0x20(%rbp),%rax
jne    11c0 <execute+0x60>
lea    0xe5a(%rip),%rdi
call   1070 <perror@plt>
mov    0x2e2a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
jmp    126c <execute+0x10c>
cmpq   $0x0,-0x18(%rbp)
je     121d <execute+0xbd>
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
cmpq   $0x0,(%rax,%rcx,8)
je     1218 <execute+0xb8>
mov    -0x18(%rbp),%rax
movslq -0x24(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    -0x20(%rbp),%rsi
call   1040 <fputs@plt>
mov    -0x20(%rbp),%rsi
lea    0xe05(%rip),%rdi
call   1040 <fputs@plt>
mov    -0x24(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x24(%rbp)
jmp    11d2 <execute+0x72>
jmp    121d <execute+0xbd>
mov    -0x20(%rbp),%rdi
call   1030 <pclose@plt>
mov    %eax,-0x28(%rbp)
cmpl   $0xffffffff,-0x28(%rbp)
je     1244 <execute+0xe4>
mov    -0x28(%rbp),%edi
call   1080 <WIFEXITED@plt>
cmp    $0x0,%eax
jne    1255 <execute+0xf5>
mov    0x2d95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x2c(%rbp)
jmp    1260 <execute+0x100>
mov    -0x28(%rbp),%edi
call   1050 <WEXITSTATUS@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x2c(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x28(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execute>:
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
test   %rsi,%rsi
lea    0xe81(%rip),%rax
lea    0xe78(%rip),%rsi
cmove  %rax,%rsi
call   1070 <popen@plt>
test   %rax,%rax
je     11f9 <execute+0x89>
mov    %rax,%r14
test   %rbx,%rbx
je     11d1 <execute+0x61>
mov    (%rbx),%rdi
test   %rdi,%rdi
je     11d1 <execute+0x61>
add    $0x8,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rsi
call   1040 <fputs@plt>
mov    $0xa,%edi
mov    %r14,%rsi
call   1050 <fputc@plt>
mov    (%rbx),%rdi
add    $0x8,%rbx
test   %rdi,%rdi
jne    11b0 <execute+0x40>
mov    %r14,%rdi
call   1030 <pclose@plt>
cmp    $0xffffffff,%eax
je     1205 <execute+0x95>
mov    %eax,%ebx
mov    %eax,%edi
call   1090 <WIFEXITED@plt>
test   %eax,%eax
je     1205 <execute+0x95>
mov    %ebx,%edi
add    $0x8,%rsp
pop    %rbx
pop    %r14
jmp    1060 <WEXITSTATUS@plt>
lea    0xe04(%rip),%rdi
call   1080 <perror@plt>
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"popen\00"
@EXIT_FAILURE = global i32 0
@.str.3 = [2 x i8] c"\0A\00"
define i32 @execute(i8* %0, i8** %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32
%8 = alloca i32
store i8* %0, i8** %4
store i8** %1, i8*** %5
%9 = load i8*, i8** %4
%10 = load i8**, i8*** %5
%11 = icmp ne i8** %10, null
%12 = zext i1 %11 to i64
%13 = select i1 %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0)
%14 = call i32* @popen(i8* %9, i8* %13)
store i32* %14, i32** %6
%15 = load i32*, i32** %6
%16 = icmp eq i32* null, %15
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%19 = load i32, i32* @EXIT_FAILURE
store i32 %19, i32* %3
br label %62
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = icmp ne i8** %21, null
br i1 %22, label %23, label %45
23: ; preds = %20
store i32 0, i32* %7
br label %24
24: ; preds = %41, %23
%25 = load i8**, i8*** %5
%26 = load i32, i32* %7
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %25, i64 %27
%29 = load i8*, i8** %28
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %44
31: ; preds = %24
%32 = load i8**, i8*** %5
%33 = load i32, i32* %7
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = load i32*, i32** %6
%38 = call i32 @fputs(i8* %36, i32* %37)
%39 = load i32*, i32** %6
%40 = call i32 @fputs(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.3, i64 0, i64 0), i32* %39)
br label %41
41: ; preds = %31
%42 = load i32, i32* %7
%43 = add nsw i32 %42, 1
store i32 %43, i32* %7
br label %24
44: ; preds = %24
br label %45
45: ; preds = %44, %20
%46 = load i32*, i32** %6
%47 = call i32 @pclose(i32* %46)
store i32 %47, i32* %8
%48 = load i32, i32* %8
%49 = icmp eq i32 %48, -1
br i1 %49, label %54, label %50
50: ; preds = %45
%51 = load i32, i32* %8
%52 = call i32 @WIFEXITED(i32 %51)
%53 = icmp ne i32 %52, 0
br i1 %53, label %56, label %54
54: ; preds = %50, %45
%55 = load i32, i32* @EXIT_FAILURE
br label %59
56: ; preds = %50
%57 = load i32, i32* %8
%58 = call i32 @WEXITSTATUS(i32 %57)
br label %59
59: ; preds = %56, %54
%60 = phi i32 [ %55, %54 ], [ %58, %56 ]
store i32 %60, i32* %8
%61 = load i32, i32* %8
store i32 %61, i32* %3
br label %62
62: ; preds = %59, %17
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str = [2 x i8] c"w\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [6 x i8] c"popen\00"
@EXIT_FAILURE = global i32 0
define i32 @execute(i8* nocapture %0, i8** %1) {
%3 = icmp eq i8** %1, null
%4 = select i1 %3, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0)
%5 = tail call i32* @popen(i8* %0, i8* %4)
%6 = icmp eq i32* %5, null
br i1 %6, label %7, label %10
7: ; preds = %2
%8 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
%9 = load i32, i32* @EXIT_FAILURE
br label %33
10: ; preds = %2
br i1 %3, label %23, label %11
11: ; preds = %10
%12 = load i8*, i8** %1
%13 = icmp eq i8* %12, null
br i1 %13, label %23, label %14
14: ; preds = %11, %14
%15 = phi i64 [ %19, %14 ], [ 0, %11 ]
%16 = phi i8* [ %21, %14 ], [ %12, %11 ]
%17 = tail call i32 @fputs(i8* %16, i32* %5)
%18 = tail call i32 @fputc(i32 10, i32* %5)
%19 = add nuw i64 %15, 1
%20 = getelementptr inbounds i8*, i8** %1, i64 %19
%21 = load i8*, i8** %20
%22 = icmp eq i8* %21, null
br i1 %22, label %23, label %14
23: ; preds = %14, %11, %10
%24 = tail call i32 @pclose(i32* %5)
%25 = icmp eq i32 %24, -1
br i1 %25, label %29, label %26
26: ; preds = %23
%27 = tail call i32 @WIFEXITED(i32 %24)
%28 = icmp eq i32 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26, %23
%30 = load i32, i32* @EXIT_FAILURE
br label %33
31: ; preds = %26
%32 = tail call i32 @WEXITSTATUS(i32 %24)
br label %33
33: ; preds = %29, %31, %7
%34 = phi i32 [ %9, %7 ], [ %30, %29 ], [ %32, %31 ]
ret i32 %34
}