#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);
char *malloc(int);
int readlink(char *, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int /*<<< orphan*/ strcpy(char *, char *);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int /*<<< orphan*/ unlink(char *);
void remove_devices(int devnum, char *path) {
  char base[40];
  char *path2;
  char link[1024];
  int n;
  int part;
  char *be;
  char *pe;
  if (!path)
    return;
  if (devnum >= 0)
    sprintf(base, "/dev/md%d", devnum);
  else
    sprintf(base, "/dev/md_d%d", -1 - devnum);
  be = base + strlen(base);
  path2 = malloc(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    %edi,-0x4(%rbp)
mov    %rsi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jne    11b2 <remove_devices+0x22>
jmp    137f <remove_devices+0x1ef>
cmpl   $0x0,-0x4(%rbp)
jl     11d4 <remove_devices+0x44>
lea    -0x40(%rbp),%rdi
mov    -0x4(%rbp),%edx
lea    0xe36(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    11ec <remove_devices+0x5c>
lea    -0x40(%rbp),%rdi
mov    $0xffffffff,%edx
sub    -0x4(%rbp),%edx
lea    0xe23(%rip),%rsi
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 <malloc@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    1376 <remove_devices+0x1e6>
cmpl   $0x0,-0x458(%rbp)
je     12f7 <remove_devices+0x167>
mov    -0x460(%rbp),%rdi
mov    -0x458(%rbp),%edx
lea    0xd7a(%rip),%rsi
call   10b0 <sprintf@plt>
mov    -0x468(%rbp),%rax
movsbl -0x1(%rax),%edi
call   1060 <isdigit@plt>
cmp    $0x0,%rax
je     12d9 <remove_devices+0x149>
mov    -0x468(%rbp),%rdi
mov    -0x458(%rbp),%edx
lea    0xd47(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    12f2 <remove_devices+0x162>
mov    -0x468(%rbp),%rdi
mov    -0x458(%rbp),%edx
lea    0xd26(%rip),%rsi
call   10b0 <sprintf@plt>
jmp    12f7 <remove_devices+0x167>
mov    -0x48(%rbp),%rdi
lea    -0x450(%rbp),%rsi
mov    $0x400,%edx
call   1080 <readlink@plt>
mov    %eax,-0x454(%rbp)
cmpl   $0x0,-0x454(%rbp)
jle    135d <remove_devices+0x1cd>
lea    -0x40(%rbp),%rdi
call   1090 <strlen@plt>
cmp    -0x454(%rbp),%eax
jne    135d <remove_devices+0x1cd>
lea    -0x450(%rbp),%rdi
lea    -0x40(%rbp),%rsi
mov    -0x454(%rbp),%edx
call   1050 <strncmp@plt>
cmp    $0x0,%rax
jne    135d <remove_devices+0x1cd>
mov    -0x48(%rbp),%rdi
call   1040 <unlink@plt>
jmp    1362 <remove_devices+0x1d2>
mov    -0x458(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x458(%rbp)
jmp    126e <remove_devices+0xde>
mov    -0x48(%rbp),%rdi
call   1030 <free@plt>
add    $0x480,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<remove_devices>:
test   %rsi,%rsi
je     12f7 <remove_devices+0x167>
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    %rsi,%rbp
mov    %edi,%edx
test   %edi,%edi
js     11bc <remove_devices+0x2c>
lea    0xe46(%rip),%rsi
jmp    11c5 <remove_devices+0x35>
not    %edx
lea    0xe45(%rip),%rsi
mov    %rsp,%rdi
call   10b0 <sprintf@plt>
mov    %rsp,%rdi
call   1090 <strlen@plt>
movslq %eax,%r15
mov    %rbp,%rdi
call   1090 <strlen@plt>
lea    0x14(%rax),%edi
call   10a0 <malloc@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    1244 <remove_devices+0xb4>
mov    %eax,%ebp
mov    %rsp,%rdi
call   1090 <strlen@plt>
cmp    %ebp,%eax
jne    1244 <remove_devices+0xb4>
lea    0x30(%rsp),%rdi
mov    %rsp,%rsi
mov    %ebp,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    1244 <remove_devices+0xb4>
mov    %r14,%rdi
call   1040 <unlink@plt>
add    %rsp,%r15
add    %r12,%rbx
mov    $0x1,%ebp
lea    0xdc0(%rip),%r12
jmp    1268 <remove_devices+0xd8>
nopl   0x0(%rax,%rax,1)
add    $0x1,%ebp
cmp    $0x10,%ebp
je     12de <remove_devices+0x14e>
mov    %r15,%rdi
mov    %r12,%rsi
mov    %ebp,%edx
call   10b0 <sprintf@plt>
movsbl -0x1(%rbx),%edi
call   1060 <isdigit@plt>
test   %rax,%rax
mov    %r12,%rsi
lea    0xd88(%rip),%rax
cmove  %rax,%rsi
mov    %rbx,%rdi
mov    %ebp,%edx
call   10b0 <sprintf@plt>
mov    %r14,%rdi
lea    0x30(%rsp),%rsi
mov    $0x400,%edx
call   1080 <readlink@plt>
test   %eax,%eax
jle    1260 <remove_devices+0xd0>
mov    %eax,%r13d
mov    %rsp,%rdi
call   1090 <strlen@plt>
cmp    %r13d,%eax
jne    1260 <remove_devices+0xd0>
lea    0x30(%rsp),%rdi
mov    %rsp,%rsi
mov    %r13d,%edx
call   1050 <strncmp@plt>
test   %rax,%rax
jne    1260 <remove_devices+0xd0>
mov    %r14,%rdi
call   1040 <unlink@plt>
jmp    1260 <remove_devices+0xd0>
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 = [10 x i8] c"/dev/md%d\00"
@.str.1 = [12 x i8] c"/dev/md_d%d\00"
@.str.2 = [4 x i8] c"p%d\00"
@.str.3 = [3 x i8] c"%d\00"
define void @remove_devices(i32 %0, i8* %1) {
%3 = alloca i32
%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 i32 %0, i32* %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 %96
15: ; preds = %2
%16 = load i32, i32* %3
%17 = icmp sge i32 %16, 0
br i1 %17, label %18, label %22
18: ; preds = %15
%19 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%20 = load i32, i32* %3
%21 = call i32 @sprintf(i8* %19, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %20)
br label %27
22: ; preds = %15
%23 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%24 = load i32, i32* %3
%25 = sub nsw i32 -1, %24
%26 = call i32 @sprintf(i8* %23, i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.1, i64 0, i64 0), i32 %25)
br label %27
27: ; preds = %22, %18
%28 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%29 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%30 = call i32 @strlen(i8* %29)
%31 = sext i32 %30 to i64
%32 = getelementptr inbounds i8, i8* %28, i64 %31
store i8* %32, i8** %10
%33 = load i8*, i8** %4
%34 = call i32 @strlen(i8* %33)
%35 = add nsw i32 %34, 20
%36 = call i8* @malloc(i32 %35)
store i8* %36, i8** %6
%37 = load i8*, i8** %6
%38 = load i8*, i8** %4
%39 = call i32 @strcpy(i8* %37, i8* %38)
%40 = load i8*, i8** %6
%41 = load i8*, i8** %6
%42 = call i32 @strlen(i8* %41)
%43 = sext i32 %42 to i64
%44 = getelementptr inbounds i8, i8* %40, i64 %43
store i8* %44, i8** %11
store i32 0, i32* %9
br label %45
45: ; preds = %90, %27
%46 = load i32, i32* %9
%47 = icmp slt i32 %46, 16
br i1 %47, label %48, label %93
48: ; preds = %45
%49 = load i32, i32* %9
%50 = icmp ne i32 %49, 0
br i1 %50, label %51, label %69
51: ; preds = %48
%52 = load i8*, i8** %10
%53 = load i32, i32* %9
%54 = call i32 @sprintf(i8* %52, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %53)
%55 = load i8*, i8** %11
%56 = getelementptr inbounds i8, i8* %55, i64 -1
%57 = load i8, i8* %56
%58 = call i64 @isdigit(i8 signext %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %51
%61 = load i8*, i8** %11
%62 = load i32, i32* %9
%63 = call i32 @sprintf(i8* %61, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.2, i64 0, i64 0), i32 %62)
br label %68
64: ; preds = %51
%65 = load i8*, i8** %11
%66 = load i32, i32* %9
%67 = call i32 @sprintf(i8* %65, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i32 %66)
br label %68
68: ; preds = %64, %60
br label %69
69: ; preds = %68, %48
%70 = load i8*, i8** %6
%71 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%72 = call i32 @readlink(i8* %70, i8* %71, i32 1024)
store i32 %72, i32* %8
%73 = load i32, i32* %8
%74 = icmp sgt i32 %73, 0
br i1 %74, label %75, label %89
75: ; preds = %69
%76 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%77 = call i32 @strlen(i8* %76)
%78 = load i32, i32* %8
%79 = icmp eq i32 %77, %78
br i1 %79, label %80, label %89
80: ; preds = %75
%81 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%82 = getelementptr inbounds [40 x i8], [40 x i8]* %5, i64 0, i64 0
%83 = load i32, i32* %8
%84 = call i64 @strncmp(i8* %81, i8* %82, i32 %83)
%85 = icmp eq i64 %84, 0
br i1 %85, label %86, label %89
86: ; preds = %80
%87 = load i8*, i8** %6
%88 = call i32 @unlink(i8* %87)
br label %89
89: ; preds = %86, %80, %75, %69
br label %90
90: ; preds = %89
%91 = load i32, i32* %9
%92 = add nsw i32 %91, 1
store i32 %92, i32* %9
br label %45
93: ; preds = %45
%94 = load i8*, i8** %6
%95 = call i32 @free(i8* %94)
br label %96
96: ; preds = %93, %14
ret void
}

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