#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 uint8_t;
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
int /*<<< orphan*/ printf(char *, ...);
int read(int, int *, int);
void discard_input(int fd) {
  uint8_t buffer[64];
  int res, i;
  int count_of_zeros;
  count_of_zeros = 0;
  do {
    res = read(fd, buffer, sizeof(buffer));
    if (res > 0) {
      for (i = 0; i < res; i++)
        if (buffer[i])
          break;
      count_of_zeros += i;
      if (i == res) {
        continue;
      }
      while (!buffer[res - 1]) {
        count_of_zeros++;
        res--;
      }
      printf("Recv[%d]:", res - i);
      for (; i < res; i++)
        printf("%02x ", buffer[i]);
      printf("\n");
    }
  } while (res > 0);
  if (count_of_zeros)
    printf("%d zeros ignored\n", count_of_zeros);
}


x86 assembly (clang, -O0):
<discard_input>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x11c(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x110(%rbp),%rsi
mov    $0x100,%edx
call   1040 <read@plt>
mov    %eax,-0x114(%rbp)
cmpl   $0x0,-0x114(%rbp)
jle    128e <discard_input+0x16e>
movl   $0x0,-0x118(%rbp)
mov    -0x118(%rbp),%eax
cmp    -0x114(%rbp),%eax
jge    11ae <discard_input+0x8e>
movslq -0x118(%rbp),%rax
cmpl   $0x0,-0x110(%rbp,%rax,4)
je     1195 <discard_input+0x75>
jmp    11ae <discard_input+0x8e>
jmp    119a <discard_input+0x7a>
mov    -0x118(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x118(%rbp)
jmp    1169 <discard_input+0x49>
mov    -0x118(%rbp),%eax
add    -0x11c(%rbp),%eax
mov    %eax,-0x11c(%rbp)
mov    -0x118(%rbp),%eax
cmp    -0x114(%rbp),%eax
jne    11d7 <discard_input+0xb7>
jmp    1293 <discard_input+0x173>
jmp    11dc <discard_input+0xbc>
mov    -0x114(%rbp),%eax
sub    $0x1,%eax
cltq
cmpl   $0x0,-0x110(%rbp,%rax,4)
setne  %al
xor    $0xff,%al
test   $0x1,%al
jne    1201 <discard_input+0xe1>
jmp    1224 <discard_input+0x104>
mov    -0x11c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x11c(%rbp)
mov    -0x114(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x114(%rbp)
jmp    11dc <discard_input+0xbc>
mov    -0x114(%rbp),%esi
sub    -0x118(%rbp),%esi
lea    0xdc9(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x118(%rbp),%eax
cmp    -0x114(%rbp),%eax
jge    1280 <discard_input+0x160>
movslq -0x118(%rbp),%rax
mov    -0x110(%rbp,%rax,4),%esi
lea    0xda5(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
mov    -0x118(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x118(%rbp)
jmp    123e <discard_input+0x11e>
lea    0xd99(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1293 <discard_input+0x173>
cmpl   $0x0,-0x114(%rbp)
jg     1138 <discard_input+0x18>
cmpl   $0x0,-0x11c(%rbp)
je     12c1 <discard_input+0x1a1>
mov    -0x11c(%rbp),%esi
lea    0xd56(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<discard_input>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
mov    %edi,%r14d
lea    0x10(%rsp),%rbp
lea    0xeba(%rip),%r12
xor    %ebx,%ebx
mov    %edi,0xc(%rsp)
jmp    117f <discard_input+0x4f>
nopl   0x0(%rax,%rax,1)
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r15d,%eax
mov    0xc(%rsp),%r14d
lea    0x10(%rsp),%rbp
test   %eax,%eax
jle    123c <discard_input+0x10c>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    $0x100,%edx
call   1050 <read@plt>
test   %eax,%eax
jle    123c <discard_input+0x10c>
mov    %eax,%ecx
xor    %r13d,%r13d
nopl   0x0(%rax)
cmpl   $0x0,0x10(%rsp,%r13,4)
jne    11c0 <discard_input+0x90>
add    $0x1,%r13
cmp    %rcx,%r13
jne    11a0 <discard_input+0x70>
mov    %eax,%ecx
add    %ebx,%ecx
mov    %ecx,%ebx
jmp    1177 <discard_input+0x47>
nopl   0x0(%rax)
cmp    %eax,%r13d
jne    11d0 <discard_input+0xa0>
add    %r13d,%ebx
jmp    1177 <discard_input+0x47>
nopw   0x0(%rax,%rax,1)
movslq %eax,%rcx
lea    0xc(%rsp),%rdx
lea    (%rdx,%rcx,4),%rcx
xor    %r14d,%r14d
nop
cmpl   $0x0,(%rcx,%r14,4)
lea    -0x1(%r14),%r14
je     11e0 <discard_input+0xb0>
add    $0x1,%eax
lea    (%rax,%r14,1),%r15
sub    %r13d,%eax
lea    (%r14,%rax,1),%esi
lea    0xe00(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0xffffffff,%ebx
add    %r13,%rbx
sub    %r14,%rbx
cmp    %r13d,%r15d
jle    1160 <discard_input+0x30>
mov    %r13d,%ebp
nopl   0x0(%rax)
mov    0x10(%rsp,%rbp,4),%esi
mov    %r12,%rdi
xor    %eax,%eax
call   1040 <printf@plt>
add    $0x1,%rbp
cmp    %r15d,%ebp
jl     1220 <discard_input+0xf0>
jmp    1160 <discard_input+0x30>
test   %ebx,%ebx
je     1261 <discard_input+0x131>
lea    0xdc9(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
jmp    1040 <printf@plt>
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [10 x i8] c"Recv[%d]:\00"
@.str.1 = [6 x i8] c"%02x \00"
@.str.2 = [2 x i8] c"\0A\00"
@.str.3 = [18 x i8] c"%d zeros ignored\0A\00"
define void @discard_input(i32 %0) {
%2 = alloca i32
%3 = alloca [64 x i32]
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %6
br label %7
7: ; preds = %72, %1
%8 = load i32, i32* %2
%9 = getelementptr inbounds [64 x i32], [64 x i32]* %3, i64 0, i64 0
%10 = call i32 @read(i32 %8, i32* %9, i32 256)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %71
13: ; preds = %7
store i32 0, i32* %5
br label %14
14: ; preds = %26, %13
%15 = load i32, i32* %5
%16 = load i32, i32* %4
%17 = icmp slt i32 %15, %16
br i1 %17, label %18, label %29
18: ; preds = %14
%19 = load i32, i32* %5
%20 = sext i32 %19 to i64
%21 = getelementptr inbounds [64 x i32], [64 x i32]* %3, i64 0, i64 %20
%22 = load i32, i32* %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %18
br label %29
25: ; preds = %18
br label %26
26: ; preds = %25
%27 = load i32, i32* %5
%28 = add nsw i32 %27, 1
store i32 %28, i32* %5
br label %14
29: ; preds = %24, %14
%30 = load i32, i32* %5
%31 = load i32, i32* %6
%32 = add nsw i32 %31, %30
store i32 %32, i32* %6
%33 = load i32, i32* %5
%34 = load i32, i32* %4
%35 = icmp eq i32 %33, %34
br i1 %35, label %36, label %37
36: ; preds = %29
br label %72
37: ; preds = %29
br label %38
38: ; preds = %46, %37
%39 = load i32, i32* %4
%40 = sub nsw i32 %39, 1
%41 = sext i32 %40 to i64
%42 = getelementptr inbounds [64 x i32], [64 x i32]* %3, i64 0, i64 %41
%43 = load i32, i32* %42
%44 = icmp ne i32 %43, 0
%45 = xor i1 %44, true
br i1 %45, label %46, label %51
46: ; preds = %38
%47 = load i32, i32* %6
%48 = add nsw i32 %47, 1
store i32 %48, i32* %6
%49 = load i32, i32* %4
%50 = add nsw i32 %49, -1
store i32 %50, i32* %4
br label %38
51: ; preds = %38
%52 = load i32, i32* %4
%53 = load i32, i32* %5
%54 = sub nsw i32 %52, %53
%55 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %54)
br label %56
56: ; preds = %66, %51
%57 = load i32, i32* %5
%58 = load i32, i32* %4
%59 = icmp slt i32 %57, %58
br i1 %59, label %60, label %69
60: ; preds = %56
%61 = load i32, i32* %5
%62 = sext i32 %61 to i64
%63 = getelementptr inbounds [64 x i32], [64 x i32]* %3, i64 0, i64 %62
%64 = load i32, i32* %63
%65 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %64)
br label %66
66: ; preds = %60
%67 = load i32, i32* %5
%68 = add nsw i32 %67, 1
store i32 %68, i32* %5
br label %56
69: ; preds = %56
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.2, i64 0, i64 0))
br label %71
71: ; preds = %69, %7
br label %72
72: ; preds = %71, %36
%73 = load i32, i32* %4
%74 = icmp sgt i32 %73, 0
br i1 %74, label %7, label %75
75: ; preds = %72
%76 = load i32, i32* %6
%77 = icmp ne i32 %76, 0
br i1 %77, label %78, label %81
78: ; preds = %75
%79 = load i32, i32* %6
%80 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %79)
br label %81
81: ; preds = %78, %75
ret void
}

LLVM IR (-O2):
@.str = [10 x i8] c"Recv[%d]:\00"
@.str.1 = [6 x i8] c"%02x \00"
@.str.3 = [18 x i8] c"%d zeros ignored\0A\00"
define void @discard_input(i32 %0) {
%2 = alloca [64 x i32]
%3 = bitcast [64 x i32]* %2 to i8*
%4 = getelementptr inbounds [64 x i32], [64 x i32]* %2, i64 0, i64 0
br label %5
5: ; preds = %52, %1
%6 = phi i32 [ 0, %1 ], [ %54, %52 ]
%7 = call i32 @read(i32 %0, i32* %4, i32 256)
%8 = icmp sgt i32 %7, 0
br i1 %8, label %9, label %56
9: ; preds = %5
%10 = zext i32 %7 to i64
br label %11
11: ; preds = %9, %16
%12 = phi i64 [ 0, %9 ], [ %17, %16 ]
%13 = getelementptr inbounds [64 x i32], [64 x i32]* %2, i64 0, i64 %12
%14 = load i32, i32* %13
%15 = icmp eq i32 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %11
%17 = add nuw nsw i64 %12, 1
%18 = icmp eq i64 %17, %10
br i1 %18, label %19, label %11
19: ; preds = %16
%20 = add nsw i32 %7, %6
br label %52
21: ; preds = %11
%22 = trunc i64 %12 to i32
%23 = add nsw i32 %6, %22
%24 = icmp eq i32 %7, %22
br i1 %24, label %52, label %25
25: ; preds = %21
%26 = sext i32 %7 to i64
br label %27
27: ; preds = %25, %27
%28 = phi i64 [ %26, %25 ], [ %30, %27 ]
%29 = phi i32 [ %23, %25 ], [ %34, %27 ]
%30 = add i64 %28, -1
%31 = getelementptr inbounds [64 x i32], [64 x i32]* %2, i64 0, i64 %30
%32 = load i32, i32* %31
%33 = icmp eq i32 %32, 0
%34 = add nsw i32 %29, 1
br i1 %33, label %27, label %35
35: ; preds = %27
%36 = trunc i64 %28 to i32
%37 = sub nsw i32 %36, %22
%38 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %37)
%39 = icmp sgt i32 %36, %22
br i1 %39, label %40, label %50
40: ; preds = %35
%41 = and i64 %12, 4294967295
br label %42
42: ; preds = %40, %42
%43 = phi i64 [ %41, %40 ], [ %47, %42 ]
%44 = getelementptr inbounds [64 x i32], [64 x i32]* %2, i64 0, i64 %43
%45 = load i32, i32* %44
%46 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i32 %45)
%47 = add nuw nsw i64 %43, 1
%48 = trunc i64 %47 to i32
%49 = icmp slt i32 %48, %36
br i1 %49, label %42, label %50
50: ; preds = %42, %35
%51 = tail call i32 @putchar(i32 10)
br label %52
52: ; preds = %19, %50, %21
%53 = phi i32 [ %7, %21 ], [ %36, %50 ], [ %7, %19 ]
%54 = phi i32 [ %23, %21 ], [ %29, %50 ], [ %20, %19 ]
%55 = icmp sgt i32 %53, 0
br i1 %55, label %5, label %56
56: ; preds = %5, %52
%57 = phi i32 [ %54, %52 ], [ %6, %5 ]
%58 = icmp eq i32 %57, 0
br i1 %58, label %61, label %59
59: ; preds = %56
%60 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i32 %57)
br label %61
61: ; preds = %59, %56
ret void
}