#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_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
struct protoent {
  int /*<<< orphan*/ p_proto;
};
typedef int /*<<< orphan*/ servaddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
struct protoent *getprotobyname(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int connect_socket() {
  int sock_fd;
  int proto;
  int result;
  struct protoent *my_protoent;
  struct sockaddr_in servaddr;
  my_protoent = getprotobyname("tcp");
  sock_fd = socket(PF_INET, SOCK_STREAM, my_protoent->p_proto);
  if (sock_fd < 0) {
    fprintf(stderr, "failed to alloc socket()\n");
    exit(1);
  }
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(13);
  inet_pton(AF_INET, "127.0.0.1", &servaddr.sin_addr);
  result = connect(sock_fd, (struct sockaddr *)&servaddr, sizeof(servaddr));
  if (result < 0) {
    fprintf(stderr, "failed to connect()\n");
    exit(1);
  }
  return sock_fd;
}


x86 assembly (clang, -O0):
<connect_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
lea    0xe71(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    %rax,-0x18(%rbp)
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    0x2e28(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jge    11e1 <connect_socket+0x61>
mov    0x2e0f(%rip),%rax
mov    (%rax),%edi
lea    0xe32(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
lea    -0x28(%rbp),%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2df2(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2dd9(%rip),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rdx
lea    0xe02(%rip),%rsi
call   1050 <inet_pton@plt>
mov    -0x4(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0x0,-0xc(%rbp)
jge    125e <connect_socket+0xde>
mov    0x2d92(%rip),%rax
mov    (%rax),%edi
lea    0xdd9(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<connect_socket>:
push   %r15
push   %r14
push   %rbx
sub    $0x10,%rsp
lea    0xe70(%rip),%rdi
call   1060 <getprotobyname@plt>
mov    0x2e2c(%rip),%rcx
mov    (%rcx),%edi
mov    0x2e2b(%rip),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   10a0 <socket@plt>
test   %eax,%eax
js     1211 <connect_socket+0x91>
mov    %eax,%ebx
mov    %rsp,%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1070 <bzero@plt>
mov    0x2e1d(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    $0xd,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r15),%edi
lea    0xe34(%rip),%rsi
mov    %r14,%rdx
call   1050 <inet_pton@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %eax,%eax
js     1223 <connect_socket+0xa3>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret
mov    0x2dc0(%rip),%rax
mov    (%rax),%edi
lea    0xde3(%rip),%rsi
jmp    1233 <connect_socket+0xb3>
mov    0x2dae(%rip),%rax
mov    (%rax),%edi
lea    0xdf5(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   1080 <exit@plt>

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

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [4 x i8] c"tcp\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@stderr = global i32 0
@.str.1 = [26 x i8] c"failed to alloc socket()\0A\00"
@AF_INET = global i32 0
@.str.2 = [10 x i8] c"127.0.0.1\00"
@.str.3 = [21 x i8] c"failed to connect()\0A\00"
define i32 @connect_socket() {
%1 = alloca %struct.sockaddr_in
%2 = bitcast %struct.sockaddr_in* %1 to i8*
%3 = tail call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%4 = load i32, i32* @PF_INET
%5 = load i32, i32* @SOCK_STREAM
%6 = getelementptr inbounds %struct.protoent, %struct.protoent* %3, i64 0, i32 0
%7 = load i32, i32* %6
%8 = tail call i32 @socket(i32 %4, i32 %5, i32 %7)
%9 = icmp slt i32 %8, 0
br i1 %9, label %10, label %14
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.1, i64 0, i64 0))
%13 = tail call i32 @exit(i32 1)
unreachable
14: ; preds = %0
%15 = call i32 @bzero(%struct.sockaddr_in* %1, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 2
store i32 %16, i32* %17
%18 = tail call i32 @htons(i32 13)
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 1
store i32 %18, i32* %19
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %1, i64 0, i32 0
%22 = call i32 @inet_pton(i32 %20, i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.2, i64 0, i64 0), i32* %21)
%23 = bitcast %struct.sockaddr_in* %1 to %struct.sockaddr*
%24 = call i32 @connect(i32 %8, %struct.sockaddr* %23, i32 12)
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %30
26: ; preds = %14
%27 = load i32, i32* @stderr
%28 = call i32 @fprintf(i32 %27, i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.3, i64 0, i64 0))
%29 = call i32 @exit(i32 1)
unreachable
30: ; preds = %14
ret i32 %8
}