#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 */
char EOF;
char getc_unlocked(int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
char *read_command(char *command, char *buf, char exiter) {
  FILE *fp;
  char a, *start = buf;
  fp = popen(command, "r");
  if (!fp) {
    return buf;
  }
  while ((a = getc_unlocked(fp)) != EOF) {
    *buf++ = a;
  }
  if (start != buf) {
    *buf++ = exiter;
  }
  pclose(fp);
  return buf;
}


x86 assembly (clang, -O0):
<read_command>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %dl,%al
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %al,-0x19(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x38(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xea8(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
jne    1179 <read_command+0x49>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
jmp    11eb <read_command+0xbb>
jmp    117e <read_command+0x4e>
mov    -0x28(%rbp),%rdi
call   1030 <getc_unlocked@plt>
mov    %al,-0x29(%rbp)
movsbl %al,%eax
mov    0x2e54(%rip),%rcx
movsbl (%rcx),%ecx
cmp    %ecx,%eax
je     11b8 <read_command+0x88>
mov    -0x29(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
jmp    117e <read_command+0x4e>
mov    -0x38(%rbp),%rax
cmp    -0x18(%rbp),%rax
je     11da <read_command+0xaa>
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %rax,%rdx
add    $0x1,%rdx
mov    %rdx,-0x18(%rbp)
mov    %cl,(%rax)
mov    -0x28(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_command>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edx,%r15d
mov    %rsi,%r14
lea    0xebb(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     119e <read_command+0x6e>
mov    %rax,%rbx
mov    %rax,%rdi
call   1030 <getc_unlocked@plt>
mov    0x2e87(%rip),%r12
cmp    (%r12),%al
je     1196 <read_command+0x66>
xor    %ebp,%ebp
nopl   0x0(%rax)
mov    %al,(%r14,%rbp,1)
mov    %rbx,%rdi
call   1030 <getc_unlocked@plt>
add    $0x1,%rbp
cmp    (%r12),%al
jne    1170 <read_command+0x40>
test   %rbp,%rbp
je     1196 <read_command+0x66>
mov    %r15b,(%r14,%rbp,1)
add    %rbp,%r14
add    $0x1,%r14
mov    %rbx,%rdi
call   1040 <pclose@plt>
mov    %r14,%rax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
define i8* @read_command(i8* %0, i8* %1, i8 signext %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8
%8 = alloca i32*
%9 = alloca i8
%10 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8 %2, i8* %7
%11 = load i8*, i8** %6
store i8* %11, i8** %10
%12 = load i8*, i8** %5
%13 = call i32* @popen(i8* %12, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %13, i32** %8
%14 = load i32*, i32** %8
%15 = icmp ne i32* %14, null
br i1 %15, label %18, label %16
16: ; preds = %3
%17 = load i8*, i8** %6
store i8* %17, i8** %4
br label %42
18: ; preds = %3
br label %19
19: ; preds = %26, %18
%20 = load i32*, i32** %8
%21 = call signext i8 @getc_unlocked(i32* %20)
store i8 %21, i8* %9
%22 = sext i8 %21 to i32
%23 = load i8, i8* @EOF
%24 = sext i8 %23 to i32
%25 = icmp ne i32 %22, %24
br i1 %25, label %26, label %30
26: ; preds = %19
%27 = load i8, i8* %9
%28 = load i8*, i8** %6
%29 = getelementptr inbounds i8, i8* %28, i32 1
store i8* %29, i8** %6
store i8 %27, i8* %28
br label %19
30: ; preds = %19
%31 = load i8*, i8** %10
%32 = load i8*, i8** %6
%33 = icmp ne i8* %31, %32
br i1 %33, label %34, label %38
34: ; preds = %30
%35 = load i8, i8* %7
%36 = load i8*, i8** %6
%37 = getelementptr inbounds i8, i8* %36, i32 1
store i8* %37, i8** %6
store i8 %35, i8* %36
br label %38
38: ; preds = %34, %30
%39 = load i32*, i32** %8
%40 = call i32 @pclose(i32* %39)
%41 = load i8*, i8** %6
store i8* %41, i8** %4
br label %42
42: ; preds = %38, %16
%43 = load i8*, i8** %4
ret i8* %43
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@EOF = global i8 0
define i8* @read_command(i8* nocapture %0, i8* writeonly %1, i8 signext %2) {
%4 = tail call i32* @popen(i8* %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %24, label %6
6: ; preds = %3
%7 = tail call signext i8 @getc_unlocked(i32* %4)
%8 = load i8, i8* @EOF
%9 = icmp eq i8 %7, %8
br i1 %9, label %21, label %10
10: ; preds = %6, %10
%11 = phi i8 [ %14, %10 ], [ %7, %6 ]
%12 = phi i8* [ %13, %10 ], [ %1, %6 ]
%13 = getelementptr inbounds i8, i8* %12, i64 1
store i8 %11, i8* %12
%14 = tail call signext i8 @getc_unlocked(i32* %4)
%15 = load i8, i8* @EOF
%16 = icmp eq i8 %14, %15
br i1 %16, label %17, label %10
17: ; preds = %10
%18 = icmp eq i8* %13, %1
br i1 %18, label %21, label %19
19: ; preds = %17
%20 = getelementptr inbounds i8, i8* %12, i64 2
store i8 %2, i8* %13
br label %21
21: ; preds = %6, %19, %17
%22 = phi i8* [ %20, %19 ], [ %1, %17 ], [ %1, %6 ]
%23 = tail call i32 @pclose(i32* %4)
br label %24
24: ; preds = %3, %21
%25 = phi i8* [ %22, %21 ], [ %1, %3 ]
ret i8* %25
}