#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 */
struct mainwindow {
  int dummy;
};

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ debug_print(char *, char *);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int fork();
int /*<<< orphan*/ pipe(int *);
int pipe_to_process(struct mainwindow *mw, int *pid, int *fd, char **args) {
  int pipefd[2];
  pipe(pipefd);
  *pid = fork();
  if (*pid == 0) {
    dup2(pipefd[0], 0);
    close(pipefd[0]);
    close(pipefd[1]);
    if (execvp(args[0], args))
      debug_print("Error spawning %s\n", args[0]);
    exit(1);
  } else if (*pid > 0) {
    close(pipefd[0]);
    *fd = pipefd[1];
  } else {
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<pipe_to_process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
mov    %rcx,-0x28(%rbp)
lea    -0x30(%rbp),%rdi
call   1050 <pipe@plt>
mov    $0x0,%al
call   1090 <fork@plt>
mov    %eax,%ecx
mov    -0x18(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jne    11fe <pipe_to_process+0x8e>
mov    -0x30(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%rax
mov    (%rax),%rdi
mov    -0x28(%rbp),%rsi
call   1060 <execvp@plt>
cmp    $0x0,%rax
je     11f4 <pipe_to_process+0x84>
mov    -0x28(%rbp),%rax
mov    (%rax),%rsi
lea    0xe11(%rip),%rdi
call   1080 <debug_print@plt>
mov    $0x1,%edi
call   1070 <exit@plt>
mov    -0x18(%rbp),%rax
cmpl   $0x0,(%rax)
jle    1221 <pipe_to_process+0xb1>
mov    -0x30(%rbp),%edi
call   1040 <close@plt>
mov    -0x2c(%rbp),%ecx
mov    -0x20(%rbp),%rax
mov    %ecx,(%rax)
jmp    122d <pipe_to_process+0xbd>
movl   $0x1,-0x4(%rbp)
jmp    1239 <pipe_to_process+0xc9>
jmp    1232 <pipe_to_process+0xc2>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<pipe_to_process>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rcx,%r14
mov    %rdx,%r15
mov    %rsi,%rbx
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
xor    %eax,%eax
call   1090 <fork@plt>
mov    %eax,(%rbx)
test   %eax,%eax
je     11be <pipe_to_process+0x4e>
jle    11af <pipe_to_process+0x3f>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%eax
mov    %eax,(%r15)
xor    %eax,%eax
jmp    11b4 <pipe_to_process+0x44>
mov    $0x1,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    (%r14),%rdi
mov    %r14,%rsi
call   1060 <execvp@plt>
test   %rax,%rax
je     11fa <pipe_to_process+0x8a>
mov    (%r14),%rsi
lea    0xe0b(%rip),%rdi
call   1080 <debug_print@plt>
mov    $0x1,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.mainwindow = type { i32 }
@.str = [19 x i8] c"Error spawning %s\0A\00"
define i32 @pipe_to_process(%struct.mainwindow* %0, i32* %1, i32* %2, i8** %3) {
%5 = alloca i32
%6 = alloca %struct.mainwindow*
%7 = alloca i32*
%8 = alloca i32*
%9 = alloca i8**
%10 = alloca [2 x i32]
store %struct.mainwindow* %0, %struct.mainwindow** %6
store i32* %1, i32** %7
store i32* %2, i32** %8
store i8** %3, i8*** %9
%11 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%12 = call i32 @pipe(i32* %11)
%13 = call i32 (...) @fork()
%14 = load i32*, i32** %7
store i32 %13, i32* %14
%15 = load i32*, i32** %7
%16 = load i32, i32* %15
%17 = icmp eq i32 %16, 0
br i1 %17, label %18, label %41
18: ; preds = %4
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%20 = load i32, i32* %19
%21 = call i32 @dup2(i32 %20, i32 0)
%22 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%23 = load i32, i32* %22
%24 = call i32 @close(i32 %23)
%25 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%26 = load i32, i32* %25
%27 = call i32 @close(i32 %26)
%28 = load i8**, i8*** %9
%29 = getelementptr inbounds i8*, i8** %28, i64 0
%30 = load i8*, i8** %29
%31 = load i8**, i8*** %9
%32 = call i64 @execvp(i8* %30, i8** %31)
%33 = icmp ne i64 %32, 0
br i1 %33, label %34, label %39
34: ; preds = %18
%35 = load i8**, i8*** %9
%36 = getelementptr inbounds i8*, i8** %35, i64 0
%37 = load i8*, i8** %36
%38 = call i32 @debug_print(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %37)
br label %39
39: ; preds = %34, %18
%40 = call i32 @exit(i32 1)
unreachable
41: ; preds = %4
%42 = load i32*, i32** %7
%43 = load i32, i32* %42
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %41
%46 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 0
%47 = load i32, i32* %46
%48 = call i32 @close(i32 %47)
%49 = getelementptr inbounds [2 x i32], [2 x i32]* %10, i64 0, i64 1
%50 = load i32, i32* %49
%51 = load i32*, i32** %8
store i32 %50, i32* %51
br label %53
52: ; preds = %41
store i32 1, i32* %5
br label %55
53: ; preds = %45
br label %54
54: ; preds = %53
store i32 0, i32* %5
br label %55
55: ; preds = %54, %52
%56 = load i32, i32* %5
ret i32 %56
}

LLVM IR (-O2):
%struct.mainwindow = type { i32 }
@.str = [19 x i8] c"Error spawning %s\0A\00"
define i32 @pipe_to_process(%struct.mainwindow* nocapture readnone %0, i32* nocapture writeonly %1, i32* nocapture writeonly %2, i8** %3) {
%5 = alloca [2 x i32]
%6 = bitcast [2 x i32]* %5 to i8*
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 0
%8 = call i32 @pipe(i32* %7)
%9 = call i32 (...) @fork()
store i32 %9, i32* %1
%10 = icmp eq i32 %9, 0
br i1 %10, label %11, label %27
11: ; preds = %4
%12 = load i32, i32* %7
%13 = call i32 @dup2(i32 %12, i32 0)
%14 = load i32, i32* %7
%15 = call i32 @close(i32 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%17 = load i32, i32* %16
%18 = call i32 @close(i32 %17)
%19 = load i8*, i8** %3
%20 = call i64 @execvp(i8* %19, i8** %3)
%21 = icmp eq i64 %20, 0
br i1 %21, label %25, label %22
22: ; preds = %11
%23 = load i8*, i8** %3
%24 = call i32 @debug_print(i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str, i64 0, i64 0), i8* %23)
br label %25
25: ; preds = %22, %11
%26 = call i32 @exit(i32 1)
unreachable
27: ; preds = %4
%28 = icmp sgt i32 %9, 0
br i1 %28, label %29, label %34
29: ; preds = %27
%30 = load i32, i32* %7
%31 = call i32 @close(i32 %30)
%32 = getelementptr inbounds [2 x i32], [2 x i32]* %5, i64 0, i64 1
%33 = load i32, i32* %32
store i32 %33, i32* %2
br label %34
34: ; preds = %27, %29
%35 = phi i32 [ 0, %29 ], [ 1, %27 ]
ret i32 %35
}