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

/* Variables and functions */
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ *popen(char *, char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
void doprocessing(int sock) {
  int n;
  char buffer[4096];
  bzero(buffer, 4096);
  n = read(sock, buffer, 4095);
  if (n < 0) {
    perror("ERROR reading from socket");
    exit(1);
  }
  FILE *fp = popen(buffer, "r");
  if (fp == NULL) {
    printf("Failed to run command.");
    return;
  }
  char buffersend[4096];
  while (fgets(buffersend, sizeof(buffersend) - 1, fp) != NULL) {
    printf("%s", buffersend);
    write(sock, buffersend, strlen(buffersend));
  }
  pclose(fp);
}


x86 assembly (clang, -O0):
<doprocessing>:
push   %rbp
mov    %rsp,%rbp
sub    $0x2030,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x1010(%rbp),%rdi
mov    $0x1000,%esi
call   10b0 <bzero@plt>
mov    -0x4(%rbp),%edi
lea    -0x1010(%rbp),%rsi
mov    $0xfff,%edx
call   1070 <read@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0x0,-0x8(%rbp)
jge    11f6 <doprocessing+0x56>
lea    0xe19(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>
lea    -0x1010(%rbp),%rdi
lea    0xe16(%rip),%rsi
call   1090 <popen@plt>
mov    %rax,-0x1018(%rbp)
cmpq   $0x0,-0x1018(%rbp)
jne    1231 <doprocessing+0x91>
lea    0xdf7(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12b5 <doprocessing+0x115>
jmp    1236 <doprocessing+0x96>
lea    -0x2020(%rbp),%rdi
mov    -0x1018(%rbp),%rdx
mov    $0xfff,%esi
call   1080 <fgets@plt>
cmp    $0x0,%rax
je     12a9 <doprocessing+0x109>
lea    -0x2020(%rbp),%rsi
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x202c(%rbp)
lea    -0x2020(%rbp),%rax
mov    %rax,-0x2028(%rbp)
lea    -0x2020(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x202c(%rbp),%edi
mov    -0x2028(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
jmp    1236 <doprocessing+0x96>
mov    -0x1018(%rbp),%rdi
call   1060 <pclose@plt>
add    $0x2030,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doprocessing>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x2008,%rsp
mov    %edi,%r15d
lea    0x1000(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x1000,%esi
call   10b0 <bzero@plt>
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xfff,%edx
call   1070 <read@plt>
test   %eax,%eax
js     1288 <doprocessing+0xe8>
lea    0xe36(%rip),%rsi
lea    0x1000(%rsp),%rdi
call   1090 <popen@plt>
test   %rax,%rax
je     126d <doprocessing+0xcd>
mov    %rax,%rbx
mov    %rsp,%rdi
mov    $0xfff,%esi
mov    %rax,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
je     1257 <doprocessing+0xb7>
lea    0xe1e(%rip),%r14
mov    %rsp,%rbp
nopl   0x0(%rax,%rax,1)
mov    %r14,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %rbp,%rdi
call   1040 <strlen@plt>
mov    %r15d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1030 <write@plt>
mov    %rbp,%rdi
mov    $0xfff,%esi
mov    %rbx,%rdx
call   1080 <fgets@plt>
test   %rax,%rax
jne    1220 <doprocessing+0x80>
mov    %rbx,%rdi
call   1060 <pclose@plt>
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xda8(%rip),%rdi
xor    %eax,%eax
add    $0x2008,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
jmp    1050 <printf@plt>
lea    0xd71(%rip),%rdi
call   10a0 <perror@plt>
mov    $0x1,%edi
call   10c0 <exit@plt>

LLVM IR (-O0):
@.str = [26 x i8] c"ERROR reading from socket\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"Failed to run command.\00"
@.str.3 = [3 x i8] c"%s\00"
define void @doprocessing(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [4096 x i8]
%5 = alloca i32*
%6 = alloca [4096 x i8]
store i32 %0, i32* %2
%7 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%8 = call i32 @bzero(i8* %7, i32 4096)
%9 = load i32, i32* %2
%10 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%11 = call i32 @read(i32 %9, i8* %10, i32 4095)
store i32 %11, i32* %3
%12 = load i32, i32* %3
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %17
14: ; preds = %1
%15 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%16 = call i32 @exit(i32 1)
unreachable
17: ; preds = %1
%18 = getelementptr inbounds [4096 x i8], [4096 x i8]* %4, i64 0, i64 0
%19 = call i32* @popen(i8* %18, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %19, i32** %5
%20 = load i32*, i32** %5
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %41
24: ; preds = %17
br label %25
25: ; preds = %30, %24
%26 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%27 = load i32*, i32** %5
%28 = call i32* @fgets(i8* %26, i32 4095, i32* %27)
%29 = icmp ne i32* %28, null
br i1 %29, label %30, label %38
30: ; preds = %25
%31 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%32 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %31)
%33 = load i32, i32* %2
%34 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%35 = getelementptr inbounds [4096 x i8], [4096 x i8]* %6, i64 0, i64 0
%36 = call i32 @strlen(i8* %35)
%37 = call i32 @write(i32 %33, i8* %34, i32 %36)
br label %25
38: ; preds = %25
%39 = load i32*, i32** %5
%40 = call i32 @pclose(i32* %39)
br label %41
41: ; preds = %38, %22
ret void
}

LLVM IR (-O2):
@.str = [26 x i8] c"ERROR reading from socket\00"
@.str.1 = [2 x i8] c"r\00"
@.str.2 = [23 x i8] c"Failed to run command.\00"
@.str.3 = [3 x i8] c"%s\00"
define void @doprocessing(i32 %0) {
%2 = alloca [4096 x i8]
%3 = alloca [4096 x i8]
%4 = getelementptr inbounds [4096 x i8], [4096 x i8]* %2, i64 0, i64 0
%5 = call i32 @bzero(i8* %4, i32 4096)
%6 = call i32 @read(i32 %0, i8* %4, i32 4095)
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %1
%9 = tail call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = tail call i32 @exit(i32 1)
unreachable
11: ; preds = %1
%12 = call i32* @popen(i8* %4, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%13 = icmp eq i32* %12, null
br i1 %13, label %14, label %16
14: ; preds = %11
%15 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @.str.2, i64 0, i64 0))
br label %28
16: ; preds = %11
%17 = getelementptr inbounds [4096 x i8], [4096 x i8]* %3, i64 0, i64 0
%18 = call i32* @fgets(i8* %17, i32 4095, i32* %12)
%19 = icmp eq i32* %18, null
br i1 %19, label %26, label %20
20: ; preds = %16, %20
%21 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %17)
%22 = call i32 @strlen(i8* %17)
%23 = call i32 @write(i32 %0, i8* %17, i32 %22)
%24 = call i32* @fgets(i8* %17, i32 4095, i32* %12)
%25 = icmp eq i32* %24, null
br i1 %25, label %26, label %20
26: ; preds = %20, %16
%27 = call i32 @pclose(i32* %12)
br label %28
28: ; preds = %26, %14
ret void
}