#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;
};
struct dynamic_buffer {
  int dummy;
};

/* Variables and functions */
char *dynbuffer_data(struct dynamic_buffer *);
int dynbuffer_data_len(struct dynamic_buffer *);
int /*<<< orphan*/ dynbuffer_free(struct dynamic_buffer *);
struct dynamic_buffer *dynbuffer_new();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ prog_communicate(struct prog_context *, int /*<<< orphan*/ *,
                                    struct dynamic_buffer *,
                                    struct dynamic_buffer *, int);
struct prog_context *prog_exec(char const *, int);
int prog_wait(struct prog_context *);
int /*<<< orphan*/ progcnx_free(struct prog_context *);
int do_test(const char *cmd, int flg) {
  printf("------------------------\n");
  printf("cmd: [%s] Flg: [%d]\n", cmd, flg);
  struct prog_context *cnt = prog_exec(cmd, flg);
  int rc = -1;
  struct dynamic_buffer *obuf = dynbuffer_new();
  struct dynamic_buffer *ebuf = dynbuffer_new();
  prog_communicate(cnt, NULL, obuf, ebuf, 5);
  printf("** xout [%d]:%s.\n", dynbuffer_data_len(obuf), dynbuffer_data(obuf));
  printf("** xerr [%d]:%s.\n", dynbuffer_data_len(ebuf), dynbuffer_data(ebuf));
  rc = prog_wait(cnt);
  printf("RC: [%d]\n", rc);
  dynbuffer_free(obuf);
  dynbuffer_free(ebuf);
  progcnx_free(cnt);
  return 0;
}


