#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 termios2 {
  int c_ispeed;
  int c_ospeed;
  int /*<<< orphan*/ c_cflag;
};

/* Variables and functions */
int /*<<< orphan*/ BOTHER;
int /*<<< orphan*/ CBAUD;
int /*<<< orphan*/ O_RDONLY;
int /*<<< orphan*/ TCGETS2;
int /*<<< orphan*/ TCSETS2;
int atoi(char *);
int /*<<< orphan*/ close(int);
int ioctl(int, int /*<<< orphan*/, struct termios2 *);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ puts(char *);
int /*<<< orphan*/ setraw(struct termios2 *);
int main(int argc, char *argv[]) {
  if (argc < 3) {
    printf("%s device speed\n\nSet speed for a serial device.\n"
           "For instance:\n"
           "    %s /dev/ttyUSB0 38400 [raw]\n\n",
           argv[0], argv[0]);
    return -1;
  }
  int fd = open(argv[1], O_RDONLY);
  int speed = atoi(argv[2]);
  struct termios2 tio;
  ioctl(fd, TCGETS2, &tio);
  printf("speed (i/o): %d %d\n", tio.c_ispeed, tio.c_ospeed);
  if (argc == 4 && argv[3][0] == 'r') {
    puts("set raw mode");
    setraw(&tio);
  }
  if ((tio.c_ispeed != speed) || (tio.c_ispeed != speed)) {
    printf("set speed to: %d %d\n", speed, speed);
  }
  tio.c_cflag &= ~CBAUD;
  tio.c_cflag |= BOTHER;
  tio.c_ispeed = speed;
  tio.c_ospeed = speed;
  int r = ioctl(fd, TCSETS2, &tio);
  if (r != 0) {
    puts("Error: ioctl:");
    perror("ioctl");
    close(fd);
    return 1;
  }
  ioctl(fd, TCGETS2, &tio);
  close(fd);
  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)
