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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ IPPROTO_TCP;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_KEEPALIVE;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htonl(unsigned long);
int /*<<< orphan*/ htons(unsigned short);
int /*<<< orphan*/ memset(char *, int /*<<< orphan*/, int);
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int init_client_ip(int *sock, unsigned char ip[4], int port, int type) {
  unsigned long addr;
  struct sockaddr_in them;
  int s, i;
  memset((char *)&them, 0, sizeof(them));
  them.sin_family = AF_INET;
  them.sin_port = htons((unsigned short)port);
  addr = (unsigned long)((unsigned long)ip[0] << 24L) |
         ((unsigned long)ip[1] << 16L) | ((unsigned long)ip[2] << 8L) |
         ((unsigned long)ip[3]);
  them.sin_addr.s_addr = htonl(addr);
  s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (s == (-1)) {
    perror("socket");
    return (0);
  }
  if (type == SOCK_STREAM) {
    i = 0;
    i = setsockopt(s, SOL_SOCKET, SO_KEEPALIVE, (char *)&i, sizeof(i));
    if (i < 0) {
      perror("keepalive");
      return (0);
    }
  }
  if (connect(s, (struct sockaddr *)&them, sizeof(them)) == -1) {
    close(s);
    perror("connect");
    return (0);
  }
  *sock = s;
  return (1);
}


