#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct sockaddr_in {
  int dummy;
};
struct sockaddr {
  int dummy;
};
struct TYPE_2__ {
  int fd;
};
struct epoll_event {
  int events;
  TYPE_1__ data;
};

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EAGAIN;
scalar_t__ EINPROGRESS;
int EPOLLERR;
int EPOLLET;
int EPOLLIN;
int EPOLLOUT;
int EPOLLRDHUP;
int /*<<< orphan*/ EPOLL_CTL_ADD;
int /*<<< orphan*/ F_SETFL;
int /*<<< orphan*/ O_NONBLOCK;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_REUSEADDR;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
scalar_t__ epoll_ctl(int, int /*<<< orphan*/, int, struct epoll_event *);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
scalar_t__ fcntl(int, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_and_connect(struct sockaddr_in target, int *epfd) {
  int yes = 1;
  int sock;
  static struct epoll_event Edgvent;
  if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
    perror("socket");
    exit(1);
  }
  if ((setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)) ||
       fcntl(sock, F_SETFL, O_NONBLOCK)) == -1) {
    perror("setsockopt || fcntl");
    exit(1);
  }
  if (connect(sock, (struct sockaddr *)&target, sizeof(struct sockaddr)) ==
          -1 &&
      errno != EINPROGRESS) {
    if (errno == EAGAIN) {
      perror("connect is EAGAIN");
      close(sock);
      exit(1);
    }
  } else {
    Edgvent.events = EPOLLOUT | EPOLLIN | EPOLLRDHUP | EPOLLERR | EPOLLET;
    Edgvent.data.fd = sock;
    if (epoll_ctl((int)epfd, EPOLL_CTL_ADD, sock, &Edgvent) != 0) {
      perror("epoll_ctl, adding socket\n");
      exit(1);
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<create_and_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x1,-0x14(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2dfa(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    11d1 <create_and_connect+0x51>
lea    0xe3e(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
mov    0x2e0d(%rip),%rax
mov    (%rax),%esi
mov    0x2d94(%rip),%rax
mov    (%rax),%edx
lea    -0x14(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %rax,%rcx
mov    $0x1,%al
cmp    $0x0,%rcx
mov    %al,-0x19(%rbp)
jne    122b <create_and_connect+0xab>
mov    -0x18(%rbp),%edi
mov    0x2d97(%rip),%rax
mov    (%rax),%esi
mov    0x2dbe(%rip),%rax
mov    (%rax),%edx
call   1040 <fcntl@plt>
cmp    $0x0,%rax
setne  %al
mov    %al,-0x19(%rbp)
mov    -0x19(%rbp),%al
and    $0x1,%al
movzbl %al,%eax
cmp    $0xffffffff,%eax
jne    1252 <create_and_connect+0xd2>
lea    0xdc4(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x18(%rbp),%edi
lea    -0x8(%rbp),%rsi
mov    $0x4,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    12c3 <create_and_connect+0x143>
mov    0x2d1d(%rip),%rax
mov    (%rax),%rax
mov    0x2d53(%rip),%rcx
cmp    (%rcx),%rax
je     12c3 <create_and_connect+0x143>
mov    0x2d03(%rip),%rax
mov    (%rax),%rax
mov    0x2cd1(%rip),%rcx
cmp    (%rcx),%rax
jne    12be <create_and_connect+0x13e>
lea    0xd74(%rip),%rdi
call   1070 <perror@plt>
mov    -0x18(%rbp),%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1342 <create_and_connect+0x1c2>
mov    0x2cce(%rip),%rax
mov    (%rax),%eax
mov    0x2cf5(%rip),%rcx
or     (%rcx),%eax
mov    0x2cac(%rip),%rcx
or     (%rcx),%eax
mov    0x2ccb(%rip),%rcx
or     (%rcx),%eax
mov    0x2c92(%rip),%rcx
or     (%rcx),%eax
mov    %eax,0x2d72(%rip)
mov    -0x18(%rbp),%eax
mov    %eax,0x2d6d(%rip)
mov    -0x10(%rbp),%rax
mov    %eax,%edi
mov    0x2cb4(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%edx
lea    0x2d50(%rip),%rcx
call   1060 <epoll_ctl@plt>
cmp    $0x0,%rax
je     133d <create_and_connect+0x1bd>
lea    0xcff(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
jmp    1342 <create_and_connect+0x1c2>
xor    %eax,%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_and_connect>:
push   %rbp
push   %rbx
push   %rax
mov    %rsi,%rbx
mov    %edi,(%rsp)
movl   $0x1,0x4(%rsp)
mov    0x2e48(%rip),%rax
mov    (%rax),%edi
mov    0x2dff(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     12a9 <create_and_connect+0x129>
mov    %eax,%ebp
mov    0x2e2d(%rip),%rax
mov    (%rax),%esi
mov    0x2db4(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebp,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
jne    11f6 <create_and_connect+0x76>
mov    0x2dc4(%rip),%rax
mov    (%rax),%esi
mov    0x2deb(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
call   1040 <fcntl@plt>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    1220 <create_and_connect+0xa0>
mov    0x2d7f(%rip),%rax
mov    (%rax),%rax
mov    0x2db5(%rip),%rcx
cmp    (%rcx),%rax
jne    1280 <create_and_connect+0x100>
mov    0x2d71(%rip),%rax
mov    0x2d9a(%rip),%rcx
mov    (%rcx),%ecx
or     (%rax),%ecx
mov    0x2d4f(%rip),%rax
or     (%rax),%ecx
mov    0x2d6e(%rip),%rax
or     (%rax),%ecx
mov    0x2d35(%rip),%rax
or     (%rax),%ecx
mov    %ecx,0x2e15(%rip)
lea    0x2e0e(%rip),%rcx
mov    %ebp,0x2e0c(%rip)
mov    0x2d59(%rip),%rax
mov    (%rax),%esi
mov    %ebx,%edi
mov    %ebp,%edx
call   1060 <epoll_ctl@plt>
test   %rax,%rax
jne    12b2 <create_and_connect+0x132>
xor    %eax,%eax
add    $0x8,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2ce1(%rip),%rcx
cmp    (%rcx),%rax
jne    1277 <create_and_connect+0xf7>
lea    0xd74(%rip),%rdi
call   1070 <perror@plt>
mov    %ebp,%edi
call   1050 <close@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xd50(%rip),%rdi
jmp    12b9 <create_and_connect+0x139>
lea    0xd60(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@create_and_connect.Edgvent = internal global %struct.epoll_event zeroinitializer
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@.str.1 = [20 x i8] c"setsockopt || fcntl\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@EAGAIN = global i64 0
@.str.2 = [18 x i8] c"connect is EAGAIN\00"
@EPOLLOUT = global i32 0
@EPOLLIN = global i32 0
@EPOLLRDHUP = global i32 0
@EPOLLERR = global i32 0
@EPOLLET = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.3 = [26 x i8] c"epoll_ctl, adding socket\0A\00"
define i32 @create_and_connect(i32 %0, i32* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32*
%5 = alloca i32
%6 = alloca i32
%7 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i32 0, i32 0
store i32 %0, i32* %7
store i32* %1, i32** %4
store i32 1, i32* %5
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_STREAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %6
%11 = icmp slt i32 %10, 0
br i1 %11, label %12, label %15
12: ; preds = %2
%13 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%14 = call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = load i32, i32* %6
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_REUSEADDR
%19 = call i64 @setsockopt(i32 %16, i32 %17, i32 %18, i32* %5, i32 4)
%20 = icmp ne i64 %19, 0
br i1 %20, label %27, label %21
21: ; preds = %15
%22 = load i32, i32* %6
%23 = load i32, i32* @F_SETFL
%24 = load i32, i32* @O_NONBLOCK
%25 = call i64 @fcntl(i32 %22, i32 %23, i32 %24)
%26 = icmp ne i64 %25, 0
br label %27
27: ; preds = %21, %15
%28 = phi i1 [ true, %15 ], [ %26, %21 ]
%29 = zext i1 %28 to i32
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %34
31: ; preds = %27
%32 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 1)
unreachable
34: ; preds = %27
%35 = load i32, i32* %6
%36 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%37 = call i32 @connect(i32 %35, %struct.sockaddr* %36, i32 4)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %53
39: ; preds = %34
%40 = load i64, i64* @errno
%41 = load i64, i64* @EINPROGRESS
%42 = icmp ne i64 %40, %41
br i1 %42, label %43, label %53
43: ; preds = %39
%44 = load i64, i64* @errno
%45 = load i64, i64* @EAGAIN
%46 = icmp eq i64 %44, %45
br i1 %46, label %47, label %52
47: ; preds = %43
%48 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%49 = load i32, i32* %6
%50 = call i32 @close(i32 %49)
%51 = call i32 @exit(i32 1)
unreachable
52: ; preds = %43
br label %74
53: ; preds = %39, %34
%54 = load i32, i32* @EPOLLOUT
%55 = load i32, i32* @EPOLLIN
%56 = or i32 %54, %55
%57 = load i32, i32* @EPOLLRDHUP
%58 = or i32 %56, %57
%59 = load i32, i32* @EPOLLERR
%60 = or i32 %58, %59
%61 = load i32, i32* @EPOLLET
%62 = or i32 %60, %61
store i32 %62, i32* getelementptr inbounds (%struct.epoll_event, %struct.epoll_event* @create_and_connect.Edgvent, i32 0, i32 0)
%63 = load i32, i32* %6
store i32 %63, i32* getelementptr inbounds (%struct.epoll_event, %struct.epoll_event* @create_and_connect.Edgvent, i32 0, i32 1, i32 0)
%64 = load i32*, i32** %4
%65 = ptrtoint i32* %64 to i32
%66 = load i32, i32* @EPOLL_CTL_ADD
%67 = load i32, i32* %6
%68 = call i64 @epoll_ctl(i32 %65, i32 %66, i32 %67, %struct.epoll_event* @create_and_connect.Edgvent)
%69 = icmp ne i64 %68, 0
br i1 %69, label %70, label %73
70: ; preds = %53
%71 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%72 = call i32 @exit(i32 1)
unreachable
73: ; preds = %53
br label %74
74: ; preds = %73, %52
ret i32 0
}

LLVM IR (-O2):
%struct.epoll_event = type { i32, %struct.TYPE_2__ }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@create_and_connect.Edgvent = internal global %struct.epoll_event zeroinitializer
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_REUSEADDR = global i32 0
@F_SETFL = global i32 0
@O_NONBLOCK = global i32 0
@errno = global i64 0
@EINPROGRESS = global i64 0
@EAGAIN = global i64 0
@.str.2 = [18 x i8] c"connect is EAGAIN\00"
@EPOLLOUT = global i32 0
@EPOLLIN = global i32 0
@EPOLLRDHUP = global i32 0
@EPOLLERR = global i32 0
@EPOLLET = global i32 0
@EPOLL_CTL_ADD = global i32 0
@.str.3 = [26 x i8] c"epoll_ctl, adding socket\0A\00"
define i32 @create_and_connect(i32 %0, i32* %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %0, i32* %5
%6 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %14
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %2
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_REUSEADDR
%17 = call i64 @setsockopt(i32 %9, i32 %15, i32 %16, i32* %4, i32 4)
%18 = icmp eq i64 %17, 0
br i1 %18, label %19, label %23
19: ; preds = %14
%20 = load i32, i32* @F_SETFL
%21 = load i32, i32* @O_NONBLOCK
%22 = call i64 @fcntl(i32 %9, i32 %20, i32 %21)
br label %23
23: ; preds = %14, %19
%24 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%25 = call i32 @connect(i32 %9, %struct.sockaddr* %24, i32 4)
%26 = icmp eq i32 %25, -1
br i1 %26, label %27, label %38
27: ; preds = %23
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINPROGRESS
%30 = icmp eq i64 %28, %29
br i1 %30, label %38, label %31
31: ; preds = %27
%32 = load i64, i64* @EAGAIN
%33 = icmp eq i64 %28, %32
br i1 %33, label %34, label %56
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.2, i64 0, i64 0))
%36 = call i32 @close(i32 %9)
%37 = call i32 @exit(i32 1)
unreachable
38: ; preds = %27, %23
%39 = load i32, i32* @EPOLLOUT
%40 = load i32, i32* @EPOLLIN
%41 = or i32 %40, %39
%42 = load i32, i32* @EPOLLRDHUP
%43 = or i32 %41, %42
%44 = load i32, i32* @EPOLLERR
%45 = or i32 %43, %44
%46 = load i32, i32* @EPOLLET
%47 = or i32 %45, %46
store i32 %47, i32* getelementptr inbounds (%struct.epoll_event, %struct.epoll_event* @create_and_connect.Edgvent, i64 0, i32 0)
store i32 %9, i32* getelementptr inbounds (%struct.epoll_event, %struct.epoll_event* @create_and_connect.Edgvent, i64 0, i32 1, i32 0)
%48 = ptrtoint i32* %1 to i64
%49 = trunc i64 %48 to i32
%50 = load i32, i32* @EPOLL_CTL_ADD
%51 = call i64 @epoll_ctl(i32 %49, i32 %50, i32 %9, %struct.epoll_event* @create_and_connect.Edgvent)
%52 = icmp eq i64 %51, 0
br i1 %52, label %56, label %53
53: ; preds = %38
%54 = call i32 @perror(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.3, i64 0, i64 0))
%55 = call i32 @exit(i32 1)
unreachable
56: ; preds = %38, %31
ret i32 0
}