#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 st_mode;
};
typedef int /*<<< orphan*/ filename;
typedef int /*<<< orphan*/ devpath;

/* Variables and functions */
scalar_t__ S_ISLNK(int);
int S_IWUSR;
scalar_t__ lstat(char const *, struct stat *);
scalar_t__ stat(char *, struct stat *);
int /*<<< orphan*/ strlcat(char *, char *, int);
int /*<<< orphan*/ strlcpy(char *, char const *, int);
scalar_t__ sysfs_resolve_link(char *, int);
int /*<<< orphan*/ trigger_uevent(char *);
int device_list_insert(const char *path) {
  char filename[512];
  char devpath[512];
  struct stat statbuf;
  do {
  } while (0);
  strlcpy(filename, path, sizeof(filename));
  strlcat(filename, "/uevent", sizeof(filename));
  if (stat(filename, &statbuf) < 0)
    return -1;
  if (!(statbuf.st_mode & S_IWUSR))
    return -1;
  strlcpy(devpath, &path[4], sizeof(devpath));
  if (lstat(path, &statbuf) < 0)
    return -1;
  if (S_ISLNK(statbuf.st_mode))
    if (sysfs_resolve_link(devpath, sizeof(devpath)) != 0)
      return -1;
  trigger_uevent(devpath);
  return 0;
}


