#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*/ link;

/* Variables and functions */
int /*<<< orphan*/ free(char *);
scalar_t__ isdigit(char);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, ...);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ unlink(char *);
char *xmalloc(int);
void remove_devices(char *devnm, char *path) {
  char base[40];
  char *path2;
  char link[1024];
  int n;
  int part;
  char *be;
  char *pe;
  if (!path)
    return;
  sprintf(base, "/dev/%s", devnm);
  be = base + strlen(base);
  path2 = xmalloc(strlen(path) + 20);
  strcpy(path2, path);
  pe = path2 + strlen(path2);
  for (part = 0; part < 16; part++) {
    if (part) {
      sprintf(be, "p%d", part);
      if (isdigit(pe[-1]))
        sprintf(pe, "p%d", part);
      else
        sprintf(pe, "%d", part);
    }
    n = readlink(path2, link, sizeof(link));
    if (n > 0 && (int)strlen(base) == n && strncmp(link, base, n) == 0)
      unlink(path2);
  }
  free(path2);
}


x86 assembly (clang, -O0):
<remove_devices>:
push   %rbp
mov    %rsp,%rbp
sub    $0x480,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b3 <remove_devices+0x23>
jmp    1362 <remove_devices+0x1d2>
lea    -0x40(%rbp),%rdi
mov    -0x8(%rbp),%rdx
lea    0xe3e(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
lea    -0x40(%rbp),%rax
mov    %rax,-0x478(%rbp)
lea    -0x40(%rbp),%rdi
call   1090 <strlen@plt>
mov    %eax,%ecx
mov    -0x478(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x460(%rbp)
mov    -0x10(%rbp),%rdi
call   1090 <strlen@plt>
mov    %eax,%edi
add    $0x14,%edi
call   10a0 <xmalloc@plt>
mov    %rax,-0x48(%rbp)
mov    -0x48(%rbp),%rdi
mov    -0x10(%rbp),%rsi
call   1070 <strcpy@plt>
mov    -0x48(%rbp),%rax
mov    %rax,-0x470(%rbp)
mov    -0x48(%rbp),%rdi
call   1090 <strlen@plt>
mov    %eax,%ecx
mov    -0x470(%rbp),%rax
movslq %ecx,%rcx
add    %rcx,%rax
mov    %rax,-0x468(%rbp)
movl   $0x0,-0x458(%rbp)
cmpl   $0x10,-0x458(%rbp)
jge    1359 <remove_devices+0x1c9>
cmpl   $0x0,-0x458(%rbp)
je     12da <remove_devices+0x14a>
mov    -0x460(%rbp),%rdi
mov    -0x458(%rbp),%edx
lea    0xd8f(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
mov    -0x468(%rbp),%rax
movsbl -0x1(%rax),%edi
call   1060 <isdigit@plt>
cmp    $0x0,%rax
je     12ba <remove_devices+0x12a>
mov    -0x468(%rbp),%rdi
mov    -0x458(%rbp),%edx
lea    0xd5a(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
jmp    12d5 <remove_devices+0x145>
mov    -0x468(%rbp),%rdi
mov    -0x458(%rbp),%edx
lea    0xd3b(%rip),%rsi
mov    $0x0,%al
call   10b0 <sprintf@plt>
jmp    12da <remove_devices+0x14a>
mov    -0x48(%rbp),%rdi
lea    -0x450(%rbp),%rsi
mov    $0x400,%edx
call   1080 <readlink@plt>
mov    %eax,-0x454(%rbp)
cmpl   $0x0,-0x454(%rbp)
jle    1340 <remove_devices+0x1b0>
lea    -0x40(%rbp),%rdi
call   1090 <strlen@plt>
cmp    -0x454(%rbp),%eax
jne    1340 <remove_devices+0x1b0>
lea    -0x450(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x454(%rbp),%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    1340 <remove_devices+0x1b0>
mov    -0x48(%rbp),%rdi
call   1040 <unlink@plt>
jmp    1345 <remove_devices+0x1b5>
mov    -0x458(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x458(%rbp)
jmp    124b <remove_devices+0xbb>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_devices>:
test   %rsi,%rsi
je     12ee <remove_devices+0x15e>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    %rsi,%rbp
mov    %rdi,%rdx
lea    0xe49(%rip),%rsi
mov    %rsp,%rbx
mov    %rbx,%rdi
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %rbx,%rdi
call   1090 <strlen@plt>
movslq %eax,%r15
mov    %rbp,%rdi
call   1090 <strlen@plt>
lea    0x14(%rax),%edi
call   10a0 <xmalloc@plt>
mov    %rax,%rbx
mov    %rax,%r14
mov    %rax,%rdi
mov    %rbp,%rsi
call   1070 <strcpy@plt>
mov    %rbx,%rdi
call   1090 <strlen@plt>
movslq %eax,%r12
lea    0x30(%rsp),%rsi
mov    %rbx,%rdi
mov    $0x400,%edx
call   1080 <readlink@plt>
test   %eax,%eax
jle    123b <remove_devices+0xab>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1090 <strlen@plt>
cmp    %ebp,%eax
jne    123b <remove_devices+0xab>
lea    0x30(%rsp),%rdi
mov    %rsp,%rsi
mov    %ebp,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    123b <remove_devices+0xab>
mov    %r14,%rdi
call   1040 <unlink@plt>
add    %rsp,%r15
add    %r12,%rbx
mov    $0x1,%ebp
lea    0xdbb(%rip),%r12
jmp    1258 <remove_devices+0xc8>
nop
add    $0x1,%ebp
cmp    $0x10,%ebp
je     12d5 <remove_devices+0x145>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
xor    %eax,%eax
call   10b0 <sprintf@plt>
movsbl -0x1(%rbx),%edi
call   1060 <isdigit@plt>
test   %rax,%rax
mov    %r12,%rsi
lea    0xd8c(%rip),%rax
cmove  %rax,%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
xor    %eax,%eax
call   10b0 <sprintf@plt>
mov    %r14,%rdi
lea    0x30(%rsp),%rsi
mov    $0x400,%edx
call   1080 <readlink@plt>
test   %eax,%eax
jle    1250 <remove_devices+0xc0>
mov    %eax,%r13d
mov    %rsp,%rdi
call   1090 <strlen@plt>
cmp    %r13d,%eax
jne    1250 <remove_devices+0xc0>
lea    0x30(%rsp),%rdi
mov    %rsp,%rsi
mov    %r13d,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    1250 <remove_devices+0xc0>
mov    %r14,%rdi
call   1040 <unlink@plt>
jmp    1250 <remove_devices+0xc0>
mov    %r14,%rdi
call   1030 <free@plt>
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [8 x i8] c"/dev/%s\00"
@.str.1 = [4 x i8] c"p%d\00"
@.str.2 = [3 x i8] c"%d\00"
define void @remove_devices(i8* %0, i8* %1) {
%3 = alloca i8*
%4 = alloca i8*
%5 = alloca [40 x i8]
%6 = alloca i8*
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i8*
%11 = alloca i8*
store i8* %0, i8** %3
store i8* %1, i8** %4
%12 = load i8*, i8** %4
%13 = icmp ne i8* %12, null
br i1 %13, label %15, label %14
14: ; preds = %2
br label %87
15: ; preds = %2
%16 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%17 = load i8*, i8** %3
%18 = call i32 (i8*, i8*, ...) @sprintf(i8* %16, i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%20 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%21 = call i32 @strlen(i8* %20)
%22 = sext i32 %21 to i64
%23 = getelementptr inbounds i8, i8* %19, i64 %22
store i8* %23, i8** %10
%24 = load i8*, i8** %4
%25 = call i32 @strlen(i8* %24)
%26 = add nsw i32 %25, 20
%27 = call i8* @xmalloc(i32 %26)
store i8* %27, i8** %6
%28 = load i8*, i8** %6
%29 = load i8*, i8** %4
%30 = call i32 @strcpy(i8* %28, i8* %29)
%31 = load i8*, i8** %6
%32 = load i8*, i8** %6
%33 = call i32 @strlen(i8* %32)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds i8, i8* %31, i64 %34
store i8* %35, i8** %11
store i32 0, i32* %9
br label %36
36: ; preds = %81, %15
%37 = load i32, i32* %9
%38 = icmp slt i32 %37, 16
br i1 %38, label %39, label %84
39: ; preds = %36
%40 = load i32, i32* %9
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %60
42: ; preds = %39
%43 = load i8*, i8** %10
%44 = load i32, i32* %9
%45 = call i32 (i8*, i8*, ...) @sprintf(i8* %43, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %44)
%46 = load i8*, i8** %11
%47 = getelementptr inbounds i8, i8* %46, i64 -1
%48 = load i8, i8* %47
%49 = call i64 @isdigit(i8 signext %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %42
%52 = load i8*, i8** %11
%53 = load i32, i32* %9
%54 = call i32 (i8*, i8*, ...) @sprintf(i8* %52, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %53)
br label %59
55: ; preds = %42
%56 = load i8*, i8** %11
%57 = load i32, i32* %9
%58 = call i32 (i8*, i8*, ...) @sprintf(i8* %56, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i32 %57)
br label %59
59: ; preds = %55, %51
br label %60
60: ; preds = %59, %39
%61 = load i8*, i8** %6
%62 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%63 = call i32 @readlink(i8* %61, i8* %62, i32 1024)
store i32 %63, i32* %8
%64 = load i32, i32* %8
%65 = icmp sgt i32 %64, 0
br i1 %65, label %66, label %80
66: ; preds = %60
%67 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%68 = call i32 @strlen(i8* %67)
%69 = load i32, i32* %8
%70 = icmp eq i32 %68, %69
br i1 %70, label %71, label %80
71: ; preds = %66
%72 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%73 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%74 = load i32, i32* %8
%75 = call i64 @strncmp(i8* %72, i8* %73, i32 %74)
%76 = icmp eq i64 %75, 0
br i1 %76, label %77, label %80
77: ; preds = %71
%78 = load i8*, i8** %6
%79 = call i32 @unlink(i8* %78)
br label %80
80: ; preds = %77, %71, %66, %60
br label %81
81: ; preds = %80
%82 = load i32, i32* %9
%83 = add nsw i32 %82, 1
store i32 %83, i32* %9
br label %36
84: ; preds = %36
%85 = load i8*, i8** %6
%86 = call i32 @free(i8* %85)
br label %87
87: ; preds = %84, %14
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"/dev/%s\00"
@.str.1 = [4 x i8] c"p%d\00"
@.str.2 = [3 x i8] c"%d\00"
define void @remove_devices(i8* %0, i8* %1) {
%3 = alloca [40 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = icmp eq i8* %1, null
br i1 %7, label %55, label %8
8: ; preds = %2
%9 = call i32 (i8*, i8*, ...) @sprintf(i8* %5, i8* dereferenceable(1) getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* %0)
%10 = call i32 @strlen(i8* %5)
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds [40 x i8], [40 x i8]* %3, i64 0, i64 %11
%13 = call i32 @strlen(i8* %1)
%14 = add nsw i32 %13, 20
%15 = call i8* @xmalloc(i32 %14)
%16 = call i32 @strcpy(i8* %15, i8* %1)
%17 = call i32 @strlen(i8* %15)
%18 = sext i32 %17 to i64
%19 = getelementptr inbounds i8, i8* %15, i64 %18
%20 = getelementptr inbounds i8, i8* %19, i64 -1
%21 = call i32 @readlink(i8* %15, i8* %6, i32 1024)
%22 = icmp sgt i32 %21, 0
br i1 %22, label %23, label %31
23: ; preds = %8
%24 = call i32 @strlen(i8* %5)
%25 = icmp eq i32 %24, %21
br i1 %25, label %26, label %31
26: ; preds = %23
%27 = call i64 @strncmp(i8* %6, i8* %5, i32 %21)
%28 = icmp eq i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %26
%30 = call i32 @unlink(i8* %15)
br label %31
31: ; preds = %8, %23, %26, %29
br label %32
32: ; preds = %31, %50
%33 = phi i32 [ %51, %50 ], [ 1, %31 ]
%34 = call i32 (i8*, i8*, ...) @sprintf(i8* %12, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %33)
%35 = load i8, i8* %20
%36 = call i64 @isdigit(i8 signext %35)
%37 = icmp eq i64 %36, 0
%38 = select i1 %37, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0)
%39 = call i32 (i8*, i8*, ...) @sprintf(i8* dereferenceable(1) %19, i8* dereferenceable(1) %38, i32 %33)
%40 = call i32 @readlink(i8* %15, i8* %6, i32 1024)
%41 = icmp sgt i32 %40, 0
br i1 %41, label %42, label %50
42: ; preds = %32
%43 = call i32 @strlen(i8* %5)
%44 = icmp eq i32 %43, %40
br i1 %44, label %45, label %50
45: ; preds = %42
%46 = call i64 @strncmp(i8* %6, i8* %5, i32 %40)
%47 = icmp eq i64 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %45
%49 = call i32 @unlink(i8* %15)
br label %50
50: ; preds = %32, %42, %45, %48
%51 = add nuw nsw i32 %33, 1
%52 = icmp eq i32 %51, 16
br i1 %52, label %53, label %32
53: ; preds = %50
%54 = call i32 @free(i8* %15)
br label %55
55: ; preds = %2, %53
ret void
}