#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*/ addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ assert(int /*<<< orphan*/);
int connect(int, struct sockaddr *, 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 sock_connect(char *host) {
  struct sockaddr_in addr;
  int fd;
  assert(host != NULL);
  fd = socket(AF_INET, SOCK_STREAM, 0);
  if (fd == -1) {
    perror("Unable to create socket");
    return -1;
  }
  memset(&addr, 0, sizeof(addr));
  addr.sin_family = AF_INET;
  inet_pton(AF_INET, host, &addr.sin_addr);
  addr.sin_port = htons(10054);
  if (connect(fd, (struct sockaddr *)(&addr), sizeof(addr)) == -1) {
    perror("Unable to connect to server");
    return -1;
  }
  return fd;
}


x86 assembly (clang, -O0):
<sock_connect>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
setne  %al
and    $0x1,%al
movzbl %al,%edi
call   1050 <assert@plt>
mov    0x2e53(%rip),%rax
mov    (%rax),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0xffffffff,-0x24(%rbp)
jne    11cc <sock_connect+0x5c>
lea    0xe45(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <sock_connect+0xd7>
lea    -0x20(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    0x2e05(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    0x2df9(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x20(%rbp),%rdx
add    $0x4,%rdx
call   1060 <inet_pton@plt>
mov    $0x2746,%edi
call   1030 <htons@plt>
mov    %eax,-0x20(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
jne    1241 <sock_connect+0xd1>
lea    0xde8(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1247 <sock_connect+0xd7>
mov    -0x24(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sock_connect>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %rdi,%r14
xor    %edi,%edi
test   %r14,%r14
setne  %dil
call   1050 <assert@plt>
mov    0x2e55(%rip),%r12
mov    (%r12),%edi
mov    0x2e3a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1090 <socket@plt>
cmp    $0xffffffff,%eax
je     1207 <sock_connect+0x97>
mov    %eax,%ebx
lea    0x8(%rsp),%r15
mov    %r15,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1040 <memset@plt>
mov    (%r12),%edi
mov    %edi,0x10(%rsp)
lea    0xc(%rsp),%rdx
mov    %r14,%rsi
call   1060 <inet_pton@plt>
mov    $0x2746,%edi
call   1030 <htons@plt>
mov    %eax,0x8(%rsp)
mov    %ebx,%edi
mov    %r15,%rsi
mov    $0xc,%edx
call   1080 <connect@plt>
cmp    $0xffffffff,%eax
je     1210 <sock_connect+0xa0>
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xdf2(%rip),%rdi
jmp    1217 <sock_connect+0xa7>
lea    0xe01(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebx
jmp    11f9 <sock_connect+0x89>

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 = [24 x i8] c"Unable to create socket\00"
@.str.1 = [28 x i8] c"Unable to connect to server\00"
define i32 @sock_connect(i8* %0) {
%2 = alloca i32
%3 = alloca i8*
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
store i8* %0, i8** %3
%6 = load i8*, i8** %3
%7 = icmp ne i8* %6, null
%8 = zext i1 %7 to i32
%9 = call i32 @assert(i32 %8)
%10 = load i32, i32* @AF_INET
%11 = load i32, i32* @SOCK_STREAM
%12 = call i32 @socket(i32 %10, i32 %11, i32 0)
store i32 %12, i32* %5
%13 = load i32, i32* %5
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %1
%16 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %35
17: ; preds = %1
%18 = call i32 @memset(%struct.sockaddr_in* %4, i32 0, i32 12)
%19 = load i32, i32* @AF_INET
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 2
store i32 %19, i32* %20
%21 = load i32, i32* @AF_INET
%22 = load i8*, i8** %3
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 1
%24 = call i32 @inet_pton(i32 %21, i8* %22, i32* %23)
%25 = call i32 @htons(i32 10054)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i32 0, i32 0
store i32 %25, i32* %26
%27 = load i32, i32* %5
%28 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%29 = call i32 @connect(i32 %27, %struct.sockaddr* %28, i32 12)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %17
%32 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %35
33: ; preds = %17
%34 = load i32, i32* %5
store i32 %34, i32* %2
br label %35
35: ; preds = %33, %31, %15
%36 = load i32, i32* %2
ret i32 %36
}

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 = [24 x i8] c"Unable to create socket\00"
@.str.1 = [28 x i8] c"Unable to connect to server\00"
define i32 @sock_connect(i8* %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = icmp ne i8* %0, null
%5 = zext i1 %4 to i32
%6 = tail call i32 @assert(i32 %5)
%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 eq i32 %9, -1
br i1 %10, label %11, label %13
11: ; preds = %1
%12 = tail call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %26
13: ; preds = %1
%14 = call i32 @memset(%struct.sockaddr_in* %2, i32 0, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
%18 = call i32 @inet_pton(i32 %15, i8* %0, i32* %17)
%19 = call i32 @htons(i32 10054)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 %19, i32* %20
%21 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%22 = call i32 @connect(i32 %9, %struct.sockaddr* %21, i32 12)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %26
24: ; preds = %13
%25 = call i32 @perror(i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0))
br label %26
26: ; preds = %13, %24, %11
%27 = phi i32 [ -1, %11 ], [ -1, %24 ], [ %9, %13 ]
ret i32 %27
}