#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 */
struct prog_context {
  int dummy;
};
typedef int /*<<< orphan*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ feof(int /*<<< orphan*/ *);
int /*<<< orphan*/ fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
struct prog_context *prog_exec(char const *, int /*<<< orphan*/);
int prog_wait(struct prog_context *);
int /*<<< orphan*/ progcnx_free(struct prog_context *);
int /*<<< orphan*/ *progcnx_get_stderr(struct prog_context *);
int /*<<< orphan*/ *progcnx_get_stdout(struct prog_context *);
int do_test(const char *cmd) {
  char buf[1024];
  struct prog_context *cnt = prog_exec(cmd, 0);
  FILE *out = progcnx_get_stdout(cnt);
  FILE *err = progcnx_get_stderr(cnt);
  while (!feof(out)) {
    memset(buf, 0x00, 1024);
    fread(buf, 1, 1024, out);
    printf("**out: %s \n", buf);
    fread(buf, 1, 1024, err);
    printf("**err: %s \n", buf);
  }
  int ret = prog_wait(cnt);
  printf("** exit status: %d\n", ret);
  progcnx_free(cnt);
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1040 <prog_exec@plt>
mov    %rax,-0x418(%rbp)
mov    -0x418(%rbp),%rdi
call   1030 <progcnx_get_stdout@plt>
mov    %rax,-0x420(%rbp)
mov    -0x418(%rbp),%rdi
call   10a0 <progcnx_get_stderr@plt>
mov    %rax,-0x428(%rbp)
mov    -0x420(%rbp),%rdi
call   1090 <feof@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    11f8 <do_test+0x68>
jmp    1274 <do_test+0xe4>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
lea    -0x410(%rbp),%rdi
mov    -0x420(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1050 <fread@plt>
lea    -0x410(%rbp),%rsi
lea    0xdca(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    -0x428(%rbp),%rcx
mov    $0x1,%esi
mov    $0x400,%edx
call   1050 <fread@plt>
lea    -0x410(%rbp),%rsi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
jmp    11d7 <do_test+0x47>
mov    -0x418(%rbp),%rdi
call   1070 <prog_wait@plt>
mov    %eax,-0x42c(%rbp)
mov    -0x42c(%rbp),%esi
lea    0xd85(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x418(%rbp),%rdi
call   10b0 <progcnx_free@plt>
xor    %eax,%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x408,%rsp
xor    %esi,%esi
call   1040 <prog_exec@plt>
mov    %rax,%r14
mov    %rax,%rdi
call   1030 <progcnx_get_stdout@plt>
mov    %rax,%r12
mov    %r14,%rdi
call   10a0 <progcnx_get_stderr@plt>
mov    %rax,%r15
mov    %r12,%rdi
call   1090 <feof@plt>
test   %eax,%eax
jne    123f <do_test+0xaf>
mov    %rsp,%rbx
lea    0xe29(%rip),%r13
lea    0xe2e(%rip),%rbp
xchg   %ax,%ax
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1080 <memset@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r12,%rcx
call   1050 <fread@plt>
mov    %r13,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    $0x1,%esi
mov    $0x400,%edx
mov    %r15,%rcx
call   1050 <fread@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12,%rdi
call   1090 <feof@plt>
test   %eax,%eax
je     11e0 <do_test+0x50>
mov    %r14,%rdi
call   1070 <prog_wait@plt>
lea    0xdca(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   10b0 <progcnx_free@plt>
xor    %eax,%eax
add    $0x408,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.prog_context = type { i32 }
@.str = [12 x i8] c"**out: %s \0A\00"
@.str.1 = [12 x i8] c"**err: %s \0A\00"
@.str.2 = [20 x i8] c"** exit status: %d\0A\00"
define i32 @do_test(i8* %0) {
%2 = alloca i8*
%3 = alloca [1024 x i8]
%4 = alloca %struct.prog_context*
%5 = alloca i32*
%6 = alloca i32*
%7 = alloca i32
store i8* %0, i8** %2
%8 = load i8*, i8** %2
%9 = call %struct.prog_context* @prog_exec(i8* %8, i32 0)
store %struct.prog_context* %9, %struct.prog_context** %4
%10 = load %struct.prog_context*, %struct.prog_context** %4
%11 = call i32* @progcnx_get_stdout(%struct.prog_context* %10)
store i32* %11, i32** %5
%12 = load %struct.prog_context*, %struct.prog_context** %4
%13 = call i32* @progcnx_get_stderr(%struct.prog_context* %12)
store i32* %13, i32** %6
br label %14
14: ; preds = %19, %1
%15 = load i32*, i32** %5
%16 = call i32 @feof(i32* %15)
%17 = icmp ne i32 %16, 0
%18 = xor i1 %17, true
br i1 %18, label %19, label %32
19: ; preds = %14
%20 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%21 = call i32 @memset(i8* %20, i32 0, i32 1024)
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%23 = load i32*, i32** %5
%24 = call i32 @fread(i8* %22, i32 1, i32 1024, i32* %23)
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %25)
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%28 = load i32*, i32** %6
%29 = call i32 @fread(i8* %27, i32 1, i32 1024, i32* %28)
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %30)
br label %14
32: ; preds = %14
%33 = load %struct.prog_context*, %struct.prog_context** %4
%34 = call i32 @prog_wait(%struct.prog_context* %33)
store i32 %34, i32* %7
%35 = load i32, i32* %7
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %35)
%37 = load %struct.prog_context*, %struct.prog_context** %4
%38 = call i32 @progcnx_free(%struct.prog_context* %37)
ret i32 0
}

LLVM IR (-O2):
%struct.prog_context = type { i32 }
@.str = [12 x i8] c"**out: %s \0A\00"
@.str.1 = [12 x i8] c"**err: %s \0A\00"
@.str.2 = [20 x i8] c"** exit status: %d\0A\00"
define i32 @do_test(i8* %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%4 = tail call %struct.prog_context* @prog_exec(i8* %0, i32 0)
%5 = tail call i32* @progcnx_get_stdout(%struct.prog_context* %4)
%6 = tail call i32* @progcnx_get_stderr(%struct.prog_context* %4)
%7 = call i32 @feof(i32* %5)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %17
9: ; preds = %1, %9
%10 = call i32 @memset(i8* %3, i32 0, i32 1024)
%11 = call i32 @fread(i8* %3, i32 1, i32 1024, i32* %5)
%12 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* %3)
%13 = call i32 @fread(i8* %3, i32 1, i32 1024, i32* %6)
%14 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i8* %3)
%15 = call i32 @feof(i32* %5)
%16 = icmp eq i32 %15, 0
br i1 %16, label %9, label %17
17: ; preds = %9, %1
%18 = call i32 @prog_wait(%struct.prog_context* %4)
%19 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i32 %18)
%20 = call i32 @progcnx_free(%struct.prog_context* %4)
ret i32 0
}