x86 assembly (clang, -O0):
<device_list_insert>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %rdi,-0x10(%rbp)
jmp    1184 <device_list_insert+0x14>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x200,%edx
call   1080 <strlcpy@plt>
lea    -0x210(%rbp),%rdi
lea    0xe59(%rip),%rsi
mov    $0x200,%edx
call   1040 <strlcat@plt>
lea    -0x210(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1070 <stat@plt>
cmp    $0x0,%rax
jge    11da <device_list_insert+0x6a>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <device_list_insert+0x121>
mov    -0x418(%rbp),%eax
mov    0x2e01(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    11fe <device_list_insert+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <device_list_insert+0x121>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    $0x200,%edx
call   1080 <strlcpy@plt>
mov    -0x10(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1060 <lstat@plt>
cmp    $0x0,%rax
jge    123d <device_list_insert+0xcd>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <device_list_insert+0x121>
mov    -0x418(%rbp),%edi
call   1030 <S_ISLNK@plt>
cmp    $0x0,%rax
je     127e <device_list_insert+0x10e>
lea    -0x410(%rbp),%rdi
mov    $0x200,%esi
call   1090 <sysfs_resolve_link@plt>
cmp    $0x0,%rax
je     1279 <device_list_insert+0x109>
movl   $0xffffffff,-0x4(%rbp)
jmp    1291 <device_list_insert+0x121>
jmp    127e <device_list_insert+0x10e>
lea    -0x410(%rbp),%rdi
call   1050 <trigger_uevent@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<device_list_insert>:
push   %rbp
push   %rbx
sub    $0x418,%rsp
mov    %rdi,%rbx
lea    0x210(%rsp),%rbp
mov    %rbp,%rdi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1080 <strlcpy@plt>
lea    0xe65(%rip),%rsi
mov    %rbp,%rdi
mov    $0x200,%edx
call   1040 <strlcat@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1070 <stat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     1221 <device_list_insert+0xb1>
mov    0x2e22(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%rsp)
je     1221 <device_list_insert+0xb1>
lea    0x4(%rbx),%rsi
lea    0x10(%rsp),%rdi
mov    $0x200,%edx
call   1080 <strlcpy@plt>
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1060 <lstat@plt>
test   %rax,%rax
js     1221 <device_list_insert+0xb1>
mov    0x8(%rsp),%edi
call   1030 <S_ISLNK@plt>
test   %rax,%rax
je     1215 <device_list_insert+0xa5>
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
call   1090 <sysfs_resolve_link@plt>
test   %rax,%rax
jne    1221 <device_list_insert+0xb1>
lea    0x10(%rsp),%rdi
call   1050 <trigger_uevent@plt>
xor    %ebp,%ebp
mov    %ebp,%eax
add    $0x418,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.stat = type { i32 }
@.str = [8 x i8] c"/uevent\00"
@S_IWUSR = global i32 0
define i32 @device_list_insert(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [512 x i8]
%5 = alloca [512 x i8]
%6 = alloca %struct.stat
store i8* %0, i8** %3
br label %7
7: ; preds = %1
br label %8
8: ; preds = %7
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%10 = load i8*, i8** %3
%11 = call i32 @strlcpy(i8* %9, i8* %10, i32 512)
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%13 = call i32 @strlcat(i8* %12, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 512)
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%15 = call i64 @stat(i8* %14, %struct.stat* %6)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %18
17: ; preds = %8
store i32 -1, i32* %2
br label %48
18: ; preds = %8
%19 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%20 = load i32, i32* %19
%21 = load i32, i32* @S_IWUSR
%22 = and i32 %20, %21
%23 = icmp ne i32 %22, 0
br i1 %23, label %25, label %24
24: ; preds = %18
store i32 -1, i32* %2
br label %48
25: ; preds = %18
%26 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%27 = load i8*, i8** %3
%28 = getelementptr inbounds i8, i8* %27, i64 4
%29 = call i32 @strlcpy(i8* %26, i8* %28, i32 512)
%30 = load i8*, i8** %3
%31 = call i64 @lstat(i8* %30, %struct.stat* %6)
%32 = icmp slt i64 %31, 0
br i1 %32, label %33, label %34
33: ; preds = %25
store i32 -1, i32* %2
br label %48
34: ; preds = %25
%35 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%36 = load i32, i32* %35
%37 = call i64 @S_ISLNK(i32 %36)
%38 = icmp ne i64 %37, 0
br i1 %38, label %39, label %45
39: ; preds = %34
%40 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%41 = call i64 @sysfs_resolve_link(i8* %40, i32 512)
%42 = icmp ne i64 %41, 0
br i1 %42, label %43, label %44
43: ; preds = %39
store i32 -1, i32* %2
br label %48
44: ; preds = %39
br label %45
45: ; preds = %44, %34
%46 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%47 = call i32 @trigger_uevent(i8* %46)
store i32 0, i32* %2
br label %48
48: ; preds = %45, %43, %33, %24, %17
%49 = load i32, i32* %2
ret i32 %49
}

LLVM IR (-O2):
%struct.stat = type { i32 }
@.str = [8 x i8] c"/uevent\00"
@S_IWUSR = global i32 0
define i32 @device_list_insert(i8* %0) {
%2 = alloca [512 x i8]
%3 = alloca [512 x i8]
%4 = alloca %struct.stat
%5 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 0
%6 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 0
%7 = bitcast %struct.stat* %4 to i8*
%8 = call i32 @strlcpy(i8* %5, i8* %0, i32 512)
%9 = call i32 @strlcat(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 512)
%10 = call i64 @stat(i8* %5, %struct.stat* %4)
%11 = icmp slt i64 %10, 0
br i1 %11, label %32, label %12
12: ; preds = %1
%13 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%14 = load i32, i32* %13
%15 = load i32, i32* @S_IWUSR
%16 = and i32 %15, %14
%17 = icmp eq i32 %16, 0
br i1 %17, label %32, label %18
18: ; preds = %12
%19 = getelementptr inbounds i8, i8* %0, i64 4
%20 = call i32 @strlcpy(i8* %6, i8* %19, i32 512)
%21 = call i64 @lstat(i8* %0, %struct.stat* %4)
%22 = icmp slt i64 %21, 0
br i1 %22, label %32, label %23
23: ; preds = %18
%24 = load i32, i32* %13
%25 = call i64 @S_ISLNK(i32 %24)
%26 = icmp eq i64 %25, 0
br i1 %26, label %30, label %27
27: ; preds = %23
%28 = call i64 @sysfs_resolve_link(i8* %6, i32 512)
%29 = icmp eq i64 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %27, %23
%31 = call i32 @trigger_uevent(i8* %6)
br label %32
32: ; preds = %27, %18, %12, %1, %30
%33 = phi i32 [ 0, %30 ], [ -1, %1 ], [ -1, %12 ], [ -1, %18 ], [ -1, %27 ]
ret i32 %33
}