#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*/ path;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *, ...);
int getpid();
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int execCommandAndLog(char *c, int index) {
  FILE *fp;
  char path[1035];
  char filename[7];
  sprintf(filename, "data/%i/%s.%i", getpid(), "out", index);
  FILE *f = fopen(filename, "w");
  if (f == NULL) {
    printf("[Error] - Error opening file!\n");
    exit(1);
  }
  sprintf(c, "%s %s", c, "2>&1");
  fp = popen(c, "r");
  if (fp == NULL) {
    fprintf(f, "[Error] - Error executing the command\n");
  }
  while (fgets(path, sizeof(path) - 1, fp) != NULL) {
    fprintf(f, "%s", path);
  }
  pclose(fp);
  fclose(f);
  return 0;
}


x86 assembly (clang, -O0):
<execCommandAndLog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    -0x437(%rbp),%rax
mov    %rax,-0x448(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    -0x448(%rbp),%rdi
mov    %eax,%edx
mov    -0xc(%rbp),%r8d
lea    0xe25(%rip),%rsi
lea    0xe2c(%rip),%rcx
mov    $0x0,%al
call   10b0 <sprintf@plt>
lea    -0x437(%rbp),%rdi
lea    0xe1b(%rip),%rsi
call   10a0 <fopen@plt>
mov    %rax,-0x440(%rbp)
cmpq   $0x0,-0x440(%rbp)
jne    1227 <execCommandAndLog+0x87>
lea    0xdfc(%rip),%rdi
call   1050 <printf@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
mov    -0x8(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xdfd(%rip),%rsi
lea    0xdfc(%rip),%rcx
mov    $0x0,%al
call   10b0 <sprintf@plt>
mov    -0x8(%rbp),%rdi
lea    0xdef(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1278 <execCommandAndLog+0xd8>
mov    -0x440(%rbp),%rdi
lea    0xdcf(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    127d <execCommandAndLog+0xdd>
lea    -0x430(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x40a,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     12bd <execCommandAndLog+0x11d>
mov    -0x440(%rbp),%rdi
lea    -0x430(%rbp),%rdx
lea    0xd85(%rip),%rsi
mov    $0x0,%al
call   1080 <fprintf@plt>
jmp    127d <execCommandAndLog+0xdd>
mov    -0x18(%rbp),%rdi
call   1060 <pclose@plt>
mov    -0x440(%rbp),%rdi
call   1040 <fclose@plt>
xor    %eax,%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<execCommandAndLog>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x428,%rsp
mov    %esi,%ebp
mov    %rdi,%rbx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe30(%rip),%rsi
lea    0xe37(%rip),%rcx
lea    0x9(%rsp),%r14
mov    %r14,%rdi
mov    %eax,%edx
mov    %ebp,%r8d
xor    %eax,%eax
call   10b0 <sprintf@plt>
lea    0xe20(%rip),%rsi
mov    %r14,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
je     12b0 <execCommandAndLog+0x100>
mov    %rax,%r14
lea    0xe07(%rip),%rsi
lea    0xe06(%rip),%rcx
mov    %rbx,%rdi
mov    %rbx,%rdx
xor    %eax,%eax
call   10b0 <sprintf@plt>
lea    0xdf7(%rip),%rsi
mov    %rbx,%rdi
call   1090 <popen@plt>
mov    %rax,%r15
test   %rax,%rax
jne    1251 <execCommandAndLog+0xa1>
lea    0xde2(%rip),%rdi
mov    $0x26,%esi
mov    $0x1,%edx
mov    %r14,%rcx
call   10d0 <fwrite@plt>
lea    0x10(%rsp),%rdi
mov    $0x40a,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1290 <execCommandAndLog+0xe0>
lea    0x10(%rsp),%rbx
nopl   (%rax)
mov    %rbx,%rdi
mov    %r14,%rsi
call   1070 <fputs@plt>
mov    %rbx,%rdi
mov    $0x40a,%esi
mov    %r15,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1270 <execCommandAndLog+0xc0>
mov    %r15,%rdi
call   1060 <pclose@plt>
mov    %r14,%rdi
call   1050 <fclose@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd91(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [14 x i8] c"data/%i/%s.%i\00"
@.str.1 = [4 x i8] c"out\00"
@.str.2 = [2 x i8] c"w\00"
@.str.3 = [31 x i8] c"[Error] - Error opening file!\0A\00"
@.str.4 = [6 x i8] c"%s %s\00"
@.str.5 = [5 x i8] c"2>&1\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [39 x i8] c"[Error] - Error executing the command\0A\00"
@.str.8 = [3 x i8] c"%s\00"
define i32 @execCommandAndLog(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca i32*
%6 = alloca [1035 x i8]
%7 = alloca [7 x i8]
%8 = alloca i32*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = getelementptr inbounds [7 x i8], [7 x i8]* %7, i64 0, i64 0
%10 = call i32 (...) @getpid()
%11 = load i32, i32* %4
%12 = call i32 (i8*, i8*, ...) @sprintf(i8* %9, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = getelementptr inbounds [7 x i8], [7 x i8]* %7, i64 0, i64 0
%14 = call i32* @fopen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
store i32* %14, i32** %8
%15 = load i32*, i32** %8
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = call i32 @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.3, i64 0, i64 0))
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8*, i8** %3
%22 = load i8*, i8** %3
%23 = call i32 (i8*, i8*, ...) @sprintf(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %22, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%24 = load i8*, i8** %3
%25 = call i32* @popen(i8* %24, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %25, i32** %5
%26 = load i32*, i32** %5
%27 = icmp eq i32* %26, null
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = load i32*, i32** %8
%30 = call i32 (i32*, i8*, ...) @fprintf(i32* %29, i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0))
br label %31
31: ; preds = %28, %20
br label %32
32: ; preds = %37, %31
%33 = getelementptr inbounds [1035 x i8], [1035 x i8]* %6, i64 0, i64 0
%34 = load i32*, i32** %5
%35 = call i32* @fgets(i8* %33, i32 1034, i32* %34)
%36 = icmp ne i32* %35, null
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = load i32*, i32** %8
%39 = getelementptr inbounds [1035 x i8], [1035 x i8]* %6, i64 0, i64 0
%40 = call i32 (i32*, i8*, ...) @fprintf(i32* %38, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.8, i64 0, i64 0), i8* %39)
br label %32
41: ; preds = %32
%42 = load i32*, i32** %5
%43 = call i32 @pclose(i32* %42)
%44 = load i32*, i32** %8
%45 = call i32 @fclose(i32* %44)
ret i32 0
}

LLVM IR (-O2):
@.str = [14 x i8] c"data/%i/%s.%i\00"
@.str.1 = [4 x i8] c"out\00"
@.str.2 = [2 x i8] c"w\00"
@.str.4 = [6 x i8] c"%s %s\00"
@.str.5 = [5 x i8] c"2>&1\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [39 x i8] c"[Error] - Error executing the command\0A\00"
@str = [30 x i8] c"[Error] - Error opening file!\00"
define i32 @execCommandAndLog(i8* %0, i32 %1) {
%3 = alloca [1035 x i8]
%4 = alloca [7 x i8]
%5 = getelementptr inbounds [1035 x i8], [1035 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [7 x i8], [7 x i8]* %4, i64 0, i64 0
%7 = tail call i32 (...) @getpid()
%8 = call i32 (i8*, i8*, ...) @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0), i32 %7, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %1)
%9 = call i32* @fopen(i8* %6, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
%10 = icmp eq i32* %9, null
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([30 x i8], [30 x i8]* @str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = tail call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %0, i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.4, i64 0, i64 0), i8* %0, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%16 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %20
18: ; preds = %14
%19 = tail call i64 @fwrite(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.7, i64 0, i64 0), i64 38, i64 1, i32* %9)
br label %20
20: ; preds = %18, %14
%21 = call i32* @fgets(i8* %5, i32 1034, i32* %16)
%22 = icmp eq i32* %21, null
br i1 %22, label %27, label %23
23: ; preds = %20, %23
%24 = call i32 @fputs(i8* %5, i32* %9)
%25 = call i32* @fgets(i8* %5, i32 1034, i32* %16)
%26 = icmp eq i32* %25, null
br i1 %26, label %27, label %23
27: ; preds = %23, %20
%28 = call i32 @pclose(i32* %16)
%29 = call i32 @fclose(i32* %9)
ret i32 0
}