#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 */
int /*<<< orphan*/ recv_ack();
scalar_t__ send_msg(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strlen(char *);
void set_motor(int side, int speed) {
  char buf[80];
  if (speed > 127)
    speed = 127;
  if (speed < -127)
    speed = -127;
  switch (side) {
  case 0:
    sprintf(buf, "M L %d\n", speed);
    break;
  case 1:
    sprintf(buf, "M R %d\n", speed);
    break;
  }
  if (send_msg(buf, strlen(buf))) {
    recv_ack();
  }
}


x86 assembly (clang, -O0):
<set_motor>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %edi,-0x4(%rbp)
mov    %esi,-0x8(%rbp)
cmpl   $0x7f,-0x8(%rbp)
jle    115f <set_motor+0x1f>
movl   $0x7f,-0x8(%rbp)
cmpl   $0xffffff81,-0x8(%rbp)
jge    1170 <set_motor+0x30>
movl   $0xffffff81,-0x8(%rbp)
mov    -0x4(%rbp),%eax
mov    %eax,-0x64(%rbp)
test   %eax,%eax
je     1194 <set_motor+0x54>
jmp    1183 <set_motor+0x43>
mov    -0x64(%rbp),%eax
sub    $0x1,%eax
je     11ac <set_motor+0x6c>
jmp    11bf <set_motor+0x7f>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe5e(%rip),%rsi
call   1060 <sprintf@plt>
jmp    11bf <set_motor+0x7f>
lea    -0x60(%rbp),%rdi
mov    -0x8(%rbp),%edx
lea    0xe4e(%rip),%rsi
call   1060 <sprintf@plt>
lea    -0x60(%rbp),%rax
mov    %rax,-0x70(%rbp)
lea    -0x60(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x70(%rbp),%rdi
mov    %eax,%esi
call   1050 <send_msg@plt>
cmp    $0x0,%rax
je     11ec <set_motor+0xac>
mov    $0x0,%al
call   1030 <recv_ack@plt>
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_motor>:
push   %rbx
sub    $0x50,%rsp
cmp    $0x7f,%esi
mov    $0x7f,%eax
cmovl  %esi,%eax
cmp    $0xffffff82,%eax
mov    $0xffffff81,%edx
cmovge %eax,%edx
test   %edi,%edi
je     116d <set_motor+0x2d>
cmp    $0x1,%edi
jne    117c <set_motor+0x3c>
lea    0xe9d(%rip),%rsi
jmp    1174 <set_motor+0x34>
lea    0xe8c(%rip),%rsi
mov    %rsp,%rdi
call   1060 <sprintf@plt>
mov    %rsp,%rbx
mov    %rbx,%rdi
call   1040 <strlen@plt>
mov    %rbx,%rdi
mov    %eax,%esi
call   1050 <send_msg@plt>
test   %rax,%rax
je     119d <set_motor+0x5d>
xor    %eax,%eax
call   1030 <recv_ack@plt>
add    $0x50,%rsp
pop    %rbx
ret

LLVM IR (-O0):
@.str = [8 x i8] c"M L %d\0A\00"
@.str.1 = [8 x i8] c"M R %d\0A\00"
define void @set_motor(i32 %0, i32 %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca [80 x i8]
store i32 %0, i32* %3
store i32 %1, i32* %4
%6 = load i32, i32* %4
%7 = icmp sgt i32 %6, 127
br i1 %7, label %8, label %9
8: ; preds = %2
store i32 127, i32* %4
br label %9
9: ; preds = %8, %2
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, -127
br i1 %11, label %12, label %13
12: ; preds = %9
store i32 -127, i32* %4
br label %13
13: ; preds = %12, %9
%14 = load i32, i32* %3
switch i32 %14, label %23 [
i32 0, label %15
i32 1, label %19
]
15: ; preds = %13
%16 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%17 = load i32, i32* %4
%18 = call i32 @sprintf(i8* %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %17)
br label %23
19: ; preds = %13
%20 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%21 = load i32, i32* %4
%22 = call i32 @sprintf(i8* %20, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), i32 %21)
br label %23
23: ; preds = %13, %19, %15
%24 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%25 = getelementptr inbounds [80 x i8], [80 x i8]* %5, i64 0, i64 0
%26 = call i32 @strlen(i8* %25)
%27 = call i64 @send_msg(i8* %24, i32 %26)
%28 = icmp ne i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 (...) @recv_ack()
br label %31
31: ; preds = %29, %23
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"M L %d\0A\00"
@.str.1 = [8 x i8] c"M R %d\0A\00"
define void @set_motor(i32 %0, i32 %1) {
%3 = alloca [80 x i8]
%4 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%5 = icmp slt i32 %1, 127
%6 = select i1 %5, i32 %1, i32 127
%7 = icmp sgt i32 %6, -127
%8 = select i1 %7, i32 %6, i32 -127
switch i32 %0, label %13 [
i32 0, label %10
i32 1, label %9
]
9: ; preds = %2
br label %10
10: ; preds = %2, %9
%11 = phi i8* [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0), %9 ], [ getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), %2 ]
%12 = call i32 @sprintf(i8* %4, i8* dereferenceable(1) %11, i32 %8)
br label %13
13: ; preds = %10, %2
%14 = call i32 @strlen(i8* %4)
%15 = call i64 @send_msg(i8* %4, i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %19, label %17
17: ; preds = %13
%18 = call i32 (...) @recv_ack()
br label %19
19: ; preds = %17, %13
ret void
}