#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 */
typedef scalar_t__ speed_t;

/* Variables and functions */
int atoi(char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ plc_close_port();
scalar_t__ plc_get_id(unsigned short *, unsigned short *);
scalar_t__ plc_nop();
scalar_t__ plc_open_port(char *, scalar_t__);
int /*<<< orphan*/ printf(char *, unsigned short, unsigned short);
int /*<<< orphan*/ stderr;
scalar_t__ tty_value_to_baud(int);
int main(int argc, char **argv) {
  unsigned short netid, id;
  speed_t baud;
  int tty_value = -1;
  if (argc != 3) {
    fprintf(stderr, "Usage: plcid DEVICE BAUD\n");
    return 1;
  }
  char *port = argv[1];
  tty_value = atoi(argv[2]);
  if (tty_value < 0) {
    fprintf(stderr, "Invalid baud rate: %s\n", argv[2]);
    return 1;
  }
  baud = tty_value_to_baud(tty_value);
  if (baud < 0) {
    fprintf(stderr, "Invalid baud rate: %s\n", argv[2]);
    return 1;
  }
  if (plc_open_port(port, baud)) {
    fprintf(stderr, "Failed to open port\n");
    plc_close_port();
    return 1;
  }
  if (plc_nop()) {
    fprintf(stderr, "NOP failed\n");
    plc_close_port();
    return 1;
  }
  if (plc_get_id(&netid, &id)) {
    fprintf(stderr, "Failed to get ID\n");
    plc_close_port();
    return 1;
  }
  printf("%d.%d\n", netid, id);
  plc_close_port();
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x24(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11ca <main+0x4a>
mov    0x2e32(%rip),%rax
mov    (%rax),%edi
lea    0xe49(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133f <main+0x1bf>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    121b <main+0x9b>
mov    0x2de9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xe12(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133f <main+0x1bf>
mov    -0x24(%rbp),%edi
call   1060 <tty_value_to_baud@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jge    125d <main+0xdd>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdx
lea    0xdd0(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    133f <main+0x1bf>
mov    -0x30(%rbp),%rdi
mov    -0x20(%rbp),%rsi
call   1080 <plc_open_port@plt>
cmp    $0x0,%rax
je     129e <main+0x11e>
mov    0x2d65(%rip),%rax
mov    (%rax),%edi
lea    0xdad(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   10a0 <plc_close_port@plt>
movl   $0x1,-0x4(%rbp)
jmp    133f <main+0x1bf>
mov    $0x0,%al
call   1070 <plc_nop@plt>
cmp    $0x0,%rax
je     12d9 <main+0x159>
mov    0x2d2a(%rip),%rax
mov    (%rax),%edi
lea    0xd87(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   10a0 <plc_close_port@plt>
movl   $0x1,-0x4(%rbp)
jmp    133f <main+0x1bf>
lea    -0x12(%rbp),%rdi
lea    -0x14(%rbp),%rsi
call   1030 <plc_get_id@plt>
cmp    $0x0,%rax
je     131a <main+0x19a>
mov    0x2ce9(%rip),%rax
mov    (%rax),%edi
lea    0xd52(%rip),%rsi
mov    $0x0,%al
call   1050 <fprintf@plt>
mov    $0x0,%al
call   10a0 <plc_close_port@plt>
movl   $0x1,-0x4(%rbp)
jmp    133f <main+0x1bf>
mov    -0x12(%rbp),%ax
lea    0xd3f(%rip),%rdi
movzwl %ax,%esi
movzwl -0x14(%rbp),%edx
call   1040 <printf@plt>
mov    $0x0,%al
call   10a0 <plc_close_port@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
cmp    $0x3,%edi
jne    11d6 <main+0x56>
mov    %rsi,%rbx
mov    0x8(%rsi),%r14
mov    0x10(%rsi),%r15
mov    %r15,%rdi
call   1090 <atoi@plt>
test   %eax,%eax
js     11f2 <main+0x72>
mov    %eax,%edi
call   1060 <tty_value_to_baud@plt>
test   %rax,%rax
js     1207 <main+0x87>
mov    %r14,%rdi
mov    %rax,%rsi
call   1080 <plc_open_port@plt>
test   %rax,%rax
je     1224 <main+0xa4>
mov    0x2e18(%rip),%rax
mov    (%rax),%edi
lea    0xe60(%rip),%rsi
jmp    1266 <main+0xe6>
mov    0x2e03(%rip),%rax
mov    (%rax),%edi
lea    0xe1a(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1274 <main+0xf4>
mov    0x2de7(%rip),%rax
mov    (%rax),%edi
lea    0xe18(%rip),%rsi
mov    %r15,%rdx
jmp    121b <main+0x9b>
mov    0x2dd2(%rip),%rax
mov    (%rax),%edi
mov    0x10(%rbx),%rdx
lea    0xdff(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
jmp    1274 <main+0xf4>
xor    %eax,%eax
call   1070 <plc_nop@plt>
test   %rax,%rax
je     1242 <main+0xc2>
mov    0x2da9(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
jmp    1266 <main+0xe6>
lea    0xe(%rsp),%rdi
lea    0xc(%rsp),%rsi
call   1030 <plc_get_id@plt>
test   %rax,%rax
je     1285 <main+0x105>
mov    0x2d83(%rip),%rax
mov    (%rax),%edi
lea    0xdec(%rip),%rsi
xor    %eax,%eax
call   1050 <fprintf@plt>
xor    %eax,%eax
call   10a0 <plc_close_port@plt>
mov    $0x1,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
movzwl 0xe(%rsp),%esi
movzwl 0xc(%rsp),%edx
lea    0xdce(%rip),%rdi
call   1040 <printf@plt>
xor    %ebx,%ebx
xor    %eax,%eax
call   10a0 <plc_close_port@plt>
jmp    1279 <main+0xf9>

LLVM IR (-O0):
@stderr = global i32 0
@.str = [26 x i8] c"Usage: plcid DEVICE BAUD\0A\00"
@.str.1 = [23 x i8] c"Invalid baud rate: %s\0A\00"
@.str.2 = [21 x i8] c"Failed to open port\0A\00"
@.str.3 = [12 x i8] c"NOP failed\0A\00"
@.str.4 = [18 x i8] c"Failed to get ID\0A\00"
@.str.5 = [7 x i8] c"%d.%d\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i16
%7 = alloca i16
%8 = alloca i64
%9 = alloca i32
%10 = alloca i8*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %9
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 3
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = call i32 (i32, i8*, ...) @fprintf(i32 %14, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %71
16: ; preds = %2
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 1
%19 = load i8*, i8** %18
store i8* %19, i8** %10
%20 = load i8**, i8*** %5
%21 = getelementptr inbounds i8*, i8** %20, i64 2
%22 = load i8*, i8** %21
%23 = call i32 @atoi(i8* %22)
store i32 %23, i32* %9
%24 = load i32, i32* %9
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %32
26: ; preds = %16
%27 = load i32, i32* @stderr
%28 = load i8**, i8*** %5
%29 = getelementptr inbounds i8*, i8** %28, i64 2
%30 = load i8*, i8** %29
%31 = call i32 (i32, i8*, ...) @fprintf(i32 %27, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %30)
store i32 1, i32* %3
br label %71
32: ; preds = %16
%33 = load i32, i32* %9
%34 = call i64 @tty_value_to_baud(i32 %33)
store i64 %34, i64* %8
%35 = load i64, i64* %8
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %43
37: ; preds = %32
%38 = load i32, i32* @stderr
%39 = load i8**, i8*** %5
%40 = getelementptr inbounds i8*, i8** %39, i64 2
%41 = load i8*, i8** %40
%42 = call i32 (i32, i8*, ...) @fprintf(i32 %38, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %41)
store i32 1, i32* %3
br label %71
43: ; preds = %32
%44 = load i8*, i8** %10
%45 = load i64, i64* %8
%46 = call i64 @plc_open_port(i8* %44, i64 %45)
%47 = icmp ne i64 %46, 0
br i1 %47, label %48, label %52
48: ; preds = %43
%49 = load i32, i32* @stderr
%50 = call i32 (i32, i8*, ...) @fprintf(i32 %49, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%51 = call i32 (...) @plc_close_port()
store i32 1, i32* %3
br label %71
52: ; preds = %43
%53 = call i64 (...) @plc_nop()
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %59
55: ; preds = %52
%56 = load i32, i32* @stderr
%57 = call i32 (i32, i8*, ...) @fprintf(i32 %56, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%58 = call i32 (...) @plc_close_port()
store i32 1, i32* %3
br label %71
59: ; preds = %52
%60 = call i64 @plc_get_id(i16* %6, i16* %7)
%61 = icmp ne i64 %60, 0
br i1 %61, label %62, label %66
62: ; preds = %59
%63 = load i32, i32* @stderr
%64 = call i32 (i32, i8*, ...) @fprintf(i32 %63, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%65 = call i32 (...) @plc_close_port()
store i32 1, i32* %3
br label %71
66: ; preds = %59
%67 = load i16, i16* %6
%68 = load i16, i16* %7
%69 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i16 zeroext %67, i16 zeroext %68)
%70 = call i32 (...) @plc_close_port()
store i32 0, i32* %3
br label %71
71: ; preds = %66, %62, %55, %48, %37, %26, %13
%72 = load i32, i32* %3
ret i32 %72
}

LLVM IR (-O2):
@stderr = global i32 0
@.str = [26 x i8] c"Usage: plcid DEVICE BAUD\0A\00"
@.str.1 = [23 x i8] c"Invalid baud rate: %s\0A\00"
@.str.2 = [21 x i8] c"Failed to open port\0A\00"
@.str.3 = [12 x i8] c"NOP failed\0A\00"
@.str.4 = [18 x i8] c"Failed to get ID\0A\00"
@.str.5 = [7 x i8] c"%d.%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca i16
%4 = alloca i16
%5 = bitcast i16* %3 to i8*
%6 = bitcast i16* %4 to i8*
%7 = icmp eq i32 %0, 3
br i1 %7, label %11, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str, i64 0, i64 0))
br label %54
11: ; preds = %2
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = getelementptr inbounds i8*, i8** %1, i64 2
%15 = load i8*, i8** %14
%16 = tail call i32 @atoi(i8* %15)
%17 = icmp slt i32 %16, 0
br i1 %17, label %18, label %21
18: ; preds = %11
%19 = load i32, i32* @stderr
%20 = tail call i32 (i32, i8*, ...) @fprintf(i32 %19, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %15)
br label %54
21: ; preds = %11
%22 = tail call i64 @tty_value_to_baud(i32 %16)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* @stderr
%26 = load i8*, i8** %14
%27 = tail call i32 (i32, i8*, ...) @fprintf(i32 %25, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0), i8* %26)
br label %54
28: ; preds = %21
%29 = tail call i64 @plc_open_port(i8* %13, i64 %22)
%30 = icmp eq i64 %29, 0
br i1 %30, label %35, label %31
31: ; preds = %28
%32 = load i32, i32* @stderr
%33 = tail call i32 (i32, i8*, ...) @fprintf(i32 %32, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
%34 = tail call i32 (...) @plc_close_port()
br label %54
35: ; preds = %28
%36 = tail call i64 (...) @plc_nop()
%37 = icmp eq i64 %36, 0
br i1 %37, label %42, label %38
38: ; preds = %35
%39 = load i32, i32* @stderr
%40 = tail call i32 (i32, i8*, ...) @fprintf(i32 %39, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
%41 = tail call i32 (...) @plc_close_port()
br label %54
42: ; preds = %35
%43 = call i64 @plc_get_id(i16* %3, i16* %4)
%44 = icmp eq i64 %43, 0
br i1 %44, label %49, label %45
45: ; preds = %42
%46 = load i32, i32* @stderr
%47 = call i32 (i32, i8*, ...) @fprintf(i32 %46, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0))
%48 = call i32 (...) @plc_close_port()
br label %54
49: ; preds = %42
%50 = load i16, i16* %3
%51 = load i16, i16* %4
%52 = call i32 @printf(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0), i16 zeroext %50, i16 zeroext %51)
%53 = call i32 (...) @plc_close_port()
br label %54
54: ; preds = %18, %24, %31, %38, %45, %49, %8
%55 = phi i32 [ 1, %8 ], [ 1, %18 ], [ 1, %24 ], [ 1, %31 ], [ 1, %38 ], [ 1, %45 ], [ 0, %49 ]
ret i32 %55
}