#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 /*<<< orphan*/ dynbuffer_free(struct dynamic_buffer *);
struct dynamic_buffer *dynbuffer_new();
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ prog_communicate(struct prog_context *, char *,
                                    struct dynamic_buffer *,
                                    struct dynamic_buffer *, int);
struct prog_context *prog_exec(char const *, int /*<<< orphan*/);
int prog_poll(struct prog_context *);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
int do_interact(const char *cmd) {
  struct prog_context *cnt = prog_exec(cmd, 0);
  int i = 0;
  do {
    struct dynamic_buffer *obuf = dynbuffer_new();
    struct dynamic_buffer *ebuf = dynbuffer_new();
    char buff[32];
    snprintf(buff, 32, "==> %d\n", i);
    printf("** in: %s", buff);
    prog_communicate(cnt, buff, obuf, ebuf, 5);
    printf("** xout: %s\n", dynbuffer_data(obuf));
    printf("** xerr: %s\n", dynbuffer_data(ebuf));
    dynbuffer_free(obuf);
    dynbuffer_free(ebuf);
  } while (prog_poll(cnt) == -1);
  return 0;
}


x86 assembly (clang, -O0):
<do_interact>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1030 <prog_exec@plt>
mov    %rax,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x0,%al
call   1060 <dynbuffer_new@plt>
mov    %rax,-0x20(%rbp)
mov    $0x0,%al
call   1060 <dynbuffer_new@plt>
mov    %rax,-0x28(%rbp)
lea    -0x50(%rbp),%rdi
mov    -0x14(%rbp),%ecx
mov    $0x20,%esi
lea    0xe35(%rip),%rdx
call   1050 <snprintf@plt>
lea    -0x50(%rbp),%rsi
lea    0xe2d(%rip),%rdi
call   1040 <printf@plt>
mov    -0x10(%rbp),%rdi
lea    -0x50(%rbp),%rsi
mov    -0x20(%rbp),%rdx
mov    -0x28(%rbp),%rcx
mov    $0x5,%r8d
call   10a0 <prog_communicate@plt>
mov    -0x20(%rbp),%rdi
call   1070 <dynbuffer_data@plt>
mov    %rax,%rsi
lea    0xe04(%rip),%rdi
call   1040 <printf@plt>
mov    -0x28(%rbp),%rdi
call   1070 <dynbuffer_data@plt>
mov    %rax,%rsi
lea    0xdf9(%rip),%rdi
call   1040 <printf@plt>
mov    -0x20(%rbp),%rdi
call   1090 <dynbuffer_free@plt>
mov    -0x28(%rbp),%rdi
call   1090 <dynbuffer_free@plt>
mov    -0x10(%rbp),%rdi
call   1080 <prog_poll@plt>
cmp    $0xffffffff,%eax
je     11a2 <do_interact+0x22>
xor    %eax,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_interact>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
xor    %esi,%esi
call   1030 <prog_exec@plt>
mov    %rax,%rbx
mov    %rsp,%r15
lea    0xe70(%rip),%r13
lea    0xe76(%rip),%rbp
nopl   0x0(%rax)
xor    %eax,%eax
call   1060 <dynbuffer_new@plt>
mov    %rax,%r14
xor    %eax,%eax
call   1060 <dynbuffer_new@plt>
mov    %rax,%r12
mov    %r15,%rdi
mov    $0x20,%esi
lea    0xe2d(%rip),%rdx
xor    %ecx,%ecx
call   1050 <snprintf@plt>
lea    0xe27(%rip),%rdi
mov    %r15,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
mov    %r15,%rsi
mov    %r14,%rdx
mov    %r12,%rcx
mov    $0x5,%r8d
call   10a0 <prog_communicate@plt>
mov    %r14,%rdi
call   1070 <dynbuffer_data@plt>
mov    %r13,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %r12,%rdi
call   1070 <dynbuffer_data@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1040 <printf@plt>
mov    %r14,%rdi
call   1090 <dynbuffer_free@plt>
mov    %r12,%rdi
call   1090 <dynbuffer_free@plt>
mov    %rbx,%rdi
call   1080 <prog_poll@plt>
cmp    $0xffffffff,%eax
je     11b0 <do_interact+0x30>
xor    %eax,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.prog_context = type { i32 }
%struct.dynamic_buffer = type { i32 }
@.str = [8 x i8] c"==> %d\0A\00"
@.str.1 = [10 x i8] c"** in: %s\00"
@.str.2 = [13 x i8] c"** xout: %s\0A\00"
@.str.3 = [13 x i8] c"** xerr: %s\0A\00"
define i32 @do_interact(i8* %0) {
%2 = alloca i8*
%3 = alloca %struct.prog_context*
%4 = alloca i32
%5 = alloca %struct.dynamic_buffer*
%6 = alloca %struct.dynamic_buffer*
%7 = alloca [32 x i8]
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** %3
store i32 0, i32* %4
br label %10
10: ; preds = %33, %1
%11 = call %struct.dynamic_buffer* (...) @dynbuffer_new()
store %struct.dynamic_buffer* %11, %struct.dynamic_buffer** %5
%12 = call %struct.dynamic_buffer* (...) @dynbuffer_new()
store %struct.dynamic_buffer* %12, %struct.dynamic_buffer** %6
%13 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%14 = load i32, i32* %4
%15 = call i32 @snprintf(i8* %13, i32 32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %14)
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%17 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %16)
%18 = load %struct.prog_context*, %struct.prog_context** %3
%19 = getelementptr inbounds [32 x i8], [32 x i8]* %7, i64 0, i64 0
%20 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %5
%21 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %6
%22 = call i32 @prog_communicate(%struct.prog_context* %18, i8* %19, %struct.dynamic_buffer* %20, %struct.dynamic_buffer* %21, i32 5)
%23 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %5
%24 = call i8* @dynbuffer_data(%struct.dynamic_buffer* %23)
%25 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %24)
%26 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %6
%27 = call i8* @dynbuffer_data(%struct.dynamic_buffer* %26)
%28 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %27)
%29 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %5
%30 = call i32 @dynbuffer_free(%struct.dynamic_buffer* %29)
%31 = load %struct.dynamic_buffer*, %struct.dynamic_buffer** %6
%32 = call i32 @dynbuffer_free(%struct.dynamic_buffer* %31)
br label %33
33: ; preds = %10
%34 = load %struct.prog_context*, %struct.prog_context** %3
%35 = call i32 @prog_poll(%struct.prog_context* %34)
%36 = icmp eq i32 %35, -1
br i1 %36, label %10, label %37
37: ; preds = %33
ret i32 0
}

