#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 FACOM_checkForErrors();
int /*<<< orphan*/ FACOM_getDiscreteAddress(unsigned char, int, char *);
int /*<<< orphan*/ FACOM_intToString(unsigned char, char *);
int FACOM_numberOfDigits(int);
int FACOM_write(char *);
int FACOM_setDiscrete(unsigned char discreteType, int discreteNumber,
                      unsigned char action) {
  if (discreteNumber < 0 || discreteNumber > 9999 || action < 1 || action > 4)
    return -2;
  char command[9];
  command[0] = '4';
  command[1] = '2';
  FACOM_intToString(action, &command[2]);
  FACOM_getDiscreteAddress(discreteType, discreteNumber, &command[3]);
  int numOfDigits = FACOM_numberOfDigits(discreteNumber);
  int error = FACOM_write(command);
  if (error < 0)
    return error;
  return FACOM_checkForErrors();
}


x86 assembly (clang, -O0):
<FACOM_setDiscrete>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %dl,%al
mov    %dil,%cl
mov    %cl,-0x5(%rbp)
mov    %esi,-0xc(%rbp)
mov    %al,-0xd(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1197 <FACOM_setDiscrete+0x47>
cmpl   $0x270f,-0xc(%rbp)
jg     1197 <FACOM_setDiscrete+0x47>
movzbl -0xd(%rbp),%eax
cmp    $0x1,%eax
jl     1197 <FACOM_setDiscrete+0x47>
movzbl -0xd(%rbp),%eax
cmp    $0x4,%eax
jle    11a3 <FACOM_setDiscrete+0x53>
movl   $0xfffffffe,-0x4(%rbp)
jmp    120a <FACOM_setDiscrete+0xba>
movb   $0x34,-0x16(%rbp)
movb   $0x32,-0x15(%rbp)
mov    -0xd(%rbp),%al
lea    -0x16(%rbp),%rsi
add    $0x2,%rsi
movzbl %al,%edi
call   1040 <FACOM_intToString@plt>
mov    -0x5(%rbp),%al
mov    -0xc(%rbp),%esi
lea    -0x16(%rbp),%rdx
add    $0x3,%rdx
movzbl %al,%edi
call   1030 <FACOM_getDiscreteAddress@plt>
mov    -0xc(%rbp),%edi
call   1070 <FACOM_numberOfDigits@plt>
mov    %eax,-0x1c(%rbp)
lea    -0x16(%rbp),%rdi
call   1050 <FACOM_write@plt>
mov    %eax,-0x20(%rbp)
cmpl   $0x0,-0x20(%rbp)
jge    1200 <FACOM_setDiscrete+0xb0>
mov    -0x20(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    120a <FACOM_setDiscrete+0xba>
mov    $0x0,%al
call   1060 <FACOM_checkForErrors@plt>
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FACOM_setDiscrete>:
mov    $0xfffffffe,%eax
cmp    $0x270f,%esi
ja     11b5 <FACOM_setDiscrete+0x65>
lea    -0x5(%rdx),%ecx
cmp    $0xfc,%cl
jb     11b5 <FACOM_setDiscrete+0x65>
push   %rbp
push   %rbx
sub    $0x18,%rsp
mov    %esi,%ebx
mov    %edi,%ebp
movw   $0x3234,0xf(%rsp)
lea    0x11(%rsp),%rsi
movzbl %dl,%edi
call   1040 <FACOM_intToString@plt>
lea    0x12(%rsp),%rdx
movzbl %bpl,%edi
mov    %ebx,%esi
call   1030 <FACOM_getDiscreteAddress@plt>
mov    %ebx,%edi
call   1070 <FACOM_numberOfDigits@plt>
lea    0xf(%rsp),%rdi
call   1050 <FACOM_write@plt>
test   %eax,%eax
js     11af <FACOM_setDiscrete+0x5f>
xor    %eax,%eax
call   1060 <FACOM_checkForErrors@plt>
add    $0x18,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
define i32 @FACOM_setDiscrete(i8 zeroext %0, i32 %1, i8 zeroext %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca i8
%8 = alloca [9 x i8]
%9 = alloca i32
%10 = alloca i32
store i8 %0, i8* %5
store i32 %1, i32* %6
store i8 %2, i8* %7
%11 = load i32, i32* %6
%12 = icmp slt i32 %11, 0
br i1 %12, label %24, label %13
13: ; preds = %3
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 9999
br i1 %15, label %24, label %16
16: ; preds = %13
%17 = load i8, i8* %7
%18 = zext i8 %17 to i32
%19 = icmp slt i32 %18, 1
br i1 %19, label %24, label %20
20: ; preds = %16
%21 = load i8, i8* %7
%22 = zext i8 %21 to i32
%23 = icmp sgt i32 %22, 4
br i1 %23, label %24, label %25
24: ; preds = %20, %16, %13, %3
store i32 -2, i32* %4
br label %45
25: ; preds = %20
%26 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 0
store i8 52, i8* %26
%27 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 1
store i8 50, i8* %27
%28 = load i8, i8* %7
%29 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 2
%30 = call i32 @FACOM_intToString(i8 zeroext %28, i8* %29)
%31 = load i8, i8* %5
%32 = load i32, i32* %6
%33 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 3
%34 = call i32 @FACOM_getDiscreteAddress(i8 zeroext %31, i32 %32, i8* %33)
%35 = load i32, i32* %6
%36 = call i32 @FACOM_numberOfDigits(i32 %35)
store i32 %36, i32* %9
%37 = getelementptr inbounds [9 x i8], [9 x i8]* %8, i64 0, i64 0
%38 = call i32 @FACOM_write(i8* %37)
store i32 %38, i32* %10
%39 = load i32, i32* %10
%40 = icmp slt i32 %39, 0
br i1 %40, label %41, label %43
41: ; preds = %25
%42 = load i32, i32* %10
store i32 %42, i32* %4
br label %45
43: ; preds = %25
%44 = call i32 (...) @FACOM_checkForErrors()
store i32 %44, i32* %4
br label %45
45: ; preds = %43, %41, %24
%46 = load i32, i32* %4
ret i32 %46
}

LLVM IR (-O2):
define i32 @FACOM_setDiscrete(i8 zeroext %0, i32 %1, i8 zeroext %2) {
%4 = alloca [9 x i8]
%5 = icmp ugt i32 %1, 9999
%6 = add i8 %2, -5
%7 = icmp ult i8 %6, -4
%8 = or i1 %5, %7
br i1 %8, label %23, label %9
9: ; preds = %3
%10 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 0
store i8 52, i8* %10
%11 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 1
store i8 50, i8* %11
%12 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 2
%13 = call i32 @FACOM_intToString(i8 zeroext %2, i8* %12)
%14 = getelementptr inbounds [9 x i8], [9 x i8]* %4, i64 0, i64 3
%15 = call i32 @FACOM_getDiscreteAddress(i8 zeroext %0, i32 %1, i8* %14)
%16 = call i32 @FACOM_numberOfDigits(i32 %1)
%17 = call i32 @FACOM_write(i8* %10)
%18 = icmp slt i32 %17, 0
br i1 %18, label %21, label %19
19: ; preds = %9
%20 = call i32 (...) @FACOM_checkForErrors()
br label %21
21: ; preds = %9, %19
%22 = phi i32 [ %20, %19 ], [ %17, %9 ]
br label %23
23: ; preds = %3, %21
%24 = phi i32 [ %22, %21 ], [ -2, %3 ]
ret i32 %24
}