#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 */
struct serial_struct {
  scalar_t__ *reserved_char;
};

/* Variables and functions */
int O_NDELAY;
int O_RDWR;
int /*<<< orphan*/ TIOCGSERIAL;
int atoi(char *);
int /*<<< orphan*/ close(int);
scalar_t__ ioctl(int, int /*<<< orphan*/, struct serial_struct *);
int open(char *, int);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *, int);
int /*<<< orphan*/ strlen(char *);
int write(int, char *, int /*<<< orphan*/);
int main(int argc, char **argv) {
  int i, ttynum, fd;
  char ttyname[80];
  struct serial_struct serinfo;
  if (argc < 3) {
    printf("Usage:\n");
    printf("%s TTYNAME TTYNUM", argv[0]);
    return 1;
  }
  ttynum = atoi(argv[2]);
  for (i = 0; i < ttynum; i++) {
    sprintf(ttyname, "/dev/%s%d", argv[1], i);
    if ((fd = open(ttyname, O_RDWR | O_NDELAY)) == -1)
      break;
    if (write(fd, ttyname, strlen(ttyname)) == -1)
      break;
    serinfo.reserved_char[0] = 0;
    if (ioctl(fd, TIOCGSERIAL, &serinfo) < 0) {
      printf("Cannot get serial info\n");
      close(fd);
      return 2;
    }
    if (serinfo.reserved_char[0] == 0) {
      printf("%s is RS232\n", ttyname);
    } else {
      printf("%s is RS422/485\n", ttyname);
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
jge    11d2 <main+0x52>
lea    0xe56(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe49(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1315 <main+0x195>
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,-0x18(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
jge    130e <main+0x18e>
lea    -0x70(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdx
mov    -0x14(%rbp),%ecx
lea    0xe0f(%rip),%rsi
call   10a0 <sprintf@plt>
lea    -0x70(%rbp),%rdi
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    0x2db4(%rip),%rax
or     (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x1c(%rbp)
cmp    $0xffffffff,%eax
jne    123c <main+0xbc>
jmp    130e <main+0x18e>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x84(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x80(%rbp)
lea    -0x70(%rbp),%rdi
call   1040 <strlen@plt>
mov    -0x84(%rbp),%edi
mov    -0x80(%rbp),%rsi
mov    %eax,%edx
call   1030 <write@plt>
cmp    $0xffffffff,%eax
jne    1275 <main+0xf5>
jmp    130e <main+0x18e>
mov    -0x78(%rbp),%rax
movq   $0x0,(%rax)
mov    -0x1c(%rbp),%edi
mov    0x2d46(%rip),%rax
mov    (%rax),%esi
lea    -0x78(%rbp),%rdx
call   1060 <ioctl@plt>
cmp    $0x0,%rax
jge    12c1 <main+0x141>
lea    0xd7e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
movl   $0x2,-0x4(%rbp)
jmp    1315 <main+0x195>
mov    -0x78(%rbp),%rax
cmpq   $0x0,(%rax)
jne    12e6 <main+0x166>
lea    -0x70(%rbp),%rsi
lea    0xd62(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12f8 <main+0x178>
lea    -0x70(%rbp),%rsi
lea    0xd58(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x1c(%rbp),%edi
call   1070 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11e9 <main+0x69>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x68,%rsp
mov    %rsi,%r13
cmp    $0x2,%edi
jg     11cf <main+0x3f>
lea    0xea4(%rip),%rdi
call   1030 <puts@plt>
mov    0x0(%r13),%rsi
lea    0xe43(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0x1,%r14d
jmp    12a6 <main+0x116>
mov    0x10(%r13),%rdi
call   10a0 <atoi@plt>
test   %eax,%eax
jle    12a3 <main+0x113>
mov    %eax,%r15d
lea    0x10(%rsp),%rbp
xor    %ebx,%ebx
xor    %r14d,%r14d
nopl   (%rax)
mov    0x8(%r13),%rdx
mov    %rbp,%rdi
lea    0xe14(%rip),%rsi
mov    %ebx,%ecx
call   10b0 <sprintf@plt>
mov    0x2dcc(%rip),%rax
mov    (%rax),%esi
mov    0x2dd3(%rip),%rax
or     (%rax),%esi
mov    %rbp,%rdi
call   1090 <open@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    %eax,%r12d
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r12d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0xffffffff,%eax
je     12a3 <main+0x113>
mov    0x8(%rsp),%rax
movq   $0x0,(%rax)
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    %r12d,%edi
lea    0x8(%rsp),%rdx
call   1070 <ioctl@plt>
test   %rax,%rax
js     12b8 <main+0x128>
mov    0x8(%rsp),%rax
cmpq   $0x0,(%rax)
lea    0xdb1(%rip),%rdi
lea    0xd9d(%rip),%rax
cmove  %rax,%rdi
mov    %rbp,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    %r12d,%edi
call   1080 <close@plt>
add    $0x1,%ebx
cmp    %ebx,%r15d
jne    11f0 <main+0x60>
jmp    12a6 <main+0x116>
xor    %r14d,%r14d
mov    %r14d,%eax
add    $0x68,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd7b(%rip),%rdi
call   1030 <puts@plt>
mov    %r12d,%edi
call   1080 <close@plt>
mov    $0x2,%r14d
jmp    12a6 <main+0x116>

LLVM IR (-O0):
%struct.serial_struct = type { i64* }
@.str = [8 x i8] c"Usage:\0A\00"
@.str.1 = [18 x i8] c"%s TTYNAME TTYNUM\00"
@.str.2 = [10 x i8] c"/dev/%s%d\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@TIOCGSERIAL = global i32 0
@.str.3 = [24 x i8] c"Cannot get serial info\0A\00"
@.str.4 = [13 x i8] c"%s is RS232\0A\00"
@.str.5 = [17 x i8] c"%s is RS422/485\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca [80 x i8]
%10 = alloca %struct.serial_struct
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp slt i32 %11, 3
br i1 %12, label %13, label %19
13: ; preds = %2
%14 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0))
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %17)
store i32 1, i32* %3
br label %81
19: ; preds = %2
%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* %7
store i32 0, i32* %6
br label %24
24: ; preds = %77, %19
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = icmp slt i32 %25, %26
br i1 %27, label %28, label %80
28: ; preds = %24
%29 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%30 = load i8**, i8*** %5
%31 = getelementptr inbounds i8*, i8** %30, i64 1
%32 = load i8*, i8** %31
%33 = load i32, i32* %6
%34 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %32, i32 %33)
%35 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%36 = load i32, i32* @O_RDWR
%37 = load i32, i32* @O_NDELAY
%38 = or i32 %36, %37
%39 = call i32 @open(i8* %35, i32 %38)
store i32 %39, i32* %8
%40 = icmp eq i32 %39, -1
br i1 %40, label %41, label %42
41: ; preds = %28
br label %80
42: ; preds = %28
%43 = load i32, i32* %8
%44 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%45 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%46 = call i32 @strlen(i8* %45)
%47 = call i32 @write(i32 %43, i8* %44, i32 %46)
%48 = icmp eq i32 %47, -1
br i1 %48, label %49, label %50
49: ; preds = %42
br label %80
50: ; preds = %42
%51 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %10, i32 0, i32 0
%52 = load i64*, i64** %51
%53 = getelementptr inbounds i64, i64* %52, i64 0
store i64 0, i64* %53
%54 = load i32, i32* %8
%55 = load i32, i32* @TIOCGSERIAL
%56 = call i64 @ioctl(i32 %54, i32 %55, %struct.serial_struct* %10)
%57 = icmp slt i64 %56, 0
br i1 %57, label %58, label %62
58: ; preds = %50
%59 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.3, i64 0, i64 0))
%60 = load i32, i32* %8
%61 = call i32 @close(i32 %60)
store i32 2, i32* %3
br label %81
62: ; preds = %50
%63 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %10, i32 0, i32 0
%64 = load i64*, i64** %63
%65 = getelementptr inbounds i64, i64* %64, i64 0
%66 = load i64, i64* %65
%67 = icmp eq i64 %66, 0
br i1 %67, label %68, label %71
68: ; preds = %62
%69 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%70 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* %69)
br label %74
71: ; preds = %62
%72 = getelementptr inbounds [80 x i8], [80 x i8]* %9, i64 0, i64 0
%73 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0), i8* %72)
br label %74
74: ; preds = %71, %68
%75 = load i32, i32* %8
%76 = call i32 @close(i32 %75)
br label %77
77: ; preds = %74
%78 = load i32, i32* %6
%79 = add nsw i32 %78, 1
store i32 %79, i32* %6
br label %24
80: ; preds = %49, %41, %24
store i32 0, i32* %3
br label %81
81: ; preds = %80, %58, %13
%82 = load i32, i32* %3
ret i32 %82
}

LLVM IR (-O2):
%struct.serial_struct = type { i64* }
@.str.1 = [18 x i8] c"%s TTYNAME TTYNUM\00"
@.str.2 = [10 x i8] c"/dev/%s%d\00"
@O_RDWR = global i32 0
@O_NDELAY = global i32 0
@TIOCGSERIAL = global i32 0
@.str.4 = [13 x i8] c"%s is RS232\0A\00"
@.str.5 = [17 x i8] c"%s is RS422/485\0A\00"
@str = [23 x i8] c"Cannot get serial info\00"
@str.6 = [7 x i8] c"Usage:\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [80 x i8]
%4 = alloca %struct.serial_struct
%5 = getelementptr inbounds [80 x i8], [80 x i8]* %3, i64 0, i64 0
%6 = bitcast %struct.serial_struct* %4 to i8*
%7 = icmp slt i32 %0, 3
br i1 %7, label %8, label %12
8: ; preds = %2
%9 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([7 x i8], [7 x i8]* @str.6, i64 0, i64 0))
%10 = load i8*, i8** %1
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.1, i64 0, i64 0), i8* %10)
br label %50
12: ; preds = %2
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = icmp sgt i32 %15, 0
br i1 %16, label %17, label %50
17: ; preds = %12
%18 = getelementptr inbounds i8*, i8** %1, i64 1
%19 = getelementptr inbounds %struct.serial_struct, %struct.serial_struct* %4, i64 0, i32 0
br label %20
20: ; preds = %17, %41
%21 = phi i32 [ 0, %17 ], [ %48, %41 ]
%22 = load i8*, i8** %18
%23 = call i32 @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i8* %22, i32 %21)
%24 = load i32, i32* @O_RDWR
%25 = load i32, i32* @O_NDELAY
%26 = or i32 %25, %24
%27 = call i32 @open(i8* %5, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %50, label %29
29: ; preds = %20
%30 = call i32 @strlen(i8* %5)
%31 = call i32 @write(i32 %27, i8* %5, i32 %30)
%32 = icmp eq i32 %31, -1
br i1 %32, label %50, label %33
33: ; preds = %29
%34 = load i64*, i64** %19
store i64 0, i64* %34
%35 = load i32, i32* @TIOCGSERIAL
%36 = call i64 @ioctl(i32 %27, i32 %35, %struct.serial_struct* %4)
%37 = icmp slt i64 %36, 0
br i1 %37, label %38, label %41
38: ; preds = %33
%39 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([23 x i8], [23 x i8]* @str, i64 0, i64 0))
%40 = call i32 @close(i32 %27)
br label %50
41: ; preds = %33
%42 = load i64*, i64** %19
%43 = load i64, i64* %42
%44 = icmp eq i64 %43, 0
%45 = select i1 %44, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.5, i64 0, i64 0)
%46 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) %45, i8* %5)
%47 = call i32 @close(i32 %27)
%48 = add nuw nsw i32 %21, 1
%49 = icmp eq i32 %48, %15
br i1 %49, label %50, label %20
50: ; preds = %29, %20, %41, %12, %38, %8
%51 = phi i32 [ 1, %8 ], [ 2, %38 ], [ 0, %12 ], [ 0, %41 ], [ 0, %20 ], [ 0, %29 ]
ret i32 %51
}