#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 uid_t;
struct sockaddr_un {
  char *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int mode_t;
typedef int gid_t;
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
scalar_t__ ENOENT;
int /*<<< orphan*/ PF_UNIX;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ chmod(char *, int);
int /*<<< orphan*/ chown(char *, int, int);
int /*<<< orphan*/ close(int);
scalar_t__ errno;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char const *, char *);
int /*<<< orphan*/ memset(struct sockaddr_un *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *, int, int, int);
int /*<<< orphan*/ snprintf(char *, int, char *, char const *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
char *strerror(scalar_t__);
int unlink(char *);
int create_socket(const char *name, int type, mode_t perm, uid_t uid,
                  gid_t gid) {
  struct sockaddr_un addr;
  int fd, ret;
  fd = socket(PF_UNIX, type, 0);
  if (fd < 0) {
    fprintf(stderr, "Failed to open socket '%s': %s\n", name, strerror(errno));
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sun_family = AF_UNIX;
  snprintf(addr.sun_path, sizeof(addr.sun_path),
           "/dev/socket"
           "/%s",
           name);
  ret = unlink(addr.sun_path);
  if (ret != 0 && errno != ENOENT) {
    fprintf(stderr, "Failed to unlink old socket '%s': %s\n", name,
            strerror(errno));
    goto out_close;
  }
  ret = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (ret) {
    fprintf(stderr, "Failed to bind socket '%s': %s\n", name, strerror(errno));
    goto out_unlink;
  }
  chown(addr.sun_path, uid, gid);
  chmod(addr.sun_path, perm);
  printf("Created socket '%s' with mode '%o', user '%d', group '%d'\n",
         addr.sun_path, perm, uid, gid);
  return fd;
out_unlink:
  unlink(addr.sun_path);
out_close:
  close(fd);
  return -1;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x70,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %edx,-0x18(%rbp)
mov    %ecx,-0x1c(%rbp)
mov    %r8d,-0x20(%rbp)
mov    0x2e28(%rip),%rax
mov    (%rax),%edi
mov    -0x14(%rbp),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    122e <create_socket+0x7e>
mov    0x2de0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dc4(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xde3(%rip),%rsi
call   1080 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1385 <create_socket+0x1d5>
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2d9b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x30(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x8,%esi
lea    0xdc2(%rip),%rdx
call   1050 <snprintf@plt>
mov    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     12d1 <create_socket+0x121>
mov    0x2d48(%rip),%rax
mov    (%rax),%rax
mov    0x2d2e(%rip),%rcx
cmp    (%rcx),%rax
je     12d1 <create_socket+0x121>
mov    0x2d36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x54(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x50(%rbp)
mov    0x2d1a(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x54(%rbp),%edi
mov    -0x50(%rbp),%rdx
mov    %rax,%rcx
lea    0xd68(%rip),%rsi
call   1080 <fprintf@plt>
jmp    1376 <create_socket+0x1c6>
mov    -0x34(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x10,%edx
call   10b0 <bind@plt>
mov    %eax,-0x38(%rbp)
cmpl   $0x0,-0x38(%rbp)
je     132d <create_socket+0x17d>
mov    0x2cda(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x60(%rbp)
mov    0x2cbe(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
mov    -0x64(%rbp),%edi
mov    -0x60(%rbp),%rdx
mov    %rax,%rcx
lea    0xd32(%rip),%rsi
call   1080 <fprintf@plt>
jmp    136d <create_socket+0x1bd>
mov    -0x30(%rbp),%rdi
mov    -0x1c(%rbp),%esi
mov    -0x20(%rbp),%edx
call   1090 <chown@plt>
mov    -0x30(%rbp),%rdi
mov    -0x18(%rbp),%esi
call   10a0 <chmod@plt>
mov    -0x30(%rbp),%rsi
mov    -0x18(%rbp),%edx
mov    -0x1c(%rbp),%ecx
mov    -0x20(%rbp),%r8d
lea    0xd18(%rip),%rdi
call   1040 <printf@plt>
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1385 <create_socket+0x1d5>
mov    -0x30(%rbp),%rdi
call   1030 <unlink@plt>
mov    -0x34(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x70,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x10,%rsp
mov    %r8d,%r15d
mov    %ecx,%r12d
mov    %edx,%ebp
mov    %rdi,%r14
mov    0x2e2a(%rip),%rax
mov    (%rax),%edi
xor    %edx,%edx
call   10d0 <socket@plt>
test   %eax,%eax
js     1286 <create_socket+0xd6>
mov    %eax,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2de9(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    (%rsp),%rdi
lea    0xe18(%rip),%rdx
mov    $0x8,%esi
mov    %r14,%rcx
call   1050 <snprintf@plt>
mov    (%rsp),%rdi
call   1030 <unlink@plt>
test   %eax,%eax
je     1238 <create_socket+0x88>
mov    0x2d9f(%rip),%rax
mov    (%rax),%rdi
mov    0x2d85(%rip),%rax
cmp    (%rax),%rdi
jne    12b4 <create_socket+0x104>
mov    %rsp,%rsi
mov    %ebx,%edi
mov    $0x10,%edx
call   10b0 <bind@plt>
test   %eax,%eax
je     12f1 <create_socket+0x141>
mov    0x2d7a(%rip),%rax
mov    (%rax),%ebp
mov    0x2d69(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xde7(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    (%rsp),%rdi
call   1030 <unlink@plt>
jmp    12d6 <create_socket+0x126>
mov    0x2d43(%rip),%rax
mov    (%rax),%ebx
mov    0x2d32(%rip),%rax
mov    (%rax),%rdi
call   10c0 <strerror@plt>
lea    0xd5b(%rip),%rsi
mov    %ebx,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
jmp    12dd <create_socket+0x12d>
mov    0x2d15(%rip),%rax
mov    (%rax),%ebp
call   10c0 <strerror@plt>
lea    0xd66(%rip),%rsi
mov    %ebp,%edi
mov    %r14,%rdx
mov    %rax,%rcx
call   1080 <fprintf@plt>
mov    %ebx,%edi
call   1070 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    (%rsp),%rdi
mov    %r12d,%esi
mov    %r15d,%edx
call   1090 <chown@plt>
mov    (%rsp),%rdi
mov    %ebp,%esi
call   10a0 <chmod@plt>
mov    (%rsp),%rsi
lea    0xd5f(%rip),%rdi
mov    %ebp,%edx
mov    %r12d,%ecx
mov    %r15d,%r8d
call   1040 <printf@plt>
jmp    12e2 <create_socket+0x132>

LLVM IR (-O0):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Failed to open socket '%s': %s\0A\00"
@errno = global i64 0
@AF_UNIX = global i32 0
@.str.1 = [15 x i8] c"/dev/socket/%s\00"
@ENOENT = global i64 0
@.str.2 = [38 x i8] c"Failed to unlink old socket '%s': %s\0A\00"
@.str.3 = [32 x i8] c"Failed to bind socket '%s': %s\0A\00"
@.str.4 = [59 x i8] c"Created socket '%s' with mode '%o', user '%d', group '%d'\0A\00"
define i32 @create_socket(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca %struct.sockaddr_un
%13 = alloca i32
%14 = alloca i32
store i8* %0, i8** %7
store i32 %1, i32* %8
store i32 %2, i32* %9
store i32 %3, i32* %10
store i32 %4, i32* %11
%15 = load i32, i32* @PF_UNIX
%16 = load i32, i32* %8
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
store i32 %17, i32* %13
%18 = load i32, i32* %13
%19 = icmp slt i32 %18, 0
br i1 %19, label %20, label %26
20: ; preds = %5
%21 = load i32, i32* @stderr
%22 = load i8*, i8** %7
%23 = load i64, i64* @errno
%24 = call i8* @strerror(i64 %23)
%25 = call i32 @fprintf(i32 %21, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %22, i8* %24)
store i32 -1, i32* %6
br label %85
26: ; preds = %5
%27 = call i32 @memset(%struct.sockaddr_un* %12, i32 0, i32 16)
%28 = load i32, i32* @AF_UNIX
%29 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 1
store i32 %28, i32* %29
%30 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
%31 = load i8*, i8** %30
%32 = load i8*, i8** %7
%33 = call i32 @snprintf(i8* %31, i32 8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %32)
%34 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
%35 = load i8*, i8** %34
%36 = call i32 @unlink(i8* %35)
store i32 %36, i32* %14
%37 = load i32, i32* %14
%38 = icmp ne i32 %37, 0
br i1 %38, label %39, label %49
39: ; preds = %26
%40 = load i64, i64* @errno
%41 = load i64, i64* @ENOENT
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %49
43: ; preds = %39
%44 = load i32, i32* @stderr
%45 = load i8*, i8** %7
%46 = load i64, i64* @errno
%47 = call i8* @strerror(i64 %46)
%48 = call i32 @fprintf(i32 %44, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %45, i8* %47)
br label %82
49: ; preds = %39, %26
%50 = load i32, i32* %13
%51 = bitcast %struct.sockaddr_un* %12 to %struct.sockaddr*
%52 = call i32 @bind(i32 %50, %struct.sockaddr* %51, i32 16)
store i32 %52, i32* %14
%53 = load i32, i32* %14
%54 = icmp ne i32 %53, 0
br i1 %54, label %55, label %61
55: ; preds = %49
%56 = load i32, i32* @stderr
%57 = load i8*, i8** %7
%58 = load i64, i64* @errno
%59 = call i8* @strerror(i64 %58)
%60 = call i32 @fprintf(i32 %56, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %57, i8* %59)
br label %78
61: ; preds = %49
%62 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
%63 = load i8*, i8** %62
%64 = load i32, i32* %10
%65 = load i32, i32* %11
%66 = call i32 @chown(i8* %63, i32 %64, i32 %65)
%67 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
%68 = load i8*, i8** %67
%69 = load i32, i32* %9
%70 = call i32 @chmod(i8* %68, i32 %69)
%71 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
%72 = load i8*, i8** %71
%73 = load i32, i32* %9
%74 = load i32, i32* %10
%75 = load i32, i32* %11
%76 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i8* %72, i32 %73, i32 %74, i32 %75)
%77 = load i32, i32* %13
store i32 %77, i32* %6
br label %85
78: ; preds = %55
%79 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %12, i32 0, i32 0
%80 = load i8*, i8** %79
%81 = call i32 @unlink(i8* %80)
br label %82
82: ; preds = %78, %43
%83 = load i32, i32* %13
%84 = call i32 @close(i32 %83)
store i32 -1, i32* %6
br label %85
85: ; preds = %82, %61, %20
%86 = load i32, i32* %6
ret i32 %86
}

LLVM IR (-O2):
%struct.sockaddr_un = type { i8*, i32 }
%struct.sockaddr = type { i32 }
@PF_UNIX = global i32 0
@stderr = global i32 0
@.str = [32 x i8] c"Failed to open socket '%s': %s\0A\00"
@errno = global i64 0
@AF_UNIX = global i32 0
@.str.1 = [15 x i8] c"/dev/socket/%s\00"
@ENOENT = global i64 0
@.str.2 = [38 x i8] c"Failed to unlink old socket '%s': %s\0A\00"
@.str.3 = [32 x i8] c"Failed to bind socket '%s': %s\0A\00"
@.str.4 = [59 x i8] c"Created socket '%s' with mode '%o', user '%d', group '%d'\0A\00"
define i32 @create_socket(i8* %0, i32 %1, i32 %2, i32 %3, i32 %4) {
%6 = alloca %struct.sockaddr_un
%7 = bitcast %struct.sockaddr_un* %6 to i8*
%8 = load i32, i32* @PF_UNIX
%9 = tail call i32 @socket(i32 %8, i32 %1, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %5
%12 = load i32, i32* @stderr
%13 = load i64, i64* @errno
%14 = tail call i8* @strerror(i64 %13)
%15 = tail call i32 @fprintf(i32 %12, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %14)
br label %54
16: ; preds = %5
%17 = call i32 @memset(%struct.sockaddr_un* %6, i32 0, i32 16)
%18 = load i32, i32* @AF_UNIX
%19 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i64 0, i32 1
store i32 %18, i32* %19
%20 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %6, i64 0, i32 0
%21 = load i8*, i8** %20
%22 = call i32 @snprintf(i8* %21, i32 8, i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.1, i64 0, i64 0), i8* %0)
%23 = load i8*, i8** %20
%24 = call i32 @unlink(i8* %23)
%25 = icmp eq i32 %24, 0
br i1 %25, label %34, label %26
26: ; preds = %16
%27 = load i64, i64* @errno
%28 = load i64, i64* @ENOENT
%29 = icmp eq i64 %27, %28
br i1 %29, label %34, label %30
30: ; preds = %26
%31 = load i32, i32* @stderr
%32 = call i8* @strerror(i64 %27)
%33 = call i32 @fprintf(i32 %31, i8* getelementptr inbounds ([38 x i8], [38 x i8]* @.str.2, i64 0, i64 0), i8* %0, i8* %32)
br label %52
34: ; preds = %26, %16
%35 = bitcast %struct.sockaddr_un* %6 to %struct.sockaddr*
%36 = call i32 @bind(i32 %9, %struct.sockaddr* %35, i32 16)
%37 = icmp eq i32 %36, 0
br i1 %37, label %45, label %38
38: ; preds = %34
%39 = load i32, i32* @stderr
%40 = load i64, i64* @errno
%41 = call i8* @strerror(i64 %40)
%42 = call i32 @fprintf(i32 %39, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0), i8* %0, i8* %41)
%43 = load i8*, i8** %20
%44 = call i32 @unlink(i8* %43)
br label %52
45: ; preds = %34
%46 = load i8*, i8** %20
%47 = call i32 @chown(i8* %46, i32 %3, i32 %4)
%48 = load i8*, i8** %20
%49 = call i32 @chmod(i8* %48, i32 %2)
%50 = load i8*, i8** %20
%51 = call i32 @printf(i8* getelementptr inbounds ([59 x i8], [59 x i8]* @.str.4, i64 0, i64 0), i8* %50, i32 %2, i32 %3, i32 %4)
br label %54
52: ; preds = %38, %30
%53 = call i32 @close(i32 %9)
br label %54
54: ; preds = %52, %45, %11
%55 = phi i32 [ -1, %11 ], [ -1, %52 ], [ %9, %45 ]
ret i32 %55
}