#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 */
typedef int /*<<< orphan*/ path;

/* Variables and functions */
int PATH_MAX;
int /*<<< orphan*/ clearenv();
scalar_t__ readuevent(char *);
int /*<<< orphan*/ setenv(char *, char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int strlen(char *);
int craftev(char *sysfspath) {
  char path[PATH_MAX];
  char *devpath;
  devpath = sysfspath + strlen("/sys");
  snprintf(path, sizeof(path), "/sys%s/uevent", devpath);
  clearenv();
  setenv("ACTION", "add", 1);
  setenv("DEVPATH", devpath, 1);
  if (readuevent(path) < 0)
    return -1;
  return 0;
}


x86 assembly (clang, -O0):
<craftev>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    0x2e8d(%rip),%rax
mov    (%rax),%eax
mov    %rax,-0x40(%rbp)
mov    %rsp,%rcx
mov    %rcx,-0x18(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x38(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x20(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x48(%rbp)
lea    0xe65(%rip),%rdi
call   1050 <strlen@plt>
mov    -0x48(%rbp),%rcx
mov    -0x38(%rbp),%rdi
mov    %eax,%edx
mov    -0x40(%rbp),%rax
movslq %edx,%rdx
add    %rdx,%rcx
mov    %rcx,-0x28(%rbp)
mov    %eax,%esi
mov    -0x28(%rbp),%rcx
lea    0xe40(%rip),%rdx
call   1060 <snprintf@plt>
mov    $0x0,%al
call   1040 <clearenv@plt>
lea    0xe3b(%rip),%rdi
lea    0xe3b(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    -0x28(%rbp),%rsi
lea    0xe2a(%rip),%rdi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    -0x38(%rbp),%rdi
call   1070 <readuevent@plt>
cmp    $0x0,%rax
jge    1224 <craftev+0xd4>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    1232 <craftev+0xe2>
movl   $0x0,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x18(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<craftev>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rdi,%r15
mov    0x2e8b(%rip),%rax
mov    (%rax),%r12d
mov    %rsp,%r14
mov    %r12,%rax
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%r14
mov    %r14,%rsp
lea    0xe7d(%rip),%rdi
call   1050 <strlen@plt>
movslq %eax,%rbx
add    %r15,%rbx
lea    0xe70(%rip),%rdx
mov    %r14,%rdi
mov    %r12d,%esi
mov    %rbx,%rcx
call   1060 <snprintf@plt>
xor    %eax,%eax
call   1040 <clearenv@plt>
lea    0xe62(%rip),%rdi
lea    0xe62(%rip),%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
lea    0xe55(%rip),%rdi
mov    %rbx,%rsi
mov    $0x1,%edx
call   1030 <setenv@plt>
mov    %r14,%rdi
call   1070 <readuevent@plt>
sar    $0x3f,%rax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"/sys\00"
@.str.1 = [14 x i8] c"/sys%s/uevent\00"
@.str.2 = [7 x i8] c"ACTION\00"
@.str.3 = [4 x i8] c"add\00"
@.str.4 = [8 x i8] c"DEVPATH\00"
define i32 @craftev(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca i64
%6 = alloca i8*
%7 = alloca i32
store i8* %0, i8** %3
%8 = load i32, i32* @PATH_MAX
%9 = zext i32 %8 to i64
%10 = call i8* @llvm.stacksave()
store i8* %10, i8** %4
%11 = alloca i8, i64 %9
store i64 %9, i64* %5
%12 = load i8*, i8** %3
%13 = call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%14 = sext i32 %13 to i64
%15 = getelementptr inbounds i8, i8* %12, i64 %14
store i8* %15, i8** %6
%16 = trunc i64 %9 to i32
%17 = load i8*, i8** %6
%18 = call i32 @snprintf(i8* %11, i32 %16, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %17)
%19 = call i32 (...) @clearenv()
%20 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 1)
%21 = load i8*, i8** %6
%22 = call i32 @setenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %21, i32 1)
%23 = call i64 @readuevent(i8* %11)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %26
25: ; preds = %1
store i32 -1, i32* %2
store i32 1, i32* %7
br label %27
26: ; preds = %1
store i32 0, i32* %2
store i32 1, i32* %7
br label %27
27: ; preds = %26, %25
%28 = load i8*, i8** %4
%29 = load i32, i32* %2
ret i32 %29
}

LLVM IR (-O2):
@PATH_MAX = global i32 0
@.str = [5 x i8] c"/sys\00"
@.str.1 = [14 x i8] c"/sys%s/uevent\00"
@.str.2 = [7 x i8] c"ACTION\00"
@.str.3 = [4 x i8] c"add\00"
@.str.4 = [8 x i8] c"DEVPATH\00"
define i32 @craftev(i8* %0) {
%2 = load i32, i32* @PATH_MAX
%3 = zext i32 %2 to i64
%4 = alloca i8, i64 %3
%5 = tail call i32 @strlen(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
%6 = sext i32 %5 to i64
%7 = getelementptr inbounds i8, i8* %0, i64 %6
%8 = call i32 @snprintf(i8* %4, i32 %2, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i8* %7)
%9 = call i32 (...) @clearenv()
%10 = call i32 @setenv(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i32 1)
%11 = call i32 @setenv(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.4, i64 0, i64 0), i8* %7, i32 1)
%12 = call i64 @readuevent(i8* %4)
%13 = ashr i64 %12, 63
%14 = trunc i64 %13 to i32
ret i32 %14
}