cmpl   $0x3,-0x8(%rbp)
jge    11c8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe4b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1318 <main+0x198>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e11(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2ddd(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x28(%rbp),%esi
mov    -0x24(%rbp),%edx
lea    0xe4d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
cmpl   $0x4,-0x8(%rbp)
jne    124d <main+0xcd>
mov    -0x10(%rbp),%rax
mov    0x18(%rax),%rax
movsbl (%rax),%eax
cmp    $0x72,%eax
jne    124d <main+0xcd>
lea    0xe35(%rip),%rdi
call   1030 <puts@plt>
lea    -0x28(%rbp),%rdi
call   1040 <setraw@plt>
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
jne    1265 <main+0xe5>
mov    -0x28(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1279 <main+0xf9>
mov    -0x18(%rbp),%esi
mov    -0x18(%rbp),%edx
lea    0xe0f(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2d38(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d36(%rip),%rax
mov    (%rax),%eax
or     -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x28(%rbp)
mov    -0x18(%rbp),%eax
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2d30(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0x0,-0x2c(%rbp)
je     12f4 <main+0x174>
lea    0xdc7(%rip),%rdi
call   1030 <puts@plt>
lea    0xdc9(%rip),%rdi
call   1090 <perror@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x1,-0x4(%rbp)
jmp    1318 <main+0x198>
mov    -0x14(%rbp),%edi
mov    0x2cda(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1060 <ioctl@plt>
mov    -0x14(%rbp),%edi
call   1070 <close@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jg     11b2 <main+0x32>
mov    (%rbx),%rdx
lea    0xe62(%rip),%rdi
mov    %rdx,%rsi
xor    %eax,%eax
call   1050 <printf@plt>
mov    $0xffffffff,%ebx
jmp    1292 <main+0x112>
mov    %edi,%ebp
mov    0x8(%rbx),%rdi
mov    0x2e29(%rip),%rax
mov    (%rax),%esi
call   1080 <open@plt>
mov    %eax,%r14d
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%r15d
mov    0x2dfc(%rip),%r12
mov    (%r12),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
mov    (%rsp),%esi
mov    0x4(%rsp),%edx
lea    0xe67(%rip),%rdi
xor    %eax,%eax
call   1050 <printf@plt>
cmp    $0x4,%ebp
jne    1222 <main+0xa2>
mov    0x18(%rbx),%rax
cmpb   $0x72,(%rax)
jne    1222 <main+0xa2>
lea    0xe5f(%rip),%rdi
call   1030 <puts@plt>
mov    %rsp,%rdi
call   1040 <setraw@plt>
cmp    %r15d,(%rsp)
je     123c <main+0xbc>
lea    0xe52(%rip),%rdi
mov    %r15d,%esi
mov    %r15d,%edx
xor    %eax,%eax
call   1050 <printf@plt>
mov    0x2d75(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2d76(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    %r15d,(%rsp)
mov    %r15d,0x4(%rsp)
mov    0x2d78(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
test   %eax,%eax
jne    12a1 <main+0x121>
mov    (%r12),%esi
mov    %rsp,%rdx
mov    %r14d,%edi
call   1060 <ioctl@plt>
xor    %ebx,%ebx
mov    %r14d,%edi
call   1070 <close@plt>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdee(%rip),%rdi
call   1030 <puts@plt>
lea    0xdf0(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%ebx
jmp    128a <main+0x10a>

LLVM IR (-O0):
%struct.termios2 = type { i32, i32, i32 }
@.str = [96 x i8] c"%s device speed\0A\0ASet speed for a serial device.\0AFor instance:\0A %s /dev/ttyUSB0 38400 [raw]\0A\0A\00"
@O_RDONLY = global i32 0
@TCGETS2 = global i32 0
@.str.1 = [20 x i8] c"speed (i/o): %d %d\0A\00"
@.str.2 = [13 x i8] c"set raw mode\00"
@.str.3 = [21 x i8] c"set speed to: %d %d\0A\00"
@CBAUD = global i32 0
@BOTHER = global i32 0
@TCSETS2 = global i32 0
@.str.4 = [14 x i8] c"Error: ioctl:\00"
@.str.5 = [6 x i8] c"ioctl\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.termios2
%9 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%10 = load i32, i32* %4
%11 = icmp slt i32 %10, 3
br i1 %11, label %12, label %20
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 0
%15 = load i8*, i8** %14
%16 = load i8**, i8*** %5
%17 = getelementptr inbounds i8*, i8** %16, i64 0
%18 = load i8*, i8** %17
%19 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([96 x i8], [96 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %18)
store i32 -1, i32* %3
br label %95
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = load i32, i32* @O_RDONLY
%25 = call i32 @open(i8* %23, i32 %24)
store i32 %25, i32* %6
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
store i32 %29, i32* %7
%30 = load i32, i32* %6
%31 = load i32, i32* @TCGETS2
%32 = call i32 @ioctl(i32 %30, i32 %31, %struct.termios2* %8)
%33 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 0
%34 = load i32, i32* %33
%35 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 1
%36 = load i32, i32* %35
%37 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %34, i32 %36)
%38 = load i32, i32* %4
%39 = icmp eq i32 %38, 4
br i1 %39, label %40, label %51
40: ; preds = %20
%41 = load i8**, i8*** %5
%42 = getelementptr inbounds i8*, i8** %41, i64 3
%43 = load i8*, i8** %42
%44 = getelementptr inbounds i8, i8* %43, i64 0
%45 = load i8, i8* %44
%46 = sext i8 %45 to i32
%47 = icmp eq i32 %46, 114
br i1 %47, label %48, label %51
48: ; preds = %40
%49 = call i32 @puts(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%50 = call i32 @setraw(%struct.termios2* %8)
br label %51
51: ; preds = %48, %40, %20
%52 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 0
%53 = load i32, i32* %52
%54 = load i32, i32* %7
%55 = icmp ne i32 %53, %54
br i1 %55, label %61, label %56
56: ; preds = %51
%57 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 0
%58 = load i32, i32* %57
%59 = load i32, i32* %7
%60 = icmp ne i32 %58, %59
br i1 %60, label %61, label %65
61: ; preds = %56, %51
%62 = load i32, i32* %7
%63 = load i32, i32* %7
%64 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %62, i32 %63)
br label %65
65: ; preds = %61, %56
%66 = load i32, i32* @CBAUD
%67 = xor i32 %66, -1
%68 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 2
%69 = load i32, i32* %68
%70 = and i32 %69, %67
store i32 %70, i32* %68
%71 = load i32, i32* @BOTHER
%72 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 2
%73 = load i32, i32* %72
%74 = or i32 %73, %71
store i32 %74, i32* %72
%75 = load i32, i32* %7
%76 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 0
store i32 %75, i32* %76
%77 = load i32, i32* %7
%78 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 1
store i32 %77, i32* %78
%79 = load i32, i32* %6
%80 = load i32, i32* @TCSETS2
%81 = call i32 @ioctl(i32 %79, i32 %80, %struct.termios2* %8)
store i32 %81, i32* %9
%82 = load i32, i32* %9
%83 = icmp ne i32 %82, 0
br i1 %83, label %84, label %89
84: ; preds = %65
%85 = call i32 @puts(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%86 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
%87 = load i32, i32* %6
%88 = call i32 @close(i32 %87)
store i32 1, i32* %3
br label %95
89: ; preds = %65
%90 = load i32, i32* %6
%91 = load i32, i32* @TCGETS2
%92 = call i32 @ioctl(i32 %90, i32 %91, %struct.termios2* %8)
%93 = load i32, i32* %6
%94 = call i32 @close(i32 %93)
store i32 0, i32* %3
br label %95
95: ; preds = %89, %84, %12
%96 = load i32, i32* %3
ret i32 %96
}

LLVM IR (-O2):
%struct.termios2 = type { i32, i32, i32 }
@.str = [96 x i8] c"%s device speed\0A\0ASet speed for a serial device.\0AFor instance:\0A %s /dev/ttyUSB0 38400 [raw]\0A\0A\00"
@O_RDONLY = global i32 0
@TCGETS2 = global i32 0
@.str.1 = [20 x i8] c"speed (i/o): %d %d\0A\00"
@.str.2 = [13 x i8] c"set raw mode\00"
@.str.3 = [21 x i8] c"set speed to: %d %d\0A\00"
@CBAUD = global i32 0
@BOTHER = global i32 0
@TCSETS2 = global i32 0
@.str.4 = [14 x i8] c"Error: ioctl:\00"
@.str.5 = [6 x i8] c"ioctl\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios2
%4 = icmp slt i32 %0, 3
br i1 %4, label %5, label %8
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([96 x i8], [96 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %6)
br label %58
8: ; preds = %2
%9 = getelementptr inbounds i8*, i8** %1, i64 1
%10 = load i8*, i8** %9
%11 = load i32, i32* @O_RDONLY
%12 = tail call i32 @open(i8* %10, i32 %11)
%13 = getelementptr inbounds i8*, i8** %1, i64 2
%14 = load i8*, i8** %13
%15 = tail call i32 @atoi(i8* %14)
%16 = bitcast %struct.termios2* %3 to i8*
%17 = load i32, i32* @TCGETS2
%18 = call i32 @ioctl(i32 %12, i32 %17, %struct.termios2* %3)
%19 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 0
%20 = load i32, i32* %19
%21 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 1
%22 = load i32, i32* %21
%23 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0), i32 %20, i32 %22)
%24 = icmp eq i32 %0, 4
br i1 %24, label %25, label %33
25: ; preds = %8
%26 = getelementptr inbounds i8*, i8** %1, i64 3
%27 = load i8*, i8** %26
%28 = load i8, i8* %27
%29 = icmp eq i8 %28, 114
br i1 %29, label %30, label %33
30: ; preds = %25
%31 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%32 = call i32 @setraw(%struct.termios2* %3)
br label %33
33: ; preds = %30, %25, %8
%34 = load i32, i32* %19
%35 = icmp eq i32 %34, %15
br i1 %35, label %38, label %36
36: ; preds = %33
%37 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0), i32 %15, i32 %15)
br label %38
38: ; preds = %33, %36
%39 = load i32, i32* @CBAUD
%40 = xor i32 %39, -1
%41 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 2
%42 = load i32, i32* %41
%43 = and i32 %42, %40
%44 = load i32, i32* @BOTHER
%45 = or i32 %44, %43
store i32 %45, i32* %41
store i32 %15, i32* %19
store i32 %15, i32* %21
%46 = load i32, i32* @TCSETS2
%47 = call i32 @ioctl(i32 %12, i32 %46, %struct.termios2* %3)
%48 = icmp eq i32 %47, 0
br i1 %48, label %52, label %49
49: ; preds = %38
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0))
%51 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.5, i64 0, i64 0))
br label %55
52: ; preds = %38
%53 = load i32, i32* @TCGETS2
%54 = call i32 @ioctl(i32 %12, i32 %53, %struct.termios2* %3)
br label %55
55: ; preds = %52, %49
%56 = phi i32 [ 1, %49 ], [ 0, %52 ]
%57 = call i32 @close(i32 %12)
br label %58
58: ; preds = %55, %5
%59 = phi i32 [ -1, %5 ], [ %56, %55 ]
ret i32 %59
}