#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*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, int /*<<< orphan*/, char *);
int main(int argc, char *argv[]) {
  FILE *fp;
  char buf[100];
  int i = 0;
  fp = popen("who | sort", "r");
  while (fgets(buf, 100, fp) != NULL)
    printf("%3d: %s", i++, buf);
  pclose(fp);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x84(%rbp)
lea    0xe96(%rip),%rdi
lea    0xe9a(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x18(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x18(%rbp),%rdx
mov    $0x64,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11bc <main+0x7c>
mov    -0x84(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
lea    -0x80(%rbp),%rdx
lea    0xe5b(%rip),%rdi
call   1030 <printf@plt>
jmp    117a <main+0x3a>
mov    -0x18(%rbp),%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x68,%rsp
lea    0xeaf(%rip),%rdi
lea    0xeb3(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%r15
mov    %rsp,%rdi
mov    $0x64,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11b5 <main+0x75>
xor    %esi,%esi
lea    0xe8f(%rip),%r14
mov    %rsp,%rbx
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax,%rax,1)
lea    0x1(%rsi),%ebp
mov    %r14,%rdi
mov    %rbx,%rdx
call   1030 <printf@plt>
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %r15,%rdx
call   1050 <fgets@plt>
mov    %ebp,%esi
test   %rax,%rax
jne    1190 <main+0x50>
mov    %r15,%rdi
call   1040 <pclose@plt>
xor    %eax,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [11 x i8] c"who | sort\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"%3d: %s\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32*
%7 = alloca [100 x i8]
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, 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** %6
br label %10
10: ; preds = %15, %2
%11 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%12 = load i32*, i32** %6
%13 = call i32* @fgets(i8* %11, i32 100, i32* %12)
%14 = icmp ne i32* %13, null
br i1 %14, label %15, label %20
15: ; preds = %10
%16 = load i32, i32* %8
%17 = add nsw i32 %16, 1
store i32 %17, i32* %8
%18 = getelementptr inbounds [100 x i8], [100 x i8]* %7, i64 0, i64 0
%19 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %16, i8* %18)
br label %10
20: ; preds = %10
%21 = load i32*, i32** %6
%22 = call i32 @pclose(i32* %21)
ret i32 0
}

LLVM IR (-O2):
@.str = [11 x i8] c"who | sort\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [8 x i8] c"%3d: %s\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [100 x i8]
%4 = getelementptr inbounds [100 x i8], [100 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 100, i32* %5)
%7 = icmp eq i32* %6, null
br i1 %7, label %14, label %8
8: ; preds = %2, %8
%9 = phi i32 [ %10, %8 ], [ 0, %2 ]
%10 = add nuw nsw i32 %9, 1
%11 = call i32 @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0), i32 %9, i8* %4)
%12 = call i32* @fgets(i8* %4, i32 100, i32* %5)
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %8
14: ; preds = %8, %2
%15 = call i32 @pclose(i32* %5)
ret i32 0
}