#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 */
scalar_t__ fwide(int /*<<< orphan*/ *, int /*<<< orphan*/);
int getline(char **, size_t *, int /*<<< orphan*/ *);
int pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
scalar_t__ strcmp(char *, char *);
int do_test(void) {
  FILE *f = popen("echo test", "r");
  int result = 0, ret;
  char *line = NULL;
  size_t len = 0;
  if (f == NULL) {
    printf("popen failed %m");
    return 1;
  }
  if (fwide(f, 0) >= 0) {
    puts("popen did not return byte-oriented stream");
    result = 1;
  }
  if (getline(&line, &len, f) != 5) {
    puts("could not read line from popen");
    result = 1;
  } else if (strcmp(line, "test\n") != 0) {
    printf("read \"%s\"\n", line);
    result = 1;
  }
  if (getline(&line, &len, f) != -1) {
    puts("second getline did not return -1");
    result = 1;
  }
  ret = pclose(f);
  if (ret != 0) {
    printf("pclose returned %d\n", ret);
    result = 1;
  }
  return result;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe81(%rip),%rdi
lea    0xe84(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11cb <do_test+0x5b>
lea    0xe54(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12be <do_test+0x14e>
mov    -0x10(%rbp),%rdi
xor    %esi,%esi
call   1070 <fwide@plt>
cmp    $0x0,%rax
jl     11f3 <do_test+0x83>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <getline@plt>
cmp    $0x5,%eax
je     1225 <do_test+0xb5>
lea    0xe32(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x14(%rbp)
jmp    125d <do_test+0xed>
mov    -0x20(%rbp),%rdi
lea    0xe35(%rip),%rsi
call   1060 <strcmp@plt>
cmp    $0x0,%rax
je     1258 <do_test+0xe8>
mov    -0x20(%rbp),%rsi
lea    0xe21(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x14(%rbp)
jmp    125d <do_test+0xed>
mov    -0x10(%rbp),%rdx
lea    -0x20(%rbp),%rdi
lea    -0x28(%rbp),%rsi
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     128a <do_test+0x11a>
lea    0xdf8(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <pclose@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x18(%rbp)
je     12b8 <do_test+0x148>
mov    -0x18(%rbp),%esi
lea    0xded(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %rbx
sub    $0x18,%rsp
lea    0xe83(%rip),%rdi
lea    0xe86(%rip),%rsi
call   1080 <popen@plt>
movq   $0x0,0x8(%rsp)
movq   $0x0,0x10(%rsp)
test   %rax,%rax
je     1207 <do_test+0x97>
mov    %rax,%rbx
xor    %ebp,%ebp
mov    %rax,%rdi
xor    %esi,%esi
call   1070 <fwide@plt>
test   %rax,%rax
js     11c5 <do_test+0x55>
lea    0xe61(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   1090 <getline@plt>
cmp    $0x5,%eax
jne    1217 <do_test+0xa7>
mov    0x8(%rsp),%rdi
lea    0xe7d(%rip),%rsi
call   1060 <strcmp@plt>
test   %rax,%rax
je     1228 <do_test+0xb8>
mov    0x8(%rsp),%rsi
lea    0xe6d(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    1223 <do_test+0xb3>
lea    0xdfe(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
jmp    126c <do_test+0xfc>
lea    0xe28(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
lea    0x8(%rsp),%rdi
lea    0x10(%rsp),%rsi
mov    %rbx,%rdx
call   1090 <getline@plt>
cmp    $0xffffffff,%eax
je     1250 <do_test+0xe0>
lea    0xe30(%rip),%rdi
call   1030 <puts@plt>
mov    $0x1,%ebp
mov    %rbx,%rdi
call   1050 <pclose@plt>
test   %eax,%eax
je     1271 <do_test+0x101>
lea    0xe34(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%ebp
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"echo test\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"popen failed %m\00"
@.str.3 = [42 x i8] c"popen did not return byte-oriented stream\00"
@.str.4 = [31 x i8] c"could not read line from popen\00"
@.str.5 = [6 x i8] c"test\0A\00"
@.str.6 = [11 x i8] c"read \22%s\22\0A\00"
@.str.7 = [33 x i8] c"second getline did not return -1\00"
@.str.8 = [20 x i8] c"pclose returned %d\0A\00"
define i32 @do_test() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = call i32* @popen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %2
store i32 0, i32* %3
store i8* null, i8** %5
store i64 0, i64* %6
%8 = load i32*, i32** %2
%9 = icmp eq i32* %8, null
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %1
br label %48
12: ; preds = %0
%13 = load i32*, i32** %2
%14 = call i64 @fwide(i32* %13, i32 0)
%15 = icmp sge i64 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %12
%17 = call i32 @puts(i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %18
18: ; preds = %16, %12
%19 = load i32*, i32** %2
%20 = call i32 @getline(i8** %5, i64* %6, i32* %19)
%21 = icmp ne i32 %20, 5
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = call i32 @puts(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %32
24: ; preds = %18
%25 = load i8*, i8** %5
%26 = call i64 @strcmp(i8* %25, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%27 = icmp ne i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = load i8*, i8** %5
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %29)
store i32 1, i32* %3
br label %31
31: ; preds = %28, %24
br label %32
32: ; preds = %31, %22
%33 = load i32*, i32** %2
%34 = call i32 @getline(i8** %5, i64* %6, i32* %33)
%35 = icmp ne i32 %34, -1
br i1 %35, label %36, label %38
36: ; preds = %32
%37 = call i32 @puts(i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
store i32 1, i32* %3
br label %38
38: ; preds = %36, %32
%39 = load i32*, i32** %2
%40 = call i32 @pclose(i32* %39)
store i32 %40, i32* %4
%41 = load i32, i32* %4
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %46
43: ; preds = %38
%44 = load i32, i32* %4
%45 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i32 %44)
store i32 1, i32* %3
br label %46
46: ; preds = %43, %38
%47 = load i32, i32* %3
store i32 %47, i32* %1
br label %48
48: ; preds = %46, %10
%49 = load i32, i32* %1
ret i32 %49
}

LLVM IR (-O2):
@.str = [10 x i8] c"echo test\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [16 x i8] c"popen failed %m\00"
@.str.3 = [42 x i8] c"popen did not return byte-oriented stream\00"
@.str.4 = [31 x i8] c"could not read line from popen\00"
@.str.5 = [6 x i8] c"test\0A\00"
@.str.6 = [11 x i8] c"read \22%s\22\0A\00"
@.str.7 = [33 x i8] c"second getline did not return -1\00"
@.str.8 = [20 x i8] c"pclose returned %d\0A\00"
define i32 @do_test() {
%1 = alloca i8*
%2 = alloca i64
%3 = tail call i32* @popen(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = bitcast i8** %1 to i8*
store i8* null, i8** %1
%5 = bitcast i64* %2 to i8*
store i64 0, i64* %2
%6 = icmp eq i32* %3, null
br i1 %6, label %7, label %9
7: ; preds = %0
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str.2, i64 0, i64 0))
br label %39
9: ; preds = %0
%10 = tail call i64 @fwide(i32* %3, i32 0)
%11 = icmp sgt i64 %10, -1
br i1 %11, label %12, label %14
12: ; preds = %9
%13 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([42 x i8], [42 x i8]* @.str.3, i64 0, i64 0))
br label %14
14: ; preds = %12, %9
%15 = phi i32 [ 1, %12 ], [ 0, %9 ]
%16 = call i32 @getline(i8** %1, i64* %2, i32* %3)
%17 = icmp eq i32 %16, 5
br i1 %17, label %20, label %18
18: ; preds = %14
%19 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.4, i64 0, i64 0))
br label %27
20: ; preds = %14
%21 = load i8*, i8** %1
%22 = call i64 @strcmp(i8* %21, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%23 = icmp eq i64 %22, 0
br i1 %23, label %27, label %24
24: ; preds = %20
%25 = load i8*, i8** %1
%26 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @.str.6, i64 0, i64 0), i8* %25)
br label %27
27: ; preds = %20, %24, %18
%28 = phi i32 [ 1, %18 ], [ 1, %24 ], [ %15, %20 ]
%29 = call i32 @getline(i8** %1, i64* %2, i32* %3)
%30 = icmp eq i32 %29, -1
br i1 %30, label %33, label %31
31: ; preds = %27
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([33 x i8], [33 x i8]* @.str.7, i64 0, i64 0))
br label %33
33: ; preds = %31, %27
%34 = phi i32 [ 1, %31 ], [ %28, %27 ]
%35 = call i32 @pclose(i32* %3)
%36 = icmp eq i32 %35, 0
br i1 %36, label %39, label %37
37: ; preds = %33
%38 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.8, i64 0, i64 0), i32 %35)
br label %39
39: ; preds = %33, %37, %7
%40 = phi i32 [ 1, %7 ], [ 1, %37 ], [ %34, %33 ]
ret i32 %40
}