#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 dummy;
};

/* Variables and functions */
int /*<<< orphan*/ B115200;
int /*<<< orphan*/ O_RDWR;
int /*<<< orphan*/ TCSAFLUSH;
scalar_t__ cfsetispeed(struct termios *, int /*<<< orphan*/);
scalar_t__ cfsetospeed(struct termios *, int /*<<< orphan*/);
int /*<<< orphan*/ dup2(int, int);
int errno;
int /*<<< orphan*/ execvp(char *, char **);
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int getopt(int, char **, char *);
int open(char *, int /*<<< orphan*/, int /*<<< orphan*/);
char *optarg;
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ putenv(char *);
char *strerror(int);
scalar_t__ tcgetattr(int, struct termios *);
scalar_t__ tcsetattr(int, int /*<<< orphan*/, struct termios *);
int /*<<< orphan*/ usage();
int main(int argc, char **argv) {
  int c;
  char *dev = NULL;
  char *bin = NULL;
  int fd;
  struct termios config;
  char *args[2];
  while ((c = getopt(argc, argv, "d:b:")) != -1) {
    switch (c) {
    case 'd':
      dev = optarg;
      break;
    case 'b':
      bin = optarg;
      break;
    }
  }
  if (dev == NULL || bin == NULL) {
    usage();
    goto DONE;
  }
  printf("Starting with shell=%s dev=%s\n", bin, dev);
  if ((fd = open(dev, O_RDWR, 0)) < 0) {
    printf("Error: can't open device. Error=%d(%s)\n", errno, strerror(errno));
    goto DONE;
  }
  if (tcgetattr(fd, &config) < 0) {
    printf("Can't get attrs from tty port \n");
    goto DONE;
  }
  if (cfsetispeed(&config, B115200) < 0 || cfsetospeed(&config, B115200) < 0) {
    printf("Can't alter tty port config\n");
    goto DONE;
  }
  if (tcsetattr(fd, TCSAFLUSH, &config) < 0) {
    printf("Can't set attrs to tty port \n");
    goto DONE;
  }
  dup2(fd, 0);
  dup2(fd, 1);
  dup2(fd, 2);
  putenv("PATH=/system/bin:/system/xbin:/sbin:/bin");
  args[0] = bin;
  args[1] = NULL;
  execvp(bin, args);
  exit(0);
DONE:
  return -1;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movq   $0x0,-0x20(%rbp)
movq   $0x0,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x10(%rbp),%rsi
lea    0xdfc(%rip),%rdx
call   10c0 <getopt@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
je     1260 <main+0x90>
mov    -0x14(%rbp),%eax
mov    %eax,-0x44(%rbp)
sub    $0x62,%eax
je     124d <main+0x7d>
jmp    1229 <main+0x59>
mov    -0x44(%rbp),%eax
sub    $0x64,%eax
jne    125b <main+0x8b>
jmp    123a <main+0x6a>
mov    0x2db7(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x20(%rbp)
jmp    125b <main+0x8b>
mov    0x2da4(%rip),%rax
mov    (%rax),%rax
mov    %rax,-0x28(%rbp)
jmp    11f6 <main+0x26>
cmpq   $0x0,-0x20(%rbp)
je     1276 <main+0xa6>
cmpq   $0x0,-0x28(%rbp)
jne    1282 <main+0xb2>
mov    $0x0,%al
call   1070 <usage@plt>
jmp    13e5 <main+0x215>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%rdx
lea    0xd74(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    -0x20(%rbp),%rdi
mov    0x2d3d(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <open@plt>
mov    %eax,-0x2c(%rbp)
cmp    $0x0,%eax
jge    12eb <main+0x11b>
mov    0x2d09(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2cfd(%rip),%rax
mov    (%rax),%edi
call   10f0 <strerror@plt>
mov    -0x48(%rbp),%esi
mov    %rax,%rdx
lea    0xd45(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13e5 <main+0x215>
mov    -0x2c(%rbp),%edi
lea    -0x30(%rbp),%rsi
call   1080 <tcgetattr@plt>
cmp    $0x0,%rax
jge    1314 <main+0x144>
lea    0xd44(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13e5 <main+0x215>
mov    0x2ca5(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   10b0 <cfsetispeed@plt>
cmp    $0x0,%rax
jl     134c <main+0x17c>
mov    0x2c89(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdi
call   1030 <cfsetospeed@plt>
cmp    $0x0,%rax
jge    135f <main+0x18f>
lea    0xd19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13e5 <main+0x215>
mov    -0x2c(%rbp),%edi
mov    0x2c6f(%rip),%rax
mov    (%rax),%esi
lea    -0x30(%rbp),%rdx
call   1090 <tcsetattr@plt>
cmp    $0x0,%rax
jge    1391 <main+0x1c1>
lea    0xd04(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    13e5 <main+0x215>
mov    -0x2c(%rbp),%edi
xor    %esi,%esi
call   1040 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    $0x1,%esi
call   1040 <dup2@plt>
mov    -0x2c(%rbp),%edi
mov    $0x2,%esi
call   1040 <dup2@plt>
lea    0xceb(%rip),%rdi
call   1060 <putenv@plt>
mov    -0x28(%rbp),%rax
mov    %rax,-0x40(%rbp)
movq   $0x0,-0x38(%rbp)
mov    -0x28(%rbp),%rdi
lea    -0x40(%rbp),%rsi
call   10d0 <execvp@plt>
xor    %edi,%edi
call   10e0 <exit@plt>
mov    $0xffffffff,%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
mov    %edi,%ebp
xor    %r14d,%r14d
lea    0xe03(%rip),%r12
mov    0x2df4(%rip),%r13
xor    %r15d,%r15d
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %r12,%rdx
call   10d0 <getopt@plt>
cmp    $0xffffffff,%eax
je     1238 <main+0x58>
cmp    $0x62,%eax
je     1232 <main+0x52>
cmp    $0x64,%eax
jne    1210 <main+0x30>
mov    0x0(%r13),%r15
jmp    1210 <main+0x30>
mov    0x0(%r13),%r14
jmp    1210 <main+0x30>
test   %r15,%r15
je     12d8 <main+0xf8>
test   %r14,%r14
je     12d8 <main+0xf8>
lea    0xdb4(%rip),%rdi
mov    %r14,%rsi
mov    %r15,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2d7b(%rip),%rax
mov    (%rax),%esi
mov    %r15,%rdi
xor    %edx,%edx
call   10b0 <open@plt>
test   %eax,%eax
js     12e1 <main+0x101>
mov    %eax,%ebp
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   1090 <tcgetattr@plt>
test   %rax,%rax
js     130f <main+0x12f>
mov    0x2d2d(%rip),%rbx
mov    (%rbx),%esi
lea    0x8(%rsp),%rdi
call   10c0 <cfsetispeed@plt>
test   %rax,%rax
js     1306 <main+0x126>
mov    (%rbx),%esi
lea    0x8(%rsp),%rdi
call   1040 <cfsetospeed@plt>
test   %rax,%rax
js     1306 <main+0x126>
mov    0x2d1c(%rip),%rax
mov    (%rax),%esi
lea    0x8(%rsp),%rdx
mov    %ebp,%edi
call   10a0 <tcsetattr@plt>
test   %rax,%rax
jns    132f <main+0x14f>
lea    0xd9f(%rip),%rdi
jmp    1316 <main+0x136>
xor    %eax,%eax
call   1080 <usage@plt>
jmp    131b <main+0x13b>
mov    0x2ce0(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1100 <strerror@plt>
lea    0xd2c(%rip),%rdi
mov    %ebx,%esi
mov    %rax,%rdx
xor    %eax,%eax
call   1060 <printf@plt>
jmp    131b <main+0x13b>
lea    0xd85(%rip),%rdi
jmp    1316 <main+0x136>
lea    0xd98(%rip),%rdi
call   1030 <puts@plt>
mov    $0xffffffff,%eax
add    $0x28,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
mov    %ebp,%edi
xor    %esi,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x1,%esi
call   1050 <dup2@plt>
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <dup2@plt>
lea    0xcf5(%rip),%rdi
call   1070 <putenv@plt>
mov    %r14,0x10(%rsp)
movq   $0x0,0x18(%rsp)
lea    0x10(%rsp),%rsi
mov    %r14,%rdi
call   10e0 <execvp@plt>
xor    %edi,%edi
call   10f0 <exit@plt>

LLVM IR (-O0):
%struct.termios = type { i32 }
@.str = [5 x i8] c"d:b:\00"
@optarg = global i8* null
@.str.1 = [31 x i8] c"Starting with shell=%s dev=%s\0A\00"
@O_RDWR = global i32 0
@.str.2 = [40 x i8] c"Error: can't open device. Error=%d(%s)\0A\00"
@errno = global i32 0
@.str.3 = [32 x i8] c"Can't get attrs from tty port \0A\00"
@B115200 = global i32 0
@.str.4 = [29 x i8] c"Can't alter tty port config\0A\00"
@TCSAFLUSH = global i32 0
@.str.5 = [30 x i8] c"Can't set attrs to tty port \0A\00"
@.str.6 = [41 x i8] c"PATH=/system/bin:/system/xbin:/sbin:/bin\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i8*
%9 = alloca i32
%10 = alloca %struct.termios
%11 = alloca [2 x i8*]
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i8* null, i8** %7
store i8* null, i8** %8
br label %12
12: ; preds = %23, %2
%13 = load i32, i32* %4
%14 = load i8**, i8*** %5
%15 = call i32 @getopt(i32 %13, i8** %14, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %6
%16 = icmp ne i32 %15, -1
br i1 %16, label %17, label %24
17: ; preds = %12
%18 = load i32, i32* %6
switch i32 %18, label %23 [
i32 100, label %19
i32 98, label %21
]
19: ; preds = %17
%20 = load i8*, i8** @optarg
store i8* %20, i8** %7
br label %23
21: ; preds = %17
%22 = load i8*, i8** @optarg
store i8* %22, i8** %8
br label %23
23: ; preds = %17, %21, %19
br label %12
24: ; preds = %12
%25 = load i8*, i8** %7
%26 = icmp eq i8* %25, null
br i1 %26, label %30, label %27
27: ; preds = %24
%28 = load i8*, i8** %8
%29 = icmp eq i8* %28, null
br i1 %29, label %30, label %32
30: ; preds = %27, %24
%31 = call i32 (...) @usage()
br label %83
32: ; preds = %27
%33 = load i8*, i8** %8
%34 = load i8*, i8** %7
%35 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %33, i8* %34)
%36 = load i8*, i8** %7
%37 = load i32, i32* @O_RDWR
%38 = call i32 @open(i8* %36, i32 %37, i32 0)
store i32 %38, i32* %9
%39 = icmp slt i32 %38, 0
br i1 %39, label %40, label %45
40: ; preds = %32
%41 = load i32, i32* @errno
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %41, i8* %43)
br label %83
45: ; preds = %32
%46 = load i32, i32* %9
%47 = call i64 @tcgetattr(i32 %46, %struct.termios* %10)
%48 = icmp slt i64 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %45
%50 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
br label %83
51: ; preds = %45
%52 = load i32, i32* @B115200
%53 = call i64 @cfsetispeed(%struct.termios* %10, i32 %52)
%54 = icmp slt i64 %53, 0
br i1 %54, label %59, label %55
55: ; preds = %51
%56 = load i32, i32* @B115200
%57 = call i64 @cfsetospeed(%struct.termios* %10, i32 %56)
%58 = icmp slt i64 %57, 0
br i1 %58, label %59, label %61
59: ; preds = %55, %51
%60 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str.4, i64 0, i64 0))
br label %83
61: ; preds = %55
%62 = load i32, i32* %9
%63 = load i32, i32* @TCSAFLUSH
%64 = call i64 @tcsetattr(i32 %62, i32 %63, %struct.termios* %10)
%65 = icmp slt i64 %64, 0
br i1 %65, label %66, label %68
66: ; preds = %61
%67 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.5, i64 0, i64 0))
br label %83
68: ; preds = %61
%69 = load i32, i32* %9
%70 = call i32 @dup2(i32 %69, i32 0)
%71 = load i32, i32* %9
%72 = call i32 @dup2(i32 %71, i32 1)
%73 = load i32, i32* %9
%74 = call i32 @dup2(i32 %73, i32 2)
%75 = call i32 @putenv(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0))
%76 = load i8*, i8** %8
%77 = getelementptr inbounds [2 x i8*], [2 x i8*]* %11, i64 0, i64 0
store i8* %76, i8** %77
%78 = getelementptr inbounds [2 x i8*], [2 x i8*]* %11, i64 0, i64 1
store i8* null, i8** %78
%79 = load i8*, i8** %8
%80 = getelementptr inbounds [2 x i8*], [2 x i8*]* %11, i64 0, i64 0
%81 = call i32 @execvp(i8* %79, i8** %80)
%82 = call i32 @exit(i32 0)
unreachable
83: ; preds = %66, %59, %49, %40, %30
ret i32 -1
}

LLVM IR (-O2):
%struct.termios = type { i32 }
@.str = [5 x i8] c"d:b:\00"
@optarg = global i8* null
@.str.1 = [31 x i8] c"Starting with shell=%s dev=%s\0A\00"
@O_RDWR = global i32 0
@.str.2 = [40 x i8] c"Error: can't open device. Error=%d(%s)\0A\00"
@errno = global i32 0
@B115200 = global i32 0
@TCSAFLUSH = global i32 0
@.str.6 = [41 x i8] c"PATH=/system/bin:/system/xbin:/sbin:/bin\00"
@str = [29 x i8] c"Can't set attrs to tty port \00"
@str.7 = [28 x i8] c"Can't alter tty port config\00"
@str.8 = [31 x i8] c"Can't get attrs from tty port \00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca %struct.termios
%4 = alloca [2 x i8*]
%5 = bitcast %struct.termios* %3 to i8*
%6 = bitcast [2 x i8*]* %4 to i8*
br label %7
7: ; preds = %16, %2
%8 = phi i8* [ %17, %16 ], [ null, %2 ]
%9 = phi i8* [ %11, %16 ], [ null, %2 ]
br label %10
10: ; preds = %7, %14
%11 = phi i8* [ %9, %7 ], [ %15, %14 ]
br label %12
12: ; preds = %10, %12
%13 = tail call i32 @getopt(i32 %0, i8** %1, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
switch i32 %13, label %12 [
i32 -1, label %18
i32 100, label %14
i32 98, label %16
]
14: ; preds = %12
%15 = load i8*, i8** @optarg
br label %10
16: ; preds = %12
%17 = load i8*, i8** @optarg
br label %7
18: ; preds = %12
%19 = icmp eq i8* %11, null
%20 = icmp eq i8* %8, null
%21 = select i1 %19, i1 true, i1 %20
br i1 %21, label %22, label %24
22: ; preds = %18
%23 = tail call i32 (...) @usage()
br label %63
24: ; preds = %18
%25 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), i8* %8, i8* %11)
%26 = load i32, i32* @O_RDWR
%27 = tail call i32 @open(i8* %11, i32 %26, i32 0)
%28 = icmp slt i32 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %24
%30 = load i32, i32* @errno
%31 = tail call i8* @strerror(i32 %30)
%32 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0), i32 %30, i8* %31)
br label %63
33: ; preds = %24
%34 = call i64 @tcgetattr(i32 %27, %struct.termios* %3)
%35 = icmp slt i64 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %33
%37 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str.8, i64 0, i64 0))
br label %63
38: ; preds = %33
%39 = load i32, i32* @B115200
%40 = call i64 @cfsetispeed(%struct.termios* %3, i32 %39)
%41 = icmp slt i64 %40, 0
br i1 %41, label %46, label %42
42: ; preds = %38
%43 = load i32, i32* @B115200
%44 = call i64 @cfsetospeed(%struct.termios* %3, i32 %43)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %48
46: ; preds = %42, %38
%47 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @str.7, i64 0, i64 0))
br label %63
48: ; preds = %42
%49 = load i32, i32* @TCSAFLUSH
%50 = call i64 @tcsetattr(i32 %27, i32 %49, %struct.termios* %3)
%51 = icmp slt i64 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %48
%53 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([29 x i8], [29 x i8]* @str, i64 0, i64 0))
br label %63
54: ; preds = %48
%55 = call i32 @dup2(i32 %27, i32 0)
%56 = call i32 @dup2(i32 %27, i32 1)
%57 = call i32 @dup2(i32 %27, i32 2)
%58 = call i32 @putenv(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.6, i64 0, i64 0))
%59 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 0
store i8* %8, i8** %59
%60 = getelementptr inbounds [2 x i8*], [2 x i8*]* %4, i64 0, i64 1
store i8* null, i8** %60
%61 = call i32 @execvp(i8* %8, i8** %59)
%62 = call i32 @exit(i32 0)
unreachable
63: ; preds = %52, %46, %36, %29, %22
ret i32 -1
}