#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 stat {
  int /*<<< orphan*/ st_uid;
};
struct group {
  int /*<<< orphan*/ gr_gid;
};
typedef int /*<<< orphan*/ mode_t;

/* Variables and functions */
int /*<<< orphan*/ FD_CLOEXEC;
int /*<<< orphan*/ F_SETFD;
int /*<<< orphan*/ LOG_ERR;
int /*<<< orphan*/ chmod(char const *, int /*<<< orphan*/);
scalar_t__ chown(char const *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
struct group *getgrnam(char const *);
int /*<<< orphan*/ mced_log(int /*<<< orphan*/, char *, char const *, ...);
scalar_t__ stat(char const *, struct stat *);
char const *strerror(int /*<<< orphan*/);
int ud_create_socket(char const *);
int open_socket(const char *path, mode_t mode, const char *group) {
  int sock_fd;
  sock_fd = ud_create_socket(path);
  if (sock_fd < 0) {
    mced_log(LOG_ERR, "ERR: can't open socket %s: %s\n", path, strerror(errno));
    return -1;
  }
  fcntl(sock_fd, F_SETFD, FD_CLOEXEC);
  chmod(path, mode);
  if (group) {
    struct group *gr;
    struct stat buf;
    gr = getgrnam(group);
    if (!gr) {
      mced_log(LOG_ERR, "ERR: group %s does not exist\n", group);
      close(sock_fd);
      return -1;
    }
    if (stat(path, &buf) < 0) {
      mced_log(LOG_ERR, "ERR: can't stat %s\n", path);
      close(sock_fd);
      return -1;
    }
    if (chown(path, buf.st_uid, gr->gr_gid) < 0) {
      mced_log(LOG_ERR, "ERR: chown(): %s\n", strerror(errno));
      close(sock_fd);
      return -1;
    }
  }
  return sock_fd;
}


x86 assembly (clang, -O0):
<open_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    %rdx,-0x20(%rbp)
mov    -0x10(%rbp),%rdi
call   1080 <ud_create_socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    11ff <open_socket+0x6f>
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    -0x10(%rbp),%rax
mov    %rax,-0x40(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x44(%rbp),%edi
mov    -0x40(%rbp),%rdx
mov    %rax,%rcx
lea    0xe14(%rip),%rsi
mov    $0x0,%al
call   1030 <mced_log@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <open_socket+0x196>
mov    -0x24(%rbp),%edi
mov    0x2dd7(%rip),%rax
mov    (%rax),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
call   10a0 <chmod@plt>
cmpq   $0x0,-0x20(%rbp)
je     1320 <open_socket+0x190>
mov    -0x20(%rbp),%rdi
call   1070 <getgrnam@plt>
mov    %rax,-0x30(%rbp)
cmpq   $0x0,-0x30(%rbp)
jne    1277 <open_socket+0xe7>
mov    0x2d71(%rip),%rax
mov    (%rax),%edi
mov    -0x20(%rbp),%rdx
lea    0xdc3(%rip),%rsi
mov    $0x0,%al
call   1030 <mced_log@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <open_socket+0x196>
mov    -0x10(%rbp),%rdi
lea    -0x38(%rbp),%rsi
call   1060 <stat@plt>
cmp    $0x0,%rax
jge    12bd <open_socket+0x12d>
mov    0x2d2b(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rdx
lea    0xd9b(%rip),%rsi
mov    $0x0,%al
call   1030 <mced_log@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <open_socket+0x196>
mov    -0x10(%rbp),%rdi
mov    -0x38(%rbp),%esi
mov    -0x30(%rbp),%rax
mov    (%rax),%edx
call   1090 <chown@plt>
cmp    $0x0,%rax
jge    131b <open_socket+0x18b>
mov    0x2ce0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x48(%rbp)
mov    0x2ce4(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
mov    -0x48(%rbp),%edi
mov    %rax,%rdx
lea    0xd51(%rip),%rsi
mov    $0x0,%al
call   1030 <mced_log@plt>
mov    -0x24(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1326 <open_socket+0x196>
jmp    1320 <open_socket+0x190>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<open_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
push   %rax
mov    %rdx,%r14
mov    %esi,%ebp
mov    %rdi,%rbx
call   1080 <ud_create_socket@plt>
test   %eax,%eax
js     1240 <open_socket+0xb0>
mov    %eax,%r15d
mov    0x2e2a(%rip),%rax
mov    (%rax),%esi
mov    0x2e19(%rip),%rax
mov    (%rax),%edx
mov    %r15d,%edi
call   1040 <fcntl@plt>
mov    %rbx,%rdi
mov    %ebp,%esi
call   10a0 <chmod@plt>
test   %r14,%r14
je     12ac <open_socket+0x11c>
mov    %r14,%rdi
call   1070 <getgrnam@plt>
test   %rax,%rax
je     126f <open_socket+0xdf>
mov    %rax,%rbp
mov    %rsp,%rsi
mov    %rbx,%rdi
call   1060 <stat@plt>
test   %rax,%rax
js     1284 <open_socket+0xf4>
mov    (%rsp),%esi
mov    0x0(%rbp),%edx
mov    %rbx,%rdi
call   1090 <chown@plt>
test   %rax,%rax
jns    12ac <open_socket+0x11c>
mov    0x2d9e(%rip),%rax
mov    (%rax),%ebx
mov    0x2da5(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xe18(%rip),%rsi
mov    %ebx,%edi
mov    %rax,%rdx
jmp    1297 <open_socket+0x107>
mov    0x2d79(%rip),%rax
mov    (%rax),%ebp
mov    0x2d80(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xda2(%rip),%rsi
mov    %ebp,%edi
mov    %rbx,%rdx
mov    %rax,%rcx
xor    %eax,%eax
call   1030 <mced_log@plt>
jmp    12a6 <open_socket+0x116>
mov    0x2d4a(%rip),%rax
mov    (%rax),%edi
lea    0xda0(%rip),%rsi
mov    %r14,%rdx
jmp    1297 <open_socket+0x107>
mov    0x2d35(%rip),%rax
mov    (%rax),%edi
lea    0xda9(%rip),%rsi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <mced_log@plt>
mov    %r15d,%edi
call   1050 <close@plt>
mov    $0xffffffff,%r15d
mov    %r15d,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.group = type { i32 }
%struct.stat = type { i32 }
@LOG_ERR = global i32 0
@.str = [31 x i8] c"ERR: can't open socket %s: %s\0A\00"
@errno = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.1 = [30 x i8] c"ERR: group %s does not exist\0A\00"
@.str.2 = [20 x i8] c"ERR: can't stat %s\0A\00"
@.str.3 = [18 x i8] c"ERR: chown(): %s\0A\00"
define i32 @open_socket(i8* %0, i32 %1, i8* %2) {
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i8*
%8 = alloca i32
%9 = alloca %struct.group*
%10 = alloca %struct.stat
store i8* %0, i8** %5
store i32 %1, i32* %6
store i8* %2, i8** %7
%11 = load i8*, i8** %5
%12 = call i32 @ud_create_socket(i8* %11)
store i32 %12, i32* %8
%13 = load i32, i32* %8
%14 = icmp slt i32 %13, 0
br i1 %14, label %15, label %21
15: ; preds = %3
%16 = load i32, i32* @LOG_ERR
%17 = load i8*, i8** %5
%18 = load i32, i32* @errno
%19 = call i8* @strerror(i32 %18)
%20 = call i32 (i32, i8*, i8*, ...) @mced_log(i32 %16, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %17, i8* %19)
store i32 -1, i32* %4
br label %71
21: ; preds = %3
%22 = load i32, i32* %8
%23 = load i32, i32* @F_SETFD
%24 = load i32, i32* @FD_CLOEXEC
%25 = call i32 @fcntl(i32 %22, i32 %23, i32 %24)
%26 = load i8*, i8** %5
%27 = load i32, i32* %6
%28 = call i32 @chmod(i8* %26, i32 %27)
%29 = load i8*, i8** %7
%30 = icmp ne i8* %29, null
br i1 %30, label %31, label %69
31: ; preds = %21
%32 = load i8*, i8** %7
%33 = call %struct.group* @getgrnam(i8* %32)
store %struct.group* %33, %struct.group** %9
%34 = load %struct.group*, %struct.group** %9
%35 = icmp ne %struct.group* %34, null
br i1 %35, label %42, label %36
36: ; preds = %31
%37 = load i32, i32* @LOG_ERR
%38 = load i8*, i8** %7
%39 = call i32 (i32, i8*, i8*, ...) @mced_log(i32 %37, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %38)
%40 = load i32, i32* %8
%41 = call i32 @close(i32 %40)
store i32 -1, i32* %4
br label %71
42: ; preds = %31
%43 = load i8*, i8** %5
%44 = call i64 @stat(i8* %43, %struct.stat* %10)
%45 = icmp slt i64 %44, 0
br i1 %45, label %46, label %52
46: ; preds = %42
%47 = load i32, i32* @LOG_ERR
%48 = load i8*, i8** %5
%49 = call i32 (i32, i8*, i8*, ...) @mced_log(i32 %47, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %48)
%50 = load i32, i32* %8
%51 = call i32 @close(i32 %50)
store i32 -1, i32* %4
br label %71
52: ; preds = %42
%53 = load i8*, i8** %5
%54 = getelementptr inbounds %struct.stat, %struct.stat* %10, i32 0, i32 0
%55 = load i32, i32* %54
%56 = load %struct.group*, %struct.group** %9
%57 = getelementptr inbounds %struct.group, %struct.group* %56, i32 0, i32 0
%58 = load i32, i32* %57
%59 = call i64 @chown(i8* %53, i32 %55, i32 %58)
%60 = icmp slt i64 %59, 0
br i1 %60, label %61, label %68
61: ; preds = %52
%62 = load i32, i32* @LOG_ERR
%63 = load i32, i32* @errno
%64 = call i8* @strerror(i32 %63)
%65 = call i32 (i32, i8*, i8*, ...) @mced_log(i32 %62, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %64)
%66 = load i32, i32* %8
%67 = call i32 @close(i32 %66)
store i32 -1, i32* %4
br label %71
68: ; preds = %52
br label %69
69: ; preds = %68, %21
%70 = load i32, i32* %8
store i32 %70, i32* %4
br label %71
71: ; preds = %69, %61, %46, %36, %15
%72 = load i32, i32* %4
ret i32 %72
}

LLVM IR (-O2):
%struct.stat = type { i32 }
%struct.group = type { i32 }
@LOG_ERR = global i32 0
@.str = [31 x i8] c"ERR: can't open socket %s: %s\0A\00"
@errno = global i32 0
@F_SETFD = global i32 0
@FD_CLOEXEC = global i32 0
@.str.1 = [30 x i8] c"ERR: group %s does not exist\0A\00"
@.str.2 = [20 x i8] c"ERR: can't stat %s\0A\00"
@.str.3 = [18 x i8] c"ERR: chown(): %s\0A\00"
define i32 @open_socket(i8* %0, i32 %1, i8* %2) {
%4 = alloca %struct.stat
%5 = tail call i32 @ud_create_socket(i8* %0)
%6 = icmp slt i32 %5, 0
br i1 %6, label %7, label %12
7: ; preds = %3
%8 = load i32, i32* @LOG_ERR
%9 = load i32, i32* @errno
%10 = tail call i8* @strerror(i32 %9)
%11 = tail call i32 (i32, i8*, i8*, ...) @mced_log(i32 %8, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), i8* %0, i8* %10)
br label %47
12: ; preds = %3
%13 = load i32, i32* @F_SETFD
%14 = load i32, i32* @FD_CLOEXEC
%15 = tail call i32 @fcntl(i32 %5, i32 %13, i32 %14)
%16 = tail call i32 @chmod(i8* %0, i32 %1)
%17 = icmp eq i8* %2, null
br i1 %17, label %47, label %18
18: ; preds = %12
%19 = bitcast %struct.stat* %4 to i8*
%20 = tail call %struct.group* @getgrnam(i8* %2)
%21 = icmp eq %struct.group* %20, null
br i1 %21, label %22, label %26
22: ; preds = %18
%23 = load i32, i32* @LOG_ERR
%24 = tail call i32 (i32, i8*, i8*, ...) @mced_log(i32 %23, i8* getelementptr inbounds ([30 x i8], [30 x i8]* @.str.1, i64 0, i64 0), i8* %2)
%25 = tail call i32 @close(i32 %5)
br label %47
26: ; preds = %18
%27 = call i64 @stat(i8* %0, %struct.stat* %4)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %33
29: ; preds = %26
%30 = load i32, i32* @LOG_ERR
%31 = call i32 (i32, i8*, i8*, ...) @mced_log(i32 %30, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0), i8* %0)
%32 = call i32 @close(i32 %5)
br label %47
33: ; preds = %26
%34 = getelementptr inbounds %struct.stat, %struct.stat* %4, i64 0, i32 0
%35 = load i32, i32* %34
%36 = getelementptr inbounds %struct.group, %struct.group* %20, i64 0, i32 0
%37 = load i32, i32* %36
%38 = call i64 @chown(i8* %0, i32 %35, i32 %37)
%39 = icmp slt i64 %38, 0
br i1 %39, label %40, label %46
40: ; preds = %33
%41 = load i32, i32* @LOG_ERR
%42 = load i32, i32* @errno
%43 = call i8* @strerror(i32 %42)
%44 = call i32 (i32, i8*, i8*, ...) @mced_log(i32 %41, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.3, i64 0, i64 0), i8* %43)
%45 = call i32 @close(i32 %5)
br label %47
46: ; preds = %33
br label %47
47: ; preds = %12, %46, %22, %29, %40, %7
%48 = phi i32 [ -1, %7 ], [ -1, %40 ], [ -1, %29 ], [ -1, %22 ], [ %5, %46 ], [ %5, %12 ]
ret i32 %48
}