#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 sockaddr_un {
  int /*<<< orphan*/ sun_path;
  int /*<<< orphan*/ sun_family;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ F_GETFL;
int /*<<< orphan*/ F_SETFL;
long O_NONBLOCK;
int /*<<< orphan*/ PF_LOCAL;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr_un *, int);
long fcntl(int, int /*<<< orphan*/, long);
scalar_t__ is_subarray(char *);
int /*<<< orphan*/ snprintf(char *, int, char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sprintf(char *, char *, ...);
char *strchr(char *, char);
int /*<<< orphan*/ strcpy(int /*<<< orphan*/, char *);
int connect_monitor(char *devname) {
  char path[100];
  int sfd;
  long fl;
  struct sockaddr_un addr;
  int pos;
  char *c;
  pos = sprintf(path, "%s/", "/run/mdadm");
  if (is_subarray(devname)) {
    devname++;
    c = strchr(devname, '/');
    if (!c)
      return -1;
    snprintf(&path[pos], c - devname + 1, "%s", devname);
    pos += c - devname;
  } else
    pos += sprintf(&path[pos], "%s", devname);
  sprintf(&path[pos], ".sock");
  sfd = socket(PF_LOCAL, SOCK_STREAM, 0);
  if (sfd < 0)
    return -1;
  addr.sun_family = PF_LOCAL;
  strcpy(addr.sun_path, path);
  if (connect(sfd, &addr, sizeof(addr)) < 0) {
    close(sfd);
    return -1;
  }
  fl = fcntl(sfd, F_GETFL, 0);
  fl |= O_NONBLOCK;
  fcntl(sfd, F_SETFL, fl);
  return sfd;
}


x86 assembly (clang, -O0):
<connect_monitor>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %rdi,-0x10(%rbp)
lea    -0x80(%rbp),%rdi
lea    0xe56(%rip),%rsi
lea    0xe53(%rip),%rdx
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    %eax,-0x9c(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <is_subarray@plt>
cmp    $0x0,%rax
je     1261 <connect_monitor+0xd1>
mov    -0x10(%rbp),%rax
add    $0x1,%rax
mov    %rax,-0x10(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x2f,%esi
call   1050 <strchr@plt>
mov    %rax,-0xa8(%rbp)
cmpq   $0x0,-0xa8(%rbp)
jne    120c <connect_monitor+0x7c>
movl   $0xffffffff,-0x4(%rbp)
jmp    1392 <connect_monitor+0x202>
movslq -0x9c(%rbp),%rax
lea    -0x80(%rbp),%rdi
add    %rax,%rdi
mov    -0xa8(%rbp),%rax
mov    -0x10(%rbp),%rcx
sub    %rcx,%rax
add    $0x1,%rax
mov    %eax,%esi
mov    -0x10(%rbp),%rcx
lea    0xdd6(%rip),%rdx
call   1060 <snprintf@plt>
mov    -0xa8(%rbp),%rcx
mov    -0x10(%rbp),%rax
sub    %rax,%rcx
movslq -0x9c(%rbp),%rax
add    %rcx,%rax
mov    %eax,-0x9c(%rbp)
jmp    128d <connect_monitor+0xfd>
movslq -0x9c(%rbp),%rax
lea    -0x80(%rbp),%rdi
add    %rax,%rdi
mov    -0x10(%rbp),%rdx
lea    0xd95(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
add    -0x9c(%rbp),%eax
mov    %eax,-0x9c(%rbp)
movslq -0x9c(%rbp),%rax
lea    -0x80(%rbp),%rdi
add    %rax,%rdi
lea    0xd70(%rip),%rsi
mov    $0x0,%al
call   1090 <sprintf@plt>
mov    0x2d10(%rip),%rax
mov    (%rax),%edi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x84(%rbp)
cmpl   $0x0,-0x84(%rbp)
jge    12e1 <connect_monitor+0x151>
movl   $0xffffffff,-0x4(%rbp)
jmp    1392 <connect_monitor+0x202>
mov    0x2cd8(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x94(%rbp)
mov    -0x98(%rbp),%edi
lea    -0x80(%rbp),%rsi
call   1030 <strcpy@plt>
mov    -0x84(%rbp),%edi
lea    -0x98(%rbp),%rsi
mov    $0x8,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1337 <connect_monitor+0x1a7>
mov    -0x84(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1392 <connect_monitor+0x202>
mov    -0x84(%rbp),%edi
mov    0x2c84(%rip),%rax
mov    (%rax),%esi
xor    %eax,%eax
mov    %eax,%edx
call   1040 <fcntl@plt>
mov    %rax,-0x90(%rbp)
mov    0x2c8b(%rip),%rax
mov    (%rax),%rax
or     -0x90(%rbp),%rax
mov    %rax,-0x90(%rbp)
mov    -0x84(%rbp),%edi
mov    0x2c5d(%rip),%rax
mov    (%rax),%esi
mov    -0x90(%rbp),%rdx
call   1040 <fcntl@plt>
mov    -0x84(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_monitor>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x78,%rsp
mov    %rdi,%rbx
lea    0xe4c(%rip),%rsi
lea    0xe49(%rip),%rdx
lea    0x10(%rsp),%rdi
xor    %eax,%eax
call   10a0 <sprintf@plt>
mov    %eax,%r14d
mov    %rbx,%rdi
call   1090 <is_subarray@plt>
test   %rax,%rax
je     1215 <connect_monitor+0x75>
add    $0x1,%rbx
mov    %rbx,%rdi
mov    $0x2f,%esi
call   1060 <strchr@plt>
test   %rax,%rax
je     12c5 <connect_monitor+0x125>
mov    %rax,%rbp
movslq %r14d,%rax
lea    (%rsp,%rax,1),%rdi
add    $0x10,%rdi
sub    %ebx,%ebp
lea    0x1(%rbp),%esi
lea    0xe04(%rip),%rdx
mov    %rbx,%rcx
call   1070 <snprintf@plt>
jmp    1231 <connect_monitor+0x91>
movslq %r14d,%rax
lea    (%rsp,%rax,1),%r15
add    $0x10,%r15
mov    %r15,%rdi
mov    %rbx,%rsi
call   1050 <stpcpy@plt>
mov    %rax,%rbp
sub    %r15d,%ebp
add    %ebp,%r14d
movslq %r14d,%rax
movw   $0x6b,0x14(%rsp,%rax,1)
movl   $0x636f732e,0x10(%rsp,%rax,1)
63
mov    0x2d73(%rip),%r14
mov    (%r14),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    $0xffffffff,%ebp
test   %eax,%eax
js     12d3 <connect_monitor+0x133>
mov    %eax,%ebx
mov    (%r14),%eax
mov    %eax,0xc(%rsp)
lea    0x10(%rsp),%rsi
call   1030 <strcpy@plt>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
call   10b0 <connect@plt>
test   %rax,%rax
js     12cc <connect_monitor+0x12c>
mov    0x2d2f(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
xor    %edx,%edx
call   1040 <fcntl@plt>
mov    0x2d3d(%rip),%rcx
or     (%rcx),%rax
mov    0x2d23(%rip),%rcx
mov    (%rcx),%esi
mov    %ebx,%edi
mov    %rax,%rdx
call   1040 <fcntl@plt>
mov    %ebx,%ebp
jmp    12d3 <connect_monitor+0x133>
mov    $0xffffffff,%ebp
jmp    12d3 <connect_monitor+0x133>
mov    %ebx,%edi
call   1080 <close@plt>
mov    %ebp,%eax
add    $0x78,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_un = type { i32, i32 }
@.str = [4 x i8] c"%s/\00"
@.str.1 = [11 x i8] c"/run/mdadm\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [6 x i8] c".sock\00"
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i64 0
@F_SETFL = global i32 0
define i32 @connect_monitor(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i64
%7 = alloca %struct.sockaddr_un
%8 = alloca i32
%9 = alloca i8*
store i8* %0, i8** %3
%10 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%11 = call i32 (i8*, i8*, ...) @sprintf(i8* %10, i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
store i32 %11, i32* %8
%12 = load i8*, i8** %3
%13 = call i64 @is_subarray(i8* %12)
%14 = icmp ne i64 %13, 0
br i1 %14, label %15, label %45
15: ; preds = %1
%16 = load i8*, i8** %3
%17 = getelementptr inbounds i8, i8* %16, i32 1
store i8* %17, i8** %3
%18 = load i8*, i8** %3
%19 = call i8* @strchr(i8* %18, i8 signext 47)
store i8* %19, i8** %9
%20 = load i8*, i8** %9
%21 = icmp ne i8* %20, null
br i1 %21, label %23, label %22
22: ; preds = %15
store i32 -1, i32* %2
br label %89
23: ; preds = %15
%24 = load i32, i32* %8
%25 = sext i32 %24 to i64
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %25
%27 = load i8*, i8** %9
%28 = load i8*, i8** %3
%29 = ptrtoint i8* %27 to i64
%30 = ptrtoint i8* %28 to i64
%31 = sub i64 %29, %30
%32 = add nsw i64 %31, 1
%33 = trunc i64 %32 to i32
%34 = load i8*, i8** %3
%35 = call i32 @snprintf(i8* %26, i32 %33, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %34)
%36 = load i8*, i8** %9
%37 = load i8*, i8** %3
%38 = ptrtoint i8* %36 to i64
%39 = ptrtoint i8* %37 to i64
%40 = sub i64 %38, %39
%41 = load i32, i32* %8
%42 = sext i32 %41 to i64
%43 = add nsw i64 %42, %40
%44 = trunc i64 %43 to i32
store i32 %44, i32* %8
br label %53
45: ; preds = %1
%46 = load i32, i32* %8
%47 = sext i32 %46 to i64
%48 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %47
%49 = load i8*, i8** %3
%50 = call i32 (i8*, i8*, ...) @sprintf(i8* %48, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %49)
%51 = load i32, i32* %8
%52 = add nsw i32 %51, %50
store i32 %52, i32* %8
br label %53
53: ; preds = %45, %23
%54 = load i32, i32* %8
%55 = sext i32 %54 to i64
%56 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 %55
%57 = call i32 (i8*, i8*, ...) @sprintf(i8* %56, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.3, i64 0, i64 0))
%58 = load i32, i32* @PF_LOCAL
%59 = load i32, i32* @SOCK_STREAM
%60 = call i32 @socket(i32 %58, i32 %59, i32 0)
store i32 %60, i32* %5
%61 = load i32, i32* %5
%62 = icmp slt i32 %61, 0
br i1 %62, label %63, label %64
63: ; preds = %53
store i32 -1, i32* %2
br label %89
64: ; preds = %53
%65 = load i32, i32* @PF_LOCAL
%66 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 1
store i32 %65, i32* %66
%67 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %7, i32 0, i32 0
%68 = load i32, i32* %67
%69 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%70 = call i32 @strcpy(i32 %68, i8* %69)
%71 = load i32, i32* %5
%72 = call i64 @connect(i32 %71, %struct.sockaddr_un* %7, i32 8)
%73 = icmp slt i64 %72, 0
br i1 %73, label %74, label %77
74: ; preds = %64
%75 = load i32, i32* %5
%76 = call i32 @close(i32 %75)
store i32 -1, i32* %2
br label %89
77: ; preds = %64
%78 = load i32, i32* %5
%79 = load i32, i32* @F_GETFL
%80 = call i64 @fcntl(i32 %78, i32 %79, i64 0)
store i64 %80, i64* %6
%81 = load i64, i64* @O_NONBLOCK
%82 = load i64, i64* %6
%83 = or i64 %82, %81
store i64 %83, i64* %6
%84 = load i32, i32* %5
%85 = load i32, i32* @F_SETFL
%86 = load i64, i64* %6
%87 = call i64 @fcntl(i32 %84, i32 %85, i64 %86)
%88 = load i32, i32* %5
store i32 %88, i32* %2
br label %89
89: ; preds = %77, %74, %63, %22
%90 = load i32, i32* %2
ret i32 %90
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i32, i32 }
@.str = [4 x i8] c"%s/\00"
@.str.1 = [11 x i8] c"/run/mdadm\00"
@.str.2 = [3 x i8] c"%s\00"
@.str.3 = [6 x i8] c".sock\00"
@PF_LOCAL = global i32 0
@SOCK_STREAM = global i32 0
@F_GETFL = global i32 0
@O_NONBLOCK = global i64 0
@F_SETFL = global i32 0
define i32 @connect_monitor(i8* %0) {
%2 = alloca [100 x i8]
%3 = alloca %struct.sockaddr_un
%4 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 0
%5 = bitcast %struct.sockaddr_un* %3 to i8*
%6 = call i32 (i8*, i8*, ...) @sprintf(i8* %4, i8* dereferenceable(1) getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.1, i64 0, i64 0))
%7 = tail call i64 @is_subarray(i8* %0)
%8 = icmp eq i64 %7, 0
br i1 %8, label %22, label %9
9: ; preds = %1
%10 = getelementptr inbounds i8, i8* %0, i64 1
%11 = tail call i8* @strchr(i8* %10, i8 signext 47)
%12 = icmp eq i8* %11, null
br i1 %12, label %54, label %13
13: ; preds = %9
%14 = sext i32 %6 to i64
%15 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %14
%16 = ptrtoint i8* %11 to i64
%17 = ptrtoint i8* %10 to i64
%18 = sub i64 %16, %17
%19 = trunc i64 %18 to i32
%20 = add i32 %19, 1
%21 = call i32 @snprintf(i8* %15, i32 %20, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.2, i64 0, i64 0), i8* %10)
br label %30
22: ; preds = %1
%23 = sext i32 %6 to i64
%24 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %23
%25 = call i8* @stpcpy(i8* %24, i8* %0)
%26 = ptrtoint i8* %25 to i64
%27 = ptrtoint i8* %24 to i64
%28 = sub i64 %26, %27
%29 = trunc i64 %28 to i32
br label %30
30: ; preds = %22, %13
%31 = phi i32 [ %29, %22 ], [ %19, %13 ]
%32 = add i32 %6, %31
%33 = sext i32 %32 to i64
%34 = getelementptr inbounds [100 x i8], [100 x i8]* %2, i64 0, i64 %33
%35 = load i32, i32* @PF_LOCAL
%36 = load i32, i32* @SOCK_STREAM
%37 = call i32 @socket(i32 %35, i32 %36, i32 0)
%38 = icmp slt i32 %37, 0
br i1 %38, label %54, label %39
39: ; preds = %30
%40 = load i32, i32* @PF_LOCAL
%41 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %3, i64 0, i32 1
store i32 %40, i32* %41
%42 = call i32 @strcpy(i32 undef, i8* %4)
%43 = call i64 @connect(i32 %37, %struct.sockaddr_un* %3, i32 8)
%44 = icmp slt i64 %43, 0
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @close(i32 %37)
br label %54
47: ; preds = %39
%48 = load i32, i32* @F_GETFL
%49 = call i64 @fcntl(i32 %37, i32 %48, i64 0)
%50 = load i64, i64* @O_NONBLOCK
%51 = or i64 %50, %49
%52 = load i32, i32* @F_SETFL
%53 = call i64 @fcntl(i32 %37, i32 %52, i64 %51)
br label %54
54: ; preds = %30, %9, %47, %45
%55 = phi i32 [ -1, %45 ], [ %37, %47 ], [ -1, %9 ], [ -1, %30 ]
ret i32 %55
}