#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;
};
struct protent {
  int dummy;
};
struct hostent {
  int /*<<< orphan*/ h_length;
  int /*<<< orphan*/ h_addr;
};
typedef int /*<<< orphan*/ server;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int SOL_SOCKET;
int SO_RCVBUF;
int SO_SNDBUF;
int TCP_NODELAY;
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getsockopt(int, int, int, int *, int *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ memcpy(int /*<<< orphan*/ *, int /*<<< orphan*/,
                          int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int, int, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int opentcpsock(char *hostip, int port) {
  char datacode, ipaddr[16];
  int sock, data, temp;
  struct sockaddr_in server;
  struct hostent *hp, *gethostbyname();
  float ftime;
  int buffer2, channel2, sample2i, option, optionlen;
  int do_scan_rx[4];
  struct protent *protocol_info;
  sock = socket(AF_INET, SOCK_STREAM, 0);
  if ((sock < 0)) {
    perror("opening stream socket");
    exit(1);
  }
  server.sin_family = AF_INET;
  hp = gethostbyname(hostip);
  if (hp == 0) {
    fprintf(stderr, "unknown host");
    exit(2);
  }
  memcpy(&server.sin_addr, hp->h_addr, hp->h_length);
  server.sin_port = htons(port);
  temp = connect(sock, (struct sockaddr *)&server, sizeof(server));
  if (temp < 0) {
    perror("connecting stream socket");
    return -1;
  }
  option = TCP_NODELAY;
  optionlen = 4;
  temp = setsockopt(sock, 6, TCP_NODELAY, &option, optionlen);
  temp = getsockopt(sock, 6, TCP_NODELAY, &option, &optionlen);
  optionlen = 4;
  option = 32768;
  temp = setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &option, optionlen);
  temp = getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &option, &optionlen);
  optionlen = 4;
  option = 32768;
  temp = setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &option, optionlen);
  temp = getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &option, &optionlen);
  return sock;
}


