#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*/ chout;

/* Variables and functions */
scalar_t__ malloc(int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ read(int, char *, int);
int /*<<< orphan*/ usleep(int);
int /*<<< orphan*/ write(int, char *, int);
int getprog(int fd) {
  char chout = 'x';
  char *sendc;
  char *recvc;
  sendc = (char *)malloc(255);
  recvc = (char *)malloc(255);
  recvc[0] = '\0';
  sendc[0] = 3;
  write(fd, sendc, 1);
  printf("dumping...\n");
  while (1) {
    read(fd, &chout, sizeof(chout));
    recvc[0] = chout;
    printf("%d ", recvc[0]);
    chout = 0;
    if (recvc[0] == -1)
      break;
    usleep(20000);
  }
  printf("\n");
  return 0;
}


x86 assembly (clang, -O0):
<getprog>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movb   $0x78,-0x5(%rbp)
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,-0x10(%rbp)
mov    $0xff,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x18(%rbp),%rax
movb   $0x0,(%rax)
mov    -0x10(%rbp),%rax
movb   $0x3,(%rax)
mov    -0x4(%rbp),%edi
mov    -0x10(%rbp),%rsi
mov    $0x1,%edx
call   1030 <write@plt>
lea    0xe5f(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    -0x4(%rbp),%edi
lea    -0x5(%rbp),%rsi
mov    $0x1,%edx
call   1050 <read@plt>
mov    -0x5(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0x18(%rbp),%rax
movsbl (%rax),%esi
lea    0xe3c(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
movb   $0x0,-0x5(%rbp)
mov    -0x18(%rbp),%rax
movsbl (%rax),%eax
cmp    $0xffffffff,%eax
jne    11f0 <getprog+0xa0>
jmp    11ff <getprog+0xaf>
mov    $0x4e20,%edi
call   1070 <usleep@plt>
jmp    11a8 <getprog+0x58>
lea    0xe04(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getprog>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebp
movb   $0x78,0x7(%rsp)
mov    $0xff,%edi
call   1080 <malloc@plt>
mov    %rax,%r14
mov    $0xff,%edi
call   1080 <malloc@plt>
mov    %rax,%rbx
movb   $0x0,(%rax)
movb   $0x3,(%r14)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1050 <write@plt>
lea    0xe4f(%rip),%rdi
call   1040 <puts@plt>
lea    0x7(%rsp),%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl 0x7(%rsp),%esi
mov    %sil,(%rbx)
lea    0xe26(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movb   $0x0,0x7(%rsp)
cmpb   $0xff,(%rbx)
je     1235 <getprog+0xc5>
lea    0x7(%rsp),%r14
lea    0xe09(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    $0x4e20,%edi
call   1090 <usleep@plt>
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0x1,%edx
call   1070 <read@plt>
movsbl 0x7(%rsp),%esi
mov    %sil,(%rbx)
mov    %r15,%rdi
xor    %eax,%eax
call   1060 <printf@plt>
movb   $0x0,0x7(%rsp)
cmpb   $0xff,(%rbx)
jne    1200 <getprog+0x90>
mov    $0xa,%edi
call   1030 <putchar@plt>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"dumping...\0A\00"
@.str.1 = [4 x i8] c"%d \00"
@.str.2 = [2 x i8] c"\0A\00"
define i32 @getprog(i32 %0) {
%2 = alloca i32
%3 = alloca i8
%4 = alloca i8*
%5 = alloca i8*
store i32 %0, i32* %2
store i8 120, i8* %3
%6 = call i64 @malloc(i32 255)
%7 = inttoptr i64 %6 to i8*
store i8* %7, i8** %4
%8 = call i64 @malloc(i32 255)
%9 = inttoptr i64 %8 to i8*
store i8* %9, i8** %5
%10 = load i8*, i8** %5
%11 = getelementptr inbounds i8, i8* %10, i64 0
store i8 0, i8* %11
%12 = load i8*, i8** %4
%13 = getelementptr inbounds i8, i8* %12, i64 0
store i8 3, i8* %13
%14 = load i32, i32* %2
%15 = load i8*, i8** %4
%16 = call i32 @write(i32 %14, i8* %15, i32 1)
%17 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
br label %18
18: ; preds = %1, %35
%19 = load i32, i32* %2
%20 = call i32 @read(i32 %19, i8* %3, i32 1)
%21 = load i8, i8* %3
%22 = load i8*, i8** %5
%23 = getelementptr inbounds i8, i8* %22, i64 0
store i8 %21, i8* %23
%24 = load i8*, i8** %5
%25 = getelementptr inbounds i8, i8* %24, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %27)
store i8 0, i8* %3
%29 = load i8*, i8** %5
%30 = getelementptr inbounds i8, i8* %29, i64 0
%31 = load i8, i8* %30
%32 = sext i8 %31 to i32
%33 = icmp eq i32 %32, -1
br i1 %33, label %34, label %35
34: ; preds = %18
br label %37
35: ; preds = %18
%36 = call i32 @usleep(i32 20000)
br label %18
37: ; preds = %34
%38 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
ret i32 0
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"%d \00"
@str = [11 x i8] c"dumping...\00"
define i32 @getprog(i32 %0) {
%2 = alloca i8
store i8 120, i8* %2
%3 = tail call i64 @malloc(i32 255)
%4 = inttoptr i64 %3 to i8*
%5 = tail call i64 @malloc(i32 255)
%6 = inttoptr i64 %5 to i8*
store i8 0, i8* %6
store i8 3, i8* %4
%7 = tail call i32 @write(i32 %0, i8* %4, i32 1)
%8 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
%9 = call i32 @read(i32 %0, i8* %2, i32 1)
%10 = load i8, i8* %2
store i8 %10, i8* %6
%11 = sext i8 %10 to i32
%12 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %11)
store i8 0, i8* %2
%13 = load i8, i8* %6
%14 = icmp eq i8 %13, -1
br i1 %14, label %23, label %15
15: ; preds = %1, %15
%16 = tail call i32 @usleep(i32 20000)
%17 = call i32 @read(i32 %0, i8* %2, i32 1)
%18 = load i8, i8* %2
store i8 %18, i8* %6
%19 = sext i8 %18 to i32
%20 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %19)
store i8 0, i8* %2
%21 = load i8, i8* %6
%22 = icmp eq i8 %21, -1
br i1 %22, label %23, label %15
23: ; preds = %15, %1
%24 = tail call i32 @putchar(i32 10)
ret i32 0
}