#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 *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char const *, char *);
int /*<<< orphan*/ strtok(char *, char *);
char *do_command_read(const char *cmd_str, char *buffer, int max_len) {
  FILE *fp;
  char *cp;
  fp = popen(cmd_str, "r");
  if (fp == NULL) {
    cp = NULL;
  } else {
    cp = fgets(buffer, max_len, fp);
    if (cp != NULL) {
      strtok(buffer, "\n");
    }
    pclose(fp);
  }
  return (cp);
}


x86 assembly (clang, -O0):
<do_command_read>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
mov    -0x8(%rbp),%rdi
lea    0xea2(%rip),%rsi
call   1050 <popen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    117f <do_command_read+0x3f>
movq   $0x0,-0x28(%rbp)
jmp    11b7 <do_command_read+0x77>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1040 <fgets@plt>
mov    %rax,-0x28(%rbp)
cmpq   $0x0,-0x28(%rbp)
je     11ae <do_command_read+0x6e>
mov    -0x10(%rbp),%rdi
lea    0xe59(%rip),%rsi
call   1060 <strtok@plt>
mov    -0x20(%rbp),%rdi
call   1030 <pclose@plt>
mov    -0x28(%rbp),%rax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_command_read>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edx,%ebp
mov    %rsi,%r14
lea    0xead(%rip),%rsi
call   1050 <popen@plt>
test   %rax,%rax
je     118e <do_command_read+0x4e>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1040 <fgets@plt>
mov    %rax,%r15
test   %rax,%rax
je     1184 <do_command_read+0x44>
lea    0xe86(%rip),%rsi
mov    %r14,%rdi
call   1060 <strtok@plt>
mov    %rbx,%rdi
call   1030 <pclose@plt>
jmp    1191 <do_command_read+0x51>
xor    %r15d,%r15d
mov    %r15,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"\0A\00"
define i8* @do_command_read(i8* %0, i8* %1, i32 %2) {
%4 = alloca i8*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32*
%8 = alloca i8*
store i8* %0, i8** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
%9 = load i8*, i8** %4
%10 = call i32* @popen(i8* %9, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %10, i32** %7
%11 = load i32*, i32** %7
%12 = icmp eq i32* %11, null
br i1 %12, label %13, label %14
13: ; preds = %3
store i8* null, i8** %8
br label %27
14: ; preds = %3
%15 = load i8*, i8** %5
%16 = load i32, i32* %6
%17 = load i32*, i32** %7
%18 = call i8* @fgets(i8* %15, i32 %16, i32* %17)
store i8* %18, i8** %8
%19 = load i8*, i8** %8
%20 = icmp ne i8* %19, null
br i1 %20, label %21, label %24
21: ; preds = %14
%22 = load i8*, i8** %5
%23 = call i32 @strtok(i8* %22, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %24
24: ; preds = %21, %14
%25 = load i32*, i32** %7
%26 = call i32 @pclose(i32* %25)
br label %27
27: ; preds = %24, %13
%28 = load i8*, i8** %8
ret i8* %28
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [2 x i8] c"\0A\00"
define i8* @do_command_read(i8* nocapture %0, i8* %1, i32 %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 %13, label %6
6: ; preds = %3
%7 = tail call i8* @fgets(i8* %1, i32 %2, i32* %4)
%8 = icmp eq i8* %7, null
br i1 %8, label %11, label %9
9: ; preds = %6
%10 = tail call i32 @strtok(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
br label %11
11: ; preds = %9, %6
%12 = tail call i32 @pclose(i32* %4)
br label %13
13: ; preds = %3, %11
%14 = phi i8* [ %7, %11 ], [ null, %3 ]
ret i8* %14
}