#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*/ free(char *);
int /*<<< orphan*/ getline(char **, size_t *, int /*<<< orphan*/);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ stdin;
int strtol(char *, int /*<<< orphan*/ *, int /*<<< orphan*/);
int get_port_type(int i) {
  bool done = false;
  char *line = NULL;
  size_t length = 0;
  int ret_val = 0;
  do {
    printf("Please enter type for port %c\n", 'A' + i);
    printf("1) RS-232\n");
    printf("2) RS-422\n");
    printf("3) RS-485(4W)\n");
    printf("4) RS-485(2W)\n");
    printf("=>");
    getline(&line, &length, stdin);
    ret_val = strtol(line, NULL, 0);
    free(line);
    if ((ret_val < 1) || (ret_val > 4)) {
      line = NULL;
      length = 0;
    } else {
      done = true;
    }
  } while (done != true);
  switch (ret_val) {
  case 1:
    ret_val = 0;
    break;
  case 2:
    ret_val = 1;
    break;
  case 3:
    ret_val = 0xb;
    break;
  case 4:
    ret_val = 0xf;
    break;
  default:
    printf("default fault:%d\n", 362);
    ret_val = 0;
    break;
  }
  return ret_val;
}


x86 assembly (clang, -O0):
<get_port_type>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
movl   $0x0,-0x8(%rbp)
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
movl   $0x0,-0x1c(%rbp)
mov    -0x4(%rbp),%esi
add    $0x41,%esi
lea    0xe9a(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xeab(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea8(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea5(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea6(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
lea    0xea7(%rip),%rdi
mov    $0x0,%al
call   1040 <printf@plt>
mov    0x2e1e(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <getline@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1050 <strtol@plt>
mov    %eax,-0x1c(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <free@plt>
cmpl   $0x1,-0x1c(%rbp)
jl     1208 <get_port_type+0xc8>
cmpl   $0x4,-0x1c(%rbp)
jle    121d <get_port_type+0xdd>
movq   $0x0,-0x10(%rbp)
movq   $0x0,-0x18(%rbp)
jmp    1224 <get_port_type+0xe4>
movl   $0x1,-0x8(%rbp)
jmp    1229 <get_port_type+0xe9>
cmpl   $0x1,-0x8(%rbp)
jne    1169 <get_port_type+0x29>
mov    -0x1c(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,%ecx
mov    %rcx,-0x28(%rbp)
sub    $0x3,%eax
ja     128c <get_port_type+0x14c>
mov    -0x28(%rbp),%rax
lea    0xdad(%rip),%rcx
movslq (%rcx,%rax,4),%rax
add    %rcx,%rax
jmp    *%rax
movl   $0x0,-0x1c(%rbp)
jmp    12a6 <get_port_type+0x166>
movl   $0x1,-0x1c(%rbp)
jmp    12a6 <get_port_type+0x166>
movl   $0xb,-0x1c(%rbp)
jmp    12a6 <get_port_type+0x166>
movl   $0xf,-0x1c(%rbp)
jmp    12a6 <get_port_type+0x166>
lea    0xdd3(%rip),%rdi
mov    $0x16a,%esi
mov    $0x0,%al
call   1040 <printf@plt>
movl   $0x0,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_port_type>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movq   $0x0,0x8(%rsp)
add    $0x41,%ebx
lea    0xeac(%rip),%r14
mov    0x2e6e(%rip),%rbp
lea    0x8(%rsp),%r15
lea    0x10(%rsp),%r12
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
movq   $0x0,0x10(%rsp)
lea    0xe60(%rip),%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1050 <printf@plt>
lea    0xe84(%rip),%rdi
call   1040 <puts@plt>
lea    0xe82(%rip),%rdi
call   1040 <puts@plt>
lea    0xe80(%rip),%rdi
call   1040 <puts@plt>
lea    0xe82(%rip),%rdi
call   1040 <puts@plt>
mov    %r14,%rdi
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x0(%rbp),%edx
mov    %r15,%rdi
mov    %r12,%rsi
call   1070 <getline@plt>
mov    0x8(%rsp),%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1060 <strtol@plt>
mov    %eax,%r13d
mov    0x8(%rsp),%rdi
call   1030 <free@plt>
lea    -0x5(%r13),%eax
cmp    $0xfffffffb,%eax
ja     1223 <get_port_type+0xd3>
movq   $0x0,0x8(%rsp)
jmp    1190 <get_port_type+0x40>
add    $0xffffffff,%r13d
cmp    $0x4,%r13d
jae    123c <get_port_type+0xec>
movslq %r13d,%rax
lea    0xe2d(%rip),%rcx
mov    (%rcx,%rax,4),%ebx
jmp    1251 <get_port_type+0x101>
lea    0xddf(%rip),%rdi
xor    %ebx,%ebx
mov    $0x16a,%esi
xor    %eax,%eax
call   1050 <printf@plt>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [31 x i8] c"Please enter type for port %c\0A\00"
@.str.1 = [11 x i8] c"1) RS-232\0A\00"
@.str.2 = [11 x i8] c"2) RS-422\0A\00"
@.str.3 = [15 x i8] c"3) RS-485(4W)\0A\00"
@.str.4 = [15 x i8] c"4) RS-485(2W)\0A\00"
@.str.5 = [3 x i8] c"=>\00"
@stdin = global i32 0
@.str.6 = [18 x i8] c"default fault:%d\0A\00"
define i32 @get_port_type(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i32
store i32 %0, i32* %2
store i32 0, i32* %3
store i8* null, i8** %4
store i64 0, i64* %5
store i32 0, i32* %6
br label %7
7: ; preds = %30, %1
%8 = load i32, i32* %2
%9 = add nsw i32 65, %8
%10 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %9)
%11 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%12 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.3, i64 0, i64 0))
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.4, i64 0, i64 0))
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%16 = load i32, i32* @stdin
%17 = call i32 @getline(i8** %4, i64* %5, i32 %16)
%18 = load i8*, i8** %4
%19 = call i32 @strtol(i8* %18, i32* null, i32 0)
store i32 %19, i32* %6
%20 = load i8*, i8** %4
%21 = call i32 @free(i8* %20)
%22 = load i32, i32* %6
%23 = icmp slt i32 %22, 1
br i1 %23, label %27, label %24
24: ; preds = %7
%25 = load i32, i32* %6
%26 = icmp sgt i32 %25, 4
br i1 %26, label %27, label %28
27: ; preds = %24, %7
store i8* null, i8** %4
store i64 0, i64* %5
br label %29
28: ; preds = %24
store i32 1, i32* %3
br label %29
29: ; preds = %28, %27
br label %30
30: ; preds = %29
%31 = load i32, i32* %3
%32 = icmp ne i32 %31, 1
br i1 %32, label %7, label %33
33: ; preds = %30
%34 = load i32, i32* %6
switch i32 %34, label %39 [
i32 1, label %35
i32 2, label %36
i32 3, label %37
i32 4, label %38
]
35: ; preds = %33
store i32 0, i32* %6
br label %41
36: ; preds = %33
store i32 1, i32* %6
br label %41
37: ; preds = %33
store i32 11, i32* %6
br label %41
38: ; preds = %33
store i32 15, i32* %6
br label %41
39: ; preds = %33
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 362)
store i32 0, i32* %6
br label %41
41: ; preds = %39, %38, %37, %36, %35
%42 = load i32, i32* %6
ret i32 %42
}

LLVM IR (-O2):
@.str = [31 x i8] c"Please enter type for port %c\0A\00"
@.str.5 = [3 x i8] c"=>\00"
@stdin = global i32 0
@.str.6 = [18 x i8] c"default fault:%d\0A\00"
@str = [10 x i8] c"1) RS-232\00"
@str.7 = [10 x i8] c"2) RS-422\00"
@str.8 = [14 x i8] c"3) RS-485(4W)\00"
@str.9 = [14 x i8] c"4) RS-485(2W)\00"
@switch.table.get_port_type = [4 x i32] [i32 0, i32 1, i32 11, i32 15]
define i32 @get_port_type(i32 %0) {
%2 = alloca i8*
%3 = alloca i64
%4 = bitcast i8** %2 to i8*
store i8* null, i8** %2
%5 = bitcast i64* %3 to i8*
%6 = add nsw i32 %0, 65
br label %7
7: ; preds = %22, %1
store i64 0, i64* %3
%8 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i32 %6)
%9 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str, i64 0, i64 0))
%10 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @str.7, i64 0, i64 0))
%11 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.8, i64 0, i64 0))
%12 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @str.9, i64 0, i64 0))
%13 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.5, i64 0, i64 0))
%14 = load i32, i32* @stdin
%15 = call i32 @getline(i8** %2, i64* %3, i32 %14)
%16 = load i8*, i8** %2
%17 = call i32 @strtol(i8* nocapture %16, i32* null, i32 0)
%18 = load i8*, i8** %2
%19 = call i32 @free(i8* %18)
%20 = add i32 %17, -5
%21 = icmp ult i32 %20, -4
br i1 %21, label %22, label %23
22: ; preds = %7
store i8* null, i8** %2
br label %7
23: ; preds = %7
%24 = add i32 %17, -1
%25 = icmp ult i32 %24, 4
br i1 %25, label %28, label %26
26: ; preds = %23
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.6, i64 0, i64 0), i32 362)
br label %32
28: ; preds = %23
%29 = sext i32 %24 to i64
%30 = getelementptr inbounds [4 x i32], [4 x i32]* @switch.table.get_port_type, i64 0, i64 %29
%31 = load i32, i32* %30
br label %32
32: ; preds = %28, %26
%33 = phi i32 [ 0, %26 ], [ %31, %28 ]
ret i32 %33
}