#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__ check_ack();
int /*<<< orphan*/ error(char *);
int /*<<< orphan*/ printf(char *, int);
int read_one();
scalar_t__ stm_cmd(int);
int stm_chip(int verbose) {
  int num;
  int i;
  int val;
  int rv = 0;
  if (stm_cmd(0x02) == 0)
    error("CHIP command rejected");
  num = read_one();
  for (i = 0; i <= num; i++) {
    val = read_one();
    if (verbose)
      printf("CHIP = %02x\n", val);
    rv = rv << 8 | val;
  }
  if (check_ack() == 0)
    error("GET chip ended badly");
  return rv;
}


x86 assembly (clang, -O0):
<stm_chip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x14(%rbp)
mov    $0x2,%edi
call   1030 <stm_cmd@plt>
cmp    $0x0,%rax
jne    1182 <stm_chip+0x32>
lea    0xe83(%rip),%rdi
call   1070 <error@plt>
mov    $0x0,%al
call   1050 <read_one@plt>
mov    %eax,-0x8(%rbp)
movl   $0x0,-0xc(%rbp)
mov    -0xc(%rbp),%eax
cmp    -0x8(%rbp),%eax
jg     11dc <stm_chip+0x8c>
mov    $0x0,%al
call   1050 <read_one@plt>
mov    %eax,-0x10(%rbp)
cmpl   $0x0,-0x4(%rbp)
je     11c2 <stm_chip+0x72>
mov    -0x10(%rbp),%esi
lea    0xe59(%rip),%rdi
call   1060 <printf@plt>
mov    -0x14(%rbp),%eax
shl    $0x8,%eax
or     -0x10(%rbp),%eax
mov    %eax,-0x14(%rbp)
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    1193 <stm_chip+0x43>
mov    $0x0,%al
call   1040 <check_ack@plt>
cmp    $0x0,%rax
jne    11f9 <stm_chip+0xa9>
lea    0xe2f(%rip),%rdi
call   1070 <error@plt>
mov    -0x14(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<stm_chip>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %edi,%r14d
mov    $0x2,%edi
call   1030 <stm_cmd@plt>
test   %rax,%rax
jne    1176 <stm_chip+0x26>
lea    0xe8f(%rip),%rdi
call   1070 <error@plt>
xor    %ebp,%ebp
xor    %eax,%eax
call   1050 <read_one@plt>
test   %eax,%eax
js     11c7 <stm_chip+0x77>
mov    %eax,%ebx
add    $0x1,%ebx
lea    0xe87(%rip),%r15
xor    %ebp,%ebp
jmp    11ab <stm_chip+0x5b>
cs nopw 0x0(%rax,%rax,1)
nopl   (%rax)
shl    $0x8,%ebp
or     %r12d,%ebp
add    $0xffffffff,%ebx
je     11c7 <stm_chip+0x77>
xor    %eax,%eax
call   1050 <read_one@plt>
mov    %eax,%r12d
test   %r14d,%r14d
je     11a0 <stm_chip+0x50>
mov    %r15,%rdi
mov    %r12d,%esi
call   1060 <printf@plt>
jmp    11a0 <stm_chip+0x50>
xor    %eax,%eax
call   1040 <check_ack@plt>
test   %rax,%rax
jne    11df <stm_chip+0x8f>
lea    0xe49(%rip),%rdi
call   1070 <error@plt>
mov    %ebp,%eax
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [22 x i8] c"CHIP command rejected\00"
@.str.1 = [13 x i8] c"CHIP = %02x\0A\00"
@.str.2 = [21 x i8] c"GET chip ended badly\00"
define i32 @stm_chip(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %6
%7 = call i64 @stm_cmd(i32 2)
%8 = icmp eq i64 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %11
11: ; preds = %9, %1
%12 = call i32 (...) @read_one()
store i32 %12, i32* %3
store i32 0, i32* %4
br label %13
13: ; preds = %29, %11
%14 = load i32, i32* %4
%15 = load i32, i32* %3
%16 = icmp sle i32 %14, %15
br i1 %16, label %17, label %32
17: ; preds = %13
%18 = call i32 (...) @read_one()
store i32 %18, i32* %5
%19 = load i32, i32* %2
%20 = icmp ne i32 %19, 0
br i1 %20, label %21, label %24
21: ; preds = %17
%22 = load i32, i32* %5
%23 = call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %22)
br label %24
24: ; preds = %21, %17
%25 = load i32, i32* %6
%26 = shl i32 %25, 8
%27 = load i32, i32* %5
%28 = or i32 %26, %27
store i32 %28, i32* %6
br label %29
29: ; preds = %24
%30 = load i32, i32* %4
%31 = add nsw i32 %30, 1
store i32 %31, i32* %4
br label %13
32: ; preds = %13
%33 = call i64 (...) @check_ack()
%34 = icmp eq i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %32
%36 = call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %37
37: ; preds = %35, %32
%38 = load i32, i32* %6
ret i32 %38
}

LLVM IR (-O2):
@.str = [22 x i8] c"CHIP command rejected\00"
@.str.1 = [13 x i8] c"CHIP = %02x\0A\00"
@.str.2 = [21 x i8] c"GET chip ended badly\00"
define i32 @stm_chip(i32 %0) {
%2 = tail call i64 @stm_cmd(i32 2)
%3 = icmp eq i64 %2, 0
br i1 %3, label %4, label %6
4: ; preds = %1
%5 = tail call i32 @error(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
br label %6
6: ; preds = %4, %1
%7 = tail call i32 (...) @read_one()
%8 = icmp slt i32 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %6
%10 = icmp eq i32 %0, 0
br label %11
11: ; preds = %9, %17
%12 = phi i32 [ 0, %9 ], [ %19, %17 ]
%13 = phi i32 [ 0, %9 ], [ %20, %17 ]
%14 = tail call i32 (...) @read_one()
br i1 %10, label %17, label %15
15: ; preds = %11
%16 = tail call i32 @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %14)
br label %17
17: ; preds = %15, %11
%18 = shl i32 %12, 8
%19 = or i32 %14, %18
%20 = add nuw i32 %13, 1
%21 = icmp eq i32 %13, %7
br i1 %21, label %22, label %11
22: ; preds = %17, %6
%23 = phi i32 [ 0, %6 ], [ %19, %17 ]
%24 = tail call i64 (...) @check_ack()
%25 = icmp eq i64 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %22
%27 = tail call i32 @error(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %28
28: ; preds = %26, %22
ret i32 %23
}