#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 {
  char *d_name;
};
typedef int /*<<< orphan*/ readBuf;
typedef int /*<<< orphan*/ productPath;
typedef int /*<<< orphan*/ devicePath;
typedef int /*<<< orphan*/ deviceLink;
typedef int /*<<< orphan*/ FILE;
typedef int /*<<< orphan*/ DIR;

/* Variables and functions */
scalar_t__ closedir(int /*<<< orphan*/ *);
int errno;
scalar_t__ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *);
int fread(char *, int, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ *opendir(char *);
struct dirent *readdir(int /*<<< orphan*/ *);
int readlink(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *, char *, ...);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ stderr;
scalar_t__ strcmp(char *, char *);
char *strerror(int);
int /*<<< orphan*/ strlen(char *);
int /*<<< orphan*/ strncmp(char *, char *, int /*<<< orphan*/);
int getTTYMappedName(char *ttyPrefix, char *productId, char *bindingDevice) {
  int rc;
  char deviceLink[255];
  char devicePath[255];
  char readBuf[32];
  char productPath[255];
  FILE *product;
  DIR *ttyDir;
  int res = 0;
  struct dirent *currentEntry;
  char *searchString;
  if (!bindingDevice) {
    return 0;
  }
  if (strcmp(ttyPrefix, "ttyUSB") == 0) {
    searchString = "%s%s/%s/../../product";
  } else {
    searchString = "%s%s/%s/../product";
  }
  sprintf(bindingDevice, "<unknown>");
  ttyDir = opendir("/sys/class/tty/");
  if (ttyDir) {
    while ((currentEntry = readdir(ttyDir))) {
      if ((!strncmp(currentEntry->d_name, ttyPrefix, strlen(ttyPrefix)))) {
        snprintf(devicePath, sizeof(devicePath), "%s%s/device",
                 "/sys/class/tty/", currentEntry->d_name);
        rc = readlink(devicePath, deviceLink, sizeof(deviceLink));
        if (rc > 0) {
          deviceLink[rc] = 0;
          snprintf(productPath, sizeof(productPath), searchString,
                   "/sys/class/tty/", currentEntry->d_name, deviceLink);
          product = fopen(productPath, "r");
          if (product) {
            rc = fread(readBuf, sizeof(char), sizeof(readBuf), product);
            if ((rc > 0) && (!strncmp(readBuf, productId, strlen(productId)))) {
              sprintf(bindingDevice, "/dev/%s", currentEntry->d_name);
              res = 1;
            } else if (-1 == rc) {
              fprintf(stderr,
                      "getTTYMappedName - Failed to read %s (errno:%d) "
                      "(err:%s)\n",
                      productPath, errno, strerror(errno));
            }
            if (0 != fclose(product)) {
              fprintf(stderr,
                      "getTTYMappedName - Failed to close %s (errno:%d) "
                      "(err:%s)\n",
                      productPath, errno, strerror(errno));
            }
          } else {
            fprintf(
                stderr,
                "getTTYMappedName - Failed to open %s (errno:%d) (err:%s)\n",
                productPath, errno, strerror(errno));
          }
        } else {
          fprintf(stderr,
                  "getTTYMappedName - Failed to read link %s (errno:%d) "
                  "(err:%s)\n",
                  devicePath, errno, strerror(errno));
        }
      }
    }
    if (0 != closedir(ttyDir)) {
      fprintf(stderr,
              "getTTYMappedName - Failed to close %s (errno:%d) (err:%s)\n",
              "/sys/class/tty/", errno, strerror(errno));
    }
  } else {
    fprintf(stderr,
            "getTTYMappedName - Failed to open %s (errno:%d) (err:%s)\n",
            "/sys/class/tty/", errno, strerror(errno));
  }
  return res;
}


x86 assembly (clang, -O0):
<getTTYMappedName>:
push   %rbp
mov    %rsp,%rbp
sub    $0x3f0,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %rdx,-0x20(%rbp)
movl   $0x0,-0x364(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    1218 <getTTYMappedName+0x38>
movl   $0x0,-0x4(%rbp)
jmp    16ab <getTTYMappedName+0x4cb>
mov    -0x10(%rbp),%rdi
lea    0xddd(%rip),%rsi
call   10b0 <strcmp@plt>
cmp    $0x0,%rax
jne    1245 <getTTYMappedName+0x65>
lea    0xdce(%rip),%rax
mov    %rax,-0x378(%rbp)
jmp    1253 <getTTYMappedName+0x73>
lea    0xdd1(%rip),%rax
mov    %rax,-0x378(%rbp)
mov    -0x20(%rbp),%rdi
lea    0xdd2(%rip),%rsi
mov    $0x0,%al
call   10f0 <sprintf@plt>
lea    0xdce(%rip),%rdi
call   1070 <opendir@plt>
mov    %rax,-0x360(%rbp)
cmpq   $0x0,-0x360(%rbp)
je     1654 <getTTYMappedName+0x474>
jmp    128b <getTTYMappedName+0xab>
mov    -0x360(%rbp),%rdi
call   10d0 <readdir@plt>
mov    %rax,-0x370(%rbp)
cmp    $0x0,%rax
je     15e7 <getTTYMappedName+0x407>
mov    -0x370(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x388(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x380(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x388(%rbp),%rdi
mov    -0x380(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    15e2 <getTTYMappedName+0x402>
lea    -0x230(%rbp),%rdi
mov    -0x370(%rbp),%rax
mov    (%rax),%r8
mov    $0xff,%esi
lea    0xd42(%rip),%rdx
lea    0xd2b(%rip),%rcx
mov    $0x0,%al
call   1090 <snprintf@plt>
lea    -0x230(%rbp),%rdi
lea    -0x130(%rbp),%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1581 <getTTYMappedName+0x3a1>
movslq -0x24(%rbp),%rax
movb   $0x0,-0x130(%rbp,%rax,1)
lea    -0x350(%rbp),%rdi
mov    -0x378(%rbp),%rdx
mov    -0x370(%rbp),%rax
mov    (%rax),%r8
lea    -0x130(%rbp),%r9
mov    $0xff,%esi
lea    0xcc8(%rip),%rcx
mov    $0x0,%al
call   1090 <snprintf@plt>
lea    -0x350(%rbp),%rdi
lea    0xccf(%rip),%rsi
call   10e0 <fopen@plt>
mov    %rax,-0x358(%rbp)
cmpq   $0x0,-0x358(%rbp)
je     1520 <getTTYMappedName+0x340>
lea    -0x250(%rbp),%rdi
mov    -0x358(%rbp),%rcx
mov    $0x1,%esi
mov    $0x20,%edx
call   1040 <fread@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jle    1436 <getTTYMappedName+0x256>
lea    -0x250(%rbp),%rax
mov    %rax,-0x398(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,-0x390(%rbp)
mov    -0x18(%rbp),%rdi
call   1080 <strlen@plt>
mov    -0x398(%rbp),%rdi
mov    -0x390(%rbp),%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
cmp    $0x0,%eax
jne    1436 <getTTYMappedName+0x256>
mov    -0x20(%rbp),%rdi
mov    -0x370(%rbp),%rax
mov    (%rax),%rdx
lea    0xc38(%rip),%rsi
mov    $0x0,%al
call   10f0 <sprintf@plt>
movl   $0x1,-0x364(%rbp)
jmp    14a5 <getTTYMappedName+0x2c5>
mov    $0xffffffff,%eax
cmp    -0x24(%rbp),%eax
jne    14a0 <getTTYMappedName+0x2c0>
mov    0x2b95(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3ac(%rbp)
lea    -0x350(%rbp),%rax
mov    %rax,-0x3a8(%rbp)
mov    0x2b70(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x39c(%rbp)
mov    0x2b61(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x3ac(%rbp),%edi
mov    -0x3a8(%rbp),%rdx
mov    -0x39c(%rbp),%ecx
mov    %rax,%r8
lea    0xbc5(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    14a5 <getTTYMappedName+0x2c5>
mov    -0x358(%rbp),%rdi
call   1060 <fclose@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     151b <getTTYMappedName+0x33b>
mov    0x2b1a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3bc(%rbp)
lea    -0x350(%rbp),%rax
mov    %rax,-0x3b8(%rbp)
mov    0x2af5(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3b0(%rbp)
mov    0x2ae6(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x3bc(%rbp),%edi
mov    -0x3b8(%rbp),%rdx
mov    -0x3b0(%rbp),%ecx
mov    %rax,%r8
lea    0xb84(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    157c <getTTYMappedName+0x39c>
mov    0x2ab9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3cc(%rbp)
lea    -0x350(%rbp),%rax
mov    %rax,-0x3c8(%rbp)
mov    0x2a94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c0(%rbp)
mov    0x2a85(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x3cc(%rbp),%edi
mov    -0x3c8(%rbp),%rdx
mov    -0x3c0(%rbp),%ecx
mov    %rax,%r8
lea    0xb5e(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    15dd <getTTYMappedName+0x3fd>
mov    0x2a58(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3dc(%rbp)
lea    -0x230(%rbp),%rax
mov    %rax,-0x3d8(%rbp)
mov    0x2a33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3d0(%rbp)
mov    0x2a24(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x3dc(%rbp),%edi
mov    -0x3d8(%rbp),%rdx
mov    -0x3d0(%rbp),%ecx
mov    %rax,%r8
lea    0xb37(%rip),%rsi
call   10c0 <fprintf@plt>
jmp    15e2 <getTTYMappedName+0x402>
jmp    128b <getTTYMappedName+0xab>
mov    -0x360(%rbp),%rdi
call   10a0 <closedir@plt>
mov    %rax,%rcx
xor    %eax,%eax
cmp    %rcx,%rax
je     164f <getTTYMappedName+0x46f>
mov    0x29d8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3e4(%rbp)
mov    0x29c1(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3e0(%rbp)
mov    0x29b2(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x3e4(%rbp),%edi
mov    -0x3e0(%rbp),%ecx
mov    %rax,%r8
lea    0xa57(%rip),%rsi
lea    0x9f0(%rip),%rdx
call   10c0 <fprintf@plt>
jmp    16a2 <getTTYMappedName+0x4c2>
mov    0x2985(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3ec(%rbp)
mov    0x296e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3e8(%rbp)
mov    0x295f(%rip),%rax
mov    (%rax),%edi
call   1100 <strerror@plt>
mov    -0x3ec(%rbp),%edi
mov    -0x3e8(%rbp),%ecx
mov    %rax,%r8
lea    0xa3f(%rip),%rsi
lea    0x99d(%rip),%rdx
call   10c0 <fprintf@plt>
mov    -0x364(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x3f0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getTTYMappedName>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x358,%rsp
mov    %rsi,0x18(%rsp)
xor    %r15d,%r15d
test   %rdx,%rdx
je     1535 <getTTYMappedName+0x355>
mov    %rdx,%rbp
mov    %rdi,%rbx
lea    0xdf1(%rip),%rsi
call   10b0 <strcmp@plt>
test   %rax,%rax
lea    0xde9(%rip),%rax
lea    0xdf8(%rip),%rcx
cmove  %rax,%rcx
mov    %rcx,0x20(%rsp)
movabs $0x6e776f6e6b6e753c,%rax
6f 77 6e
mov    %rax,0x0(%rbp)
movw   $0x3e,0x8(%rbp)
lea    0xdf1(%rip),%rdi
call   1070 <opendir@plt>
test   %rax,%rax
je     14af <getTTYMappedName+0x2cf>
mov    %rbp,0x10(%rsp)
mov    %rax,0x8(%rsp)
mov    %rax,%rdi
call   10d0 <readdir@plt>
test   %rax,%rax
je     14e6 <getTTYMappedName+0x306>
mov    %rax,%r14
mov    %rbx,0x28(%rsp)
lea    0xdb9(%rip),%r12
lea    0x250(%rsp),%r13
lea    0x30(%rsp),%r15
movl   $0x0,0x4(%rsp)
jmp    12b9 <getTTYMappedName+0xd9>
nopl   0x0(%rax,%rax,1)
mov    0x8(%rsp),%rdi
mov    %rbp,%r12
call   10d0 <readdir@plt>
mov    %rax,%r14
test   %rax,%rax
je     14ee <getTTYMappedName+0x30e>
mov    %r12,%rbp
mov    (%r14),%rbx
mov    0x28(%rsp),%r12
mov    %r12,%rdi
call   1080 <strlen@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    12a0 <getTTYMappedName+0xc0>
mov    (%r14),%r8
lea    0x50(%rsp),%rbx
mov    %rbx,%rdi
mov    $0xff,%esi
lea    0xd56(%rip),%rdx
mov    %rbp,%r12
mov    %rbp,%rcx
xor    %eax,%eax
call   1090 <snprintf@plt>
mov    %rbx,%rdi
lea    0x150(%rsp),%rbx
mov    %rbx,%rsi
mov    $0xff,%edx
call   1050 <readlink@plt>
test   %eax,%eax
jle    13cb <getTTYMappedName+0x1eb>
mov    %eax,%eax
movb   $0x0,0x150(%rsp,%rax,1)
mov    (%r14),%r8
mov    %r13,%rdi
mov    $0xff,%esi
mov    0x20(%rsp),%rdx
mov    %rbx,%r9
mov    %r13,%rbx
mov    %r12,%rcx
xor    %eax,%eax
call   1090 <snprintf@plt>
mov    %r13,%rdi
lea    0xd01(%rip),%rsi
call   10e0 <fopen@plt>
test   %rax,%rax
je     1401 <getTTYMappedName+0x221>
mov    %rax,%r12
mov    %r15,%rbp
mov    %r15,%rdi
mov    $0x1,%esi
mov    $0x20,%edx
mov    %rax,%rcx
call   1040 <fread@plt>
test   %eax,%eax
jle    1427 <getTTYMappedName+0x247>
mov    0x18(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <strlen@plt>
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    %eax,%edx
call   1030 <strncmp@plt>
test   %eax,%eax
jne    1460 <getTTYMappedName+0x280>
mov    (%r14),%rdx
mov    0x10(%rsp),%rdi
lea    0xca1(%rip),%rsi
xor    %eax,%eax
call   10f0 <sprintf@plt>
movl   $0x1,0x4(%rsp)
jmp    1460 <getTTYMappedName+0x280>
mov    0x2c0e(%rip),%rax
mov    (%rax),%ebx
mov    0x2bfd(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1100 <strerror@plt>
mov    %ebx,%edi
lea    0xd22(%rip),%rsi
lea    0x50(%rsp),%rdx
mov    %ebp,%ecx
mov    %rax,%r8
call   10c0 <fprintf@plt>
jmp    14a5 <getTTYMappedName+0x2c5>
mov    0x2bd8(%rip),%rax
mov    (%rax),%r14d
mov    0x2bc6(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1100 <strerror@plt>
mov    %r14d,%edi
lea    0xcb0(%rip),%rsi
jmp    1491 <getTTYMappedName+0x2b1>
cmp    $0xffffffff,%eax
jne    1460 <getTTYMappedName+0x280>
mov    0x2bad(%rip),%rax
mov    (%rax),%r14d
mov    %rbx,%rbp
mov    0x2b98(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1100 <strerror@plt>
mov    %r14d,%edi
lea    0xc0d(%rip),%rsi
mov    %rbp,%rdx
mov    %ebx,%ecx
mov    %rax,%r8
call   10c0 <fprintf@plt>
mov    %r12,%rdi
call   1060 <fclose@plt>
test   %rax,%rax
je     149e <getTTYMappedName+0x2be>
mov    0x2b6c(%rip),%rax
mov    (%rax),%r14d
mov    0x2b5a(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   1100 <strerror@plt>
mov    %r14d,%edi
lea    0xc09(%rip),%rsi
mov    %r13,%rdx
mov    %ebx,%ecx
mov    %rax,%r8
call   10c0 <fprintf@plt>
lea    0xb95(%rip),%r12
mov    0x8(%rsp),%rdi
jmp    12a8 <getTTYMappedName+0xc8>
mov    0x2b2a(%rip),%rax
mov    (%rax),%r14d
mov    0x2b18(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1100 <strerror@plt>
lea    0xc05(%rip),%rsi
lea    0xb63(%rip),%rdx
mov    %r14d,%edi
mov    %ebp,%ecx
mov    %rax,%r8
call   10c0 <fprintf@plt>
jmp    1535 <getTTYMappedName+0x355>
movl   $0x0,0x4(%rsp)
mov    0x8(%rsp),%rdi
call   10a0 <closedir@plt>
test   %rax,%rax
je     1530 <getTTYMappedName+0x350>
mov    0x2adc(%rip),%rax
mov    (%rax),%ebx
mov    0x2acb(%rip),%rax
mov    (%rax),%ebp
mov    %ebp,%edi
call   1100 <strerror@plt>
lea    0xb7d(%rip),%rsi
lea    0xb16(%rip),%rdx
mov    %ebx,%edi
mov    %ebp,%ecx
mov    %rax,%r8
call   10c0 <fprintf@plt>
mov    0x4(%rsp),%r15d
mov    %r15d,%eax
add    $0x358,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.dirent = type { i8* }
@.str = [7 x i8] c"ttyUSB\00"
@.str.1 = [22 x i8] c"%s%s/%s/../../product\00"
@.str.2 = [19 x i8] c"%s%s/%s/../product\00"
@.str.3 = [10 x i8] c"<unknown>\00"
@.str.4 = [16 x i8] c"/sys/class/tty/\00"
@.str.5 = [12 x i8] c"%s%s/device\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [8 x i8] c"/dev/%s\00"
@stderr = global i32 0
@.str.8 = [58 x i8] c"getTTYMappedName - Failed to read %s (errno:%d) (err:%s)\0A\00"
@errno = global i32 0
@.str.9 = [59 x i8] c"getTTYMappedName - Failed to close %s (errno:%d) (err:%s)\0A\00"
@.str.10 = [58 x i8] c"getTTYMappedName - Failed to open %s (errno:%d) (err:%s)\0A\00"
@.str.11 = [63 x i8] c"getTTYMappedName - Failed to read link %s (errno:%d) (err:%s)\0A\00"
define i32 @getTTYMappedName(i8* %0, i8* %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i8*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca [255 x i8]
%10 = alloca [255 x i8]
%11 = alloca [32 x i8]
%12 = alloca [255 x i8]
%13 = alloca i32*
%14 = alloca i32*
%15 = alloca i32
%16 = alloca %struct.dirent*
%17 = alloca i8*
store i8* %0, i8** %5
store i8* %1, i8** %6
store i8* %2, i8** %7
store i32 0, i32* %15
%18 = load i8*, i8** %7
%19 = icmp ne i8* %18, null
br i1 %19, label %21, label %20
20: ; preds = %3
store i32 0, i32* %4
br label %151
21: ; preds = %3
%22 = load i8*, i8** %5
%23 = call i64 @strcmp(i8* %22, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %21
store i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8** %17
br label %27
26: ; preds = %21
store i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0), i8** %17
br label %27
27: ; preds = %26, %25
%28 = load i8*, i8** %7
%29 = call i32 (i8*, i8*, ...) @sprintf(i8* %28, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32* @opendir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
store i32* %30, i32** %14
%31 = load i32*, i32** %14
%32 = icmp ne i32* %31, null
br i1 %32, label %33, label %143
33: ; preds = %27
br label %34
34: ; preds = %131, %33
%35 = load i32*, i32** %14
%36 = call %struct.dirent* @readdir(i32* %35)
store %struct.dirent* %36, %struct.dirent** %16
%37 = icmp ne %struct.dirent* %36, null
br i1 %37, label %38, label %132
38: ; preds = %34
%39 = load %struct.dirent*, %struct.dirent** %16
%40 = getelementptr inbounds %struct.dirent, %struct.dirent* %39, i32 0, i32 0
%41 = load i8*, i8** %40
%42 = load i8*, i8** %5
%43 = load i8*, i8** %5
%44 = call i32 @strlen(i8* %43)
%45 = call i32 @strncmp(i8* %41, i8* %42, i32 %44)
%46 = icmp ne i32 %45, 0
br i1 %46, label %131, label %47
47: ; preds = %38
%48 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%49 = load %struct.dirent*, %struct.dirent** %16
%50 = getelementptr inbounds %struct.dirent, %struct.dirent* %49, i32 0, i32 0
%51 = load i8*, i8** %50
%52 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %48, i32 255, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %51)
%53 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%54 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%55 = call i32 @readlink(i8* %53, i8* %54, i32 255)
store i32 %55, i32* %8
%56 = load i32, i32* %8
%57 = icmp sgt i32 %56, 0
br i1 %57, label %58, label %123
58: ; preds = %47
%59 = load i32, i32* %8
%60 = sext i32 %59 to i64
%61 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 %60
store i8 0, i8* %61
%62 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%63 = load i8*, i8** %17
%64 = load %struct.dirent*, %struct.dirent** %16
%65 = getelementptr inbounds %struct.dirent, %struct.dirent* %64, i32 0, i32 0
%66 = load i8*, i8** %65
%67 = getelementptr inbounds [255 x i8], [255 x i8]* %9, i64 0, i64 0
%68 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %62, i32 255, i8* %63, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %66, i8* %67)
%69 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%70 = call i32* @fopen(i8* %69, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
store i32* %70, i32** %13
%71 = load i32*, i32** %13
%72 = icmp ne i32* %71, null
br i1 %72, label %73, label %115
73: ; preds = %58
%74 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%75 = load i32*, i32** %13
%76 = call i32 @fread(i8* %74, i32 1, i32 32, i32* %75)
store i32 %76, i32* %8
%77 = load i32, i32* %8
%78 = icmp sgt i32 %77, 0
br i1 %78, label %79, label %92
79: ; preds = %73
%80 = getelementptr inbounds [32 x i8], [32 x i8]* %11, i64 0, i64 0
%81 = load i8*, i8** %6
%82 = load i8*, i8** %6
%83 = call i32 @strlen(i8* %82)
%84 = call i32 @strncmp(i8* %80, i8* %81, i32 %83)
%85 = icmp ne i32 %84, 0
br i1 %85, label %92, label %86
86: ; preds = %79
%87 = load i8*, i8** %7
%88 = load %struct.dirent*, %struct.dirent** %16
%89 = getelementptr inbounds %struct.dirent, %struct.dirent* %88, i32 0, i32 0
%90 = load i8*, i8** %89
%91 = call i32 (i8*, i8*, ...) @sprintf(i8* %87, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* %90)
store i32 1, i32* %15
br label %103
92: ; preds = %79, %73
%93 = load i32, i32* %8
%94 = icmp eq i32 -1, %93
br i1 %94, label %95, label %102
95: ; preds = %92
%96 = load i32, i32* @stderr
%97 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%98 = load i32, i32* @errno
%99 = load i32, i32* @errno
%100 = call i8* @strerror(i32 %99)
%101 = call i32 @fprintf(i32 %96, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.8, i64 0, i64 0), i8* %97, i32 %98, i8* %100)
br label %102
102: ; preds = %95, %92
br label %103
103: ; preds = %102, %86
%104 = load i32*, i32** %13
%105 = call i64 @fclose(i32* %104)
%106 = icmp ne i64 0, %105
br i1 %106, label %107, label %114
107: ; preds = %103
%108 = load i32, i32* @stderr
%109 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%110 = load i32, i32* @errno
%111 = load i32, i32* @errno
%112 = call i8* @strerror(i32 %111)
%113 = call i32 @fprintf(i32 %108, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.9, i64 0, i64 0), i8* %109, i32 %110, i8* %112)
br label %114
114: ; preds = %107, %103
br label %122
115: ; preds = %58
%116 = load i32, i32* @stderr
%117 = getelementptr inbounds [255 x i8], [255 x i8]* %12, i64 0, i64 0
%118 = load i32, i32* @errno
%119 = load i32, i32* @errno
%120 = call i8* @strerror(i32 %119)
%121 = call i32 @fprintf(i32 %116, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.10, i64 0, i64 0), i8* %117, i32 %118, i8* %120)
br label %122
122: ; preds = %115, %114
br label %130
123: ; preds = %47
%124 = load i32, i32* @stderr
%125 = getelementptr inbounds [255 x i8], [255 x i8]* %10, i64 0, i64 0
%126 = load i32, i32* @errno
%127 = load i32, i32* @errno
%128 = call i8* @strerror(i32 %127)
%129 = call i32 @fprintf(i32 %124, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.11, i64 0, i64 0), i8* %125, i32 %126, i8* %128)
br label %130
130: ; preds = %123, %122
br label %131
131: ; preds = %130, %38
br label %34
132: ; preds = %34
%133 = load i32*, i32** %14
%134 = call i64 @closedir(i32* %133)
%135 = icmp ne i64 0, %134
br i1 %135, label %136, label %142
136: ; preds = %132
%137 = load i32, i32* @stderr
%138 = load i32, i32* @errno
%139 = load i32, i32* @errno
%140 = call i8* @strerror(i32 %139)
%141 = call i32 @fprintf(i32 %137, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %138, i8* %140)
br label %142
142: ; preds = %136, %132
br label %149
143: ; preds = %27
%144 = load i32, i32* @stderr
%145 = load i32, i32* @errno
%146 = load i32, i32* @errno
%147 = call i8* @strerror(i32 %146)
%148 = call i32 @fprintf(i32 %144, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %145, i8* %147)
br label %149
149: ; preds = %143, %142
%150 = load i32, i32* %15
store i32 %150, i32* %4
br label %151
151: ; preds = %149, %20
%152 = load i32, i32* %4
ret i32 %152
}

LLVM IR (-O2):
%struct.dirent = type { i8* }
@.str = [7 x i8] c"ttyUSB\00"
@.str.1 = [22 x i8] c"%s%s/%s/../../product\00"
@.str.2 = [19 x i8] c"%s%s/%s/../product\00"
@.str.3 = [10 x i8] c"<unknown>\00"
@.str.4 = [16 x i8] c"/sys/class/tty/\00"
@.str.5 = [12 x i8] c"%s%s/device\00"
@.str.6 = [2 x i8] c"r\00"
@.str.7 = [8 x i8] c"/dev/%s\00"
@stderr = global i32 0
@.str.8 = [58 x i8] c"getTTYMappedName - Failed to read %s (errno:%d) (err:%s)\0A\00"
@errno = global i32 0
@.str.9 = [59 x i8] c"getTTYMappedName - Failed to close %s (errno:%d) (err:%s)\0A\00"
@.str.10 = [58 x i8] c"getTTYMappedName - Failed to open %s (errno:%d) (err:%s)\0A\00"
@.str.11 = [63 x i8] c"getTTYMappedName - Failed to read link %s (errno:%d) (err:%s)\0A\00"
define i32 @getTTYMappedName(i8* %0, i8* %1, i8* writeonly %2) {
%4 = alloca [255 x i8]
%5 = alloca [255 x i8]
%6 = alloca [32 x i8]
%7 = alloca [255 x i8]
%8 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 0
%9 = getelementptr inbounds [255 x i8], [255 x i8]* %5, i64 0, i64 0
%10 = getelementptr inbounds [32 x i8], [32 x i8]* %6, i64 0, i64 0
%11 = getelementptr inbounds [255 x i8], [255 x i8]* %7, i64 0, i64 0
%12 = icmp eq i8* %2, null
br i1 %12, label %96, label %13
13: ; preds = %3
%14 = tail call i64 @strcmp(i8* %0, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%15 = icmp eq i64 %14, 0
%16 = select i1 %15, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([19 x i8], [19 x i8]* @.str.2, i64 0, i64 0)
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 dereferenceable(10) %2, i8* align 1 dereferenceable(10) getelementptr inbounds ([10 x i8], [10 x i8]* @.str.3, i64 0, i64 0), i64 10, i1 false)
%17 = tail call i32* @opendir(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
%18 = icmp eq i32* %17, null
br i1 %18, label %91, label %19
19: ; preds = %13
%20 = call %struct.dirent* @readdir(i32* %17)
%21 = icmp eq %struct.dirent* %20, null
br i1 %21, label %82, label %22
22: ; preds = %19, %78
%23 = phi %struct.dirent* [ %80, %78 ], [ %20, %19 ]
%24 = phi i32 [ %79, %78 ], [ 0, %19 ]
%25 = getelementptr inbounds %struct.dirent, %struct.dirent* %23, i64 0, i32 0
%26 = load i8*, i8** %25
%27 = call i32 @strlen(i8* %0)
%28 = call i32 @strncmp(i8* %26, i8* %0, i32 %27)
%29 = icmp eq i32 %28, 0
br i1 %29, label %30, label %78
30: ; preds = %22
%31 = load i8*, i8** %25
%32 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %9, i32 255, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.5, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %31)
%33 = call i32 @readlink(i8* %9, i8* %8, i32 255)
%34 = icmp sgt i32 %33, 0
br i1 %34, label %35, label %73
35: ; preds = %30
%36 = zext i32 %33 to i64
%37 = getelementptr inbounds [255 x i8], [255 x i8]* %4, i64 0, i64 %36
store i8 0, i8* %37
%38 = load i8*, i8** %25
%39 = call i32 (i8*, i32, i8*, i8*, i8*, ...) @snprintf(i8* %11, i32 255, i8* %16, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i8* %38, i8* %8)
%40 = call i32* @fopen(i8* %11, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.6, i64 0, i64 0))
%41 = icmp eq i32* %40, null
br i1 %41, label %68, label %42
42: ; preds = %35
%43 = call i32 @fread(i8* %10, i32 1, i32 32, i32* %40)
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %52
45: ; preds = %42
%46 = call i32 @strlen(i8* %1)
%47 = call i32 @strncmp(i8* %10, i8* %1, i32 %46)
%48 = icmp eq i32 %47, 0
br i1 %48, label %49, label %59
49: ; preds = %45
%50 = load i8*, i8** %25
%51 = call i32 (i8*, i8*, ...) @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str.7, i64 0, i64 0), i8* %50)
br label %59
52: ; preds = %42
%53 = icmp eq i32 %43, -1
br i1 %53, label %54, label %59
54: ; preds = %52
%55 = load i32, i32* @stderr
%56 = load i32, i32* @errno
%57 = call i8* @strerror(i32 %56)
%58 = call i32 @fprintf(i32 %55, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.8, i64 0, i64 0), i8* %11, i32 %56, i8* %57)
br label %59
59: ; preds = %45, %52, %54, %49
%60 = phi i32 [ %24, %54 ], [ %24, %52 ], [ 1, %49 ], [ %24, %45 ]
%61 = call i64 @fclose(i32* %40)
%62 = icmp eq i64 %61, 0
br i1 %62, label %78, label %63
63: ; preds = %59
%64 = load i32, i32* @stderr
%65 = load i32, i32* @errno
%66 = call i8* @strerror(i32 %65)
%67 = call i32 @fprintf(i32 %64, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.9, i64 0, i64 0), i8* %11, i32 %65, i8* %66)
br label %78
68: ; preds = %35
%69 = load i32, i32* @stderr
%70 = load i32, i32* @errno
%71 = call i8* @strerror(i32 %70)
%72 = call i32 @fprintf(i32 %69, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.10, i64 0, i64 0), i8* %11, i32 %70, i8* %71)
br label %78
73: ; preds = %30
%74 = load i32, i32* @stderr
%75 = load i32, i32* @errno
%76 = call i8* @strerror(i32 %75)
%77 = call i32 @fprintf(i32 %74, i8* getelementptr inbounds ([63 x i8], [63 x i8]* @.str.11, i64 0, i64 0), i8* %9, i32 %75, i8* %76)
br label %78
78: ; preds = %73, %59, %63, %68, %22
%79 = phi i32 [ %24, %22 ], [ %60, %63 ], [ %60, %59 ], [ %24, %68 ], [ %24, %73 ]
%80 = call %struct.dirent* @readdir(i32* %17)
%81 = icmp eq %struct.dirent* %80, null
br i1 %81, label %82, label %22
82: ; preds = %78, %19
%83 = phi i32 [ 0, %19 ], [ %79, %78 ]
%84 = call i64 @closedir(i32* %17)
%85 = icmp eq i64 %84, 0
br i1 %85, label %96, label %86
86: ; preds = %82
%87 = load i32, i32* @stderr
%88 = load i32, i32* @errno
%89 = call i8* @strerror(i32 %88)
%90 = call i32 @fprintf(i32 %87, i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.9, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %88, i8* %89)
br label %96
91: ; preds = %13
%92 = load i32, i32* @stderr
%93 = load i32, i32* @errno
%94 = tail call i8* @strerror(i32 %93)
%95 = tail call i32 @fprintf(i32 %92, i8* getelementptr inbounds ([58 x i8], [58 x i8]* @.str.10, i64 0, i64 0), i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0), i32 %93, i8* %94)
br label %96
96: ; preds = %91, %86, %82, %3
%97 = phi i32 [ 0, %3 ], [ %83, %86 ], [ %83, %82 ], [ 0, %91 ]
ret i32 %97
}