#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 scalar_t__ pid_t;
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ *fdopen(int, char *);
scalar_t__ fork();
scalar_t__ pipe(int *);
int /*<<< orphan*/ run_from_fork(int *, int /*<<< orphan*/, char *);
FILE *read_cmd_output(const char cmd[]) {
  FILE *fp;
  pid_t pid;
  int out_pipe[2];
  if (pipe(out_pipe) != 0) {
    return NULL;
  }
  pid = fork();
  if (pid == (pid_t)-1) {
    return NULL;
  }
  if (pid == 0) {
    run_from_fork(out_pipe, 0, (char *)cmd);
    return NULL;
  }
  close(out_pipe[1]);
  fp = fdopen(out_pipe[0], "r");
  if (fp == NULL) {
    close(out_pipe[0]);
  }
  return fp;
}


x86 assembly (clang, -O0):
<read_cmd_output>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x28(%rbp),%rdi
call   1040 <pipe@plt>
cmp    $0x0,%rax
je     117c <read_cmd_output+0x2c>
movq   $0x0,-0x8(%rbp)
jmp    11fc <read_cmd_output+0xac>
mov    $0x0,%al
call   1070 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0xffffffffffffffff,-0x20(%rbp)
jne    119f <read_cmd_output+0x4f>
movq   $0x0,-0x8(%rbp)
jmp    11fc <read_cmd_output+0xac>
cmpq   $0x0,-0x20(%rbp)
jne    11c6 <read_cmd_output+0x76>
lea    -0x28(%rbp),%rdi
mov    -0x10(%rbp),%rdx
xor    %esi,%esi
call   1060 <run_from_fork@plt>
movq   $0x0,-0x8(%rbp)
jmp    11fc <read_cmd_output+0xac>
mov    -0x24(%rbp),%edi
call   1030 <close@plt>
mov    -0x28(%rbp),%edi
lea    0xe28(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11f4 <read_cmd_output+0xa4>
mov    -0x28(%rbp),%edi
call   1030 <close@plt>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<read_cmd_output>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    %rsp,%rdi
call   1040 <pipe@plt>
test   %rax,%rax
je     1171 <read_cmd_output+0x21>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
xor    %ebx,%ebx
xor    %eax,%eax
call   1070 <fork@plt>
cmp    $0xffffffffffffffff,%rax
je     1166 <read_cmd_output+0x16>
test   %rax,%rax
jne    1196 <read_cmd_output+0x46>
mov    %rsp,%rdi
xor    %ebx,%ebx
xor    %esi,%esi
mov    %r14,%rdx
call   1060 <run_from_fork@plt>
jmp    1166 <read_cmd_output+0x16>
mov    0x4(%rsp),%edi
call   1030 <close@plt>
mov    (%rsp),%edi
lea    0xe57(%rip),%rsi
call   1050 <fdopen@plt>
mov    %rax,%rbx
test   %rax,%rax
jne    1166 <read_cmd_output+0x16>
mov    (%rsp),%edi
call   1030 <close@plt>
jmp    1164 <read_cmd_output+0x14>

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

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
define noalias i32* @read_cmd_output(i8* %0) {
%2 = alloca [2 x i32]
%3 = bitcast [2 x i32]* %2 to i8*
%4 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 0
%5 = call i64 @pipe(i32* %4)
%6 = icmp eq i64 %5, 0
br i1 %6, label %7, label %21
7: ; preds = %1
%8 = call i64 (...) @fork()
switch i64 %8, label %11 [
i64 -1, label %21
i64 0, label %9
]
9: ; preds = %7
%10 = call i32 @run_from_fork(i32* %4, i32 0, i8* %0)
br label %21
11: ; preds = %7
%12 = getelementptr inbounds [2 x i32], [2 x i32]* %2, i64 0, i64 1
%13 = load i32, i32* %12
%14 = call i32 @close(i32 %13)
%15 = load i32, i32* %4
%16 = call i32* @fdopen(i32 %15, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%17 = icmp eq i32* %16, null
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* %4
%20 = call i32 @close(i32 %19)
br label %21
21: ; preds = %11, %18, %7, %1, %9
%22 = phi i32* [ null, %9 ], [ null, %1 ], [ null, %7 ], [ null, %18 ], [ %16, %11 ]
ret i32* %22
}