#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*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ ferror(int /*<<< orphan*/ *);
size_t fread(char *, int, size_t, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int shell_copy(char *buf, size_t n, char *cmd) {
  int total = 0;
  FILE *pf;
  size_t ret, remaining = n;
  pf = popen(cmd, "r");
  if (pf == NULL) {
    perror("popen");
    return 0;
  }
  while (remaining > 0 && !feof(pf) && !ferror(pf)) {
    ret = fread(buf, 1, remaining, pf);
    remaining -= ret;
    buf += ret;
    total += ret;
  }
  pclose(pf);
  return total;
}


x86 assembly (clang, -O0):
<shell_copy>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x24(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xe72(%rip),%rsi
call   1070 <popen@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    11ba <shell_copy+0x5a>
lea    0xe59(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    1263 <shell_copy+0x103>
jmp    11bf <shell_copy+0x5f>
xor    %eax,%eax
cmpq   $0x0,-0x40(%rbp)
mov    %al,-0x41(%rbp)
jbe    11fc <shell_copy+0x9c>
mov    -0x30(%rbp),%rdi
call   1060 <feof@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0x0,%ecx
mov    %al,-0x41(%rbp)
jne    11fc <shell_copy+0x9c>
mov    -0x30(%rbp),%rdi
call   1030 <ferror@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
mov    %al,-0x41(%rbp)
mov    -0x41(%rbp),%al
test   $0x1,%al
jne    120c <shell_copy+0xac>
jmp    1254 <shell_copy+0xf4>
mov    -0x10(%rbp),%rdi
mov    -0x40(%rbp),%rdx
mov    -0x30(%rbp),%rcx
mov    $0x1,%esi
call   1040 <fread@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rcx
mov    -0x40(%rbp),%rax
sub    %rcx,%rax
mov    %rax,-0x40(%rbp)
mov    -0x38(%rbp),%rax
add    -0x10(%rbp),%rax
mov    %rax,-0x10(%rbp)
mov    -0x38(%rbp),%rcx
movslq -0x24(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x24(%rbp)
jmp    11bf <shell_copy+0x5f>
mov    -0x30(%rbp),%rdi
call   1050 <pclose@plt>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<shell_copy>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %rdi,%rbp
lea    0xe8c(%rip),%rsi
mov    %rdx,%rdi
call   1070 <popen@plt>
test   %rax,%rax
je     11dc <shell_copy+0x7c>
mov    %rax,%r14
xor    %r15d,%r15d
test   %rbx,%rbx
je     11c6 <shell_copy+0x66>
nopl   0x0(%rax)
mov    %r14,%rdi
call   1060 <feof@plt>
test   %eax,%eax
jne    11c6 <shell_copy+0x66>
mov    %r14,%rdi
call   1030 <ferror@plt>
test   %eax,%eax
jne    11c6 <shell_copy+0x66>
mov    %rbp,%rdi
mov    $0x1,%esi
mov    %rbx,%rdx
mov    %r14,%rcx
call   1040 <fread@plt>
add    %rax,%rbp
add    %eax,%r15d
sub    %rax,%rbx
jne    1190 <shell_copy+0x30>
mov    %r14,%rdi
call   1050 <pclose@plt>
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xe1f(%rip),%rdi
call   1080 <perror@plt>
xor    %r15d,%r15d
jmp    11ce <shell_copy+0x6e>

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"popen\00"
define i32 @shell_copy(i8* %0, i64 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i64
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32*
%10 = alloca i64
%11 = alloca i64
store i8* %0, i8** %5
store i64 %1, i64* %6
store i8* %2, i8** %7
store i32 0, i32* %8
%12 = load i64, i64* %6
store i64 %12, i64* %11
%13 = load i8*, i8** %7
%14 = call i32* @popen(i8* %13, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %14, i32** %9
%15 = load i32*, i32** %9
%16 = icmp eq i32* %15, null
br i1 %16, label %17, label %19
17: ; preds = %3
%18 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %4
br label %54
19: ; preds = %3
br label %20
20: ; preds = %34, %19
%21 = load i64, i64* %11
%22 = icmp ugt i64 %21, 0
br i1 %22, label %23, label %32
23: ; preds = %20
%24 = load i32*, i32** %9
%25 = call i32 @feof(i32* %24)
%26 = icmp ne i32 %25, 0
br i1 %26, label %32, label %27
27: ; preds = %23
%28 = load i32*, i32** %9
%29 = call i32 @ferror(i32* %28)
%30 = icmp ne i32 %29, 0
%31 = xor i1 %30, true
br label %32
32: ; preds = %27, %23, %20
%33 = phi i1 [ false, %23 ], [ false, %20 ], [ %31, %27 ]
br i1 %33, label %34, label %50
34: ; preds = %32
%35 = load i8*, i8** %5
%36 = load i64, i64* %11
%37 = load i32*, i32** %9
%38 = call i64 @fread(i8* %35, i32 1, i64 %36, i32* %37)
store i64 %38, i64* %10
%39 = load i64, i64* %10
%40 = load i64, i64* %11
%41 = sub i64 %40, %39
store i64 %41, i64* %11
%42 = load i64, i64* %10
%43 = load i8*, i8** %5
%44 = getelementptr inbounds i8, i8* %43, i64 %42
store i8* %44, i8** %5
%45 = load i64, i64* %10
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = add i64 %47, %45
%49 = trunc i64 %48 to i32
store i32 %49, i32* %8
br label %20
50: ; preds = %32
%51 = load i32*, i32** %9
%52 = call i32 @pclose(i32* %51)
%53 = load i32, i32* %8
store i32 %53, i32* %4
br label %54
54: ; preds = %50, %17
%55 = load i32, i32* %4
ret i32 %55
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [6 x i8] c"popen\00"
define i32 @shell_copy(i8* nocapture %0, i64 %1, i8* nocapture %2) {
%4 = tail call i32* @popen(i8* %2, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %8, label %6
6: ; preds = %3
%7 = icmp eq i64 %1, 0
br i1 %7, label %26, label %10
8: ; preds = %3
%9 = tail call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0))
br label %29
10: ; preds = %6, %19
%11 = phi i64 [ %21, %19 ], [ %1, %6 ]
%12 = phi i8* [ %22, %19 ], [ %0, %6 ]
%13 = phi i32 [ %24, %19 ], [ 0, %6 ]
%14 = tail call i32 @feof(i32* %4)
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %26
16: ; preds = %10
%17 = tail call i32 @ferror(i32* %4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %26
19: ; preds = %16
%20 = tail call i64 @fread(i8* %12, i32 1, i64 %11, i32* %4)
%21 = sub i64 %11, %20
%22 = getelementptr inbounds i8, i8* %12, i64 %20
%23 = trunc i64 %20 to i32
%24 = add i32 %13, %23
%25 = icmp eq i64 %21, 0
br i1 %25, label %26, label %10
26: ; preds = %16, %10, %19, %6
%27 = phi i32 [ 0, %6 ], [ %24, %19 ], [ %13, %10 ], [ %13, %16 ]
%28 = tail call i32 @pclose(i32* %4)
br label %29
29: ; preds = %26, %8
%30 = phi i32 [ 0, %8 ], [ %27, %26 ]
ret i32 %30
}