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

/* Variables and functions */
int /*<<< orphan*/ *Fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ Fputs(char *, int /*<<< orphan*/);
int /*<<< orphan*/ Pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *Popen(char *, char *);
int /*<<< orphan*/ printf(char *, char *);
size_t read(int, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int /*<<< orphan*/ stdout;
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ write(int, char *, int /*<<< orphan*/);
ssize_t process(int sockfd, char *cmd) {
  char sendline[4096 + 1], recvline[4096 + 1];
  ssize_t n;
  FILE *fp;
  char buf[4096 + 1];
  fp = Popen(cmd, "r");
  while (Fgets(buf, 4096, fp) != NULL) {
    Fputs(buf, stdout);
    snprintf(sendline, 200, "%s\n", buf);
    write(sockfd, sendline, strlen(sendline));
    n = read(sockfd, recvline, 4096);
    recvline[n] = '\0';
    printf("%s", recvline);
  }
  Pclose(fp);
  return n;
}


x86 assembly (clang, -O0):
<process>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3060,%rsp
mov    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
lea    0xe53(%rip),%rsi
call   1060 <Popen@plt>
mov    %rax,-0x2040(%rbp)
lea    -0x3050(%rbp),%rdi
mov    -0x2040(%rbp),%rdx
mov    $0x1000,%esi
call   1090 <Fgets@plt>
cmp    $0x0,%rax
je     1288 <process+0xf8>
lea    -0x3050(%rbp),%rdi
mov    0x2df7(%rip),%rax
mov    (%rax),%esi
call   1030 <Fputs@plt>
lea    -0x1020(%rbp),%rdi
lea    -0x3050(%rbp),%rcx
mov    $0xc8,%esi
lea    0xdf8(%rip),%rdx
call   1080 <snprintf@plt>
mov    -0x4(%rbp),%eax
mov    %eax,-0x305c(%rbp)
lea    -0x1020(%rbp),%rax
mov    %rax,-0x3058(%rbp)
lea    -0x1020(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x305c(%rbp),%edi
mov    -0x3058(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    -0x4(%rbp),%edi
lea    -0x2030(%rbp),%rsi
mov    $0x1000,%edx
call   10a0 <read@plt>
mov    %rax,-0x2038(%rbp)
mov    -0x2038(%rbp),%rax
movb   $0x0,-0x2030(%rbp,%rax,1)
lea    -0x2030(%rbp),%rsi
lea    0xd88(%rip),%rdi
call   1070 <printf@plt>
jmp    11b9 <process+0x29>
mov    -0x2040(%rbp),%rdi
call   10b0 <Pclose@plt>
mov    -0x2038(%rbp),%rax
add    $0x3060,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<process>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3028,%rsp
mov    %rsi,%rax
mov    %edi,%ebp
lea    0xe53(%rip),%rsi
mov    %rax,%rdi
call   1060 <Popen@plt>
mov    %rax,%r14
mov    %rsp,%rdi
mov    $0x1000,%esi
mov    %rax,%rdx
call   1090 <Fgets@plt>
test   %rax,%rax
je     126c <process+0xdc>
mov    %rsp,%r12
lea    0x2020(%rsp),%rbx
lea    0x1010(%rsp),%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    0x2de9(%rip),%rax
mov    (%rax),%esi
mov    %r12,%rdi
call   1030 <Fputs@plt>
mov    %rbx,%rdi
mov    $0xc8,%esi
lea    0xdf2(%rip),%rdx
mov    %r12,%rcx
call   1080 <snprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %ebp,%edi
mov    %r13,%rsi
mov    $0x1000,%edx
call   10a0 <read@plt>
mov    %rax,%r15
movb   $0x0,0x1010(%rsp,%rax,1)
lea    0xdb9(%rip),%rdi
mov    %r13,%rsi
call   1070 <printf@plt>
mov    %r12,%rdi
mov    $0x1000,%esi
mov    %r14,%rdx
call   1090 <Fgets@plt>
test   %rax,%rax
jne    11f0 <process+0x60>
jmp    126c <process+0xdc>
mov    %r14,%rdi
call   10b0 <Pclose@plt>
mov    %r15,%rax
add    $0x3028,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i64 @process(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca [4097 x i8]
%6 = alloca [4097 x i8]
%7 = alloca i64
%8 = alloca i32*
%9 = alloca [4097 x 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** %8
br label %12
12: ; preds = %17, %2
%13 = getelementptr inbounds [4097 x i8], [4097 x i8]* %9, i64 0, i64 0
%14 = load i32*, i32** %8
%15 = call i32* @Fgets(i8* %13, i32 4096, i32* %14)
%16 = icmp ne i32* %15, null
br i1 %16, label %17, label %36
17: ; preds = %12
%18 = getelementptr inbounds [4097 x i8], [4097 x i8]* %9, i64 0, i64 0
%19 = load i32, i32* @stdout
%20 = call i32 @Fputs(i8* %18, i32 %19)
%21 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 0
%22 = getelementptr inbounds [4097 x i8], [4097 x i8]* %9, i64 0, i64 0
%23 = call i32 @snprintf(i8* %21, i32 200, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %22)
%24 = load i32, i32* %3
%25 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 0
%26 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @write(i32 %24, i8* %25, i32 %27)
%29 = load i32, i32* %3
%30 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 0
%31 = call i64 @read(i32 %29, i8* %30, i32 4096)
store i64 %31, i64* %7
%32 = load i64, i64* %7
%33 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 %32
store i8 0, i8* %33
%34 = getelementptr inbounds [4097 x i8], [4097 x i8]* %6, i64 0, i64 0
%35 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %34)
br label %12
36: ; preds = %12
%37 = load i32*, i32** %8
%38 = call i32 @Pclose(i32* %37)
%39 = load i64, i64* %7
ret i64 %39
}

LLVM IR (-O2):
@.str = [2 x i8] c"r\00"
@stdout = global i32 0
@.str.1 = [4 x i8] c"%s\0A\00"
@.str.2 = [3 x i8] c"%s\00"
define i64 @process(i32 %0, i8* %1) {
%3 = alloca [4097 x i8]
%4 = alloca [4097 x i8]
%5 = alloca [4097 x i8]
%6 = getelementptr inbounds [4097 x i8], [4097 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [4097 x i8], [4097 x i8]* %5, i64 0, i64 0
%9 = tail call i32* @Popen(i8* %1, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str, i64 0, i64 0))
%10 = call i32* @Fgets(i8* %8, i32 4096, i32* %9)
%11 = icmp eq i32* %10, null
br i1 %11, label %23, label %12
12: ; preds = %2, %12
%13 = load i32, i32* @stdout
%14 = call i32 @Fputs(i8* %8, i32 %13)
%15 = call i32 @snprintf(i8* %6, i32 200, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i8* %8)
%16 = call i32 @strlen(i8* %6)
%17 = call i32 @write(i32 %0, i8* %6, i32 %16)
%18 = call i64 @read(i32 %0, i8* %7, i32 4096)
%19 = getelementptr inbounds [4097 x i8], [4097 x i8]* %4, i64 0, i64 %18
store i8 0, i8* %19
%20 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %7)
%21 = call i32* @Fgets(i8* %8, i32 4096, i32* %9)
%22 = icmp eq i32* %21, null
br i1 %22, label %23, label %12
23: ; preds = %12, %2
%24 = phi i64 [ undef, %2 ], [ %18, %12 ]
%25 = call i32 @Pclose(i32* %9)
ret i64 %24
}