#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 main(int argc, char *argv[]) {
  if (argc != 3) {
    printf("%s device speed\n\nSet speed for a serial device.\nFor instance:\n "
           "   %s /dev/ttyUSB0 75000\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);
  tio.c_cflag &= ~CBAUD;
  tio.c_cflag |= BOTHER;
  tio.c_ispeed = speed;
  tio.c_ospeed = speed;
  int r = ioctl(fd, TCSETS2, &tio);
  close(fd);
  if (r == 0) {
    printf("Changed successfully.\n");
  } else {
    perror("ioctl");
  }
}


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)
je     11a8 <main+0x48>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe6b(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    125c <main+0xfc>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
mov    0x2e31(%rip),%rax
mov    (%rax),%esi
call   1060 <open@plt>
mov    %eax,-0x14(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x18(%rbp)
mov    -0x14(%rbp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    0x2dcb(%rip),%rax
mov    (%rax),%eax
xor    $0xffffffff,%eax
and    -0x20(%rbp),%eax
mov    %eax,-0x20(%rbp)
mov    0x2dc9(%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    0x2dc3(%rip),%rax
mov    (%rax),%esi
lea    -0x28(%rbp),%rdx
call   1040 <ioctl@plt>
mov    %eax,-0x2c(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <close@plt>
cmpl   $0x0,-0x2c(%rbp)
jne    1250 <main+0xf0>
lea    0xe15(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    125c <main+0xfc>
lea    0xe19(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    120c <main+0x9c>
mov    0x8(%rbx),%rdi
mov    0x2e59(%rip),%rax
mov    (%rax),%esi
call   1070 <open@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
mov    0x2e2e(%rip),%rax
mov    (%rax),%esi
mov    %rsp,%r14
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
mov    0x2df8(%rip),%rax
mov    (%rax),%eax
not    %eax
and    0x8(%rsp),%eax
mov    0x2df9(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x8(%rsp)
mov    %ebx,(%rsp)
mov    %ebx,0x4(%rsp)
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    %ebp,%edi
mov    %r14,%rdx
call   1050 <ioctl@plt>
mov    %eax,%ebx
mov    %ebp,%edi
call   1060 <close@plt>
test   %ebx,%ebx
jne    122e <main+0xbe>
lea    0xe5c(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1225 <main+0xb5>
mov    (%rbx),%rdx
lea    0xdea(%rip),%rdi
mov    %rdx,%rsi
xor    %eax,%eax
call   1040 <printf@plt>
mov    $0xffffffff,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe24(%rip),%rdi
call   1080 <perror@plt>
jmp    1208 <main+0x98>

LLVM IR (-O0):
%struct.termios2 = type { i32, i32, i32 }
@.str = [89 x i8] c"%s device speed\0A\0ASet speed for a serial device.\0AFor instance:\0A %s /dev/ttyUSB0 75000\0A\00"
@O_RDONLY = global i32 0
@TCGETS2 = global i32 0
@CBAUD = global i32 0
@BOTHER = global i32 0
@TCSETS2 = global i32 0
@.str.1 = [23 x i8] c"Changed successfully.\0A\00"
@.str.2 = [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 ne 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 ([89 x i8], [89 x i8]* @.str, i64 0, i64 0), i8* %15, i8* %18)
store i32 -1, i32* %3
br label %57
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 = load i32, i32* @CBAUD
%34 = xor i32 %33, -1
%35 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 2
%36 = load i32, i32* %35
%37 = and i32 %36, %34
store i32 %37, i32* %35
%38 = load i32, i32* @BOTHER
%39 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 2
%40 = load i32, i32* %39
%41 = or i32 %40, %38
store i32 %41, i32* %39
%42 = load i32, i32* %7
%43 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 0
store i32 %42, i32* %43
%44 = load i32, i32* %7
%45 = getelementptr inbounds %struct.termios2, %struct.termios2* %8, i32 0, i32 1
store i32 %44, i32* %45
%46 = load i32, i32* %6
%47 = load i32, i32* @TCSETS2
%48 = call i32 @ioctl(i32 %46, i32 %47, %struct.termios2* %8)
store i32 %48, i32* %9
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
%51 = load i32, i32* %9
%52 = icmp eq i32 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %20
%54 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.1, i64 0, i64 0))
br label %57
55: ; preds = %20
%56 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %57
57: ; preds = %12, %55, %53
%58 = load i32, i32* %3
ret i32 %58
}

LLVM IR (-O2):
%struct.termios2 = type { i32, i32, i32 }
@.str = [89 x i8] c"%s device speed\0A\0ASet speed for a serial device.\0AFor instance:\0A %s /dev/ttyUSB0 75000\0A\00"
@O_RDONLY = global i32 0
@TCGETS2 = global i32 0
@CBAUD = global i32 0
@BOTHER = global i32 0
@TCSETS2 = global i32 0
@.str.2 = [6 x i8] c"ioctl\00"
@str = [22 x i8] c"Changed successfully.\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.termios2
%4 = icmp eq i32 %0, 3
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([89 x i8], [89 x i8]* @.str, i64 0, i64 0), i8* %6, i8* %6)
br label %37
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 = load i32, i32* @CBAUD
%20 = xor i32 %19, -1
%21 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 2
%22 = load i32, i32* %21
%23 = and i32 %22, %20
%24 = load i32, i32* @BOTHER
%25 = or i32 %24, %23
store i32 %25, i32* %21
%26 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 0
store i32 %15, i32* %26
%27 = getelementptr inbounds %struct.termios2, %struct.termios2* %3, i64 0, i32 1
store i32 %15, i32* %27
%28 = load i32, i32* @TCSETS2
%29 = call i32 @ioctl(i32 %12, i32 %28, %struct.termios2* %3)
%30 = call i32 @close(i32 %12)
%31 = icmp eq i32 %29, 0
br i1 %31, label %32, label %34
32: ; preds = %8
%33 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @str, i64 0, i64 0))
br label %36
34: ; preds = %8
%35 = call i32 @perror(i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.2, i64 0, i64 0))
br label %36
36: ; preds = %34, %32
br label %37
37: ; preds = %36, %5
%38 = phi i32 [ -1, %5 ], [ 0, %36 ]
ret i32 %38
}