#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*/ FACOM_intToString(int, char *);
int FACOM_numberOfDigits(int);
int FACOM_getDiscreteAddress(unsigned char discreteType, int discreteNumber,
                             char *address) {
  if (discreteNumber < 0 || discreteNumber > 9999 || address == NULL)
    return -2;
  char type;
  switch (discreteType) {
  case 1:
    type = 'X';
    break;
  case 2:
    type = 'Y';
    break;
  case 3:
    type = 'M';
    break;
  case 4:
    type = 'S';
    break;
  case 5:
    type = 'T';
    break;
  case 6:
    type = 'C';
    break;
  default:
    return -2;
    break;
  }
  address[0] = type;
  int numOfDigits = FACOM_numberOfDigits(discreteNumber);
  size_t i;
  for (i = 0; i < 4 - numOfDigits; i++)
    address[i + 1] = '0';
  FACOM_intToString(discreteNumber, &address[i + 1]);
  return 0;
}


x86 assembly (clang, -O0):
<FACOM_getDiscreteAddress>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %dil,%al
mov    %al,-0x5(%rbp)
mov    %esi,-0xc(%rbp)
mov    %rdx,-0x18(%rbp)
cmpl   $0x0,-0xc(%rbp)
jl     1157 <FACOM_getDiscreteAddress+0x37>
cmpl   $0x270f,-0xc(%rbp)
jg     1157 <FACOM_getDiscreteAddress+0x37>
cmpq   $0x0,-0x18(%rbp)
jne    1163 <FACOM_getDiscreteAddress+0x43>
movl   $0xfffffffe,-0x4(%rbp)
jmp    123f <FACOM_getDiscreteAddress+0x11f>
movzbl -0x5(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x30(%rbp)
sub    $0x5,%eax
ja     11c3 <FACOM_getDiscreteAddress+0xa3>
mov    -0x30(%rbp),%rax
lea    0xe7c(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movb   $0x58,-0x19(%rbp)
jmp    11cf <FACOM_getDiscreteAddress+0xaf>
movb   $0x59,-0x19(%rbp)
jmp    11cf <FACOM_getDiscreteAddress+0xaf>
movb   $0x4d,-0x19(%rbp)
jmp    11cf <FACOM_getDiscreteAddress+0xaf>
movb   $0x53,-0x19(%rbp)
jmp    11cf <FACOM_getDiscreteAddress+0xaf>
movb   $0x54,-0x19(%rbp)
jmp    11cf <FACOM_getDiscreteAddress+0xaf>
movb   $0x43,-0x19(%rbp)
jmp    11cf <FACOM_getDiscreteAddress+0xaf>
movl   $0xfffffffe,-0x4(%rbp)
jmp    123f <FACOM_getDiscreteAddress+0x11f>
mov    -0x19(%rbp),%cl
mov    -0x18(%rbp),%rax
mov    %cl,(%rax)
mov    -0xc(%rbp),%edi
call   1040 <FACOM_numberOfDigits@plt>
mov    %eax,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x28(%rbp),%rax
mov    $0x4,%ecx
sub    -0x20(%rbp),%ecx
movslq %ecx,%rcx
cmp    %rcx,%rax
jae    1221 <FACOM_getDiscreteAddress+0x101>
mov    -0x18(%rbp),%rax
mov    -0x28(%rbp),%rcx
movb   $0x30,0x1(%rax,%rcx,1)
mov    -0x28(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x28(%rbp)
jmp    11eb <FACOM_getDiscreteAddress+0xcb>
mov    -0xc(%rbp),%edi
mov    -0x18(%rbp),%rsi
mov    -0x28(%rbp),%rax
add    $0x1,%rax
add    %rax,%rsi
call   1030 <FACOM_intToString@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<FACOM_getDiscreteAddress>:
push   %rbp
push   %r14
push   %rbx
mov    $0xfffffffe,%eax
cmp    $0x270f,%esi
ja     11b6 <FACOM_getDiscreteAddress+0x86>
mov    %rdx,%rbx
test   %rdx,%rdx
je     11b6 <FACOM_getDiscreteAddress+0x86>
add    $0xff,%dil
cmp    $0x5,%dil
ja     11b6 <FACOM_getDiscreteAddress+0x86>
mov    %esi,%ebp
movzbl %dil,%ecx
shl    $0x3,%ecx
movabs $0x4354534d5958,%rax
shr    %cl,%rax
mov    %al,(%rbx)
mov    %esi,%edi
call   1050 <FACOM_numberOfDigits@plt>
mov    $0x1,%r14d
cmp    $0x4,%eax
je     11a7 <FACOM_getDiscreteAddress+0x77>
mov    $0x4,%ecx
sub    %eax,%ecx
lea    0x1(%rbx),%rdi
cmp    $0x2,%ecx
mov    $0x1,%eax
cmovae %ecx,%eax
movslq %eax,%r14
mov    $0x30,%esi
mov    %r14,%rdx
call   1040 <memset@plt>
add    $0x1,%r14
add    %r14,%rbx
mov    %ebp,%edi
mov    %rbx,%rsi
call   1030 <FACOM_intToString@plt>
xor    %eax,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
define i32 @FACOM_getDiscreteAddress(i8 zeroext %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8
%9 = alloca i32
%10 = alloca i64
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 %19, label %13
13: ; preds = %3
%14 = load i32, i32* %6
%15 = icmp sgt i32 %14, 9999
br i1 %15, label %19, label %16
16: ; preds = %13
%17 = load i8*, i8** %7
%18 = icmp eq i8* %17, null
br i1 %18, label %19, label %20
19: ; preds = %16, %13, %3
store i32 -2, i32* %4
br label %57
20: ; preds = %16
%21 = load i8, i8* %5
%22 = zext i8 %21 to i32
switch i32 %22, label %29 [
i32 1, label %23
i32 2, label %24
i32 3, label %25
i32 4, label %26
i32 5, label %27
i32 6, label %28
]
23: ; preds = %20
store i8 88, i8* %8
br label %30
24: ; preds = %20
store i8 89, i8* %8
br label %30
25: ; preds = %20
store i8 77, i8* %8
br label %30
26: ; preds = %20
store i8 83, i8* %8
br label %30
27: ; preds = %20
store i8 84, i8* %8
br label %30
28: ; preds = %20
store i8 67, i8* %8
br label %30
29: ; preds = %20
store i32 -2, i32* %4
br label %57
30: ; preds = %28, %27, %26, %25, %24, %23
%31 = load i8, i8* %8
%32 = load i8*, i8** %7
%33 = getelementptr inbounds i8, i8* %32, i64 0
store i8 %31, i8* %33
%34 = load i32, i32* %6
%35 = call i32 @FACOM_numberOfDigits(i32 %34)
store i32 %35, i32* %9
store i64 0, i64* %10
br label %36
36: ; preds = %47, %30
%37 = load i64, i64* %10
%38 = load i32, i32* %9
%39 = sub nsw i32 4, %38
%40 = sext i32 %39 to i64
%41 = icmp ult i64 %37, %40
br i1 %41, label %42, label %50
42: ; preds = %36
%43 = load i8*, i8** %7
%44 = load i64, i64* %10
%45 = add i64 %44, 1
%46 = getelementptr inbounds i8, i8* %43, i64 %45
store i8 48, i8* %46
br label %47
47: ; preds = %42
%48 = load i64, i64* %10
%49 = add i64 %48, 1
store i64 %49, i64* %10
br label %36
50: ; preds = %36
%51 = load i32, i32* %6
%52 = load i8*, i8** %7
%53 = load i64, i64* %10
%54 = add i64 %53, 1
%55 = getelementptr inbounds i8, i8* %52, i64 %54
%56 = call i32 @FACOM_intToString(i32 %51, i8* %55)
store i32 0, i32* %4
br label %57
57: ; preds = %50, %29, %19
%58 = load i32, i32* %4
ret i32 %58
}

LLVM IR (-O2):
define i32 @FACOM_getDiscreteAddress(i8 zeroext %0, i32 %1, i8* %2) {
%4 = icmp ugt i32 %1, 9999
%5 = icmp eq i8* %2, null
%6 = or i1 %4, %5
br i1 %6, label %27, label %7
7: ; preds = %3
%8 = add i8 %0, -1
%9 = icmp ult i8 %8, 6
br i1 %9, label %10, label %27
10: ; preds = %7
%11 = zext i8 %8 to i48
%12 = shl nuw nsw i48 %11, 3
%13 = lshr i48 74029453891928, %12
%14 = trunc i48 %13 to i8
store i8 %14, i8* %2
%15 = tail call i32 @FACOM_numberOfDigits(i32 %1)
%16 = icmp eq i32 %15, 4
br i1 %16, label %23, label %17
17: ; preds = %10
%18 = sub nsw i32 4, %15
%19 = getelementptr i8, i8* %2, i64 1
%20 = call i32 @llvm.umax.i32(i32 %18, i32 1)
%21 = sext i32 %20 to i64
%22 = add nsw i64 %21, 1
br label %23
23: ; preds = %17, %10
%24 = phi i64 [ 1, %10 ], [ %22, %17 ]
%25 = getelementptr inbounds i8, i8* %2, i64 %24
%26 = tail call i32 @FACOM_intToString(i32 %1, i8* %25)
br label %27
27: ; preds = %7, %23, %3
%28 = phi i32 [ -2, %3 ], [ 0, %23 ], [ -2, %7 ]
ret i32 %28
}