#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() {
  FILE *fp = NULL;
  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)
movq   $0x0,-0x10(%rbp)
movl   $0x0,-0x84(%rbp)
lea    0xe95(%rip),%rdi
lea    0xe97(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
mov    -0x10(%rbp),%rdx
mov    $0x64,%esi
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11bd <main+0x7d>
mov    -0x84(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x84(%rbp)
lea    -0x80(%rbp),%rdx
lea    0xe58(%rip),%rdi
call   1030 <printf@plt>
jmp    117b <main+0x3b>
mov    -0x10(%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    0xeb1(%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    0xe8d(%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 = [9 x i8] c"who|sort\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [7 x i8] c"%3d %s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32*
%3 = alloca [100 x i8]
%4 = alloca i32
store i32 0, i32* %1
store i32* null, i32** %2
store i32 0, i32* %4
%5 = call i32* @popen(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %5, i32** %2
br label %6
6: ; preds = %11, %0
%7 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%8 = load i32*, i32** %2
%9 = call i32* @fgets(i8* %7, i32 100, i32* %8)
%10 = icmp ne i32* %9, null
br i1 %10, label %11, label %16
11: ; preds = %6
%12 = load i32, i32* %4
%13 = add nsw i32 %12, 1
store i32 %13, i32* %4
%14 = getelementptr inbounds [100 x i8], [100 x i8]* %3, i64 0, i64 0
%15 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i32 %12, i8* %14)
br label %6
16: ; preds = %6
%17 = load i32*, i32** %2
%18 = call i32 @pclose(i32* %17)
ret i32 0
}

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