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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ close(int);
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ fputs(char *, int /*<<< orphan*/);
struct protoent *getprotobyname(char *);
scalar_t__ htons(int);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int send(int, char *, int, int /*<<< orphan*/);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int start(in_addr_t ip) {
  int sock;
  const struct protoent *proto;
  struct sockaddr_in server;
  memset((char *)&server, 0, sizeof(struct sockaddr_in));
  server.sin_family = AF_INET;
  server.sin_port = (in_port_t)htons(3310);
  server.sin_addr.s_addr = ip;
  proto = getprotobyname("tcp");
  if (proto == NULL) {
    fputs("Unknown protocol tcp, check /etc/protocols\n", stderr);
    return -1;
  } else if ((sock = socket(AF_INET, SOCK_STREAM, proto->p_proto)) < 0) {
    perror("socket");
    return -1;
  } else if (connect(sock, (struct sockaddr *)&server,
                     sizeof(struct sockaddr_in)) < 0) {
    perror("connect");
    close(sock);
    return -1;
  } else if (send(sock, "SESSION\n", 8, 0) < 8) {
    perror("send");
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<start>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %edi,-0x8(%rbp)
lea    -0x30(%rbp),%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e36(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    $0xcee,%edi
call   1030 <htons@plt>
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x30(%rbp)
lea    0xe2e(%rip),%rdi
call   1080 <getprotobyname@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    1207 <start+0x77>
mov    0x2deb(%rip),%rax
mov    (%rax),%esi
lea    0xe0e(%rip),%rdi
call   1050 <fputs@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <start+0x147>
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%esi
mov    -0x18(%rbp),%rax
mov    (%rax),%edx
call   10b0 <socket@plt>
mov    %eax,-0xc(%rbp)
cmp    $0x0,%eax
jge    1248 <start+0xb8>
lea    0xdf9(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <start+0x147>
mov    -0xc(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x18,%edx
call   10a0 <connect@plt>
cmp    $0x0,%rax
jge    1283 <start+0xf3>
lea    0xdcd(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <start+0x147>
mov    -0xc(%rbp),%edi
lea    0xdb2(%rip),%rsi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0x8,%eax
jge    12c2 <start+0x132>
lea    0xd9f(%rip),%rdi
call   1090 <perror@plt>
mov    -0xc(%rbp),%edi
call   1070 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    12d7 <start+0x147>
jmp    12c7 <start+0x137>
jmp    12cc <start+0x13c>
jmp    12d1 <start+0x141>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<start>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
mov    %rsp,%rdi
xor    %esi,%esi
mov    $0x18,%edx
call   1060 <memset@plt>
mov    0x2e39(%rip),%r14
mov    (%r14),%eax
mov    %eax,0x10(%rsp)
mov    $0xcee,%edi
call   1030 <htons@plt>
mov    %rax,0x8(%rsp)
mov    %ebx,(%rsp)
lea    0xe31(%rip),%rdi
call   1080 <getprotobyname@plt>
test   %rax,%rax
je     1229 <start+0x99>
mov    (%r14),%edi
mov    0x2ded(%rip),%rcx
mov    (%rcx),%esi
mov    (%rax),%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     124d <start+0xbd>
mov    %eax,%ebx
mov    %rsp,%rsi
mov    %eax,%edi
mov    $0x18,%edx
call   10a0 <connect@plt>
test   %rax,%rax
js     125b <start+0xcb>
lea    0xe32(%rip),%rsi
mov    %ebx,%edi
mov    $0x8,%edx
xor    %ecx,%ecx
call   1040 <send@plt>
cmp    $0x7,%eax
jg     1243 <start+0xb3>
lea    0xe21(%rip),%rdi
jmp    1262 <start+0xd2>
mov    0x2da8(%rip),%rax
mov    (%rax),%esi
lea    0xdcb(%rip),%rdi
call   1050 <fputs@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
lea    0xddc(%rip),%rdi
call   1090 <perror@plt>
jmp    123e <start+0xae>
lea    0xdd5(%rip),%rdi
call   1090 <perror@plt>
mov    %ebx,%edi
call   1070 <close@plt>
jmp    123e <start+0xae>

LLVM IR (-O0):
%struct.protoent = type { i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [4 x i8] c"tcp\00"
@.str.1 = [44 x i8] c"Unknown protocol tcp, check /etc/protocols\0A\00"
@stderr = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [9 x i8] c"SESSION\0A\00"
@.str.5 = [5 x i8] c"send\00"
define i32 @start(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.protoent*
%6 = alloca %struct.sockaddr_in
store i32 %0, i32* %3
%7 = bitcast %struct.sockaddr_in* %6 to i8*
%8 = call i32 @memset(i8* %7, i32 0, i32 24)
%9 = load i32, i32* @AF_INET
%10 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %9, i32* %10
%11 = call i64 @htons(i32 3310)
%12 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i64 %11, i64* %12
%13 = load i32, i32* %3
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%15 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %14, i32 0, i32 0
store i32 %13, i32* %15
%16 = call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
store %struct.protoent* %16, %struct.protoent** %5
%17 = load %struct.protoent*, %struct.protoent** %5
%18 = icmp eq %struct.protoent* %17, null
br i1 %18, label %19, label %22
19: ; preds = %1
%20 = load i32, i32* @stderr
%21 = call i32 @fputs(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %20)
store i32 -1, i32* %2
br label %54
22: ; preds = %1
%23 = load i32, i32* @AF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = load %struct.protoent*, %struct.protoent** %5
%26 = getelementptr inbounds %struct.protoent, %struct.protoent* %25, i32 0, i32 0
%27 = load i32, i32* %26
%28 = call i32 @socket(i32 %23, i32 %24, i32 %27)
store i32 %28, i32* %4
%29 = icmp slt i32 %28, 0
br i1 %29, label %30, label %32
30: ; preds = %22
%31 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %2
br label %54
32: ; preds = %22
%33 = load i32, i32* %4
%34 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%35 = call i64 @connect(i32 %33, %struct.sockaddr* %34, i32 24)
%36 = icmp slt i64 %35, 0
br i1 %36, label %37, label %41
37: ; preds = %32
%38 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%39 = load i32, i32* %4
%40 = call i32 @close(i32 %39)
store i32 -1, i32* %2
br label %54
41: ; preds = %32
%42 = load i32, i32* %4
%43 = call i32 @send(i32 %42, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8, i32 0)
%44 = icmp slt i32 %43, 8
br i1 %44, label %45, label %49
45: ; preds = %41
%46 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%47 = load i32, i32* %4
%48 = call i32 @close(i32 %47)
store i32 -1, i32* %2
br label %54
49: ; preds = %41
br label %50
50: ; preds = %49
br label %51
51: ; preds = %50
br label %52
52: ; preds = %51
%53 = load i32, i32* %4
store i32 %53, i32* %2
br label %54
54: ; preds = %52, %45, %37, %30, %19
%55 = load i32, i32* %2
ret i32 %55
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i64, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.protoent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@.str = [4 x i8] c"tcp\00"
@.str.1 = [44 x i8] c"Unknown protocol tcp, check /etc/protocols\0A\00"
@stderr = global i32 0
@SOCK_STREAM = global i32 0
@.str.2 = [7 x i8] c"socket\00"
@.str.3 = [8 x i8] c"connect\00"
@.str.4 = [9 x i8] c"SESSION\0A\00"
@.str.5 = [5 x i8] c"send\00"
define i32 @start(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = bitcast %struct.sockaddr_in* %2 to i8*
%4 = call i32 @memset(i8* %3, i32 0, i32 24)
%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 i64 @htons(i32 3310)
%8 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i64 %7, i64* %8
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %0, i32* %9
%10 = call %struct.protoent* @getprotobyname(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str, i64 0, i64 0))
%11 = icmp eq %struct.protoent* %10, null
br i1 %11, label %12, label %15
12: ; preds = %1
%13 = load i32, i32* @stderr
%14 = call i32 @fputs(i8* getelementptr inbounds ([44 x i8], [44 x i8]* @.str.1, i64 0, i64 0), i32 %13)
br label %37
15: ; preds = %1
%16 = load i32, i32* @AF_INET
%17 = load i32, i32* @SOCK_STREAM
%18 = getelementptr inbounds %struct.protoent, %struct.protoent* %10, i64 0, i32 0
%19 = load i32, i32* %18
%20 = call i32 @socket(i32 %16, i32 %17, i32 %19)
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %15
%23 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0))
br label %37
24: ; preds = %15
%25 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%26 = call i64 @connect(i32 %20, %struct.sockaddr* %25, i32 24)
%27 = icmp slt i64 %26, 0
br i1 %27, label %28, label %31
28: ; preds = %24
%29 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.3, i64 0, i64 0))
%30 = call i32 @close(i32 %20)
br label %37
31: ; preds = %24
%32 = call i32 @send(i32 %20, i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.4, i64 0, i64 0), i32 8, i32 0)
%33 = icmp slt i32 %32, 8
br i1 %33, label %34, label %37
34: ; preds = %31
%35 = call i32 @perror(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.5, i64 0, i64 0))
%36 = call i32 @close(i32 %20)
br label %37
37: ; preds = %31, %34, %28, %22, %12
%38 = phi i32 [ -1, %12 ], [ -1, %22 ], [ -1, %28 ], [ -1, %34 ], [ %20, %31 ]
ret i32 %38
}