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

/* Variables and functions */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sleep(int);
int main() {
  char buf[12800];
  FILE *fp = popen("traceroute www.baidu.com", "r");
  if (!fp) {
    printf("popen failed\n");
  } else {
    printf("open success\n");
  }
  while (fgets(buf, sizeof(buf), fp)) {
    printf("buf = %s\n", buf);
    sleep(1);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3220,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xea7(%rip),%rdi
lea    0xeb9(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x3218(%rbp)
cmpq   $0x0,-0x3218(%rbp)
jne    118d <main+0x4d>
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    119b <main+0x5b>
lea    0xe95(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    11a0 <main+0x60>
lea    -0x3210(%rbp),%rdi
mov    -0x3218(%rbp),%rdx
mov    $0x3200,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11e6 <main+0xa6>
lea    -0x3210(%rbp),%rsi
lea    0xe67(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    $0x1,%edi
call   1060 <sleep@plt>
jmp    11a0 <main+0x60>
mov    -0x4(%rbp),%eax
add    $0x3220,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x3200,%rsp
lea    0xe9d(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%r15
test   %rax,%rax
lea    0xea9(%rip),%rax
lea    0xeaf(%rip),%rdi
cmove  %rax,%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
mov    $0x3200,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11dc <main+0x8c>
lea    0xe73(%rip),%r14
mov    %rsp,%rbx
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0x1,%edi
call   1070 <sleep@plt>
mov    %rbx,%rdi
mov    $0x3200,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
jne    11b0 <main+0x60>
xor    %eax,%eax
add    $0x3200,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [25 x i8] c"traceroute www.baidu.com\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [14 x i8] c"popen failed\0A\00"
@.str.3 = [14 x i8] c"open success\0A\00"
@.str.4 = [10 x i8] c"buf = %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [12800 x i8]
%3 = alloca i32*
store i32 0, i32* %1
%4 = call i32* @popen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %4, i32** %3
%5 = load i32*, i32** %3
%6 = icmp ne i32* %5, null
br i1 %6, label %9, label %7
7: ; preds = %0
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %11
9: ; preds = %0
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %11
11: ; preds = %9, %7
br label %12
12: ; preds = %17, %11
%13 = getelementptr inbounds [12800 x i8], [12800 x i8]* %2, i64 0, i64 0
%14 = load i32*, i32** %3
%15 = call i64 @fgets(i8* %13, i32 12800, i32* %14)
%16 = icmp ne i64 %15, 0
br i1 %16, label %17, label %21
17: ; preds = %12
%18 = getelementptr inbounds [12800 x i8], [12800 x i8]* %2, i64 0, i64 0
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %18)
%20 = call i32 @sleep(i32 1)
br label %12
21: ; preds = %12
%22 = load i32, i32* %1
ret i32 %22
}

LLVM IR (-O2):
@.str = [25 x i8] c"traceroute www.baidu.com\00"
@.str.1 = [2 x i8] c"r\00"
@.str.4 = [10 x i8] c"buf = %s\0A\00"
@str = [13 x i8] c"popen failed\00"
@str.5 = [13 x i8] c"open success\00"
define i32 @main() {
%1 = alloca [12800 x i8]
%2 = getelementptr inbounds [12800 x i8], [12800 x i8]* %1, i64 0, i64 0
%3 = tail call i32* @popen(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
%5 = select i1 %4, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str, i64 0, i64 0), i8* getelementptr inbounds ([13 x i8], [13 x i8]* @str.5, i64 0, i64 0)
%6 = tail call i32 @puts(i8* dereferenceable(1) %5)
%7 = call i64 @fgets(i8* %2, i32 12800, i32* %3)
%8 = icmp eq i64 %7, 0
br i1 %8, label %14, label %9
9: ; preds = %0, %9
%10 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i8* %2)
%11 = call i32 @sleep(i32 1)
%12 = call i64 @fgets(i8* %2, i32 12800, i32* %3)
%13 = icmp eq i64 %12, 0
br i1 %13, label %14, label %9
14: ; preds = %9, %0
ret i32 0
}