#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 {
  scalar_t__ *sun_path;
  int /*<<< orphan*/ sun_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ sock_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_UNIX;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOMAXCONN;
scalar_t__ bind(int, struct sockaddr *, int);
scalar_t__ chmod(char *, int);
scalar_t__ chown(char *, int, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fatal_with_reason(int /*<<< orphan*/, char *, ...);
scalar_t__ listen(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strncpy(scalar_t__ *, char *, int);
int socket_create(char *socket_name, int mode, int uid, int gid) {
  struct sockaddr_un sock_addr;
  int daemon_socket;
  daemon_socket = socket(AF_UNIX, SOCK_STREAM, 0);
  if (daemon_socket == -1) {
    fatal_with_reason(errno, "Could not create daemon socket");
  }
  sock_addr.sun_family = AF_UNIX;
  strncpy(sock_addr.sun_path, socket_name, sizeof(sock_addr.sun_path) - 1);
  sock_addr.sun_path[sizeof(sock_addr.sun_path) - 1] = 0;
  if (bind(daemon_socket, (struct sockaddr *)&sock_addr, sizeof(sock_addr)) !=
      0) {
    fatal_with_reason(errno, "Could not bind socket '%s'", socket_name);
  }
  if (chmod(socket_name, 0) != 0) {
    fatal_with_reason(errno, "Could not clear permissions on socket '%s'",
                      socket_name);
  }
  if (chown(socket_name, uid, gid) != 0) {
    fatal_with_reason(errno, "Could not chown socket '%s'", socket_name);
  }
  if (chmod(socket_name, mode) != 0) {
    fatal_with_reason(errno, "Could not set permissions on socket '%s'",
                      socket_name);
  }
  if (listen(daemon_socket, SOMAXCONN) != 0) {
    fatal_with_reason(errno, "Could not set socket to listen.");
  }
  return (daemon_socket);
}


x86 assembly (clang, -O0):
<socket_create>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x8(%rbp)
mov    %esi,-0xc(%rbp)
mov    %edx,-0x10(%rbp)
mov    %ecx,-0x14(%rbp)
mov    0x2e5c(%rip),%rax
mov    (%rax),%edi
mov    0x2e3b(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x2c(%rbp)
cmpl   $0xffffffff,-0x2c(%rbp)
jne    11c2 <socket_create+0x52>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
lea    0xe45(%rip),%rsi
mov    $0x0,%al
call   1080 <fatal_with_reason@plt>
mov    0x2e1f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x28(%rbp),%rdi
mov    -0x8(%rbp),%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>
mov    -0x28(%rbp),%rax
movq   $0x0,0x38(%rax)
mov    -0x2c(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0x10,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     1222 <socket_create+0xb2>
mov    0x2dba(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xe04(%rip),%rsi
mov    $0x0,%al
call   1080 <fatal_with_reason@plt>
mov    -0x8(%rbp),%rdi
xor    %esi,%esi
call   1060 <chmod@plt>
cmp    $0x0,%rax
je     1252 <socket_create+0xe2>
mov    0x2d8a(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdef(%rip),%rsi
mov    $0x0,%al
call   1080 <fatal_with_reason@plt>
mov    -0x8(%rbp),%rdi
mov    -0x10(%rbp),%esi
mov    -0x14(%rbp),%edx
call   1050 <chown@plt>
cmp    $0x0,%rax
je     1286 <socket_create+0x116>
mov    0x2d56(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xde6(%rip),%rsi
mov    $0x0,%al
call   1080 <fatal_with_reason@plt>
mov    -0x8(%rbp),%rdi
mov    -0xc(%rbp),%esi
call   1060 <chmod@plt>
cmp    $0x0,%rax
je     12b7 <socket_create+0x147>
mov    0x2d25(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rdx
lea    0xdd1(%rip),%rsi
mov    $0x0,%al
call   1080 <fatal_with_reason@plt>
mov    -0x2c(%rbp),%edi
mov    0x2d17(%rip),%rax
mov    (%rax),%esi
call   1040 <listen@plt>
cmp    $0x0,%rax
je     12e9 <socket_create+0x179>
mov    0x2cef(%rip),%rax
mov    (%rax),%edi
lea    0xdc8(%rip),%rsi
mov    $0x0,%al
call   1080 <fatal_with_reason@plt>
mov    -0x2c(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<socket_create>:
push   %rbx
mov    %rdi,%rbx
mov    0x2ead(%rip),%rax
mov    (%rax),%edi
mov    0x2e94(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
cmp    $0xffffffff,%eax
jne    1169 <socket_create+0x39>
mov    0x2e77(%rip),%rax
mov    (%rax),%edi
lea    0xe9e(%rip),%rsi
xor    %eax,%eax
call   1040 <fatal_with_reason@plt>
mov    %rbx,%rsi
mov    $0x7,%edx
call   1030 <strncpy@plt>

LLVM IR (-O0):
%struct.sockaddr_un = type { i64*, i32 }
%struct.sockaddr = type { i32 }
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@.str = [31 x i8] c"Could not create daemon socket\00"
@.str.1 = [27 x i8] c"Could not bind socket '%s'\00"
@.str.2 = [43 x i8] c"Could not clear permissions on socket '%s'\00"
@.str.3 = [28 x i8] c"Could not chown socket '%s'\00"
@.str.4 = [41 x i8] c"Could not set permissions on socket '%s'\00"
@SOMAXCONN = global i32 0
@.str.5 = [32 x i8] c"Could not set socket to listen.\00"
define i32 @socket_create(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca %struct.sockaddr_un
%10 = alloca i32
store i8* %0, i8** %5
store i32 %1, i32* %6
store i32 %2, i32* %7
store i32 %3, i32* %8
%11 = load i32, i32* @AF_UNIX
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %10
%14 = load i32, i32* %10
%15 = icmp eq i32 %14, -1
br i1 %15, label %16, label %19
16: ; preds = %4
%17 = load i32, i32* @errno
%18 = call i32 (i32, i8*, ...) @fatal_with_reason(i32 %17, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %19
19: ; preds = %16, %4
%20 = load i32, i32* @AF_UNIX
%21 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%23 = load i64*, i64** %22
%24 = load i8*, i8** %5
%25 = call i32 @strncpy(i64* %23, i8* %24, i32 7)
%26 = getelementptr inbounds %struct.sockaddr_un, %struct.sockaddr_un* %9, i32 0, i32 0
%27 = load i64*, i64** %26
%28 = getelementptr inbounds i64, i64* %27, i64 7
store i64 0, i64* %28
%29 = load i32, i32* %10
%30 = bitcast %struct.sockaddr_un* %9 to %struct.sockaddr*
%31 = call i64 @bind(i32 %29, %struct.sockaddr* %30, i32 16)
%32 = icmp ne i64 %31, 0
br i1 %32, label %33, label %37
33: ; preds = %19
%34 = load i32, i32* @errno
%35 = load i8*, i8** %5
%36 = call i32 (i32, i8*, ...) @fatal_with_reason(i32 %34, i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0), i8* %35)
br label %37
37: ; preds = %33, %19
%38 = load i8*, i8** %5
%39 = call i64 @chmod(i8* %38, i32 0)
%40 = icmp ne i64 %39, 0
br i1 %40, label %41, label %45
41: ; preds = %37
%42 = load i32, i32* @errno
%43 = load i8*, i8** %5
%44 = call i32 (i32, i8*, ...) @fatal_with_reason(i32 %42, i8* getelementptr inbounds ([43 x i8], [43 x i8]* @.str.2, i64 0, i64 0), i8* %43)
br label %45
45: ; preds = %41, %37
%46 = load i8*, i8** %5
%47 = load i32, i32* %7
%48 = load i32, i32* %8
%49 = call i64 @chown(i8* %46, i32 %47, i32 %48)
%50 = icmp ne i64 %49, 0
br i1 %50, label %51, label %55
51: ; preds = %45
%52 = load i32, i32* @errno
%53 = load i8*, i8** %5
%54 = call i32 (i32, i8*, ...) @fatal_with_reason(i32 %52, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.3, i64 0, i64 0), i8* %53)
br label %55
55: ; preds = %51, %45
%56 = load i8*, i8** %5
%57 = load i32, i32* %6
%58 = call i64 @chmod(i8* %56, i32 %57)
%59 = icmp ne i64 %58, 0
br i1 %59, label %60, label %64
60: ; preds = %55
%61 = load i32, i32* @errno
%62 = load i8*, i8** %5
%63 = call i32 (i32, i8*, ...) @fatal_with_reason(i32 %61, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.4, i64 0, i64 0), i8* %62)
br label %64
64: ; preds = %60, %55
%65 = load i32, i32* %10
%66 = load i32, i32* @SOMAXCONN
%67 = call i64 @listen(i32 %65, i32 %66)
%68 = icmp ne i64 %67, 0
br i1 %68, label %69, label %72
69: ; preds = %64
%70 = load i32, i32* @errno
%71 = call i32 (i32, i8*, ...) @fatal_with_reason(i32 %70, i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.5, i64 0, i64 0))
br label %72
72: ; preds = %69, %64
%73 = load i32, i32* %10
ret i32 %73
}

LLVM IR (-O2):
@AF_UNIX = global i32 0
@SOCK_STREAM = global i32 0
@errno = global i32 0
@.str = [31 x i8] c"Could not create daemon socket\00"
@SOMAXCONN = global i32 0
define i32 @socket_create(i8* %0, i32 %1, i32 %2, i32 %3) {
%5 = load i32, i32* @AF_UNIX
%6 = load i32, i32* @SOCK_STREAM
%7 = tail call i32 @socket(i32 %5, i32 %6, i32 0)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %12
9: ; preds = %4
%10 = load i32, i32* @errno
%11 = tail call i32 (i32, i8*, ...) @fatal_with_reason(i32 %10, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %9, %4
%13 = tail call i32 @strncpy(i64* undef, i8* %0, i32 7)
unreachable
}