x86 assembly (clang, -O0):
<init_client_ip>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %rdi,-0x10(%rbp)
mov    %rsi,-0x18(%rbp)
mov    %edx,-0x1c(%rbp)
mov    %ecx,-0x20(%rbp)
lea    -0x38(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x30(%rbp)
mov    -0x1c(%rbp),%eax
movzwl %ax,%edi
call   1040 <htons@plt>
mov    %eax,-0x34(%rbp)
mov    -0x18(%rbp),%rax
movzbl (%rax),%eax
shl    $0x18,%rax
mov    -0x18(%rbp),%rcx
movzbl 0x1(%rcx),%ecx
shl    $0x10,%rcx
or     %rcx,%rax
mov    -0x18(%rbp),%rcx
movzbl 0x2(%rcx),%ecx
shl    $0x8,%rcx
or     %rcx,%rax
mov    -0x18(%rbp),%rcx
movzbl 0x3(%rcx),%ecx
or     %rcx,%rax
mov    %rax,-0x28(%rbp)
mov    -0x28(%rbp),%rdi
call   1050 <htonl@plt>
mov    %eax,-0x38(%rbp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%edi
mov    0x2db4(%rip),%rax
mov    (%rax),%esi
mov    0x2dbb(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
mov    %eax,-0x3c(%rbp)
cmpl   $0xffffffff,-0x3c(%rbp)
jne    1249 <init_client_ip+0xc9>
lea    0xdc8(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fa <init_client_ip+0x17a>
mov    -0x20(%rbp),%eax
mov    0x2d75(%rip),%rcx
cmp    (%rcx),%eax
jne    12b0 <init_client_ip+0x130>
movl   $0x0,-0x40(%rbp)
mov    -0x3c(%rbp),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
mov    0x2d43(%rip),%rax
mov    (%rax),%edx
lea    -0x40(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x40(%rbp)
cmpl   $0x0,-0x40(%rbp)
jge    12ab <init_client_ip+0x12b>
lea    0xd6d(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fa <init_client_ip+0x17a>
jmp    12b0 <init_client_ip+0x130>
mov    -0x3c(%rbp),%edi
lea    -0x38(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
jne    12ea <init_client_ip+0x16a>
mov    -0x3c(%rbp),%edi
call   1070 <close@plt>
lea    0xd38(%rip),%rdi
call   1080 <perror@plt>
movl   $0x0,-0x4(%rbp)
jmp    12fa <init_client_ip+0x17a>
mov    -0x3c(%rbp),%ecx
mov    -0x10(%rbp),%rax
mov    %ecx,(%rax)
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<init_client_ip>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
mov    %ecx,%r15d
mov    %edx,%ebx
mov    %rsi,%rbp
mov    %rdi,%r14
lea    0x8(%rsp),%rdi
xor    %r12d,%r12d
xor    %esi,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    0x2e2c(%rip),%r13
mov    0x0(%r13),%eax
mov    %eax,0x10(%rsp)
movzwl %bx,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    0x0(%rbp),%edi
shl    $0x20,%rdi
bswap  %rdi
call   1050 <htonl@plt>
mov    %eax,0x8(%rsp)
mov    0x0(%r13),%edi
mov    0x2de2(%rip),%rbp
mov    0x0(%rbp),%esi
mov    0x2de8(%rip),%rax
mov    (%rax),%edx
call   10a0 <socket@plt>
cmp    $0xffffffff,%eax
je     1269 <init_client_ip+0xe9>
mov    %eax,%ebx
cmp    %r15d,0x0(%rbp)
jne    1238 <init_client_ip+0xb8>
movl   $0x0,0x4(%rsp)
mov    0x2dd5(%rip),%rax
mov    (%rax),%esi
mov    0x2d9c(%rip),%rax
mov    (%rax),%edx
lea    0x4(%rsp),%rcx
mov    %ebx,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
mov    %eax,0x4(%rsp)
test   %eax,%eax
js     1287 <init_client_ip+0x107>
lea    0x8(%rsp),%rsi
mov    %ebx,%edi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0xffffffff,%eax
je     1277 <init_client_ip+0xf7>
mov    %ebx,(%r14)
mov    $0x1,%r12d
mov    %r12d,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret
lea    0xd90(%rip),%rdi
call   1080 <perror@plt>
jmp    1257 <init_client_ip+0xd7>
mov    %ebx,%edi
call   1070 <close@plt>
lea    0xd8c(%rip),%rdi
jmp    128e <init_client_ip+0x10e>
lea    0xd79(%rip),%rdi
call   1080 <perror@plt>
xor    %r12d,%r12d
jmp    1257 <init_client_ip+0xd7>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.1 = [10 x i8] c"keepalive\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @init_client_ip(i32* %0, i8* %1, i32 %2, i32 %3) {
%5 = alloca i32
%6 = alloca i32*
%7 = alloca i8*
%8 = alloca i32
%9 = alloca i32
%10 = alloca i64
%11 = alloca %struct.sockaddr_in
%12 = alloca i32
%13 = alloca i32
store i32* %0, i32** %6
store i8* %1, i8** %7
store i32 %2, i32* %8
store i32 %3, i32* %9
%14 = bitcast %struct.sockaddr_in* %11 to i8*
%15 = call i32 @memset(i8* %14, i32 0, i32 12)
%16 = load i32, i32* @AF_INET
%17 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 2
store i32 %16, i32* %17
%18 = load i32, i32* %8
%19 = trunc i32 %18 to i16
%20 = call i32 @htons(i16 zeroext %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 1
store i32 %20, i32* %21
%22 = load i8*, i8** %7
%23 = getelementptr inbounds i8, i8* %22, i64 0
%24 = load i8, i8* %23
%25 = zext i8 %24 to i64
%26 = shl i64 %25, 24
%27 = load i8*, i8** %7
%28 = getelementptr inbounds i8, i8* %27, i64 1
%29 = load i8, i8* %28
%30 = zext i8 %29 to i64
%31 = shl i64 %30, 16
%32 = or i64 %26, %31
%33 = load i8*, i8** %7
%34 = getelementptr inbounds i8, i8* %33, i64 2
%35 = load i8, i8* %34
%36 = zext i8 %35 to i64
%37 = shl i64 %36, 8
%38 = or i64 %32, %37
%39 = load i8*, i8** %7
%40 = getelementptr inbounds i8, i8* %39, i64 3
%41 = load i8, i8* %40
%42 = zext i8 %41 to i64
%43 = or i64 %38, %42
store i64 %43, i64* %10
%44 = load i64, i64* %10
%45 = call i32 @htonl(i64 %44)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %11, i32 0, i32 0
%47 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %46, i32 0, i32 0
store i32 %45, i32* %47
%48 = load i32, i32* @AF_INET
%49 = load i32, i32* @SOCK_STREAM
%50 = load i32, i32* @IPPROTO_TCP
%51 = call i32 @socket(i32 %48, i32 %49, i32 %50)
store i32 %51, i32* %12
%52 = load i32, i32* %12
%53 = icmp eq i32 %52, -1
br i1 %53, label %54, label %56
54: ; preds = %4
%55 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
store i32 0, i32* %5
br label %83
56: ; preds = %4
%57 = load i32, i32* %9
%58 = load i32, i32* @SOCK_STREAM
%59 = icmp eq i32 %57, %58
br i1 %59, label %60, label %71
60: ; preds = %56
store i32 0, i32* %13
%61 = load i32, i32* %12
%62 = load i32, i32* @SOL_SOCKET
%63 = load i32, i32* @SO_KEEPALIVE
%64 = bitcast i32* %13 to i8*
%65 = call i32 @setsockopt(i32 %61, i32 %62, i32 %63, i8* %64, i32 4)
store i32 %65, i32* %13
%66 = load i32, i32* %13
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %60
%69 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
store i32 0, i32* %5
br label %83
70: ; preds = %60
br label %71
71: ; preds = %70, %56
%72 = load i32, i32* %12
%73 = bitcast %struct.sockaddr_in* %11 to %struct.sockaddr*
%74 = call i32 @connect(i32 %72, %struct.sockaddr* %73, i32 12)
%75 = icmp eq i32 %74, -1
br i1 %75, label %76, label %80
76: ; preds = %71
%77 = load i32, i32* %12
%78 = call i32 @close(i32 %77)
%79 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
store i32 0, i32* %5
br label %83
80: ; preds = %71
%81 = load i32, i32* %12
%82 = load i32*, i32** %6
store i32 %81, i32* %82
store i32 1, i32* %5
br label %83
83: ; preds = %80, %76, %68, %54
%84 = load i32, i32* %5
ret i32 %84
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str = [7 x i8] c"socket\00"
@SOL_SOCKET = global i32 0
@SO_KEEPALIVE = global i32 0
@.str.1 = [10 x i8] c"keepalive\00"
@.str.2 = [8 x i8] c"connect\00"
define i32 @init_client_ip(i32* nocapture writeonly %0, i8* nocapture %1, i32 %2, i32 %3) {
%5 = alloca %struct.sockaddr_in
%6 = alloca i32
%7 = bitcast %struct.sockaddr_in* %5 to i8*
%8 = bitcast i32* %6 to i8*
%9 = call i32 @memset(i8* %7, i32 0, i32 12)
%10 = load i32, i32* @AF_INET
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 2
store i32 %10, i32* %11
%12 = trunc i32 %2 to i16
%13 = call i32 @htons(i16 zeroext %12)
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 1
store i32 %13, i32* %14
%15 = load i8, i8* %1
%16 = zext i8 %15 to i64
%17 = shl nuw nsw i64 %16, 24
%18 = getelementptr inbounds i8, i8* %1, i64 1
%19 = load i8, i8* %18
%20 = zext i8 %19 to i64
%21 = shl nuw nsw i64 %20, 16
%22 = or i64 %21, %17
%23 = getelementptr inbounds i8, i8* %1, i64 2
%24 = load i8, i8* %23
%25 = zext i8 %24 to i64
%26 = shl nuw nsw i64 %25, 8
%27 = or i64 %22, %26
%28 = getelementptr inbounds i8, i8* %1, i64 3
%29 = load i8, i8* %28
%30 = zext i8 %29 to i64
%31 = or i64 %27, %30
%32 = call i32 @htonl(i64 %31)
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i64 0, i32 0, i32 0
store i32 %32, i32* %33
%34 = load i32, i32* @AF_INET
%35 = load i32, i32* @SOCK_STREAM
%36 = load i32, i32* @IPPROTO_TCP
%37 = call i32 @socket(i32 %34, i32 %35, i32 %36)
%38 = icmp eq i32 %37, -1
br i1 %38, label %39, label %41
39: ; preds = %4
%40 = call i32 @perror(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str, i64 0, i64 0))
br label %59
41: ; preds = %4
%42 = load i32, i32* @SOCK_STREAM
%43 = icmp eq i32 %42, %3
br i1 %43, label %44, label %51
44: ; preds = %41
store i32 0, i32* %6
%45 = load i32, i32* @SOL_SOCKET
%46 = load i32, i32* @SO_KEEPALIVE
%47 = call i32 @setsockopt(i32 %37, i32 %45, i32 %46, i8* %8, i32 4)
store i32 %47, i32* %6
%48 = icmp slt i32 %47, 0
br i1 %48, label %49, label %51
49: ; preds = %44
%50 = call i32 @perror(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str.1, i64 0, i64 0))
br label %59
51: ; preds = %44, %41
%52 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%53 = call i32 @connect(i32 %37, %struct.sockaddr* %52, i32 12)
%54 = icmp eq i32 %53, -1
br i1 %54, label %55, label %58
55: ; preds = %51
%56 = call i32 @close(i32 %37)
%57 = call i32 @perror(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.2, i64 0, i64 0))
br label %59
58: ; preds = %51
store i32 %37, i32* %0
br label %59
59: ; preds = %58, %55, %49, %39
%60 = phi i32 [ 0, %39 ], [ 0, %49 ], [ 0, %55 ], [ 1, %58 ]
ret i32 %60
}