#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 TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_family;
  int /*<<< orphan*/ sin_port;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int do_connect() {
  int s = socket(AF_INET, SOCK_STREAM, 0);
  if (s < 0) {
    perror("test socket");
    exit(1);
  }
  struct sockaddr_in addr;
  memset(&addr, 0, sizeof(addr));
  addr.sin_port = htons(8880);
  addr.sin_family = AF_INET;
  addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  if (connect(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
    perror("test connect");
    close(s);
    exit(2);
  }
  return s;
}


x86 assembly (clang, -O0):
<do_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
mov    0x2e40(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11c4 <do_connect+0x44>
lea    0xe4b(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x10(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    $0x22b0,%edi
call   1030 <htons@plt>
mov    %eax,-0x8(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0xc(%rbp)
lea    0xe18(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
lea    -0x10(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jge    1235 <do_connect+0xb5>
lea    0xdf8(%rip),%rdi
call   1070 <perror@plt>
mov    -0x4(%rbp),%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_connect>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    0x2e58(%rip),%r15
mov    (%r15),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     11ff <do_connect+0x7f>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    $0x22b0,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    (%r15),%eax
mov    %eax,0x4(%rsp)
lea    0xe35(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
js     1215 <do_connect+0x95>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    0xdfa(%rip),%rdi
call   1070 <perror@plt>
mov    %ebx,%edi
call   1050 <close@plt>
mov    $0x2,%edi
call   1080 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [12 x i8] c"test socket\00"
@.str.1 = [10 x i8] c"127.0.0.1\00"
@.str.2 = [13 x i8] c"test connect\00"
define i32 @do_connect() {
%1 = alloca i32
%2 = alloca %struct.sockaddr_in
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_STREAM
%5 = call i32 @socket(i32 %3, i32 %4, i32 0)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp slt i32 %6, 0
br i1 %7, label %8, label %11
8: ; preds = %0
%9 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0))
%10 = call i32 @exit(i32 1)
unreachable
11: ; preds = %0
%12 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%13 = call i32 @htons(i32 8880)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 2
store i32 %13, i32* %14
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 1
store i32 %15, i32* %16
%17 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i32 0, i32 0
%19 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %18, i32 0, i32 0
store i32 %17, i32* %19
%20 = load i32, i32* %1
%21 = bitcast %struct.sockaddr_in* %2 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 %29
24: ; preds = %11
%25 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.2, i64 0, i64 0))
%26 = load i32, i32* %1
%27 = call i32 @close(i32 %26)
%28 = call i32 @exit(i32 2)
unreachable
29: ; preds = %11
%30 = load i32, i32* %1
ret i32 %30
}

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