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


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
mov    0x2e83(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x10(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x18(%rbp)
lea    0xe77(%rip),%rdi
lea    0xe7b(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    0x2e3c(%rip),%rax
mov    (%rax),%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11d9 <main+0x99>
mov    -0x28(%rbp),%rdx
mov    -0x8(%rbp),%esi
mov    %esi,%eax
add    $0x1,%eax
mov    %eax,-0x8(%rbp)
lea    0xe3e(%rip),%rdi
call   1030 <printf@plt>
jmp    1199 <main+0x59>
mov    -0x20(%rbp),%rdi
call   1040 <pclose@plt>
mov    0x2def(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x4(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
push   %rax
mov    0x2e8b(%rip),%r12
mov    (%r12),%eax
mov    %rsp,%r13
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r13
mov    %r13,%rsp
lea    0xe8f(%rip),%rdi
lea    0xe93(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,%r14
mov    (%r12),%esi
mov    %r13,%rdi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     11c4 <main+0x84>
xor    %esi,%esi
lea    0xe70(%rip),%r15
nopl   (%rax)
lea    0x1(%rsi),%ebx
mov    %r15,%rdi
mov    %r13,%rdx
call   1030 <printf@plt>
mov    (%r12),%esi
mov    %r13,%rdi
mov    %r14,%rdx
call   1050 <fgets@plt>
mov    %ebx,%esi
test   %rax,%rax
jne    11a0 <main+0x60>
mov    %r14,%rdi
call   1040 <pclose@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

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

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