#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*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, int, char *);
int main(int argc, char *argv[]) {
  char buf[255];
  FILE *fd;
  int line = 1;
  fd = popen("/bin/ls -l", "r");
  while (fgets(buf, 255, fd) != NULL) {
    printf("line %d: %s", line, buf);
    line++;
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x11c(%rbp)
lea    0xea6(%rip),%rdi
lea    0xeaa(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x118(%rbp)
lea    -0x110(%rbp),%rdi
mov    -0x118(%rbp),%rdx
mov    $0xff,%esi
call   1040 <fgets@plt>
cmp    $0x0,%rax
je     11bc <main+0x8c>
mov    -0x11c(%rbp),%esi
lea    -0x110(%rbp),%rdx
lea    0xe6a(%rip),%rdi
call   1030 <printf@plt>
mov    -0x11c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x11c(%rbp)
jmp    116d <main+0x3d>
xor    %eax,%eax
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x108,%rsp
lea    0xebc(%rip),%rdi
lea    0xec0(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0xff,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
je     11a5 <main+0x75>
mov    $0x1,%ebp
lea    0xe99(%rip),%r14
mov    %rsp,%rbx
nopw   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rbx,%rdx
call   1030 <printf@plt>
add    $0x1,%ebp
mov    %rbx,%rdi
mov    $0xff,%esi
mov    %r15,%rdx
call   1040 <fgets@plt>
test   %rax,%rax
jne    1180 <main+0x50>
xor    %eax,%eax
add    $0x108,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"/bin/ls -l\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"line %d: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [255 x i8]
%7 = alloca i32*
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %8
%9 = call i32* @popen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %9, i32** %7
br label %10
10: ; preds = %15, %2
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%12 = load i32*, i32** %7
%13 = call i32* @fgets(i8* %11, i32 255, i32* %12)
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %21
15: ; preds = %10
%16 = load i32, i32* %8
%17 = getelementptr inbounds [255 x i8], [255 x i8]* %6, i64 0, i64 0
%18 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %16, i8* %17)
%19 = load i32, i32* %8
%20 = add nsw i32 %19, 1
store i32 %20, i32* %8
br label %10
21: ; preds = %10
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"/bin/ls -l\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [12 x i8] c"line %d: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [255 x i8]
%4 = getelementptr inbounds [255 x i8], [255 x i8]* %3, i64 0, i64 0
%5 = tail call i32* @popen(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%6 = call i32* @fgets(i8* %4, i32 255, i32* %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %14, label %8
8: ; preds = %2, %8
%9 = phi i32 [ %11, %8 ], [ 1, %2 ]
%10 = call i32 @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.2, i64 0, i64 0), i32 %9, i8* %4)
%11 = add nuw nsw i32 %9, 1
%12 = call i32* @fgets(i8* %4, i32 255, i32* %5)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %8
14: ; preds = %8, %2
ret i32 0
}