#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 strlen(char *);
int /*<<< orphan*/ strncat(char *, char *, int);
int /*<<< orphan*/ strncpy(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);
  (strncpy(filename, path, sizeof(filename)),
   (filename)[(sizeof(filename)) - 1] = '\0');
  strncat(filename, "/uevent", (sizeof(filename)) - strlen(filename) - 1);
  if (stat(filename, &statbuf) < 0)
    return -1;
  if (!(statbuf.st_mode & S_IWUSR))
    return -1;
  (strncpy(devpath, &path[4], sizeof(devpath)),
   (devpath)[(sizeof(devpath)) - 1] = '\0');
  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    1194 <device_list_insert+0x14>
lea    -0x210(%rbp),%rdi
mov    -0x10(%rbp),%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x11(%rbp)
lea    -0x210(%rbp),%rax
mov    %rax,-0x420(%rbp)
lea    -0x210(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x420(%rbp),%rdi
movslq %eax,%rcx
mov    $0x200,%eax
sub    %rcx,%rax
sub    $0x1,%rax
mov    %eax,%edx
lea    0xe1a(%rip),%rsi
call   1060 <strncat@plt>
lea    -0x210(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1090 <stat@plt>
cmp    $0x0,%rax
jge    1214 <device_list_insert+0x94>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <device_list_insert+0x152>
mov    -0x418(%rbp),%eax
mov    0x2dc7(%rip),%rcx
and    (%rcx),%eax
cmp    $0x0,%eax
jne    1238 <device_list_insert+0xb8>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <device_list_insert+0x152>
lea    -0x410(%rbp),%rdi
mov    -0x10(%rbp),%rsi
add    $0x4,%rsi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,-0x211(%rbp)
mov    -0x10(%rbp),%rdi
lea    -0x418(%rbp),%rsi
call   1080 <lstat@plt>
cmp    $0x0,%rax
jge    127e <device_list_insert+0xfe>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <device_list_insert+0x152>
mov    -0x418(%rbp),%edi
call   1040 <S_ISLNK@plt>
cmp    $0x0,%rax
je     12bf <device_list_insert+0x13f>
lea    -0x410(%rbp),%rdi
mov    $0x200,%esi
call   10a0 <sysfs_resolve_link@plt>
cmp    $0x0,%rax
je     12ba <device_list_insert+0x13a>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d2 <device_list_insert+0x152>
jmp    12bf <device_list_insert+0x13f>
lea    -0x410(%rbp),%rdi
call   1070 <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   1030 <strncpy@plt>
movb   $0x0,0x40f(%rsp)
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    $0x1ff,%edx
sub    %eax,%edx
lea    0xe3e(%rip),%rsi
mov    %rbp,%rdi
call   1060 <strncat@plt>
lea    0x8(%rsp),%rsi
mov    %rbp,%rdi
call   1090 <stat@plt>
mov    $0xffffffff,%ebp
test   %rax,%rax
js     124b <device_list_insert+0xcb>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
test   %eax,0x8(%rsp)
je     124b <device_list_insert+0xcb>
lea    0x4(%rbx),%rsi
lea    0x10(%rsp),%rdi
mov    $0x200,%edx
call   1030 <strncpy@plt>
movb   $0x0,0x20f(%rsp)
lea    0x8(%rsp),%rsi
mov    %rbx,%rdi
call   1080 <lstat@plt>
test   %rax,%rax
js     124b <device_list_insert+0xcb>
mov    0x8(%rsp),%edi
call   1040 <S_ISLNK@plt>
test   %rax,%rax
je     123f <device_list_insert+0xbf>
lea    0x10(%rsp),%rdi
mov    $0x200,%esi
call   10a0 <sysfs_resolve_link@plt>
test   %rax,%rax
jne    124b <device_list_insert+0xcb>
lea    0x10(%rsp),%rdi
call   1070 <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 @strncpy(i8* %9, i8* %10, i32 512)
%12 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 511
store i8 0, i8* %12
%13 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%14 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%15 = call i32 @strlen(i8* %14)
%16 = sext i32 %15 to i64
%17 = sub i64 512, %16
%18 = sub i64 %17, 1
%19 = trunc i64 %18 to i32
%20 = call i32 @strncat(i8* %13, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %19)
%21 = getelementptr inbounds [512 x i8], [512 x i8]* %4, i64 0, i64 0
%22 = call i64 @stat(i8* %21, %struct.stat* %6)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %25
24: ; preds = %8
store i32 -1, i32* %2
br label %56
25: ; preds = %8
%26 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%27 = load i32, i32* %26
%28 = load i32, i32* @S_IWUSR
%29 = and i32 %27, %28
%30 = icmp ne i32 %29, 0
br i1 %30, label %32, label %31
31: ; preds = %25
store i32 -1, i32* %2
br label %56
32: ; preds = %25
%33 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%34 = load i8*, i8** %3
%35 = getelementptr inbounds i8, i8* %34, i64 4
%36 = call i32 @strncpy(i8* %33, i8* %35, i32 512)
%37 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 511
store i8 0, i8* %37
%38 = load i8*, i8** %3
%39 = call i64 @lstat(i8* %38, %struct.stat* %6)
%40 = icmp slt i64 %39, 0
br i1 %40, label %41, label %42
41: ; preds = %32
store i32 -1, i32* %2
br label %56
42: ; preds = %32
%43 = getelementptr inbounds %struct.stat, %struct.stat* %6, i32 0, i32 0
%44 = load i32, i32* %43
%45 = call i64 @S_ISLNK(i32 %44)
%46 = icmp ne i64 %45, 0
br i1 %46, label %47, label %53
47: ; preds = %42
%48 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%49 = call i64 @sysfs_resolve_link(i8* %48, i32 512)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %52
51: ; preds = %47
store i32 -1, i32* %2
br label %56
52: ; preds = %47
br label %53
53: ; preds = %52, %42
%54 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
%55 = call i32 @trigger_uevent(i8* %54)
store i32 0, i32* %2
br label %56
56: ; preds = %53, %51, %41, %31, %24
%57 = load i32, i32* %2
ret i32 %57
}

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 @strncpy(i8* %5, i8* %0, i32 512)
%9 = getelementptr inbounds [512 x i8], [512 x i8]* %2, i64 0, i64 511
store i8 0, i8* %9
%10 = call i32 @strlen(i8* %5)
%11 = sub i32 511, %10
%12 = call i32 @strncat(i8* %5, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i32 %11)
%13 = call i64 @stat(i8* %5, %struct.stat* %4)
%14 = icmp slt i64 %13, 0
br i1 %14, label %36, label %15
15: ; preds = %1
%16 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%17 = load i32, i32* %16
%18 = load i32, i32* @S_IWUSR
%19 = and i32 %18, %17
%20 = icmp eq i32 %19, 0
br i1 %20, label %36, label %21
21: ; preds = %15
%22 = getelementptr inbounds i8, i8* %0, i64 4
%23 = call i32 @strncpy(i8* %6, i8* %22, i32 512)
%24 = getelementptr inbounds [512 x i8], [512 x i8]* %3, i64 0, i64 511
store i8 0, i8* %24
%25 = call i64 @lstat(i8* %0, %struct.stat* %4)
%26 = icmp slt i64 %25, 0
br i1 %26, label %36, label %27
27: ; preds = %21
%28 = load i32, i32* %16
%29 = call i64 @S_ISLNK(i32 %28)
%30 = icmp eq i64 %29, 0
br i1 %30, label %34, label %31
31: ; preds = %27
%32 = call i64 @sysfs_resolve_link(i8* %6, i32 512)
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %31, %27
%35 = call i32 @trigger_uevent(i8* %6)
br label %36
36: ; preds = %31, %21, %15, %1, %34
%37 = phi i32 [ 0, %34 ], [ -1, %1 ], [ -1, %15 ], [ -1, %21 ], [ -1, %31 ]
ret i32 %37
}