#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 */
typedef int /*<<< orphan*/ timeout;
struct timeval {
  int tv_sec;
  scalar_t__ tv_usec;
};
struct sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ serv_addr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ SO_SNDTIMEO;
scalar_t__ connect(int, struct sockaddr *, int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ htons(int);
scalar_t__ inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ strerror(int /*<<< orphan*/);
int /*<<< orphan*/ uiError(char *, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ uiMessage(int, char *, char *, int);
int sckCreate(int *sock, char *addr, int port) {
  int ret;
  struct sockaddr_in serv_addr;
  struct timeval timeout;
  ret = -1;
  uiMessage(4, "Creating socket %s:%d", addr, port);
  timeout.tv_sec = 3;
  timeout.tv_usec = 0;
  *sock = socket(AF_INET, SOCK_STREAM, 0);
  if (*sock != -1) {
    if (setsockopt(*sock, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
                   sizeof(timeout)) == -1)
      if (setsockopt(*sock, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
                     sizeof(timeout)) == -1)
        memset(&serv_addr, '0', sizeof(serv_addr));
    serv_addr.sin_family = AF_INET;
    serv_addr.sin_port = htons(port);
    if (inet_pton(AF_INET, addr, &serv_addr.sin_addr) > 0)
      if (connect(*sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) == 0)
        ret = 0;
      else
        uiError("Error connecting socket", errno, strerror(errno));
    else
      uiError("Error setting IP address", errno, strerror(errno));
  } else
    uiError("Error creating socket or setting timeout", errno, strerror(errno));
  return ret;
}


x86 assembly (clang, -O0):
<sckCreate>:
push   %rbp
mov    %rsp,%rbp
sub    $0x50,%rsp
mov    %rdi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %edx,-0x14(%rbp)
movl   $0xffffffff,-0x18(%rbp)
mov    -0x10(%rbp),%rdx
mov    -0x14(%rbp),%ecx
mov    $0x4,%edi
lea    0xe43(%rip),%rsi
call   1070 <uiMessage@plt>
movl   $0x3,-0x38(%rbp)
movq   $0x0,-0x30(%rbp)
mov    0x2e00(%rip),%rax
mov    (%rax),%edi
mov    0x2ddf(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,%ecx
mov    -0x8(%rbp),%rax
mov    %ecx,(%rax)
mov    -0x8(%rbp),%rax
cmpl   $0xffffffff,(%rax)
je     1338 <sckCreate+0x1a8>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2dd4(%rip),%rax
mov    (%rax),%esi
mov    0x2db3(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1277 <sckCreate+0xe7>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
mov    0x2da4(%rip),%rax
mov    (%rax),%esi
mov    0x2dab(%rip),%rax
mov    (%rax),%edx
lea    -0x38(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1272 <sckCreate+0xe2>
lea    -0x28(%rbp),%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
jmp    1277 <sckCreate+0xe7>
mov    0x2d5a(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    -0x14(%rbp),%edi
call   1050 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    0x2d43(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rsi
lea    -0x28(%rbp),%rdx
call   1080 <inet_pton@plt>
cmp    $0x0,%rax
jle    1308 <sckCreate+0x178>
mov    -0x8(%rbp),%rax
mov    (%rax),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
cmp    $0x0,%rax
jne    12d8 <sckCreate+0x148>
movl   $0x0,-0x18(%rbp)
jmp    1303 <sckCreate+0x173>
mov    0x2cd9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x3c(%rbp)
mov    0x2ccd(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x3c(%rbp),%esi
mov    %eax,%edx
lea    0xd18(%rip),%rdi
call   1040 <uiError@plt>
jmp    1333 <sckCreate+0x1a3>
mov    0x2ca9(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x40(%rbp)
mov    0x2c9d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x40(%rbp),%esi
mov    %eax,%edx
lea    0xd00(%rip),%rdi
call   1040 <uiError@plt>
jmp    1363 <sckCreate+0x1d3>
mov    0x2c79(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x44(%rbp)
mov    0x2c6d(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    -0x44(%rbp),%esi
mov    %eax,%edx
lea    0xce9(%rip),%rdi
call   1040 <uiError@plt>
mov    -0x18(%rbp),%eax
add    $0x50,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sckCreate>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edx,%ebp
mov    %rsi,%rbx
mov    %rdi,%r14
lea    0xe55(%rip),%rsi
mov    $0x4,%edi
mov    %rbx,%rdx
mov    %ebp,%ecx
call   1070 <uiMessage@plt>
movl   $0x3,0x10(%rsp)
movq   $0x0,0x18(%rsp)
mov    0x2e06(%rip),%r15
mov    (%r15),%edi
mov    0x2de4(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,(%r14)
cmp    $0xffffffff,%eax
je     12a6 <sckCreate+0x116>
mov    0x2de8(%rip),%r12
mov    (%r12),%esi
mov    0x2dc5(%rip),%rcx
mov    (%rcx),%edx
lea    0x10(%rsp),%rcx
mov    %eax,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1253 <sckCreate+0xc3>
mov    (%r14),%edi
mov    (%r12),%esi
mov    0x2dc6(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1253 <sckCreate+0xc3>
mov    %rsp,%rdi
mov    $0x30,%esi
mov    $0xc,%edx
call   1060 <memset@plt>
mov    (%r15),%eax
mov    %eax,0x8(%rsp)
mov    %ebp,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
mov    (%r15),%edi
mov    %rsp,%rdx
mov    %rbx,%rsi
call   1080 <inet_pton@plt>
test   %rax,%rax
jle    12bf <sckCreate+0x12f>
mov    (%r14),%edi
mov    %rsp,%rsi
mov    $0xc,%edx
call   1090 <connect@plt>
test   %rax,%rax
je     12f1 <sckCreate+0x161>
mov    0x2d24(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xd72(%rip),%rdi
jmp    12d6 <sckCreate+0x146>
mov    0x2d0b(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xd8a(%rip),%rdi
jmp    12d6 <sckCreate+0x146>
mov    0x2cf2(%rip),%rax
mov    (%rax),%ebx
mov    %ebx,%edi
call   10a0 <strerror@plt>
lea    0xd58(%rip),%rdi
mov    %ebx,%esi
mov    %eax,%edx
call   1040 <uiError@plt>
mov    $0xffffffff,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
xor    %eax,%eax
jmp    12e4 <sckCreate+0x154>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Creating socket %s:%d\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@SO_SNDTIMEO = global i32 0
@.str.1 = [24 x i8] c"Error connecting socket\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Error setting IP address\00"
@.str.3 = [41 x i8] c"Error creating socket or setting timeout\00"
define i32 @sckCreate(i32* %0, i8* %1, i32 %2) {
%4 = alloca i32*
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca %struct.timeval
store i32* %0, i32** %4
store i8* %1, i8** %5
store i32 %2, i32* %6
store i32 -1, i32* %7
%10 = load i8*, i8** %5
%11 = load i32, i32* %6
%12 = call i32 @uiMessage(i32 4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %10, i32 %11)
%13 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 0
store i32 3, i32* %13
%14 = getelementptr inbounds %struct.timeval, %struct.timeval* %9, i32 0, i32 1
store i64 0, i64* %14
%15 = load i32, i32* @AF_INET
%16 = load i32, i32* @SOCK_STREAM
%17 = call i32 @socket(i32 %15, i32 %16, i32 0)
%18 = load i32*, i32** %4
store i32 %17, i32* %18
%19 = load i32*, i32** %4
%20 = load i32, i32* %19
%21 = icmp ne i32 %20, -1
br i1 %21, label %22, label %71
22: ; preds = %3
%23 = load i32*, i32** %4
%24 = load i32, i32* %23
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_RCVTIMEO
%27 = bitcast %struct.timeval* %9 to i8*
%28 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, i8* %27, i32 16)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %41
30: ; preds = %22
%31 = load i32*, i32** %4
%32 = load i32, i32* %31
%33 = load i32, i32* @SOL_SOCKET
%34 = load i32, i32* @SO_SNDTIMEO
%35 = bitcast %struct.timeval* %9 to i8*
%36 = call i32 @setsockopt(i32 %32, i32 %33, i32 %34, i8* %35, i32 16)
%37 = icmp eq i32 %36, -1
br i1 %37, label %38, label %40
38: ; preds = %30
%39 = call i32 @memset(%struct.sockaddr_in* %8, i32 48, i32 12)
br label %40
40: ; preds = %38, %30
br label %41
41: ; preds = %40, %22
%42 = load i32, i32* @AF_INET
%43 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %42, i32* %43
%44 = load i32, i32* %6
%45 = call i32 @htons(i32 %44)
%46 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %45, i32* %46
%47 = load i32, i32* @AF_INET
%48 = load i8*, i8** %5
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%50 = call i64 @inet_pton(i32 %47, i8* %48, i32* %49)
%51 = icmp sgt i64 %50, 0
br i1 %51, label %52, label %65
52: ; preds = %41
%53 = load i32*, i32** %4
%54 = load i32, i32* %53
%55 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%56 = call i64 @connect(i32 %54, %struct.sockaddr* %55, i32 12)
%57 = icmp eq i64 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %52
store i32 0, i32* %7
br label %64
59: ; preds = %52
%60 = load i32, i32* @errno
%61 = load i32, i32* @errno
%62 = call i32 @strerror(i32 %61)
%63 = call i32 @uiError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %60, i32 %62)
br label %64
64: ; preds = %59, %58
br label %70
65: ; preds = %41
%66 = load i32, i32* @errno
%67 = load i32, i32* @errno
%68 = call i32 @strerror(i32 %67)
%69 = call i32 @uiError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %66, i32 %68)
br label %70
70: ; preds = %65, %64
br label %76
71: ; preds = %3
%72 = load i32, i32* @errno
%73 = load i32, i32* @errno
%74 = call i32 @strerror(i32 %73)
%75 = call i32 @uiError(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %72, i32 %74)
br label %76
76: ; preds = %71, %70
%77 = load i32, i32* %7
ret i32 %77
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.timeval = type { i32, i64 }
%struct.sockaddr = type { i32 }
@.str = [22 x i8] c"Creating socket %s:%d\00"
@AF_INET = global i32 0
@SOCK_STREAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@SO_SNDTIMEO = global i32 0
@.str.1 = [24 x i8] c"Error connecting socket\00"
@errno = global i32 0
@.str.2 = [25 x i8] c"Error setting IP address\00"
@.str.3 = [41 x i8] c"Error creating socket or setting timeout\00"
define i32 @sckCreate(i32* nocapture %0, i8* %1, i32 %2) {
%4 = alloca %struct.sockaddr_in
%5 = alloca %struct.timeval
%6 = bitcast %struct.sockaddr_in* %4 to i8*
%7 = bitcast %struct.timeval* %5 to i8*
%8 = tail call i32 @uiMessage(i32 4, i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str, i64 0, i64 0), i8* %1, i32 %2)
%9 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 0
store i32 3, i32* %9
%10 = getelementptr inbounds %struct.timeval, %struct.timeval* %5, i64 0, i32 1
store i64 0, i64* %10
%11 = load i32, i32* @AF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = tail call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %0
%14 = icmp eq i32 %13, -1
br i1 %14, label %50, label %15
15: ; preds = %3
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_RCVTIMEO
%18 = call i32 @setsockopt(i32 %13, i32 %16, i32 %17, i8* %7, i32 16)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %28
20: ; preds = %15
%21 = load i32, i32* %0
%22 = load i32, i32* @SOL_SOCKET
%23 = load i32, i32* @SO_SNDTIMEO
%24 = call i32 @setsockopt(i32 %21, i32 %22, i32 %23, i8* %7, i32 16)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %28
26: ; preds = %20
%27 = call i32 @memset(%struct.sockaddr_in* %4, i32 48, i32 12)
br label %28
28: ; preds = %20, %26, %15
%29 = load i32, i32* @AF_INET
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 2
store i32 %29, i32* %30
%31 = call i32 @htons(i32 %2)
%32 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 1
store i32 %31, i32* %32
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0
%35 = call i64 @inet_pton(i32 %33, i8* %1, i32* %34)
%36 = icmp sgt i64 %35, 0
br i1 %36, label %37, label %46
37: ; preds = %28
%38 = load i32, i32* %0
%39 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%40 = call i64 @connect(i32 %38, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i64 %40, 0
br i1 %41, label %54, label %42
42: ; preds = %37
%43 = load i32, i32* @errno
%44 = call i32 @strerror(i32 %43)
%45 = call i32 @uiError(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0), i32 %43, i32 %44)
br label %54
46: ; preds = %28
%47 = load i32, i32* @errno
%48 = call i32 @strerror(i32 %47)
%49 = call i32 @uiError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %47, i32 %48)
br label %54
50: ; preds = %3
%51 = load i32, i32* @errno
%52 = tail call i32 @strerror(i32 %51)
%53 = tail call i32 @uiError(i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.3, i64 0, i64 0), i32 %51, i32 %52)
br label %54
54: ; preds = %37, %46, %42, %50
%55 = phi i32 [ -1, %42 ], [ -1, %46 ], [ -1, %50 ], [ 0, %37 ]
ret i32 %55
}