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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, int);
int unified_sockets__connect(int _sd) {
  int ret;
  struct sockaddr_in serv_addr;
  uint16_t portno;
  memset((char *)&serv_addr, 0x00, sizeof(serv_addr));
  serv_addr.sin_family = AF_INET;
  serv_addr.sin_addr.s_addr = inet_addr("127.0.0.1");
  ;
  portno = 9999;
  serv_addr.sin_port = htons(9999);
  ret = connect(_sd, (struct sockaddr *)&serv_addr, sizeof(serv_addr));
  switch (ret) {
  case 0:
    break;
  default:
    printf("error on connecting %i\n", ret);
    ret = -1;
    break;
  }
  return ret;
}


x86 assembly (clang, -O0):
<unified_sockets__connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x18(%rbp),%rdi
mov    %rdi,-0x28(%rbp)
xor    %esi,%esi
mov    $0xc,%edx
mov    %edx,-0x20(%rbp)
call   1050 <memset@plt>
mov    0x2e6f(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
lea    0xe7b(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,-0x14(%rbp)
movl   $0x270f,-0x1c(%rbp)
mov    $0x270f,%edi
call   1030 <htons@plt>
mov    -0x28(%rbp),%rsi
mov    -0x20(%rbp),%edx
mov    %eax,-0x18(%rbp)
mov    -0x4(%rbp),%edi
call   1070 <connect@plt>
mov    %eax,-0x8(%rbp)
mov    -0x8(%rbp),%eax
test   %eax,%eax
jne    11c8 <unified_sockets__connect+0x78>
jmp    11c3 <unified_sockets__connect+0x73>
jmp    11de <unified_sockets__connect+0x8e>
mov    -0x8(%rbp),%esi
lea    0xe38(%rip),%rdi
call   1040 <printf@plt>
movl   $0xffffffff,-0x8(%rbp)
mov    -0x8(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<unified_sockets__connect>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %edi,%ebp
mov    %rsp,%r14
xor    %ebx,%ebx
mov    %r14,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2e73(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
lea    0xe7e(%rip),%rdi
call   1060 <inet_addr@plt>
mov    %eax,0x4(%rsp)
mov    $0x270f,%edi
call   1030 <htons@plt>
mov    %eax,(%rsp)
mov    %ebp,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1070 <connect@plt>
test   %eax,%eax
je     11be <unified_sockets__connect+0x6e>
lea    0xe58(%rip),%rdi
mov    %eax,%esi
call   1040 <printf@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [24 x i8] c"error on connecting %i\0A\00"
define i32 @unified_sockets__connect(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i32 %0, i32* %2
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = call i32 @memset(i8* %6, i32 0, i32 12)
%8 = load i32, i32* @AF_INET
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %8, i32* %9
%10 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%12 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %11, i32 0, i32 0
store i32 %10, i32* %12
store i32 9999, i32* %5
%13 = call i32 @htons(i32 9999)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %13, i32* %14
%15 = load i32, i32* %2
%16 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%17 = call i32 @connect(i32 %15, %struct.sockaddr* %16, i32 12)
store i32 %17, i32* %3
%18 = load i32, i32* %3
switch i32 %18, label %20 [
i32 0, label %19
]
19: ; preds = %1
br label %23
20: ; preds = %1
%21 = load i32, i32* %3
%22 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %21)
store i32 -1, i32* %3
br label %23
23: ; preds = %20, %19
%24 = load i32, i32* %3
ret i32 %24
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, %struct.TYPE_2__, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [10 x i8] c"127.0.0.1\00"
@.str.1 = [24 x i8] c"error on connecting %i\0A\00"
define i32 @unified_sockets__connect(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = call i32 @memset(i8* %3, i32 0, i32 12)
%5 = load i32, i32* @AF_INET
%6 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %5, i32* %6
%7 = call i32 @inet_addr(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0))
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1, i32 0
store i32 %7, i32* %8
%9 = call i32 @htons(i32 9999)
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %9, i32* %10
%11 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%12 = call i32 @connect(i32 %0, %struct.sockaddr* %11, i32 12)
%13 = icmp eq i32 %12, 0
br i1 %13, label %16, label %14
14: ; preds = %1
%15 = call i32 @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %12)
br label %16
16: ; preds = %1, %14
%17 = phi i32 [ 0, %1 ], [ -1, %14 ]
ret i32 %17
}