#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 *, char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ send(int, char *, int, int /*<<< orphan*/);
int strlen(char *);
void exec_and_send(int clientfd, char *cmd) {
  FILE *cmd_result = popen(cmd, "r");
  printf("EXEC_AND_SEND_OUTPUT: %s\n", cmd);
  char buffer[1024];
  int nbytes = 0;
  bool eof = false;
  *buffer = '\0';
  while (!eof) {
    while (nbytes < 1024 - 1) {
      char *str = fgets(buffer + nbytes, 1024 - nbytes, cmd_result);
      if (str == NULL) {
        eof = true;
        break;
      }
      nbytes += strlen(str);
    }
    send(clientfd, buffer, strlen(buffer), 0);
    nbytes = 0;
  }
  send(clientfd, "\0", 1, 0);
  pclose(cmd_result);
}


x86 assembly (clang, -O0):
<exec_and_send>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe83(%rip),%rsi
call   1080 <popen@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe71(%rip),%rdi
call   1050 <printf@plt>
movl   $0x0,-0x424(%rbp)
movl   $0x0,-0x428(%rbp)
movb   $0x0,-0x420(%rbp)
cmpl   $0x0,-0x428(%rbp)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11ca <exec_and_send+0x6a>
jmp    128d <exec_and_send+0x12d>
jmp    11cf <exec_and_send+0x6f>
cmpl   $0x3ff,-0x424(%rbp)
jge    1245 <exec_and_send+0xe5>
lea    -0x420(%rbp),%rdi
movslq -0x424(%rbp),%rax
add    %rax,%rdi
mov    $0x400,%esi
sub    -0x424(%rbp),%esi
mov    -0x18(%rbp),%rdx
call   1070 <fgets@plt>
mov    %rax,-0x430(%rbp)
cmpq   $0x0,-0x430(%rbp)
jne    1228 <exec_and_send+0xc8>
movl   $0x1,-0x428(%rbp)
jmp    1245 <exec_and_send+0xe5>
mov    -0x430(%rbp),%rdi
call   1030 <strlen@plt>
add    -0x424(%rbp),%eax
mov    %eax,-0x424(%rbp)
jmp    11cf <exec_and_send+0x6f>
mov    -0x4(%rbp),%eax
mov    %eax,-0x43c(%rbp)
lea    -0x420(%rbp),%rax
mov    %rax,-0x438(%rbp)
lea    -0x420(%rbp),%rdi
call   1030 <strlen@plt>
mov    -0x43c(%rbp),%edi
mov    -0x438(%rbp),%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
movl   $0x0,-0x424(%rbp)
jmp    11b1 <exec_and_send+0x51>
mov    -0x4(%rbp),%edi
lea    0xd85(%rip),%rsi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    -0x18(%rbp),%rdi
call   1060 <pclose@plt>
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<exec_and_send>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x400,%rsp
mov    %rsi,%rbp
mov    %edi,%r14d
lea    0xe84(%rip),%rsi
mov    %rbp,%rdi
call   1080 <popen@plt>
mov    %rax,%r12
lea    0xe74(%rip),%rdi
mov    %rbp,%rsi
call   1050 <printf@plt>
movb   $0x0,(%rsp)
xor    %ebx,%ebx
mov    %rsp,%r15
nop
movslq %ebx,%rax
lea    (%rsp,%rax,1),%rdi
mov    $0x400,%esi
sub    %eax,%esi
mov    %r12,%rdx
call   1070 <fgets@plt>
test   %rax,%rax
je     11f0 <exec_and_send+0x90>
mov    %rax,%rbp
mov    %rax,%rdi
call   1030 <strlen@plt>
add    %eax,%ebx
cmp    $0x3ff,%ebx
jl     11a0 <exec_and_send+0x40>
mov    %r15,%rdi
call   1030 <strlen@plt>
xor    %ebx,%ebx
mov    %r14d,%edi
mov    %r15,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
test   %rbp,%rbp
jne    11a0 <exec_and_send+0x40>
jmp    120a <exec_and_send+0xaa>
mov    %rsp,%rbp
mov    %rbp,%rdi
call   1030 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
lea    0xe0b(%rip),%rsi
mov    %r14d,%edi
mov    $0x1,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
mov    %r12,%rdi
call   1060 <pclose@plt>
add    $0x400,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"EXEC_AND_SEND_OUTPUT: %s\0A\00"
@.str.2 = [2 x i8] zeroinitializer
define void @exec_and_send(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32*
%6 = alloca [1024 x i8]
%7 = alloca i32
%8 = alloca i32
%9 = alloca i8*
store i32 %0, i32* %3
store i8* %1, i8** %4
%10 = load i8*, i8** %4
%11 = call i32* @popen(i8* %10, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
store i32* %11, i32** %5
%12 = load i8*, i8** %4
%13 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %12)
store i32 0, i32* %7
store i32 0, i32* %8
%14 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
store i8 0, i8* %14
br label %15
15: ; preds = %40, %2
%16 = load i32, i32* %8
%17 = icmp ne i32 %16, 0
%18 = xor i1 %17, true
br i1 %18, label %19, label %46
19: ; preds = %15
br label %20
20: ; preds = %35, %19
%21 = load i32, i32* %7
%22 = icmp slt i32 %21, 1023
br i1 %22, label %23, label %40
23: ; preds = %20
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = load i32, i32* %7
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8, i8* %24, i64 %26
%28 = load i32, i32* %7
%29 = sub nsw i32 1024, %28
%30 = load i32*, i32** %5
%31 = call i8* @fgets(i8* %27, i32 %29, i32* %30)
store i8* %31, i8** %9
%32 = load i8*, i8** %9
%33 = icmp eq i8* %32, null
br i1 %33, label %34, label %35
34: ; preds = %23
store i32 1, i32* %8
br label %40
35: ; preds = %23
%36 = load i8*, i8** %9
%37 = call i32 @strlen(i8* %36)
%38 = load i32, i32* %7
%39 = add nsw i32 %38, %37
store i32 %39, i32* %7
br label %20
40: ; preds = %34, %20
%41 = load i32, i32* %3
%42 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @send(i32 %41, i8* %42, i32 %44, i32 0)
store i32 0, i32* %7
br label %15
46: ; preds = %15
%47 = load i32, i32* %3
%48 = call i32 @send(i32 %47, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 0)
%49 = load i32*, i32** %5
%50 = call i32 @pclose(i32* %49)
ret void
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@.str.1 = [26 x i8] c"EXEC_AND_SEND_OUTPUT: %s\0A\00"
@.str.2 = [2 x i8] zeroinitializer
define void @exec_and_send(i32 %0, i8* %1) {
%3 = alloca [1024 x i8]
%4 = tail call i32* @popen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%5 = tail call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0), i8* %1)
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
store i8 0, i8* %6
br label %7
7: ; preds = %18, %2
%8 = phi i32 [ 0, %2 ], [ %19, %18 ]
%9 = sext i32 %8 to i64
%10 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %9
%11 = sub nsw i32 1024, %8
%12 = call i8* @fgets(i8* %10, i32 %11, i32* %4)
%13 = icmp eq i8* %12, null
br i1 %13, label %23, label %14
14: ; preds = %7
%15 = call i32 @strlen(i8* %12)
%16 = add nsw i32 %15, %8
%17 = icmp slt i32 %16, 1023
br i1 %17, label %18, label %20
18: ; preds = %14, %20
%19 = phi i32 [ %16, %14 ], [ 0, %20 ]
br label %7
20: ; preds = %14
%21 = call i32 @strlen(i8* %6)
%22 = call i32 @send(i32 %0, i8* %6, i32 %21, i32 0)
br i1 %13, label %26, label %18
23: ; preds = %7
%24 = call i32 @strlen(i8* %6)
%25 = call i32 @send(i32 %0, i8* %6, i32 %24, i32 0)
br label %26
26: ; preds = %20, %23
%27 = call i32 @send(i32 %0, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0), i32 1, i32 0)
%28 = call i32 @pclose(i32* %4)
ret void
}