#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 dirent {
  int /*<<< orphan*/ d_name;
};

/* Variables and functions */
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, char *);
int /*<<< orphan*/ serialFlush(int);
int serialOpen(char *, int);
int serialReadChar(int);
int /*<<< orphan*/ serialWriteString(int, char *);
scalar_t__ startsWith(char *, char *);
int /*<<< orphan*/ strcat(char *, int /*<<< orphan*/);
int /*<<< orphan*/ strcpy(char *, char *);
char *strstr(char *, char *);
int openDevice(struct dirent *dir) {
  char buf[1024 + 1];
  char devicePath[32];
  strcpy(devicePath, "/dev/");
  strcat(devicePath, dir->d_name);
  int fd = serialOpen(devicePath, 115200);
  if (fd > 0) {
    char getIdCmd[] = {0x55, 0x30, 0x01, 0x0D, 0x0A, 0x00};
    serialWriteString(fd, getIdCmd);
    serialFlush(fd);
    int i = 0;
    int j = 0;
    int head = 0;
    int callId = 0;
    int chr;
    memset(buf, 0, sizeof buf);
    while ((chr = serialReadChar(fd)) > 0) {
      if (head == 0 && chr == '\t') {
        head = chr;
      } else if (head == '\t') {
        if (callId == 0) {
          callId = chr;
        } else {
          buf[j++] = (chr & 0xFF);
        }
      }
    }
    if (startsWith(buf, "UUGear-")) {
      char *end = strstr(buf, ":)");
      if (end != NULL) {
        buf[end - buf] = 0;
      }
      printf("%s\t(%s)\n", buf, devicePath);
      return fd;
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<openDevice>:
push   %rbp
mov    %rsp,%rbp
sub    $0x470,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x440(%rbp),%rdi
lea    0xe43(%rip),%rsi
call   1040 <strcpy@plt>
lea    -0x440(%rbp),%rdi
mov    -0x10(%rbp),%rax
mov    (%rax),%esi
call   10b0 <strcat@plt>
lea    -0x440(%rbp),%rdi
mov    $0x1c200,%esi
call   1030 <serialOpen@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0x0,-0x444(%rbp)
jle    139b <openDevice+0x1fb>
mov    0xe08(%rip),%eax
mov    %eax,-0x44a(%rbp)
mov    0xdff(%rip),%ax
mov    %ax,-0x446(%rbp)
mov    -0x444(%rbp),%edi
lea    -0x44a(%rbp),%rsi
call   10a0 <serialWriteString@plt>
mov    -0x444(%rbp),%edi
call   1090 <serialFlush@plt>
movl   $0x0,-0x450(%rbp)
movl   $0x0,-0x454(%rbp)
movl   $0x0,-0x458(%rbp)
movl   $0x0,-0x45c(%rbp)
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0x401,%edx
call   1070 <memset@plt>
mov    -0x444(%rbp),%edi
call   1080 <serialReadChar@plt>
mov    %eax,-0x460(%rbp)
cmp    $0x0,%eax
jle    1310 <openDevice+0x170>
cmpl   $0x0,-0x458(%rbp)
jne    12af <openDevice+0x10f>
cmpl   $0x9,-0x460(%rbp)
jne    12af <openDevice+0x10f>
mov    -0x460(%rbp),%eax
mov    %eax,-0x458(%rbp)
jmp    130b <openDevice+0x16b>
cmpl   $0x9,-0x458(%rbp)
jne    1306 <openDevice+0x166>
cmpl   $0x0,-0x45c(%rbp)
jne    12da <openDevice+0x13a>
mov    -0x460(%rbp),%eax
mov    %eax,-0x45c(%rbp)
jmp    1301 <openDevice+0x161>
mov    -0x460(%rbp),%eax
and    $0xff,%eax
mov    %al,%cl
mov    -0x454(%rbp),%eax
mov    %eax,%edx
add    $0x1,%edx
mov    %edx,-0x454(%rbp)
cltq
mov    %cl,-0x420(%rbp,%rax,1)
jmp    1306 <openDevice+0x166>
jmp    130b <openDevice+0x16b>
jmp    126a <openDevice+0xca>
lea    -0x420(%rbp),%rdi
lea    0xcee(%rip),%rsi
call   1050 <startsWith@plt>
cmp    $0x0,%rax
je     1396 <openDevice+0x1f6>
lea    -0x420(%rbp),%rdi
lea    0xcd9(%rip),%rsi
call   10c0 <strstr@plt>
mov    %rax,-0x468(%rbp)
cmpq   $0x0,-0x468(%rbp)
je     136e <openDevice+0x1ce>
mov    -0x468(%rbp),%rax
lea    -0x420(%rbp),%rcx
sub    %rcx,%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    -0x440(%rbp),%rdx
lea    0xc94(%rip),%rdi
call   1060 <printf@plt>
mov    -0x444(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    13a2 <openDevice+0x202>
jmp    139b <openDevice+0x1fb>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x470,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<openDevice>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x438,%rsp
mov    %rdi,%rbx
lea    0xe49(%rip),%rsi
lea    0x10(%rsp),%r14
mov    %r14,%rdi
call   1040 <strcpy@plt>
mov    (%rbx),%esi
mov    %r14,%rdi
call   10b0 <strcat@plt>
mov    %r14,%rdi
mov    $0x1c200,%esi
call   1030 <serialOpen@plt>
test   %eax,%eax
jle    12b0 <openDevice+0x110>
mov    %eax,%ebx
movw   $0xa,0xc(%rsp)
movl   $0xd013055,0x8(%rsp)
0d
lea    0x8(%rsp),%rsi
mov    %eax,%edi
call   10a0 <serialWriteString@plt>
mov    %ebx,%edi
call   1090 <serialFlush@plt>
lea    0x30(%rsp),%rdi
xor    %r15d,%r15d
xor    %esi,%esi
mov    $0x401,%edx
call   1070 <memset@plt>
mov    %ebx,%edi
call   1080 <serialReadChar@plt>
test   %eax,%eax
jle    1269 <openDevice+0xc9>
xor    %r14d,%r14d
xor    %ecx,%ecx
jmp    123f <openDevice+0x9f>
nopl   (%rax)
mov    %ecx,%ebp
mov    %ebx,%edi
call   1080 <serialReadChar@plt>
mov    %ebp,%ecx
test   %eax,%eax
jle    1269 <openDevice+0xc9>
mov    $0x9,%ebp
test   %ecx,%ecx
jne    124d <openDevice+0xad>
cmp    $0x9,%eax
je     1232 <openDevice+0x92>
cmp    $0x9,%ecx
jne    1230 <openDevice+0x90>
test   %r14d,%r14d
je     1264 <openDevice+0xc4>
movslq %r15d,%rcx
add    $0x1,%r15d
mov    %al,0x30(%rsp,%rcx,1)
jmp    1232 <openDevice+0x92>
mov    %eax,%r14d
jmp    1232 <openDevice+0x92>
lea    0xd9c(%rip),%rsi
lea    0x30(%rsp),%rdi
call   1050 <startsWith@plt>
test   %rax,%rax
je     12b0 <openDevice+0x110>
lea    0xd8e(%rip),%rsi
lea    0x30(%rsp),%rdi
call   10c0 <strstr@plt>
test   %rax,%rax
je     1298 <openDevice+0xf8>
movb   $0x0,(%rax)
lea    0xd78(%rip),%rdi
lea    0x30(%rsp),%rsi
lea    0x10(%rsp),%rdx
call   1060 <printf@plt>
jmp    12b2 <openDevice+0x112>
xor    %ebx,%ebx
mov    %ebx,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/dev/\00"
@__const.openDevice.getIdCmd = [6 x i8] c"U0\01\0D\0A\00"
@.str.1 = [8 x i8] c"UUGear-\00"
@.str.2 = [3 x i8] c":)\00"
@.str.3 = [9 x i8] c"%s\09(%s)\0A\00"
define i32 @openDevice(%struct.dirent* %0) {
%2 = alloca i32
%3 = alloca %struct.dirent*
%4 = alloca [1025 x i8]
%5 = alloca [32 x i8]
%6 = alloca i32
%7 = alloca [6 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i8*
store %struct.dirent* %0, %struct.dirent** %3
%14 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%15 = call i32 @strcpy(i8* %14, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%16 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%17 = load %struct.dirent*, %struct.dirent** %3
%18 = getelementptr inbounds %struct.dirent, %struct.dirent* %17, i32 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @strcat(i8* %16, i32 %19)
%21 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%22 = call i32 @serialOpen(i8* %21, i32 115200)
store i32 %22, i32* %6
%23 = load i32, i32* %6
%24 = icmp sgt i32 %23, 0
br i1 %24, label %25, label %87
25: ; preds = %1
%26 = bitcast [6 x i8]* %7 to i8*
%27 = load i32, i32* %6
%28 = getelementptr inbounds [6 x i8], [6 x i8]* %7, i64 0, i64 0
%29 = call i32 @serialWriteString(i32 %27, i8* %28)
%30 = load i32, i32* %6
%31 = call i32 @serialFlush(i32 %30)
store i32 0, i32* %8
store i32 0, i32* %9
store i32 0, i32* %10
store i32 0, i32* %11
%32 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%33 = call i32 @memset(i8* %32, i32 0, i32 1025)
br label %34
34: ; preds = %64, %25
%35 = load i32, i32* %6
%36 = call i32 @serialReadChar(i32 %35)
store i32 %36, i32* %12
%37 = icmp sgt i32 %36, 0
br i1 %37, label %38, label %65
38: ; preds = %34
%39 = load i32, i32* %10
%40 = icmp eq i32 %39, 0
br i1 %40, label %41, label %46
41: ; preds = %38
%42 = load i32, i32* %12
%43 = icmp eq i32 %42, 9
br i1 %43, label %44, label %46
44: ; preds = %41
%45 = load i32, i32* %12
store i32 %45, i32* %10
br label %64
46: ; preds = %41, %38
%47 = load i32, i32* %10
%48 = icmp eq i32 %47, 9
br i1 %48, label %49, label %63
49: ; preds = %46
%50 = load i32, i32* %11
%51 = icmp eq i32 %50, 0
br i1 %51, label %52, label %54
52: ; preds = %49
%53 = load i32, i32* %12
store i32 %53, i32* %11
br label %62
54: ; preds = %49
%55 = load i32, i32* %12
%56 = and i32 %55, 255
%57 = trunc i32 %56 to i8
%58 = load i32, i32* %9
%59 = add nsw i32 %58, 1
store i32 %59, i32* %9
%60 = sext i32 %58 to i64
%61 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 %60
store i8 %57, i8* %61
br label %62
62: ; preds = %54, %52
br label %63
63: ; preds = %62, %46
br label %64
64: ; preds = %63, %44
br label %34
65: ; preds = %34
%66 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%67 = call i64 @startsWith(i8* %66, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %86
69: ; preds = %65
%70 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%71 = call i8* @strstr(i8* %70, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
store i8* %71, i8** %13
%72 = load i8*, i8** %13
%73 = icmp ne i8* %72, null
br i1 %73, label %74, label %81
74: ; preds = %69
%75 = load i8*, i8** %13
%76 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%77 = ptrtoint i8* %75 to i64
%78 = ptrtoint i8* %76 to i64
%79 = sub i64 %77, %78
%80 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 %79
store i8 0, i8* %80
br label %81
81: ; preds = %74, %69
%82 = getelementptr inbounds [1025 x i8], [1025 x i8]* %4, i64 0, i64 0
%83 = getelementptr inbounds [32 x i8], [32 x i8]* %5, i64 0, i64 0
%84 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %82, i8* %83)
%85 = load i32, i32* %6
store i32 %85, i32* %2
br label %88
86: ; preds = %65
br label %87
87: ; preds = %86, %1
store i32 0, i32* %2
br label %88
88: ; preds = %87, %81
%89 = load i32, i32* %2
ret i32 %89
}

LLVM IR (-O2):
%struct.dirent = type { i32 }
@.str = [6 x i8] c"/dev/\00"
@__const.openDevice.getIdCmd = [6 x i8] c"U0\01\0D\0A\00"
@.str.1 = [8 x i8] c"UUGear-\00"
@.str.2 = [3 x i8] c":)\00"
@.str.3 = [9 x i8] c"%s\09(%s)\0A\00"
define i32 @openDevice(%struct.dirent* nocapture %0) {
%2 = alloca [1025 x i8]
%3 = alloca [32 x i8]
%4 = alloca [6 x i8]
%5 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [32 x i8], [32 x i8]* %3, i64 0, i64 0
%7 = call i32 @strcpy(i8* %6, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds %struct.dirent, %struct.dirent* %0, i64 0, i32 0
%9 = load i32, i32* %8
%10 = call i32 @strcat(i8* %6, i32 %9)
%11 = call i32 @serialOpen(i8* %6, i32 115200)
%12 = icmp sgt i32 %11, 0
br i1 %12, label %13, label %57
13: ; preds = %1
%14 = getelementptr inbounds [6 x i8], [6 x i8]* %4, i64 0, i64 0
%15 = call i32 @serialWriteString(i32 %11, i8* %14)
%16 = call i32 @serialFlush(i32 %11)
%17 = call i32 @memset(i8* %5, i32 0, i32 1025)
%18 = call i32 @serialReadChar(i32 %11)
%19 = icmp sgt i32 %18, 0
br i1 %19, label %20, label %43
20: ; preds = %13, %37
%21 = phi i32 [ %41, %37 ], [ %18, %13 ]
%22 = phi i32 [ %40, %37 ], [ 0, %13 ]
%23 = phi i32 [ %39, %37 ], [ 0, %13 ]
%24 = phi i32 [ %38, %37 ], [ 0, %13 ]
%25 = icmp eq i32 %24, 0
%26 = icmp eq i32 %21, 9
%27 = select i1 %25, i1 %26, i1 false
br i1 %27, label %37, label %28
28: ; preds = %20
%29 = icmp eq i32 %24, 9
br i1 %29, label %30, label %37
30: ; preds = %28
%31 = icmp eq i32 %23, 0
br i1 %31, label %37, label %32
32: ; preds = %30
%33 = trunc i32 %21 to i8
%34 = add nsw i32 %22, 1
%35 = sext i32 %22 to i64
%36 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 %35
store i8 %33, i8* %36
br label %37
37: ; preds = %30, %20, %28, %32
%38 = phi i32 [ 9, %32 ], [ %24, %28 ], [ 9, %20 ], [ 9, %30 ]
%39 = phi i32 [ %23, %32 ], [ %23, %28 ], [ %23, %20 ], [ %21, %30 ]
%40 = phi i32 [ %34, %32 ], [ %22, %28 ], [ %22, %20 ], [ %22, %30 ]
%41 = call i32 @serialReadChar(i32 %11)
%42 = icmp sgt i32 %41, 0
br i1 %42, label %20, label %43
43: ; preds = %37, %13
%44 = call i64 @startsWith(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%45 = icmp eq i64 %44, 0
br i1 %45, label %56, label %46
46: ; preds = %43
%47 = call i8* @strstr(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0))
%48 = icmp eq i8* %47, null
br i1 %48, label %54, label %49
49: ; preds = %46
%50 = ptrtoint i8* %47 to i64
%51 = ptrtoint [1025 x i8]* %2 to i64
%52 = sub i64 %50, %51
%53 = getelementptr inbounds [1025 x i8], [1025 x i8]* %2, i64 0, i64 %52
store i8 0, i8* %53
br label %54
54: ; preds = %49, %46
%55 = call i32 @printf(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0), i8* %5, i8* %6)
br label %57
56: ; preds = %43
br label %57
57: ; preds = %1, %56, %54
%58 = phi i32 [ %11, %54 ], [ 0, %56 ], [ 0, %1 ]
ret i32 %58
}