#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 addrinfo {
  int /*<<< orphan*/ ai_addrlen;
  int /*<<< orphan*/ ai_addr;
  int /*<<< orphan*/ ai_protocol;
  int /*<<< orphan*/ ai_socktype;
  int /*<<< orphan*/ ai_family;
  struct addrinfo *ai_next;
};
typedef int /*<<< orphan*/ hints;

/* Variables and functions */
int /*<<< orphan*/ AF_LOCAL;
int /*<<< orphan*/ EINVAL;
int O_ACCMODE;
#define O_RDONLY 129
#define O_WRONLY 128
int PATH_MAX;
int /*<<< orphan*/ SHUT_RD;
int /*<<< orphan*/ SHUT_WR;
int /*<<< orphan*/ bzero(struct addrinfo *, int);
int /*<<< orphan*/ close(int);
int connect(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ freeaddrinfo(struct addrinfo *);
int /*<<< orphan*/ gai_strerror(int);
int getaddrinfo(char *, int /*<<< orphan*/ *, struct addrinfo *,
                struct addrinfo **);
int /*<<< orphan*/ *realpath(char const *, char *);
int shutdown(int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ warn(char *, ...);
int udom_open(const char *path, int flags) {
  struct addrinfo hints, *res, *res0;
  char rpath[PATH_MAX];
  int fd = -1;
  int error;
  bzero(&hints, sizeof(hints));
  hints.ai_family = AF_LOCAL;
  if (realpath(path, rpath) == NULL)
    return (-1);
  error = getaddrinfo(rpath, NULL, &hints, &res0);
  if (error) {
    warn("%s", gai_strerror(error));
    errno = EINVAL;
    return (-1);
  }
  for (res = res0; res != NULL; res = res->ai_next) {
    fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
    if (fd < 0) {
      freeaddrinfo(res0);
      return (-1);
    }
    error = connect(fd, res->ai_addr, res->ai_addrlen);
    if (error == 0)
      break;
    else {
      close(fd);
      fd = -1;
    }
  }
  freeaddrinfo(res0);
  if (fd >= 0) {
    switch (flags & O_ACCMODE) {
    case O_RDONLY:
      if (shutdown(fd, SHUT_WR) == -1)
        warn(NULL);
      break;
    case O_WRONLY:
      if (shutdown(fd, SHUT_RD) == -1)
        warn(NULL);
      break;
    default:
      break;
    }
  }
  return (fd);
}


x86 assembly (clang, -O0):
<udom_open>:
push   %rbp
mov    %rsp,%rbp
sub    $0x80,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e2f(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rcx
mov    %rcx,-0x50(%rbp)
mov    %rax,%rdx
add    $0xf,%rdx
and    $0xfffffffffffffff0,%rdx
mov    %rsp,%rcx
sub    %rdx,%rcx
mov    %rcx,-0x70(%rbp)
mov    %rcx,%rsp
mov    %rax,-0x58(%rbp)
movl   $0xffffffff,-0x5c(%rbp)
lea    -0x38(%rbp),%rdi
mov    $0x20,%esi
call   1080 <bzero@plt>
mov    -0x70(%rbp),%rsi
mov    0x2dca(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1070 <realpath@plt>
cmp    $0x0,%rax
jne    1229 <udom_open+0x89>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    13dc <udom_open+0x23c>
mov    -0x70(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
lea    -0x38(%rbp),%rdx
lea    -0x48(%rbp),%rcx
call   10a0 <getaddrinfo@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
je     1288 <udom_open+0xe8>
mov    -0x60(%rbp),%edi
call   1050 <gai_strerror@plt>
mov    %eax,%esi
lea    0xda4(%rip),%rdi
mov    $0x0,%al
call   1030 <warn@plt>
mov    0x2d46(%rip),%rax
mov    (%rax),%ecx
mov    0x2d45(%rip),%rax
mov    %ecx,(%rax)
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    13dc <udom_open+0x23c>
mov    -0x48(%rbp),%rax
mov    %rax,-0x40(%rbp)
cmpq   $0x0,-0x40(%rbp)
je     132a <udom_open+0x18a>
mov    -0x40(%rbp),%rax
mov    0x10(%rax),%edi
mov    -0x40(%rbp),%rax
mov    0xc(%rax),%esi
mov    -0x40(%rbp),%rax
mov    0x8(%rax),%edx
call   10c0 <socket@plt>
mov    %eax,-0x5c(%rbp)
cmpl   $0x0,-0x5c(%rbp)
jge    12de <udom_open+0x13e>
mov    -0x48(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
jmp    13dc <udom_open+0x23c>
mov    -0x5c(%rbp),%edi
mov    -0x40(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x40(%rbp),%rax
mov    (%rax),%edx
call   1090 <connect@plt>
mov    %eax,-0x60(%rbp)
cmpl   $0x0,-0x60(%rbp)
jne    1305 <udom_open+0x165>
jmp    132a <udom_open+0x18a>
mov    -0x5c(%rbp),%edi
call   1060 <close@plt>
movl   $0xffffffff,-0x5c(%rbp)
jmp    1319 <udom_open+0x179>
mov    -0x40(%rbp),%rax
mov    0x18(%rax),%rax
mov    %rax,-0x40(%rbp)
jmp    1290 <udom_open+0xf0>
mov    -0x48(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
cmpl   $0x0,-0x5c(%rbp)
jl     13cf <udom_open+0x22f>
mov    -0x14(%rbp),%eax
mov    0x2ca9(%rip),%rcx
mov    (%rcx),%ecx
and    %ecx,%eax
mov    %eax,-0x74(%rbp)
sub    $0x80,%eax
je     139b <udom_open+0x1fb>
jmp    135e <udom_open+0x1be>
mov    -0x74(%rbp),%eax
sub    $0x81,%eax
jne    13c5 <udom_open+0x225>
jmp    1371 <udom_open+0x1d1>
mov    -0x5c(%rbp),%edi
mov    0x2c5d(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
cmp    $0xffffffff,%eax
jne    1396 <udom_open+0x1f6>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x0,%al
call   1030 <warn@plt>
jmp    13ca <udom_open+0x22a>
mov    -0x5c(%rbp),%edi
mov    0x2c2b(%rip),%rax
mov    (%rax),%esi
call   1040 <shutdown@plt>
cmp    $0xffffffff,%eax
jne    13c0 <udom_open+0x220>
xor    %eax,%eax
mov    %eax,%edi
mov    $0x0,%al
call   1030 <warn@plt>
jmp    13ca <udom_open+0x22a>
jmp    13ca <udom_open+0x22a>
jmp    13cf <udom_open+0x22f>
mov    -0x5c(%rbp),%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x64(%rbp)
mov    -0x50(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<udom_open>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %esi,%r14d
mov    %rdi,%r15
mov    %rsp,%r12
mov    0x2e27(%rip),%rax
mov    (%rax),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
lea    -0x50(%rbp),%rdi
mov    $0x20,%esi
call   1080 <bzero@plt>
mov    0x2ddf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    %r15,%rdi
mov    %rbx,%rsi
call   1070 <realpath@plt>
mov    $0xffffffff,%r15d
test   %rax,%rax
je     12fa <udom_open+0x15a>
lea    -0x50(%rbp),%rdx
lea    -0x30(%rbp),%rcx
mov    %rbx,%rdi
xor    %esi,%esi
call   10a0 <getaddrinfo@plt>
test   %eax,%eax
je     124c <udom_open+0xac>
mov    %eax,%edi
call   1050 <gai_strerror@plt>
lea    0xdd4(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1030 <warn@plt>
mov    0x2d74(%rip),%rax
mov    (%rax),%eax
mov    0x2d73(%rip),%rcx
mov    %eax,(%rcx)
jmp    12fa <udom_open+0x15a>
mov    -0x30(%rbp),%rbx
test   %rbx,%rbx
je     12a1 <udom_open+0x101>
cs nopw 0x0(%rax,%rax,1)
nop
mov    0x10(%rbx),%edi
mov    0x8(%rbx),%edx
mov    0xc(%rbx),%esi
call   10c0 <socket@plt>
test   %eax,%eax
js     1296 <udom_open+0xf6>
mov    %eax,%r13d
mov    (%rbx),%edx
mov    0x4(%rbx),%esi
mov    %eax,%edi
call   1090 <connect@plt>
test   %eax,%eax
je     12aa <udom_open+0x10a>
mov    %r13d,%edi
call   1060 <close@plt>
mov    0x18(%rbx),%rbx
test   %rbx,%rbx
jne    1260 <udom_open+0xc0>
mov    -0x30(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
jmp    12fa <udom_open+0x15a>
xor    %edi,%edi
call   10b0 <freeaddrinfo@plt>
jmp    12fa <udom_open+0x15a>
mov    -0x30(%rbp),%rdi
call   10b0 <freeaddrinfo@plt>
mov    0x2d36(%rip),%rax
and    (%rax),%r14d
cmp    $0x80,%r14d
je     12d8 <udom_open+0x138>
cmp    $0x81,%r14d
jne    12f7 <udom_open+0x157>
mov    0x2d02(%rip),%rax
jmp    12df <udom_open+0x13f>
mov    0x2cf1(%rip),%rax
mov    (%rax),%esi
mov    %r13d,%edi
call   1040 <shutdown@plt>
cmp    $0xffffffff,%eax
jne    12f7 <udom_open+0x157>
xor    %edi,%edi
xor    %eax,%eax
call   1030 <warn@plt>
mov    %r13d,%r15d
mov    %r12,%rsp
mov    %r15d,%eax
lea    -0x28(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@PATH_MAX = global i32 0
@AF_LOCAL = global i32 0
@.str = [3 x i8] c"%s\00"
@EINVAL = global i32 0
@errno = global i32 0
@O_ACCMODE = global i32 0
@SHUT_WR = global i32 0
@SHUT_RD = global i32 0
define i32 @udom_open(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca %struct.addrinfo
%7 = alloca %struct.addrinfo*
%8 = alloca %struct.addrinfo*
%9 = alloca i8*
%10 = alloca i64
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i8* %0, i8** %4
store i32 %1, i32* %5
%14 = load i32, i32* @PATH_MAX
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %9
%17 = alloca i8, i64 %15
store i64 %15, i64* %10
store i32 -1, i32* %11
%18 = call i32 @bzero(%struct.addrinfo* %6, i32 32)
%19 = load i32, i32* @AF_LOCAL
%20 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %6, i32 0, i32 4
store i32 %19, i32* %20
%21 = load i8*, i8** %4
%22 = call i32* @realpath(i8* %21, i8* %17)
%23 = icmp eq i32* %22, null
br i1 %23, label %24, label %25
24: ; preds = %2
store i32 -1, i32* %3
store i32 1, i32* %13
br label %104
25: ; preds = %2
%26 = call i32 @getaddrinfo(i8* %17, i32* null, %struct.addrinfo* %6, %struct.addrinfo** %8)
store i32 %26, i32* %12
%27 = load i32, i32* %12
%28 = icmp ne i32 %27, 0
br i1 %28, label %29, label %34
29: ; preds = %25
%30 = load i32, i32* %12
%31 = call i32 @gai_strerror(i32 %30)
%32 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %31)
%33 = load i32, i32* @EINVAL
store i32 %33, i32* @errno
store i32 -1, i32* %3
store i32 1, i32* %13
br label %104
34: ; preds = %25
%35 = load %struct.addrinfo*, %struct.addrinfo** %8
store %struct.addrinfo* %35, %struct.addrinfo** %7
br label %36
36: ; preds = %71, %34
%37 = load %struct.addrinfo*, %struct.addrinfo** %7
%38 = icmp ne %struct.addrinfo* %37, null
br i1 %38, label %39, label %75
39: ; preds = %36
%40 = load %struct.addrinfo*, %struct.addrinfo** %7
%41 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %40, i32 0, i32 4
%42 = load i32, i32* %41
%43 = load %struct.addrinfo*, %struct.addrinfo** %7
%44 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %43, i32 0, i32 3
%45 = load i32, i32* %44
%46 = load %struct.addrinfo*, %struct.addrinfo** %7
%47 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %46, i32 0, i32 2
%48 = load i32, i32* %47
%49 = call i32 @socket(i32 %42, i32 %45, i32 %48)
store i32 %49, i32* %11
%50 = load i32, i32* %11
%51 = icmp slt i32 %50, 0
br i1 %51, label %52, label %55
52: ; preds = %39
%53 = load %struct.addrinfo*, %struct.addrinfo** %8
%54 = call i32 @freeaddrinfo(%struct.addrinfo* %53)
store i32 -1, i32* %3
store i32 1, i32* %13
br label %104
55: ; preds = %39
%56 = load i32, i32* %11
%57 = load %struct.addrinfo*, %struct.addrinfo** %7
%58 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %57, i32 0, i32 1
%59 = load i32, i32* %58
%60 = load %struct.addrinfo*, %struct.addrinfo** %7
%61 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %60, i32 0, i32 0
%62 = load i32, i32* %61
%63 = call i32 @connect(i32 %56, i32 %59, i32 %62)
store i32 %63, i32* %12
%64 = load i32, i32* %12
%65 = icmp eq i32 %64, 0
br i1 %65, label %66, label %67
66: ; preds = %55
br label %75
67: ; preds = %55
%68 = load i32, i32* %11
%69 = call i32 @close(i32 %68)
store i32 -1, i32* %11
br label %70
70: ; preds = %67
br label %71
71: ; preds = %70
%72 = load %struct.addrinfo*, %struct.addrinfo** %7
%73 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %72, i32 0, i32 5
%74 = load %struct.addrinfo*, %struct.addrinfo** %73
store %struct.addrinfo* %74, %struct.addrinfo** %7
br label %36
75: ; preds = %66, %36
%76 = load %struct.addrinfo*, %struct.addrinfo** %8
%77 = call i32 @freeaddrinfo(%struct.addrinfo* %76)
%78 = load i32, i32* %11
%79 = icmp sge i32 %78, 0
br i1 %79, label %80, label %102
80: ; preds = %75
%81 = load i32, i32* %5
%82 = load i32, i32* @O_ACCMODE
%83 = and i32 %81, %82
switch i32 %83, label %100 [
i32 129, label %84
i32 128, label %92
]
84: ; preds = %80
%85 = load i32, i32* %11
%86 = load i32, i32* @SHUT_WR
%87 = call i32 @shutdown(i32 %85, i32 %86)
%88 = icmp eq i32 %87, -1
br i1 %88, label %89, label %91
89: ; preds = %84
%90 = call i32 (i8*, ...) @warn(i8* null)
br label %91
91: ; preds = %89, %84
br label %101
92: ; preds = %80
%93 = load i32, i32* %11
%94 = load i32, i32* @SHUT_RD
%95 = call i32 @shutdown(i32 %93, i32 %94)
%96 = icmp eq i32 %95, -1
br i1 %96, label %97, label %99
97: ; preds = %92
%98 = call i32 (i8*, ...) @warn(i8* null)
br label %99
99: ; preds = %97, %92
br label %101
100: ; preds = %80
br label %101
101: ; preds = %100, %99, %91
br label %102
102: ; preds = %101, %75
%103 = load i32, i32* %11
store i32 %103, i32* %3
store i32 1, i32* %13
br label %104
104: ; preds = %102, %52, %29, %24
%105 = load i8*, i8** %9
%106 = load i32, i32* %3
ret i32 %106
}

LLVM IR (-O2):
%struct.addrinfo = type { i32, i32, i32, i32, i32, %struct.addrinfo* }
@PATH_MAX = global i32 0
@AF_LOCAL = global i32 0
@.str = [3 x i8] c"%s\00"
@EINVAL = global i32 0
@errno = global i32 0
@O_ACCMODE = global i32 0
@SHUT_WR = global i32 0
@SHUT_RD = global i32 0
define i32 @udom_open(i8* nocapture %0, i32 %1) {
%3 = alloca %struct.addrinfo
%4 = alloca %struct.addrinfo*
%5 = bitcast %struct.addrinfo* %3 to i8*
%6 = bitcast %struct.addrinfo** %4 to i8*
%7 = load i32, i32* @PATH_MAX
%8 = zext i32 %7 to i64
%9 = tail call i8* @llvm.stacksave()
%10 = alloca i8, i64 %8
%11 = call i32 @bzero(%struct.addrinfo* %3, i32 32)
%12 = load i32, i32* @AF_LOCAL
%13 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %3, i64 0, i32 4
store i32 %12, i32* %13
%14 = call i32* @realpath(i8* %0, i8* %10)
%15 = icmp eq i32* %14, null
br i1 %15, label %73, label %16
16: ; preds = %2
%17 = call i32 @getaddrinfo(i8* %10, i32* null, %struct.addrinfo* %3, %struct.addrinfo** %4)
%18 = icmp eq i32 %17, 0
br i1 %18, label %19, label %22
19: ; preds = %16
%20 = load %struct.addrinfo*, %struct.addrinfo** %4
%21 = icmp eq %struct.addrinfo* %20, null
br i1 %21, label %53, label %26
22: ; preds = %16
%23 = call i32 @gai_strerror(i32 %17)
%24 = call i32 (i8*, ...) @warn(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i32 %23)
%25 = load i32, i32* @EINVAL
store i32 %25, i32* @errno
br label %73
26: ; preds = %19, %46
%27 = phi %struct.addrinfo* [ %49, %46 ], [ %20, %19 ]
%28 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 4
%29 = load i32, i32* %28
%30 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 3
%31 = load i32, i32* %30
%32 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 2
%33 = load i32, i32* %32
%34 = call i32 @socket(i32 %29, i32 %31, i32 %33)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %26
%37 = load %struct.addrinfo*, %struct.addrinfo** %4
%38 = call i32 @freeaddrinfo(%struct.addrinfo* %37)
br label %73
39: ; preds = %26
%40 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 1
%41 = load i32, i32* %40
%42 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 0
%43 = load i32, i32* %42
%44 = call i32 @connect(i32 %34, i32 %41, i32 %43)
%45 = icmp eq i32 %44, 0
br i1 %45, label %56, label %46
46: ; preds = %39
%47 = call i32 @close(i32 %34)
%48 = getelementptr inbounds %struct.addrinfo, %struct.addrinfo* %27, i64 0, i32 5
%49 = load %struct.addrinfo*, %struct.addrinfo** %48
%50 = icmp eq %struct.addrinfo* %49, null
br i1 %50, label %51, label %26
51: ; preds = %46
%52 = load %struct.addrinfo*, %struct.addrinfo** %4
br label %53
53: ; preds = %51, %19
%54 = phi %struct.addrinfo* [ %52, %51 ], [ null, %19 ]
%55 = call i32 @freeaddrinfo(%struct.addrinfo* %54)
br label %73
56: ; preds = %39
%57 = load %struct.addrinfo*, %struct.addrinfo** %4
%58 = call i32 @freeaddrinfo(%struct.addrinfo* %57)
%59 = load i32, i32* @O_ACCMODE
%60 = and i32 %59, %1
switch i32 %60, label %73 [
i32 129, label %61
i32 128, label %67
]
61: ; preds = %56
%62 = load i32, i32* @SHUT_WR
%63 = call i32 @shutdown(i32 %34, i32 %62)
%64 = icmp eq i32 %63, -1
br i1 %64, label %65, label %73
65: ; preds = %61
%66 = call i32 (i8*, ...) @warn(i8* null)
br label %73
67: ; preds = %56
%68 = load i32, i32* @SHUT_RD
%69 = call i32 @shutdown(i32 %34, i32 %68)
%70 = icmp eq i32 %69, -1
br i1 %70, label %71, label %73
71: ; preds = %67
%72 = call i32 (i8*, ...) @warn(i8* null)
br label %73
73: ; preds = %53, %56, %67, %71, %61, %65, %2, %36, %22
%74 = phi i32 [ -1, %22 ], [ -1, %36 ], [ -1, %2 ], [ %34, %65 ], [ %34, %61 ], [ %34, %71 ], [ %34, %67 ], [ %34, %56 ], [ -1, %53 ]
ret i32 %74
}