#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*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ dup2(int, int /*<<< orphan*/);
int /*<<< orphan*/ execlp(char *, char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ *fdopen(int, char *);
int fileno(int /*<<< orphan*/ *);
scalar_t__ fork();
int /*<<< orphan*/ perror(char *);
scalar_t__ pipe(int *);
FILE *smodels_internal(FILE *in) {
  FILE *out = NULL;
  pid_t nPid;
  int pipefrom[2];
  if (pipe(pipefrom) != 0) {
    perror("pipe() from");
    return NULL;
  }
  nPid = fork();
  if (nPid < 0) {
    perror("fork() 1");
    return NULL;
  } else if (nPid == 0) {
    dup2(fileno(in), STDIN_FILENO);
    dup2(pipefrom[1], STDOUT_FILENO);
    close(pipefrom[0]);
    close(pipefrom[1]);
    execlp("smodels", "smodels", "-internal", NULL);
    perror("execlp()");
    return NULL;
  } else {
    close(pipefrom[1]);
    if ((out = fdopen(pipefrom[0], "r")) == NULL) {
      perror("fdopen() r");
      return NULL;
    }
  }
  return out;
}


x86 assembly (clang, -O0):
<smodels_internal>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
lea    -0x28(%rbp),%rdi
call   1050 <pipe@plt>
cmp    $0x0,%rax
je     11c0 <smodels_internal+0x40>
lea    0xe52(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b7 <smodels_internal+0x137>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    11ef <smodels_internal+0x6f>
lea    0xe2f(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b7 <smodels_internal+0x137>
cmpq   $0x0,-0x20(%rbp)
jne    1267 <smodels_internal+0xe7>
mov    -0x10(%rbp),%rdi
call   1060 <fileno@plt>
mov    %eax,%edi
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x24(%rbp),%edi
mov    0x2dd3(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    -0x28(%rbp),%edi
call   1040 <close@plt>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
lea    0xdda(%rip),%rsi
lea    0xddb(%rip),%rdx
xor    %eax,%eax
mov    %eax,%ecx
mov    %rsi,%rdi
call   1090 <execlp@plt>
lea    0xdd2(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b7 <smodels_internal+0x137>
mov    -0x24(%rbp),%edi
call   1040 <close@plt>
mov    -0x28(%rbp),%edi
lea    0xdc0(%rip),%rsi
call   1070 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmp    $0x0,%rax
jne    12a5 <smodels_internal+0x125>
lea    0xd9d(%rip),%rdi
call   1080 <perror@plt>
movq   $0x0,-0x8(%rbp)
jmp    12b7 <smodels_internal+0x137>
jmp    12aa <smodels_internal+0x12a>
jmp    12af <smodels_internal+0x12f>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<smodels_internal>:
push   %rbx
sub    $0x10,%rsp
mov    %rdi,%rbx
lea    0x8(%rsp),%rdi
call   1050 <pipe@plt>
test   %rax,%rax
jne    11cc <smodels_internal+0x4c>
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
js     11d5 <smodels_internal+0x55>
je     11de <smodels_internal+0x5e>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
mov    0x8(%rsp),%edi
lea    0xe80(%rip),%rsi
call   1070 <fdopen@plt>
test   %rax,%rax
jne    1240 <smodels_internal+0xc0>
lea    0xe66(%rip),%rdi
jmp    1239 <smodels_internal+0xb9>
lea    0xe2d(%rip),%rdi
jmp    1239 <smodels_internal+0xb9>
lea    0xe30(%rip),%rdi
jmp    1239 <smodels_internal+0xb9>
mov    %rbx,%rdi
call   1060 <fileno@plt>
mov    0x2deb(%rip),%rcx
mov    (%rcx),%esi
mov    %eax,%edi
call   1030 <dup2@plt>
mov    0xc(%rsp),%edi
mov    0x2def(%rip),%rax
mov    (%rax),%esi
call   1030 <dup2@plt>
mov    0x8(%rsp),%edi
call   1040 <close@plt>
mov    0xc(%rsp),%edi
call   1040 <close@plt>
lea    0xdf4(%rip),%rdi
lea    0xdf5(%rip),%rdx
mov    %rdi,%rsi
xor    %ecx,%ecx
call   1090 <execlp@plt>
lea    0xdee(%rip),%rdi
call   1080 <perror@plt>
xor    %eax,%eax
add    $0x10,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [12 x i8] c"pipe() from\00"
@.str.1 = [9 x i8] c"fork() 1\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [8 x i8] c"smodels\00"
@.str.3 = [10 x i8] c"-internal\00"
@.str.4 = [9 x i8] c"execlp()\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [11 x i8] c"fdopen() r\00"
define i32* @smodels_internal(i32* %0) {
%2 = alloca i32*
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca i64
%6 = alloca [2 x i32]
store i32* %0, i32** %3
store i32* null, i32** %4
%7 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%8 = call i64 @pipe(i32* %7)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %12
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
store i32* null, i32** %2
br label %52
12: ; preds = %1
%13 = call i64 (...) @fork()
store i64 %13, i64* %5
%14 = load i64, i64* %5
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0))
store i32* null, i32** %2
br label %52
18: ; preds = %12
%19 = load i64, i64* %5
%20 = icmp eq i64 %19, 0
br i1 %20, label %21, label %38
21: ; preds = %18
%22 = load i32*, i32** %3
%23 = call i32 @fileno(i32* %22)
%24 = load i32, i32* @STDIN_FILENO
%25 = call i32 @dup2(i32 %23, i32 %24)
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%27 = load i32, i32* %26
%28 = load i32, i32* @STDOUT_FILENO
%29 = call i32 @dup2(i32 %27, i32 %28)
%30 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%31 = load i32, i32* %30
%32 = call i32 @close(i32 %31)
%33 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%34 = load i32, i32* %33
%35 = call i32 @close(i32 %34)
%36 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32* null)
%37 = call i32 @perror(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0))
store i32* null, i32** %2
br label %52
38: ; preds = %18
%39 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 1
%40 = load i32, i32* %39
%41 = call i32 @close(i32 %40)
%42 = getelementptr inbounds [2 x i32], [2 x i32]* %6, i64 0, i64 0
%43 = load i32, i32* %42
%44 = call i32* @fdopen(i32 %43, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %44, i32** %4
%45 = icmp eq i32* %44, null
br i1 %45, label %46, label %48
46: ; preds = %38
%47 = call i32 @perror(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0))
store i32* null, i32** %2
br label %52
48: ; preds = %38
br label %49
49: ; preds = %48
br label %50
50: ; preds = %49
%51 = load i32*, i32** %4
store i32* %51, i32** %2
br label %52
52: ; preds = %50, %46, %21, %16, %10
%53 = load i32*, i32** %2
ret i32* %53
}

LLVM IR (-O2):
@.str = [12 x i8] c"pipe() from\00"
@.str.1 = [9 x i8] c"fork() 1\00"
@STDIN_FILENO = global i32 0
@STDOUT_FILENO = global i32 0
@.str.2 = [8 x i8] c"smodels\00"
@.str.3 = [10 x i8] c"-internal\00"
@.str.4 = [9 x i8] c"execlp()\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [11 x i8] c"fdopen() r\00"
define noalias i32* @smodels_internal(i32* nocapture %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %32
7: ; preds = %1
%8 = call i64 (...) @fork()
%9 = icmp slt i64 %8, 0
br i1 %9, label %32, label %10
10: ; preds = %7
%11 = icmp eq i64 %8, 0
br i1 %11, label %12, label %25
12: ; preds = %10
%13 = call i32 @fileno(i32* %0)
%14 = load i32, i32* @STDIN_FILENO
%15 = call i32 @dup2(i32 %13, i32 %14)
%16 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%17 = load i32, i32* %16
%18 = load i32, i32* @STDOUT_FILENO
%19 = call i32 @dup2(i32 %17, i32 %18)
%20 = load i32, i32* %4
%21 = call i32 @close(i32 %20)
%22 = load i32, i32* %16
%23 = call i32 @close(i32 %22)
%24 = call i32 @execlp(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i32* null)
br label %32
25: ; preds = %10
%26 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%27 = load i32, i32* %26
%28 = call i32 @close(i32 %27)
%29 = load i32, i32* %4
%30 = call i32* @fdopen(i32 %29, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%31 = icmp eq i32* %30, null
br i1 %31, label %32, label %35
32: ; preds = %25, %7, %1, %12
%33 = phi i8* [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), %12 ], [ getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([9 x i8], [9 x i8]* @.str.1, i64 0, i64 0), %7 ], [ getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), %25 ]
%34 = call i32 @perror(i8* %33)
br label %35
35: ; preds = %32, %25
%36 = phi i32* [ %30, %25 ], [ null, %32 ]
ret i32* %36
}