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

/* Variables and functions */
scalar_t__ do_getc(unsigned char *);
int /*<<< orphan*/ printf(char *, ...);
int wait_for_ack(void) {
  unsigned char ch;
  int i;
  for (i = 0; i < 5; i++) {
    if (do_getc(&ch)) {
      if (ch == '+')
        return 1;
      printf("Bad ack [0x%x]\n", ch);
    }
  }
  printf("No ack\n");
  return 0;
}


x86 assembly (clang, -O0):
<wait_for_ack>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0xc(%rbp)
cmpl   $0x5,-0xc(%rbp)
jge    118a <wait_for_ack+0x6a>
lea    -0x5(%rbp),%rdi
call   1040 <do_getc@plt>
cmp    $0x0,%rax
je     1177 <wait_for_ack+0x57>
movzbl -0x5(%rbp),%eax
cmp    $0x2b,%eax
jne    1165 <wait_for_ack+0x45>
movl   $0x1,-0x4(%rbp)
jmp    119f <wait_for_ack+0x7f>
movzbl -0x5(%rbp),%esi
lea    0xe90(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    117c <wait_for_ack+0x5c>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    112f <wait_for_ack+0xf>
lea    0xe7f(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<wait_for_ack>:
push   %rax
lea    0x7(%rsp),%rdi
call   1050 <do_getc@plt>
test   %rax,%rax
je     1161 <wait_for_ack+0x31>
movzbl 0x7(%rsp),%esi
mov    $0x1,%eax
cmp    $0x2b,%esi
je     1223 <wait_for_ack+0xf3>
lea    0xea6(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x7(%rsp),%rdi
call   1050 <do_getc@plt>
test   %rax,%rax
je     1191 <wait_for_ack+0x61>
movzbl 0x7(%rsp),%esi
mov    $0x1,%eax
cmp    $0x2b,%esi
je     1223 <wait_for_ack+0xf3>
lea    0xe76(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x7(%rsp),%rdi
call   1050 <do_getc@plt>
test   %rax,%rax
je     11bd <wait_for_ack+0x8d>
movzbl 0x7(%rsp),%esi
mov    $0x1,%eax
cmp    $0x2b,%esi
je     1223 <wait_for_ack+0xf3>
lea    0xe4a(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x7(%rsp),%rdi
call   1050 <do_getc@plt>
test   %rax,%rax
je     11e9 <wait_for_ack+0xb9>
movzbl 0x7(%rsp),%esi
mov    $0x1,%eax
cmp    $0x2b,%esi
je     1223 <wait_for_ack+0xf3>
lea    0xe1e(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0x7(%rsp),%rdi
call   1050 <do_getc@plt>
test   %rax,%rax
je     1215 <wait_for_ack+0xe5>
movzbl 0x7(%rsp),%esi
mov    $0x1,%eax
cmp    $0x2b,%esi
je     1223 <wait_for_ack+0xf3>
lea    0xdf2(%rip),%rdi
xor    %eax,%eax
call   1040 <printf@plt>
lea    0xdf4(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
pop    %rcx
ret

LLVM IR (-O0):
@.str = [16 x i8] c"Bad ack [0x%x]\0A\00"
@.str.1 = [8 x i8] c"No ack\0A\00"
define i32 @wait_for_ack() {
%1 = alloca i32
%2 = alloca i8
%3 = alloca i32
store i32 0, i32* %3
br label %4
4: ; preds = %20, %0
%5 = load i32, i32* %3
%6 = icmp slt i32 %5, 5
br i1 %6, label %7, label %23
7: ; preds = %4
%8 = call i64 @do_getc(i8* %2)
%9 = icmp ne i64 %8, 0
br i1 %9, label %10, label %19
10: ; preds = %7
%11 = load i8, i8* %2
%12 = zext i8 %11 to i32
%13 = icmp eq i32 %12, 43
br i1 %13, label %14, label %15
14: ; preds = %10
store i32 1, i32* %1
br label %25
15: ; preds = %10
%16 = load i8, i8* %2
%17 = zext i8 %16 to i32
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %15, %7
br label %20
20: ; preds = %19
%21 = load i32, i32* %3
%22 = add nsw i32 %21, 1
store i32 %22, i32* %3
br label %4
23: ; preds = %4
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %1
br label %25
25: ; preds = %23, %14
%26 = load i32, i32* %1
ret i32 %26
}

LLVM IR (-O2):
@.str = [16 x i8] c"Bad ack [0x%x]\0A\00"
@str = [7 x i8] c"No ack\00"
define i32 @wait_for_ack() {
%1 = alloca i8
%2 = call i64 @do_getc(i8* %1)
%3 = icmp eq i64 %2, 0
br i1 %3, label %10, label %4
4: ; preds = %0
%5 = load i8, i8* %1
%6 = icmp eq i8 %5, 43
br i1 %6, label %48, label %7
7: ; preds = %4
%8 = zext i8 %5 to i32
%9 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %8)
br label %10
10: ; preds = %0, %7
%11 = call i64 @do_getc(i8* %1)
%12 = icmp eq i64 %11, 0
br i1 %12, label %19, label %13
13: ; preds = %10
%14 = load i8, i8* %1
%15 = icmp eq i8 %14, 43
br i1 %15, label %48, label %16
16: ; preds = %13
%17 = zext i8 %14 to i32
%18 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %19
19: ; preds = %16, %10
%20 = call i64 @do_getc(i8* %1)
%21 = icmp eq i64 %20, 0
br i1 %21, label %28, label %22
22: ; preds = %19
%23 = load i8, i8* %1
%24 = icmp eq i8 %23, 43
br i1 %24, label %48, label %25
25: ; preds = %22
%26 = zext i8 %23 to i32
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %26)
br label %28
28: ; preds = %25, %19
%29 = call i64 @do_getc(i8* %1)
%30 = icmp eq i64 %29, 0
br i1 %30, label %37, label %31
31: ; preds = %28
%32 = load i8, i8* %1
%33 = icmp eq i8 %32, 43
br i1 %33, label %48, label %34
34: ; preds = %31
%35 = zext i8 %32 to i32
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %35)
br label %37
37: ; preds = %34, %28
%38 = call i64 @do_getc(i8* %1)
%39 = icmp eq i64 %38, 0
br i1 %39, label %46, label %40
40: ; preds = %37
%41 = load i8, i8* %1
%42 = icmp eq i8 %41, 43
br i1 %42, label %48, label %43
43: ; preds = %40
%44 = zext i8 %41 to i32
%45 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i32 %44)
br label %46
46: ; preds = %43, %37
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str, i64 0, i64 0))
br label %48
48: ; preds = %4, %13, %22, %31, %40, %46
%49 = phi i32 [ 0, %46 ], [ 1, %40 ], [ 1, %31 ], [ 1, %22 ], [ 1, %13 ], [ 1, %4 ]
ret i32 %49
}