#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 termios {
  int c_cflag;
};

/* Variables and functions */
int CRTSCTS;
int O_NDELAY;
int O_RDONLY;
int /*<<< orphan*/ TCSANOW;
int /*<<< orphan*/ close(int);
int open(char *, int);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ *strchr(char *, char);
int /*<<< orphan*/ strcpy(char *, char *);
int tcgetattr(int, struct termios *);
int tcsetattr(int, int /*<<< orphan*/, struct termios *);
int main(int argc, char **argv) {
  int i, fd;
  struct termios tio;
  char device[1000];
  for (i = 1; i < argc; i++) {
    if (strchr(argv[i], '/') == NULL)
      sprintf(device, "/dev/%s", argv[i]);
    else
      strcpy(device, argv[i]);
    printf("setting CRTSCTS for device %s...\n", device);
    fd = open(device, O_RDONLY | O_NDELAY);
    if (fd == -1) {
      perror("open");
      break;
    }
    if (tcgetattr(fd, &tio) == -1) {
      perror("tcgetattr");
      break;
    }
    if ((tio.c_cflag & CRTSCTS) != 0)
      printf("CRTSCTS was already set!\n");
    tio.c_cflag |= CRTSCTS;
    if (tcsetattr(fd, TCSANOW, &tio) == -1) {
      perror("tcsetattr");
      break;
    }
    close(fd);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x410,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    -0x14(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    1306 <main+0x176>
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdi
mov    $0x2f,%esi
call   1040 <strchr@plt>
cmp    $0x0,%rax
jne    1200 <main+0x70>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe0a(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    1218 <main+0x88>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rax
movslq -0x14(%rbp),%rcx
mov    (%rax,%rcx,8),%rsi
call   1030 <strcpy@plt>
lea    -0x410(%rbp),%rsi
lea    0xde2(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x410(%rbp),%rdi
mov    0x2dad(%rip),%rax
mov    (%rax),%esi
mov    0x2d8c(%rip),%rax
or     (%rax),%esi
call   1090 <open@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0xffffffff,-0x18(%rbp)
jne    1269 <main+0xd9>
lea    0xdcb(%rip),%rdi
call   10a0 <perror@plt>
jmp    1306 <main+0x176>
mov    -0x18(%rbp),%edi
lea    -0x20(%rbp),%rsi
call   1070 <tcgetattr@plt>
cmp    $0xffffffff,%eax
jne    128f <main+0xff>
lea    0xdaa(%rip),%rdi
call   10a0 <perror@plt>
jmp    1306 <main+0x176>
mov    -0x20(%rbp),%eax
mov    0x2d27(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
je     12b2 <main+0x122>
lea    0xd8e(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d07(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%edi
mov    0x2d15(%rip),%rax
mov    (%rax),%esi
lea    -0x20(%rbp),%rdx
call   1080 <tcsetattr@plt>
cmp    $0xffffffff,%eax
jne    12f0 <main+0x160>
lea    0xd6d(%rip),%rdi
call   10a0 <perror@plt>
jmp    1306 <main+0x176>
mov    -0x18(%rbp),%edi
call   1060 <close@plt>
mov    -0x14(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x14(%rbp)
jmp    11b0 <main+0x20>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x3f8,%rsp
cmp    $0x2,%edi
jl     12b6 <main+0x116>
mov    %rsi,%rbx
mov    %edi,%eax
mov    %rax,0x8(%rsp)
mov    $0x1,%r13d
lea    0x10(%rsp),%r12
mov    %rsp,%rbp
cs nopw 0x0(%rax,%rax,1)
nopl   0x0(%rax)
mov    (%rbx,%r13,8),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    (%rbx,%r13,8),%rdx
mov    %r12,%rdi
test   %rax,%rax
je     1210 <main+0x70>
mov    %rdx,%rsi
call   1030 <strcpy@plt>
jmp    121c <main+0x7c>
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
lea    0xde9(%rip),%rsi
call   10c0 <sprintf@plt>
lea    0xde5(%rip),%rdi
mov    %r12,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d9c(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
or     (%rax),%esi
mov    %r12,%rdi
call   10a0 <open@plt>
cmp    $0xffffffff,%eax
je     12ca <main+0x12a>
mov    %eax,%r15d
mov    %eax,%edi
mov    %rbp,%rsi
call   1080 <tcgetattr@plt>
cmp    $0xffffffff,%eax
je     12d3 <main+0x133>
mov    (%rsp),%eax
mov    0x2d58(%rip),%r14
mov    (%r14),%ecx
test   %eax,%ecx
je     1281 <main+0xe1>
lea    0xdcd(%rip),%rdi
call   1040 <puts@plt>
mov    (%r14),%ecx
mov    (%rsp),%eax
or     %ecx,%eax
mov    %eax,(%rsp)
mov    0x2d53(%rip),%rax
mov    (%rax),%esi
mov    %r15d,%edi
mov    %rbp,%rdx
call   1090 <tcsetattr@plt>
cmp    $0xffffffff,%eax
je     12dc <main+0x13c>
mov    %r15d,%edi
call   1070 <close@plt>
add    $0x1,%r13
cmp    %r13,0x8(%rsp)
jne    11e0 <main+0x40>
xor    %eax,%eax
add    $0x3f8,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd59(%rip),%rdi
jmp    12e3 <main+0x143>
lea    0xd55(%rip),%rdi
jmp    12e3 <main+0x143>
lea    0xd56(%rip),%rdi
call   10b0 <perror@plt>
jmp    12b6 <main+0x116>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [8 x i8] c"/dev/%s\00"
@.str.1 = [34 x i8] c"setting CRTSCTS for device %s...\0A\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [10 x i8] c"tcgetattr\00"
@CRTSCTS = global i32 0
@.str.4 = [26 x i8] c"CRTSCTS was already set!\0A\00"
@TCSANOW = global i32 0
@.str.5 = [10 x i8] c"tcsetattr\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 %struct.termios
%9 = alloca [1000 x i8]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 1, i32* %6
br label %10
10: ; preds = %78, %2
%11 = load i32, i32* %6
%12 = load i32, i32* %4
%13 = icmp slt i32 %11, %12
br i1 %13, label %14, label %81
14: ; preds = %10
%15 = load i8**, i8*** %5
%16 = load i32, i32* %6
%17 = sext i32 %16 to i64
%18 = getelementptr inbounds i8*, i8** %15, i64 %17
%19 = load i8*, i8** %18
%20 = call i32* @strchr(i8* %19, i8 signext 47)
%21 = icmp eq i32* %20, null
br i1 %21, label %22, label %30
22: ; preds = %14
%23 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%24 = load i8**, i8*** %5
%25 = load i32, i32* %6
%26 = sext i32 %25 to i64
%27 = getelementptr inbounds i8*, i8** %24, i64 %26
%28 = load i8*, i8** %27
%29 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %28)
br label %38
30: ; preds = %14
%31 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%32 = load i8**, i8*** %5
%33 = load i32, i32* %6
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8*, i8** %32, i64 %34
%36 = load i8*, i8** %35
%37 = call i32 @strcpy(i8* %31, i8* %36)
br label %38
38: ; preds = %30, %22
%39 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%40 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %39)
%41 = getelementptr inbounds [1000 x i8], [1000 x i8]* %9, i64 0, i64 0
%42 = load i32, i32* @O_RDONLY
%43 = load i32, i32* @O_NDELAY
%44 = or i32 %42, %43
%45 = call i32 @open(i8* %41, i32 %44)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp eq i32 %46, -1
br i1 %47, label %48, label %50
48: ; preds = %38
%49 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0))
br label %81
50: ; preds = %38
%51 = load i32, i32* %7
%52 = call i32 @tcgetattr(i32 %51, %struct.termios* %8)
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %50
%55 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
br label %81
56: ; preds = %50
%57 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%58 = load i32, i32* %57
%59 = load i32, i32* @CRTSCTS
%60 = and i32 %58, %59
%61 = icmp ne i32 %60, 0
br i1 %61, label %62, label %64
62: ; preds = %56
%63 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.4, i64 0, i64 0))
br label %64
64: ; preds = %62, %56
%65 = load i32, i32* @CRTSCTS
%66 = getelementptr inbounds %struct.termios, %struct.termios* %8, i32 0, i32 0
%67 = load i32, i32* %66
%68 = or i32 %67, %65
store i32 %68, i32* %66
%69 = load i32, i32* %7
%70 = load i32, i32* @TCSANOW
%71 = call i32 @tcsetattr(i32 %69, i32 %70, %struct.termios* %8)
%72 = icmp eq i32 %71, -1
br i1 %72, label %73, label %75
73: ; preds = %64
%74 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0))
br label %81
75: ; preds = %64
%76 = load i32, i32* %7
%77 = call i32 @close(i32 %76)
br label %78
78: ; preds = %75
%79 = load i32, i32* %6
%80 = add nsw i32 %79, 1
store i32 %80, i32* %6
br label %10
81: ; preds = %73, %54, %48, %10
ret i32 0
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [8 x i8] c"/dev/%s\00"
@.str.1 = [34 x i8] c"setting CRTSCTS for device %s...\0A\00"
@O_RDONLY = global i32 0
@O_NDELAY = global i32 0
@.str.2 = [5 x i8] c"open\00"
@.str.3 = [10 x i8] c"tcgetattr\00"
@CRTSCTS = global i32 0
@TCSANOW = global i32 0
@.str.5 = [10 x i8] c"tcsetattr\00"
@str = [25 x i8] c"CRTSCTS was already set!\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios
%4 = alloca [1000 x i8]
%5 = bitcast %struct.termios* %3 to i8*
%6 = getelementptr inbounds [1000 x i8], [1000 x i8]* %4, i64 0, i64 0
%7 = icmp sgt i32 %0, 1
br i1 %7, label %8, label %55
8: ; preds = %2
%9 = getelementptr inbounds %struct.termios, %struct.termios* %3, i64 0, i32 0
%10 = zext i32 %0 to i64
br label %11
11: ; preds = %8, %48
%12 = phi i64 [ 1, %8 ], [ %50, %48 ]
%13 = getelementptr inbounds i8*, i8** %1, i64 %12
%14 = load i8*, i8** %13
%15 = call i32* @strchr(i8* %14, i8 signext 47)
%16 = icmp eq i32* %15, null
%17 = load i8*, i8** %13
br i1 %16, label %18, label %20
18: ; preds = %11
%19 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %17)
br label %22
20: ; preds = %11
%21 = call i32 @strcpy(i8* %6, i8* %17)
br label %22
22: ; preds = %20, %18
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([34 x i8], [34 x i8]* @.str.1, i64 0, i64 0), i8* %6)
%24 = load i32, i32* @O_RDONLY
%25 = load i32, i32* @O_NDELAY
%26 = or i32 %25, %24
%27 = call i32 @open(i8* %6, i32 %26)
%28 = icmp eq i32 %27, -1
br i1 %28, label %52, label %29
29: ; preds = %22
%30 = call i32 @tcgetattr(i32 %27, %struct.termios* %3)
%31 = icmp eq i32 %30, -1
br i1 %31, label %52, label %32
32: ; preds = %29
%33 = load i32, i32* %9
%34 = load i32, i32* @CRTSCTS
%35 = and i32 %34, %33
%36 = icmp eq i32 %35, 0
br i1 %36, label %41, label %37
37: ; preds = %32
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @str, i64 0, i64 0))
%39 = load i32, i32* @CRTSCTS
%40 = load i32, i32* %9
br label %41
41: ; preds = %37, %32
%42 = phi i32 [ %40, %37 ], [ %33, %32 ]
%43 = phi i32 [ %39, %37 ], [ %34, %32 ]
%44 = or i32 %42, %43
store i32 %44, i32* %9
%45 = load i32, i32* @TCSANOW
%46 = call i32 @tcsetattr(i32 %27, i32 %45, %struct.termios* %3)
%47 = icmp eq i32 %46, -1
br i1 %47, label %52, label %48
48: ; preds = %41
%49 = call i32 @close(i32 %27)
%50 = add nuw nsw i64 %12, 1
%51 = icmp eq i64 %50, %10
br i1 %51, label %55, label %11
52: ; preds = %41, %29, %22
%53 = phi i8* [ getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), %22 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), %29 ], [ getelementptr inbounds ([10 x i8], [10 x i8]* @.str.5, i64 0, i64 0), %41 ]
%54 = call i32 @perror(i8* %53)
br label %55
55: ; preds = %48, %52, %2
ret i32 0
}