#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_in {
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ ia;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
scalar_t__ EINPROGRESS;
int /*<<< orphan*/ SOCK_STREAM;
scalar_t__ connect(int, struct sockaddr *, int);
scalar_t__ errno;
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int connect_to_socket(bool blocking) {
  int s;
  struct sockaddr_in ia;
  s = socket(AF_INET, SOCK_STREAM, 0);
  memset(&ia, 0, sizeof(ia));
  ia.sin_family = AF_INET;
  inet_pton(AF_INET, "127.0.0.1", &ia.sin_addr);
  ia.sin_port = htons(12345);
  if (!blocking) {
  }
  if (connect(s, (struct sockaddr *)&ia, sizeof(ia)) < 0 &&
      (blocking || errno != EINPROGRESS)) {
    perror("connect");
    exit(77);
  }
  return s;
}


x86 assembly (clang, -O0):
<connect_to_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x4(%rbp)
mov    0x2e66(%rip),%rax
mov    (%rax),%edi
mov    0x2e45(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x8(%rbp)
lea    -0x18(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e3a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    0x2e2e(%rip),%rax
mov    (%rax),%edi
lea    -0x18(%rbp),%rdx
add    $0x4,%rdx
lea    0xe35(%rip),%rsi
call   1050 <inet_pton@plt>
mov    $0x3039,%edi
call   1030 <htons@plt>
mov    %eax,-0x18(%rbp)
cmpl   $0x0,-0x4(%rbp)
jne    11ec <connect_to_socket+0x7c>
jmp    11ec <connect_to_socket+0x7c>
mov    -0x8(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0x0,%rax
jge    1241 <connect_to_socket+0xd1>
cmpl   $0x0,-0x4(%rbp)
jne    122b <connect_to_socket+0xbb>
mov    0x2db0(%rip),%rax
mov    (%rax),%rax
mov    0x2dbe(%rip),%rcx
cmp    (%rcx),%rax
je     1241 <connect_to_socket+0xd1>
lea    0xdd8(%rip),%rdi
call   1060 <perror@plt>
mov    $0x4d,%edi
call   1070 <exit@plt>
mov    -0x8(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_to_socket>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%r14d
mov    0x2e64(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e43(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,%ebp
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%rbx),%edi
mov    %edi,0x10(%rsp)
lea    0xc(%rsp),%rdx
lea    0xe42(%rip),%rsi
call   1050 <inet_pton@plt>
mov    $0x3039,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
test   %rax,%rax
jns    1200 <connect_to_socket+0x90>
test   %r14d,%r14d
jne    120d <connect_to_socket+0x9d>
mov    0x2dd7(%rip),%rax
mov    (%rax),%rax
mov    0x2de5(%rip),%rcx
cmp    (%rcx),%rax
jne    120d <connect_to_socket+0x9d>
mov    %ebp,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xdf6(%rip),%rdi
call   1060 <perror@plt>
mov    $0x4d,%edi
call   1070 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.1 = [8 x i8] c"connect\00"
define i32 @connect_to_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
store i32 %0, i32* %2
%5 = load i32, i32* @AF_INET
%6 = load i32, i32* @SOCK_STREAM
%7 = call i32 @socket(i32 %5, i32 %6, i32 0)
store i32 %7, i32* %3
%8 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %9, i32* %10
%11 = load i32, i32* @AF_INET
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%13 = call i32 @inet_pton(i32 %11, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %12)
%14 = call i32 @htons(i32 12345)
%15 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %14, i32* %15
%16 = load i32, i32* %2
%17 = icmp ne i32 %16, 0
br i1 %17, label %19, label %18
18: ; preds = %1
br label %19
19: ; preds = %18, %1
%20 = load i32, i32* %3
%21 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%22 = call i64 @connect(i32 %20, %struct.sockaddr* %21, i32 12)
%23 = icmp slt i64 %22, 0
br i1 %23, label %24, label %34
24: ; preds = %19
%25 = load i32, i32* %2
%26 = icmp ne i32 %25, 0
br i1 %26, label %31, label %27
27: ; preds = %24
%28 = load i64, i64* @errno
%29 = load i64, i64* @EINPROGRESS
%30 = icmp ne i64 %28, %29
br i1 %30, label %31, label %34
31: ; preds = %27, %24
%32 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%33 = call i32 @exit(i32 77)
unreachable
34: ; preds = %27, %19
%35 = load i32, i32* %3
ret i32 %35
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@errno = global i64 0
@EINPROGRESS = global i64 0
@.str.1 = [8 x i8] c"connect\00"
define i32 @connect_to_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = load i32, i32* @AF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = tail call i32 @socket(i32 %4, i32 %5, i32 0)
%7 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %8, i32* %9
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%11 = call i32 @inet_pton(i32 %8, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32* %10)
%12 = call i32 @htons(i32 12345)
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %12, i32* %13
%14 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%15 = call i64 @connect(i32 %6, %struct.sockaddr* %14, i32 12)
%16 = icmp slt i64 %15, 0
br i1 %16, label %17, label %26
17: ; preds = %1
%18 = icmp eq i32 %0, 0
br i1 %18, label %19, label %23
19: ; preds = %17
%20 = load i64, i64* @errno
%21 = load i64, i64* @EINPROGRESS
%22 = icmp eq i64 %20, %21
br i1 %22, label %26, label %23
23: ; preds = %19, %17
%24 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
%25 = call i32 @exit(i32 77)
unreachable
26: ; preds = %19, %1
ret i32 %6
}