LLVM IR (-O2):
%struct.prog_context = type { i32 }
%struct.dynamic_buffer = type { i32 }
@.str = [8 x i8] c"==> %d\0A\00"
@.str.1 = [10 x i8] c"** in: %s\00"
@.str.2 = [13 x i8] c"** xout: %s\0A\00"
@.str.3 = [13 x i8] c"** xerr: %s\0A\00"
define i32 @do_interact(i8* %0) {
%2 = alloca [32 x i8]
%3 = tail call %struct.prog_context* @prog_exec(i8* %0, i32 0)
%4 = getelementptr inbounds [32 x i8], [32 x i8]* %2, i64 0, i64 0
br label %5
5: ; preds = %5, %1
%6 = call %struct.dynamic_buffer* (...) @dynbuffer_new()
%7 = call %struct.dynamic_buffer* (...) @dynbuffer_new()
%8 = call i32 @snprintf(i8* %4, i32 32, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 0)
%9 = call i32 @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0), i8* %4)
%10 = call i32 @prog_communicate(%struct.prog_context* %3, i8* %4, %struct.dynamic_buffer* %6, %struct.dynamic_buffer* %7, i32 5)
%11 = call i8* @dynbuffer_data(%struct.dynamic_buffer* %6)
%12 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0), i8* %11)
%13 = call i8* @dynbuffer_data(%struct.dynamic_buffer* %7)
%14 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0), i8* %13)
%15 = call i32 @dynbuffer_free(%struct.dynamic_buffer* %6)
%16 = call i32 @dynbuffer_free(%struct.dynamic_buffer* %7)
%17 = call i32 @prog_poll(%struct.prog_context* %3)
%18 = icmp eq i32 %17, -1
br i1 %18, label %5, label %19
19: ; preds = %5
ret i32 0
}