#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 /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/ *, char *);
int popen2(char **, int /*<<< orphan*/ **, int /*<<< orphan*/ **);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ waitpid(int, int *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  FILE *pin, *pout;
  char buf[256];
  char *args[] = {"bc", NULL};
  int pid, status;
  if ((pid = popen2(args, &pin, &pout)) == -1)
    return (1);
  fprintf(pin, "2+2\n");
  fclose(pin);
  while (!feof(pout)) {
    fgets(buf, 255, pout);
    printf("%s", buf);
    buf[0] = '\0';
  }
  fclose(pout);
  waitpid(pid, &status, 0);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x140,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2c80(%rip),%rax
mov    %rax,-0x130(%rbp)
mov    0x2c7a(%rip),%rax
mov    %rax,-0x128(%rbp)
lea    -0x130(%rbp),%rdi
lea    -0x18(%rbp),%rsi
lea    -0x20(%rbp),%rdx
call   1090 <popen2@plt>
mov    %eax,-0x134(%rbp)
cmp    $0xffffffff,%eax
jne    11d4 <main+0x64>
movl   $0x1,-0x4(%rbp)
jmp    1263 <main+0xf3>
mov    -0x18(%rbp),%rdi
lea    0xe24(%rip),%rsi
call   1060 <fprintf@plt>
mov    -0x18(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x20(%rbp),%rdi
call   1070 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    120b <main+0x9b>
jmp    123f <main+0xcf>
lea    -0x120(%rbp),%rdi
mov    -0x20(%rbp),%rdx
mov    $0xff,%esi
call   1050 <fgets@plt>
lea    -0x120(%rbp),%rsi
lea    0xdda(%rip),%rdi
call   1040 <printf@plt>
movb   $0x0,-0x120(%rbp)
jmp    11ed <main+0x7d>
mov    -0x20(%rbp),%rdi
call   1030 <fclose@plt>
mov    -0x134(%rbp),%edi
lea    -0x138(%rbp),%rsi
xor    %edx,%edx
call   1080 <waitpid@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x140,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x130,%rsp
movaps 0x2c8e(%rip),%xmm0
movaps %xmm0,0x20(%rsp)
lea    0x20(%rsp),%rdi
lea    0x10(%rsp),%rsi
lea    0x8(%rsp),%rdx
call   1090 <popen2@plt>
cmp    $0xffffffff,%eax
je     123d <main+0xcd>
mov    %eax,%r14d
mov    0x10(%rsp),%rcx
lea    0xe50(%rip),%rdi
mov    $0x4,%esi
mov    $0x1,%edx
call   1080 <fwrite@plt>
mov    0x10(%rsp),%rdi
call   1030 <fclose@plt>
mov    0x8(%rsp),%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    1220 <main+0xb0>
lea    0x30(%rsp),%rbx
lea    0xe22(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
mov    0x8(%rsp),%rdx
mov    %rbx,%rdi
mov    $0xff,%esi
call   1050 <fgets@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
movb   $0x0,0x30(%rsp)
mov    0x8(%rsp),%rdi
call   1060 <feof@plt>
test   %eax,%eax
je     11f0 <main+0x80>
mov    0x8(%rsp),%rdi
call   1030 <fclose@plt>
lea    0x1c(%rsp),%rsi
xor    %ebx,%ebx
mov    %r14d,%edi
xor    %edx,%edx
call   1070 <waitpid@plt>
jmp    1242 <main+0xd2>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x130,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"bc\00"
@__const.main.args = [2 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [5 x i8] c"2+2\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca i32*
%8 = alloca [256 x i8]
%9 = alloca [2 x i8*]
%10 = alloca i32
%11 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%12 = bitcast [2 x i8*]* %9 to i8*
%13 = getelementptr inbounds [2 x i8*], [2 x i8*]* %9, i64 0, i64 0
%14 = call i32 @popen2(i8** %13, i32** %6, i32** %7)
store i32 %14, i32* %10
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %17
16: ; preds = %2
store i32 1, i32* %3
br label %39
17: ; preds = %2
%18 = load i32*, i32** %6
%19 = call i32 @fprintf(i32* %18, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
%20 = load i32*, i32** %6
%21 = call i32 @fclose(i32* %20)
br label %22
22: ; preds = %27, %17
%23 = load i32*, i32** %7
%24 = call i32 @feof(i32* %23)
%25 = icmp ne i32 %24, 0
%26 = xor i1 %25, true
br i1 %26, label %27, label %34
27: ; preds = %22
%28 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%29 = load i32*, i32** %7
%30 = call i32 @fgets(i8* %28, i32 255, i32* %29)
%31 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
%32 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %31)
%33 = getelementptr inbounds [256 x i8], [256 x i8]* %8, i64 0, i64 0
store i8 0, i8* %33
br label %22
34: ; preds = %22
%35 = load i32*, i32** %7
%36 = call i32 @fclose(i32* %35)
%37 = load i32, i32* %10
%38 = call i32 @waitpid(i32 %37, i32* %11, i32 0)
store i32 0, i32* %3
br label %39
39: ; preds = %34, %16
%40 = load i32, i32* %3
ret i32 %40
}

LLVM IR (-O2):
@.str = [3 x i8] c"bc\00"
@__const.main.args = [2 x i8*] [i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i32 0, i32 0), i8* null]
@.str.1 = [5 x i8] c"2+2\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32*
%4 = alloca i32*
%5 = alloca [256 x i8]
%6 = alloca [2 x i8*]
%7 = alloca i32
%8 = bitcast i32** %3 to i8*
%9 = bitcast i32** %4 to i8*
%10 = getelementptr inbounds [256 x i8], [256 x i8]* %5, i64 0, i64 0
%11 = bitcast [2 x i8*]* %6 to i8*
%12 = bitcast i32* %7 to i8*
%13 = getelementptr inbounds [2 x i8*], [2 x i8*]* %6, i64 0, i64 0
%14 = call i32 @popen2(i8** %13, i32** %3, i32** %4)
%15 = icmp eq i32 %14, -1
br i1 %15, label %35, label %16
16: ; preds = %2
%17 = load i32*, i32** %3
%18 = call i64 @fwrite(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0), i64 4, i64 1, i32* %17)
%19 = load i32*, i32** %3
%20 = call i32 @fclose(i32* %19)
%21 = load i32*, i32** %4
%22 = call i32 @feof(i32* %21)
%23 = icmp eq i32 %22, 0
br i1 %23, label %24, label %31
24: ; preds = %16, %24
%25 = load i32*, i32** %4
%26 = call i32 @fgets(i8* %10, i32 255, i32* %25)
%27 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %10)
store i8 0, i8* %10
%28 = load i32*, i32** %4
%29 = call i32 @feof(i32* %28)
%30 = icmp eq i32 %29, 0
br i1 %30, label %24, label %31
31: ; preds = %24, %16
%32 = load i32*, i32** %4
%33 = call i32 @fclose(i32* %32)
%34 = call i32 @waitpid(i32 %14, i32* %7, i32 0)
br label %35
35: ; preds = %2, %31
%36 = phi i32 [ 0, %31 ], [ 1, %2 ]
ret i32 %36
}