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

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
scalar_t__ execvp(char *, char **);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
FILE *gnuplot() {
  int p[2];
  if (pipe(p)) {
    perror("could not create pipes for gnuplot");
    exit(1);
  }
  pid_t pid = fork();
  if (pid < 0) {
    perror("could not fork");
    exit(1);
  }
  if (!pid) {
    close(p[1]);
    dup2(p[0], 0);
    char *args[] = {0};
    if (execvp("gnuplot", args) < 0) {
      perror("error starting gnuplot");
      exit(1);
    }
  }
  close(p[0]);
  return fdopen(p[1], "w");
}


x86 assembly (clang, -O0):
<gnuplot>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
lea    -0x8(%rbp),%rdi
call   1060 <pipe@plt>
cmp    $0x0,%rax
je     11c1 <gnuplot+0x31>
lea    0xe4e(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10b0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    11ed <gnuplot+0x5d>
lea    0xe45(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    124f <gnuplot+0xbf>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    -0x8(%rbp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0x8,%edx
call   1040 <memset@plt>
lea    -0x18(%rbp),%rsi
lea    0xe1c(%rip),%rdi
call   1090 <execvp@plt>
cmp    $0x0,%rax
jge    124a <gnuplot+0xba>
lea    0xdf7(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    124f <gnuplot+0xbf>
mov    -0x8(%rbp),%edi
call   1050 <close@plt>
mov    -0x4(%rbp),%edi
lea    0xde8(%rip),%rsi
call   1070 <fdopen@plt>
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<gnuplot>:
sub    $0x18,%rsp
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    11f2 <gnuplot+0x72>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11fb <gnuplot+0x7b>
jne    11d4 <gnuplot+0x54>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
xor    %esi,%esi
call   1030 <dup2@plt>
movq   $0x0,0x10(%rsp)
lea    0xe7c(%rip),%rdi
lea    0x10(%rsp),%rsi
call   1080 <execvp@plt>
test   %rax,%rax
js     1204 <gnuplot+0x84>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
lea    0xe61(%rip),%rsi
call   1060 <fdopen@plt>
add    $0x18,%rsp
ret
lea    0xe07(%rip),%rdi
jmp    120b <gnuplot+0x8b>
lea    0xe21(%rip),%rdi
jmp    120b <gnuplot+0x8b>
lea    0xe27(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1090 <exit@plt>

LLVM IR (-O0):
@.str = [35 x i8] c"could not create pipes for gnuplot\00"
@.str.1 = [15 x i8] c"could not fork\00"
@.str.2 = [8 x i8] c"gnuplot\00"
@.str.3 = [23 x i8] c"error starting gnuplot\00"
@.str.4 = [2 x i8] c"w\00"
define i32* @gnuplot() {
%1 = alloca [2 x i32]
%2 = alloca i64
%3 = alloca [1 x i8*]
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp ne i64 %5, 0
br i1 %6, label %7, label %10
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i64 (...) @fork()
store i64 %11, i64* %2
%12 = load i64, i64* %2
%13 = icmp slt i64 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %10
%15 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %10
%18 = load i64, i64* %2
%19 = icmp ne i64 %18, 0
br i1 %19, label %35, label %20
20: ; preds = %17
%21 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%22 = load i32, i32* %21
%23 = call i32 @close(i32 %22)
%24 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%25 = load i32, i32* %24
%26 = call i32 @dup2(i32 %25, i32 0)
%27 = bitcast [1 x i8*]* %3 to i8*
%28 = getelementptr inbounds [1 x i8*], [1 x i8*]* %3, i64 0, i64 0
%29 = call i64 @execvp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %28)
%30 = icmp slt i64 %29, 0
br i1 %30, label %31, label %34
31: ; preds = %20
%32 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %20
br label %35
35: ; preds = %34, %17
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%37 = load i32, i32* %36
%38 = call i32 @close(i32 %37)
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32* @fdopen(i32 %40, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
ret i32* %41
}

LLVM IR (-O2):
@.str = [35 x i8] c"could not create pipes for gnuplot\00"
@.str.1 = [15 x i8] c"could not fork\00"
@.str.2 = [8 x i8] c"gnuplot\00"
@.str.3 = [23 x i8] c"error starting gnuplot\00"
@.str.4 = [2 x i8] c"w\00"
define noalias i32* @gnuplot() {
%1 = alloca [2 x i32]
%2 = alloca [1 x i8*]
%3 = bitcast [2 x i32]* %1 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %10, label %7
7: ; preds = %0
%8 = call i32 @perror(i8* getelementptr inbounds ([35 x i8], [35 x i8]* @.str, i64 0, i64 0))
%9 = call i32 @exit(i32 1)
unreachable
10: ; preds = %0
%11 = call i64 (...) @fork()
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %10
%14 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0))
%15 = call i32 @exit(i32 1)
unreachable
16: ; preds = %10
%17 = icmp eq i64 %11, 0
br i1 %17, label %18, label %33
18: ; preds = %16
%19 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%20 = load i32, i32* %19
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %4
%23 = call i32 @dup2(i32 %22, i32 0)
%24 = bitcast [1 x i8*]* %2 to i8*
%25 = bitcast [1 x i8*]* %2 to i64*
store i64 0, i64* %25
%26 = getelementptr inbounds [1 x i8*], [1 x i8*]* %2, i64 0, i64 0
%27 = call i64 @execvp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8** %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %32
29: ; preds = %18
%30 = call i32 @perror(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.3, i64 0, i64 0))
%31 = call i32 @exit(i32 1)
unreachable
32: ; preds = %18
br label %33
33: ; preds = %32, %16
%34 = load i32, i32* %4
%35 = call i32 @close(i32 %34)
%36 = getelementptr inbounds [2 x i32], [2 x i32]* %1, i64 0, i64 1
%37 = load i32, i32* %36
%38 = call i32* @fdopen(i32 %37, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.4, i64 0, i64 0))
ret i32* %38
}