#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 stat {
  int /*<<< orphan*/ st_mode;
};
typedef int /*<<< orphan*/ buffer;

/* Variables and functions */
scalar_t__ S_ISLNK(int /*<<< orphan*/);
int /*<<< orphan*/ basename(char *);
scalar_t__ lstat(char *, struct stat *);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, char *);
int /*<<< orphan*/ strcat(char *, char *);
int /*<<< orphan*/ strcpy(char *, int /*<<< orphan*/);
void get_driver(char *ttydir, char *ttydrv) {
  struct stat st;
  char devicedir[1024];
  char buffer[1024];
  int len = 0;
  sprintf(devicedir, "%s/device", ttydir);
  if (lstat(devicedir, &st) == 0 && S_ISLNK(st.st_mode)) {
    buffer[0] = '\0';
    strcat(devicedir, "/driver");
    if ((len = readlink(devicedir, buffer, sizeof(buffer))) > 0) {
      buffer[len] = '\0';
      strcpy(ttydrv, basename(buffer));
    } else {
      ttydrv[0] = '\0';
    }
  } else {
    ttydrv[0] = '\0';
  }
}


x86 assembly (clang, -O0):
<get_driver>:
push   %rbp
mov    %rsp,%rbp
sub    $0x830,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x824(%rbp)
lea    -0x420(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe61(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x420(%rbp),%rdi
lea    -0x18(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
jne    1256 <get_driver+0xe6>
mov    -0x18(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%rax
je     1256 <get_driver+0xe6>
movb   $0x0,-0x820(%rbp)
lea    -0x420(%rbp),%rdi
lea    0xe25(%rip),%rsi
call   1080 <strcat@plt>
lea    -0x420(%rbp),%rdi
lea    -0x820(%rbp),%rsi
mov    $0x400,%edx
call   1050 <readlink@plt>
mov    %eax,-0x824(%rbp)
cmp    $0x0,%eax
jle    124a <get_driver+0xda>
movslq -0x824(%rbp),%rax
movb   $0x0,-0x820(%rbp,%rax,1)
mov    -0x10(%rbp),%rax
mov    %rax,-0x830(%rbp)
lea    -0x820(%rbp),%rdi
call   1070 <basename@plt>
mov    -0x830(%rbp),%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
jmp    1251 <get_driver+0xe1>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
jmp    125d <get_driver+0xed>
mov    -0x10(%rbp),%rax
movb   $0x0,(%rax)
add    $0x830,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<get_driver>:
push   %r14
push   %rbx
sub    $0x818,%rsp
mov    %rsi,%r14
mov    %rdi,%rdx
lea    0xe79(%rip),%rsi
lea    0x410(%rsp),%rbx
mov    %rbx,%rdi
call   1090 <sprintf@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
test   %rax,%rax
je     11b8 <get_driver+0x48>
movb   $0x0,(%r14)
add    $0x818,%rsp
pop    %rbx
pop    %r14
ret
mov    0x8(%rsp),%edi
call   1040 <S_ISLNK@plt>
test   %rax,%rax
je     11a9 <get_driver+0x39>
movb   $0x0,0x10(%rsp)
lea    0xe38(%rip),%rsi
lea    0x410(%rsp),%rbx
mov    %rbx,%rdi
call   1080 <strcat@plt>
lea    0x10(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x400,%edx
call   1050 <readlink@plt>
test   %eax,%eax
jle    11a9 <get_driver+0x39>
mov    %eax,%eax
movb   $0x0,0x10(%rsp,%rax,1)
lea    0x10(%rsp),%rdi
call   1070 <basename@plt>
mov    %r14,%rdi
mov    %eax,%esi
call   1030 <strcpy@plt>
jmp    11ad <get_driver+0x3d>

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [10 x i8] c"%s/device\00"
@.str.1 = [8 x i8] c"/driver\00"
define void @get_driver(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca %struct.stat
%6 = alloca [1024 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i32
store i8* %0, i8** %3
store i8* %1, i8** %4
store i32 0, i32* %8
%9 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @sprintf(i8* %9, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %10)
%12 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%13 = call i64 @lstat(i8* %12, %struct.stat* %5)
%14 = icmp eq i64 %13, 0
br i1 %14, label %15, label %40
15: ; preds = %2
%16 = getelementptr inbounds %struct.stat, %struct.stat* %5, i32 0, i32 0
%17 = load i32, i32* %16
%18 = call i64 @S_ISLNK(i32 %17)
%19 = icmp ne i64 %18, 0
br i1 %19, label %20, label %40
20: ; preds = %15
%21 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
store i8 0, i8* %21
%22 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%23 = call i32 @strcat(i8* %22, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%24 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%26 = call i32 @readlink(i8* %24, i8* %25, i32 1024)
store i32 %26, i32* %8
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %36
28: ; preds = %20
%29 = load i32, i32* %8
%30 = sext i32 %29 to i64
%31 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %30
store i8 0, i8* %31
%32 = load i8*, i8** %4
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%34 = call i32 @basename(i8* %33)
%35 = call i32 @strcpy(i8* %32, i32 %34)
br label %39
36: ; preds = %20
%37 = load i8*, i8** %4
%38 = getelementptr inbounds i8, i8* %37, i64 0
store i8 0, i8* %38
br label %39
39: ; preds = %36, %28
br label %43
40: ; preds = %15, %2
%41 = load i8*, i8** %4
%42 = getelementptr inbounds i8, i8* %41, i64 0
store i8 0, i8* %42
br label %43
43: ; preds = %40, %39
ret void
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [10 x i8] c"%s/device\00"
@.str.1 = [8 x i8] c"/driver\00"
define void @get_driver(i8* %0, i8* %1) {
%3 = alloca %struct.stat
%4 = alloca [1024 x i8]
%5 = alloca [1024 x i8]
%6 = bitcast %struct.stat* %3 to i8*
%7 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%9 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = call i64 @lstat(i8* %7, %struct.stat* %3)
%11 = icmp eq i64 %10, 0
br i1 %11, label %12, label %27
12: ; preds = %2
%13 = getelementptr inbounds %struct.stat, %struct.stat* %3, i64 0, i32 0
%14 = load i32, i32* %13
%15 = tail call i64 @S_ISLNK(i32 %14)
%16 = icmp eq i64 %15, 0
br i1 %16, label %27, label %17
17: ; preds = %12
store i8 0, i8* %8
%18 = call i32 @strcat(i8* %7, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%19 = call i32 @readlink(i8* %7, i8* %8, i32 1024)
%20 = icmp sgt i32 %19, 0
br i1 %20, label %21, label %26
21: ; preds = %17
%22 = zext i32 %19 to i64
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 %22
store i8 0, i8* %23
%24 = call i32 @basename(i8* %8)
%25 = call i32 @strcpy(i8* %1, i32 %24)
br label %28
26: ; preds = %17
store i8 0, i8* %1
br label %28
27: ; preds = %12, %2
store i8 0, i8* %1
br label %28
28: ; preds = %21, %26, %27
ret void
}