x86 assembly (clang, -O0):
<do_test>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
lea    0xe5a(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rsi
mov    -0xc(%rbp),%edx
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1030 <prog_exec@plt>
mov    %rax,-0x18(%rbp)
movl   $0xffffffff,-0x1c(%rbp)
mov    $0x0,%al
call   1070 <dynbuffer_new@plt>
mov    %rax,-0x28(%rbp)
mov    $0x0,%al
call   1070 <dynbuffer_new@plt>
mov    %rax,-0x30(%rbp)
mov    -0x18(%rbp),%rdi
mov    -0x28(%rbp),%rdx
mov    -0x30(%rbp),%rcx
xor    %eax,%eax
mov    %eax,%esi
mov    $0x5,%r8d
call   10a0 <prog_communicate@plt>
mov    -0x28(%rbp),%rdi
call   1040 <dynbuffer_data_len@plt>
mov    %eax,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
call   1080 <dynbuffer_data@plt>
mov    -0x38(%rbp),%esi
mov    %rax,%rdx
lea    0xe03(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x30(%rbp),%rdi
call   1040 <dynbuffer_data_len@plt>
mov    %eax,-0x34(%rbp)
mov    -0x30(%rbp),%rdi
call   1080 <dynbuffer_data@plt>
mov    -0x34(%rbp),%esi
mov    %rax,%rdx
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x18(%rbp),%rdi
call   1060 <prog_wait@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x1c(%rbp),%esi
lea    0xde1(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1090 <dynbuffer_free@plt>
mov    -0x30(%rbp),%rdi
call   1090 <dynbuffer_free@plt>
mov    -0x18(%rbp),%rdi
call   10b0 <progcnx_free@plt>
xor    %eax,%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_test>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %esi,%ebp
mov    %rdi,%rbx
lea    0xe90(%rip),%rdi
call   1030 <puts@plt>
lea    0xe41(%rip),%rdi
mov    %rbx,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   1040 <prog_exec@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1080 <dynbuffer_new@plt>
mov    %rax,%r15
xor    %eax,%eax
call   1080 <dynbuffer_new@plt>
mov    %rax,%rbx
mov    %r14,%rdi
xor    %esi,%esi
mov    %r15,%rdx
mov    %rax,%rcx
mov    $0x5,%r8d
call   10b0 <prog_communicate@plt>
mov    %r15,%rdi
call   1050 <dynbuffer_data_len@plt>
mov    %eax,%ebp
mov    %r15,%rdi
call   1090 <dynbuffer_data@plt>
lea    0xdfa(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %rbx,%rdi
call   1050 <dynbuffer_data_len@plt>
mov    %eax,%ebp
mov    %rbx,%rdi
call   1090 <dynbuffer_data@plt>
lea    0xde7(%rip),%rdi
mov    %ebp,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r14,%rdi
call   1070 <prog_wait@plt>
lea    0xdde(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r15,%rdi
call   10a0 <dynbuffer_free@plt>
mov    %rbx,%rdi
call   10a0 <dynbuffer_free@plt>
mov    %r14,%rdi
call   10c0 <progcnx_free@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.prog_context = type { i32 }
%struct.dynamic_buffer = type { i32 }
@.str = [26 x i8] c"------------------------\0A\00"
@.str.1 = [21 x i8] c"cmd: [%s] Flg: [%d]\0A\00"
@.str.2 = [18 x i8] c"** xout [%d]:%s.\0A\00"
@.str.3 = [18 x i8] c"** xerr [%d]:%s.\0A\00"
@.str.4 = [10 x i8] c"RC: [%d]\0A\00"
define i32 @do_test(i8* %0, i32 %1) {
%3 = alloca i8*
%4 = alloca i32
%5 = alloca %struct.prog_context*
%6 = alloca i32
%7 = alloca %struct.dynamic_buffer*
%8 = alloca %struct.dynamic_buffer*
store i8* %0, i8** %3
store i32 %1, i32* %4
%9 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
%10 = load i8*, i8** %3
%11 = load i32, i32* %4
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %10, i32 %11)
%13 = load i8*, i8** %3
%14 = load i32, i32* %4
%15 = call %struct.prog_context* @prog_exec(i8* %13, i32 %14)
store %struct.prog_context* %15, %struct.prog_context** %5
store i32 -1, i32* %6
%16 = call %struct.dynamic_buffer* (...) @dynbuffer_new()
store %struct.dynamic_buffer* %16, %struct.dynamic_buffer** %7
%17 = call %struct.dynamic_buffer* (...) @dynbuffer_new()
store %struct.dynamic_buffer* %17, %struct.dynamic_buffer** %8
%18 = load %struct.prog_context*, %struct.prog_context** %5
%19 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %7
%20 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %8
%21 = call i32 @prog_communicate(%struct.prog_context* %18, i32* null, %struct.dynamic_buffer* %19, %struct.dynamic_buffer* %20, i32 5)
%22 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %7
%23 = call i32 @dynbuffer_data_len(%struct.dynamic_buffer* %22)
%24 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %7
%25 = call i8* @dynbuffer_data(%struct.dynamic_buffer* %24)
%26 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %23, i8* %25)
%27 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %8
%28 = call i32 @dynbuffer_data_len(%struct.dynamic_buffer* %27)
%29 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %8
%30 = call i8* @dynbuffer_data(%struct.dynamic_buffer* %29)
%31 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %28, i8* %30)
%32 = load %struct.prog_context*, %struct.prog_context** %5
%33 = call i32 @prog_wait(%struct.prog_context* %32)
store i32 %33, i32* %6
%34 = load i32, i32* %6
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %34)
%36 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %7
%37 = call i32 @dynbuffer_free(%struct.dynamic_buffer* %36)
%38 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %8
%39 = call i32 @dynbuffer_free(%struct.dynamic_buffer* %38)
%40 = load %struct.prog_context*, %struct.prog_context** %5
%41 = call i32 @progcnx_free(%struct.prog_context* %40)
ret i32 0
}

LLVM IR (-O2):
%struct.prog_context = type { i32 }
%struct.dynamic_buffer = type { i32 }
@.str.1 = [21 x i8] c"cmd: [%s] Flg: [%d]\0A\00"
@.str.2 = [18 x i8] c"** xout [%d]:%s.\0A\00"
@.str.3 = [18 x i8] c"** xerr [%d]:%s.\0A\00"
@.str.4 = [10 x i8] c"RC: [%d]\0A\00"
@str = [25 x i8] c"------------------------\00"
define i32 @do_test(i8* %0, i32 %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%4 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.1, i64 0, i64 0), i8* %0, i32 %1)
%5 = tail call %struct.prog_context* @prog_exec(i8* %0, i32 %1)
%6 = tail call %struct.dynamic_buffer* (...) @dynbuffer_new()
%7 = tail call %struct.dynamic_buffer* (...) @dynbuffer_new()
%8 = tail call i32 @prog_communicate(%struct.prog_context* %5, i32* null, %struct.dynamic_buffer* %6, %struct.dynamic_buffer* %7, i32 5)
%9 = tail call i32 @dynbuffer_data_len(%struct.dynamic_buffer* %6)
%10 = tail call i8* @dynbuffer_data(%struct.dynamic_buffer* %6)
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0), i32 %9, i8* %10)
%12 = tail call i32 @dynbuffer_data_len(%struct.dynamic_buffer* %7)
%13 = tail call i8* @dynbuffer_data(%struct.dynamic_buffer* %7)
%14 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %12, i8* %13)
%15 = tail call i32 @prog_wait(%struct.prog_context* %5)
%16 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.4, i64 0, i64 0), i32 %15)
%17 = tail call i32 @dynbuffer_free(%struct.dynamic_buffer* %6)
%18 = tail call i32 @dynbuffer_free(%struct.dynamic_buffer* %7)
%19 = tail call i32 @progcnx_free(%struct.prog_context* %5)
ret i32 0
}