x86 assembly (clang, -O0):
<opentcpsock>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
mov    0x2e1f(%rip),%rax
mov    (%rax),%edi
mov    0x2dee(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x34(%rbp)
cmpl   $0x0,-0x34(%rbp)
jge    11ee <opentcpsock+0x4e>
lea    0xe21(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2de3(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    -0x10(%rbp),%rdi
mov    $0x0,%al
call   1060 <gethostbyname@plt>
mov    %rax,-0x50(%rbp)
cmpq   $0x0,-0x50(%rbp)
jne    1233 <opentcpsock+0x93>
mov    0x2d9d(%rip),%rax
mov    (%rax),%edi
lea    0xdf2(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>
lea    -0x48(%rbp),%rdi
add    $0x4,%rdi
mov    -0x50(%rbp),%rax
mov    0x4(%rax),%esi
mov    -0x50(%rbp),%rax
mov    (%rax),%edx
call   1080 <memcpy@plt>
mov    -0x14(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x48(%rbp)
mov    -0x34(%rbp),%edi
lea    -0x48(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <connect@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0x0,-0x3c(%rbp)
jge    128e <opentcpsock+0xee>
lea    0xda6(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1399 <opentcpsock+0x1f9>
mov    0x2d4b(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x64(%rbp)
movl   $0x4,-0x68(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d35(%rip),%rax
mov    (%rax),%edx
mov    -0x68(%rbp),%r8d
mov    $0x6,%esi
lea    -0x64(%rbp),%rcx
call   1030 <setsockopt@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2d14(%rip),%rax
mov    (%rax),%edx
mov    $0x6,%esi
lea    -0x64(%rbp),%rcx
lea    -0x68(%rbp),%r8
call   1050 <getsockopt@plt>
mov    %eax,-0x3c(%rbp)
movl   $0x4,-0x68(%rbp)
movl   $0x8000,-0x64(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2ced(%rip),%rax
mov    (%rax),%esi
mov    0x2ccc(%rip),%rax
mov    (%rax),%edx
mov    -0x68(%rbp),%r8d
lea    -0x64(%rbp),%rcx
call   1030 <setsockopt@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2cc8(%rip),%rax
mov    (%rax),%esi
mov    0x2ca7(%rip),%rax
mov    (%rax),%edx
lea    -0x64(%rbp),%rcx
lea    -0x68(%rbp),%r8
call   1050 <getsockopt@plt>
mov    %eax,-0x3c(%rbp)
movl   $0x4,-0x68(%rbp)
movl   $0x8000,-0x64(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2c95(%rip),%rax
mov    (%rax),%esi
mov    0x2c64(%rip),%rax
mov    (%rax),%edx
mov    -0x68(%rbp),%r8d
lea    -0x64(%rbp),%rcx
call   1030 <setsockopt@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%edi
mov    0x2c70(%rip),%rax
mov    (%rax),%esi
mov    0x2c3f(%rip),%rax
mov    (%rax),%edx
lea    -0x64(%rbp),%rcx
lea    -0x68(%rbp),%r8
call   1050 <getsockopt@plt>
mov    %eax,-0x3c(%rbp)
mov    -0x34(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<opentcpsock>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %esi,%r14d
mov    %rdi,%rbp
mov    0x2e1f(%rip),%r15
mov    (%r15),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
test   %eax,%eax
js     131b <opentcpsock+0x17b>
mov    %eax,%ebx
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    %rbp,%rdi
xor    %eax,%eax
call   1060 <gethostbyname@plt>
test   %rax,%rax
je     1331 <opentcpsock+0x191>
lea    0x14(%rsp),%rdi
mov    (%rax),%edx
mov    0x4(%rax),%esi
call   1080 <memcpy@plt>
mov    %r14d,%edi
call   1040 <htons@plt>
mov    %eax,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   10b0 <connect@plt>
test   %eax,%eax
js     1308 <opentcpsock+0x168>
mov    0x2db5(%rip),%rbp
mov    0x0(%rbp),%edx
mov    %edx,0xc(%rsp)
movl   $0x4,0x8(%rsp)
lea    0xc(%rsp),%r14
mov    %ebx,%edi
mov    $0x6,%esi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%edx
lea    0x8(%rsp),%r15
mov    %ebx,%edi
mov    $0x6,%esi
mov    %r14,%rcx
mov    %r15,%r8
call   1050 <getsockopt@plt>
movl   $0x4,0x8(%rsp)
movl   $0x8000,0xc(%rsp)
mov    0x2d63(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2d41(%rip),%r12
mov    (%r12),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%esi
mov    (%r12),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    %r15,%r8
call   1050 <getsockopt@plt>
movl   $0x4,0x8(%rsp)
movl   $0x8000,0xc(%rsp)
mov    0x0(%rbp),%esi
mov    0x2cef(%rip),%r12
mov    (%r12),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    0x0(%rbp),%esi
mov    (%r12),%edx
mov    %ebx,%edi
mov    %r14,%rcx
mov    %r15,%r8
call   1050 <getsockopt@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd14(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    12f9 <opentcpsock+0x159>
lea    0xcde(%rip),%rdi
call   1090 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    0x2c80(%rip),%rax
mov    (%rax),%edi
lea    0xcd5(%rip),%rsi
call   1070 <fprintf@plt>
mov    $0x2,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.hostent = type { i32, i32 }
%struct.protent = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"opening stream socket\00"
@stderr = global i32 0
@.str.1 = [13 x i8] c"unknown host\00"
@.str.2 = [25 x i8] c"connecting stream socket\00"
@TCP_NODELAY = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
define i32 @opentcpsock(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i8
%7 = alloca [16 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
%11 = alloca %struct.sockaddr_in
%12 = alloca %struct.hostent*
%13 = alloca float
%14 = alloca i32
%15 = alloca i32
%16 = alloca i32
%17 = alloca i32
%18 = alloca i32
%19 = alloca [4 x i32]
%20 = alloca %struct.protent*
store i8* %0, i8** %4
store i32 %1, i32* %5
%21 = load i32, i32* @AF_INET
%22 = load i32, i32* @SOCK_STREAM
%23 = call i32 @socket(i32 %21, i32 %22, i32 0)
store i32 %23, i32* %8
%24 = load i32, i32* %8
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %29
26: ; preds = %2
%27 = call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%28 = call i32 @exit(i32 1)
unreachable
29: ; preds = %2
%30 = load i32, i32* @AF_INET
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %30, i32* %31
%32 = load i8*, i8** %4
%33 = call %struct.hostent* (i8*, ...) bitcast (%struct.hostent* (...)* @gethostbyname to %struct.hostent* (i8*, ...)*)(i8* %32)
store %struct.hostent* %33, %struct.hostent** %12
%34 = load %struct.hostent*, %struct.hostent** %12
%35 = icmp eq %struct.hostent* %34, null
br i1 %35, label %36, label %40
36: ; preds = %29
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%39 = call i32 @exit(i32 2)
unreachable
40: ; preds = %29
%41 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
%42 = load %struct.hostent*, %struct.hostent** %12
%43 = getelementptr inbounds %struct.hostent, %struct.hostent* %42, i32 0, i32 1
%44 = load i32, i32* %43
%45 = load %struct.hostent*, %struct.hostent** %12
%46 = getelementptr inbounds %struct.hostent, %struct.hostent* %45, i32 0, i32 0
%47 = load i32, i32* %46
%48 = call i32 @memcpy(i32* %41, i32 %44, i32 %47)
%49 = load i32, i32* %5
%50 = call i32 @htons(i32 %49)
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
store i32 %50, i32* %51
%52 = load i32, i32* %8
%53 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%54 = call i32 @connect(i32 %52, %struct.sockaddr* %53, i32 12)
store i32 %54, i32* %10
%55 = load i32, i32* %10
%56 = icmp slt i32 %55, 0
br i1 %56, label %57, label %59
57: ; preds = %40
%58 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %87
59: ; preds = %40
%60 = load i32, i32* @TCP_NODELAY
store i32 %60, i32* %17
store i32 4, i32* %18
%61 = load i32, i32* %8
%62 = load i32, i32* @TCP_NODELAY
%63 = load i32, i32* %18
%64 = call i32 @setsockopt(i32 %61, i32 6, i32 %62, i32* %17, i32 %63)
store i32 %64, i32* %10
%65 = load i32, i32* %8
%66 = load i32, i32* @TCP_NODELAY
%67 = call i32 @getsockopt(i32 %65, i32 6, i32 %66, i32* %17, i32* %18)
store i32 %67, i32* %10
store i32 4, i32* %18
store i32 32768, i32* %17
%68 = load i32, i32* %8
%69 = load i32, i32* @SOL_SOCKET
%70 = load i32, i32* @SO_SNDBUF
%71 = load i32, i32* %18
%72 = call i32 @setsockopt(i32 %68, i32 %69, i32 %70, i32* %17, i32 %71)
store i32 %72, i32* %10
%73 = load i32, i32* %8
%74 = load i32, i32* @SOL_SOCKET
%75 = load i32, i32* @SO_SNDBUF
%76 = call i32 @getsockopt(i32 %73, i32 %74, i32 %75, i32* %17, i32* %18)
store i32 %76, i32* %10
store i32 4, i32* %18
store i32 32768, i32* %17
%77 = load i32, i32* %8
%78 = load i32, i32* @SOL_SOCKET
%79 = load i32, i32* @SO_RCVBUF
%80 = load i32, i32* %18
%81 = call i32 @setsockopt(i32 %77, i32 %78, i32 %79, i32* %17, i32 %80)
store i32 %81, i32* %10
%82 = load i32, i32* %8
%83 = load i32, i32* @SOL_SOCKET
%84 = load i32, i32* @SO_RCVBUF
%85 = call i32 @getsockopt(i32 %82, i32 %83, i32 %84, i32* %17, i32* %18)
store i32 %85, i32* %10
%86 = load i32, i32* %8
store i32 %86, i32* %3
br label %87
87: ; preds = %59, %57
%88 = load i32, i32* %3
ret i32 %88
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.protent = type { i32 }
%struct.hostent = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [22 x i8] c"opening stream socket\00"
@stderr = global i32 0
@.str.1 = [13 x i8] c"unknown host\00"
@.str.2 = [25 x i8] c"connecting stream socket\00"
@TCP_NODELAY = global i32 0
@SOL_SOCKET = global i32 0
@SO_SNDBUF = global i32 0
@SO_RCVBUF = global i32 0
define i32 @opentcpsock(i8* %0, i32 %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast i32* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_STREAM
%11 = tail call i32 @socket(i32 %9, i32 %10, i32 0)
%12 = icmp slt i32 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %2
%14 = tail call i32 @perror(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0))
%15 = tail call i32 @exit(i32 1)
unreachable
16: ; preds = %2
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %17, i32* %18
%19 = tail call %struct.hostent* (i8*, ...) bitcast (%struct.hostent* (...)* @gethostbyname to %struct.hostent* (i8*, ...)*)(i8* %0)
%20 = icmp eq %struct.hostent* %19, null
br i1 %20, label %21, label %25
21: ; preds = %16
%22 = load i32, i32* @stderr
%23 = tail call i32 @fprintf(i32 %22, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0))
%24 = tail call i32 @exit(i32 2)
unreachable
25: ; preds = %16
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%27 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 1
%28 = load i32, i32* %27
%29 = getelementptr inbounds %struct.hostent, %struct.hostent* %19, i64 0, i32 0
%30 = load i32, i32* %29
%31 = call i32 @memcpy(i32* %26, i32 %28, i32 %30)
%32 = call i32 @htons(i32 %1)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
store i32 %32, i32* %33
%34 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%35 = call i32 @connect(i32 %11, %struct.sockaddr* %34, i32 12)
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %25
%38 = call i32 @perror(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0))
br label %56
39: ; preds = %25
%40 = load i32, i32* @TCP_NODELAY
store i32 %40, i32* %4
store i32 4, i32* %5
%41 = call i32 @setsockopt(i32 %11, i32 6, i32 %40, i32* %4, i32 4)
%42 = load i32, i32* @TCP_NODELAY
%43 = call i32 @getsockopt(i32 %11, i32 6, i32 %42, i32* %4, i32* %5)
store i32 4, i32* %5
store i32 32768, i32* %4
%44 = load i32, i32* @SOL_SOCKET
%45 = load i32, i32* @SO_SNDBUF
%46 = call i32 @setsockopt(i32 %11, i32 %44, i32 %45, i32* %4, i32 4)
%47 = load i32, i32* @SOL_SOCKET
%48 = load i32, i32* @SO_SNDBUF
%49 = call i32 @getsockopt(i32 %11, i32 %47, i32 %48, i32* %4, i32* %5)
store i32 4, i32* %5
store i32 32768, i32* %4
%50 = load i32, i32* @SOL_SOCKET
%51 = load i32, i32* @SO_RCVBUF
%52 = call i32 @setsockopt(i32 %11, i32 %50, i32 %51, i32* %4, i32 4)
%53 = load i32, i32* @SOL_SOCKET
%54 = load i32, i32* @SO_RCVBUF
%55 = call i32 @getsockopt(i32 %11, i32 %53, i32 %54, i32* %4, i32* %5)
br label %56
56: ; preds = %39, %37
%57 = phi i32 [ -1, %37 ], [ %11, %39 ]
ret i32 %57
}