#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 timeval {
  int member_0;
  int /*<<< orphan*/ member_1;
};
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*/ in_addr_t;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ SO_SNDTIMEO;
int /*<<< orphan*/ close(int);
int connect(int, struct sockaddr *, int);
int /*<<< orphan*/ htons(int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int create_socket(in_addr_t dns_server) {
  int sock;
  struct sockaddr_in server;
  struct timeval timeout = {1, 0};
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock == -1) {
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
                 sizeof(struct timeval)) == -1) {
    close(sock);
    return -1;
  }
  if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout,
                 sizeof(struct timeval)) == -1) {
    close(sock);
    return -1;
  }
  server.sin_family = AF_INET;
  server.sin_port = htons(53);
  server.sin_addr.s_addr = dns_server;
  if (connect(sock, (struct sockaddr *)&server, sizeof(struct sockaddr)) ==
      -1) {
    close(sock);
    return -1;
  }
  return sock;
}


x86 assembly (clang, -O0):
<create_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
mov    0xe9e(%rip),%rax
mov    %rax,-0x20(%rbp)
mov    0x2e6b(%rip),%rax
mov    (%rax),%edi
mov    0x2e5a(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1198 <create_socket+0x48>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <create_socket+0x11d>
mov    -0xc(%rbp),%edi
mov    0x2e3e(%rip),%rax
mov    (%rax),%esi
mov    0x2e15(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11d9 <create_socket+0x89>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <create_socket+0x11d>
mov    -0xc(%rbp),%edi
mov    0x2dfd(%rip),%rax
mov    (%rax),%esi
mov    0x2e04(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    121a <create_socket+0xca>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <create_socket+0x11d>
mov    0x2db7(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x10(%rbp)
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,-0x14(%rbp)
mov    -0x8(%rbp),%eax
mov    %eax,-0x18(%rbp)
mov    -0xc(%rbp),%edi
lea    -0x18(%rbp),%rsi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
jne    1267 <create_socket+0x117>
mov    -0xc(%rbp),%edi
call   1050 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    126d <create_socket+0x11d>
mov    -0xc(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<create_socket>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x20,%rsp
mov    %edi,%r14d
movq   $0x1,0x8(%rsp)
mov    0x2e69(%rip),%r15
mov    (%r15),%edi
mov    0x2e57(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    $0xffffffff,%ebx
cmp    $0xffffffff,%eax
je     121c <create_socket+0xcc>
mov    %eax,%ebp
mov    0x2e47(%rip),%r12
mov    (%r12),%esi
mov    0x2e1c(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1215 <create_socket+0xc5>
mov    (%r12),%esi
mov    0x2e28(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebp,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1215 <create_socket+0xc5>
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    $0x35,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    %r14d,0x10(%rsp)
lea    0x10(%rsp),%rsi
mov    %ebp,%edi
mov    $0x4,%edx
call   1060 <connect@plt>
cmp    $0xffffffff,%eax
je     1215 <create_socket+0xc5>
mov    %ebp,%ebx
jmp    121c <create_socket+0xcc>
mov    %ebp,%edi
call   1050 <close@plt>
mov    %ebx,%eax
add    $0x20,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@__const.create_socket.timeout = %struct.timeval { i32 1, i32 0 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@SO_SNDTIMEO = global i32 0
define i32 @create_socket(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = alloca %struct.timeval
store i32 %0, i32* %3
%7 = bitcast %struct.timeval* %6 to i8*
%8 = load i32, i32* @AF_INET
%9 = load i32, i32* @SOCK_DGRAM
%10 = call i32 @socket(i32 %8, i32 %9, i32 0)
store i32 %10, i32* %4
%11 = load i32, i32* %4
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %14
13: ; preds = %1
store i32 -1, i32* %2
br label %49
14: ; preds = %1
%15 = load i32, i32* %4
%16 = load i32, i32* @SOL_SOCKET
%17 = load i32, i32* @SO_RCVTIMEO
%18 = call i32 @setsockopt(i32 %15, i32 %16, i32 %17, %struct.timeval* %6, i32 8)
%19 = icmp eq i32 %18, -1
br i1 %19, label %20, label %23
20: ; preds = %14
%21 = load i32, i32* %4
%22 = call i32 @close(i32 %21)
store i32 -1, i32* %2
br label %49
23: ; preds = %14
%24 = load i32, i32* %4
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_SNDTIMEO
%27 = call i32 @setsockopt(i32 %24, i32 %25, i32 %26, %struct.timeval* %6, i32 8)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %32
29: ; preds = %23
%30 = load i32, i32* %4
%31 = call i32 @close(i32 %30)
store i32 -1, i32* %2
br label %49
32: ; preds = %23
%33 = load i32, i32* @AF_INET
%34 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %33, i32* %34
%35 = call i32 @htons(i32 53)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %35, i32* %36
%37 = load i32, i32* %3
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%39 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %38, i32 0, i32 0
store i32 %37, i32* %39
%40 = load i32, i32* %4
%41 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%42 = call i32 @connect(i32 %40, %struct.sockaddr* %41, i32 4)
%43 = icmp eq i32 %42, -1
br i1 %43, label %44, label %47
44: ; preds = %32
%45 = load i32, i32* %4
%46 = call i32 @close(i32 %45)
store i32 -1, i32* %2
br label %49
47: ; preds = %32
%48 = load i32, i32* %4
store i32 %48, i32* %2
br label %49
49: ; preds = %47, %44, %29, %20, %13
%50 = load i32, i32* %2
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.timeval = type { i32, i32 }
%struct.sockaddr = type { i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@SO_SNDTIMEO = global i32 0
define i32 @create_socket(i32 %0) {
%2 = alloca %struct.sockaddr_in
%3 = alloca i64
%4 = bitcast i64* %3 to %struct.timeval*
%5 = bitcast %struct.sockaddr_in* %2 to i8*
%6 = bitcast i64* %3 to i8*
store i64 1, i64* %3
%7 = load i32, i32* @AF_INET
%8 = load i32, i32* @SOCK_DGRAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp eq i32 %9, -1
br i1 %10, label %32, label %11
11: ; preds = %1
%12 = load i32, i32* @SOL_SOCKET
%13 = load i32, i32* @SO_RCVTIMEO
%14 = call i32 @setsockopt(i32 %9, i32 %12, i32 %13, %struct.timeval* %4, i32 8)
%15 = icmp eq i32 %14, -1
br i1 %15, label %30, label %16
16: ; preds = %11
%17 = load i32, i32* @SOL_SOCKET
%18 = load i32, i32* @SO_SNDTIMEO
%19 = call i32 @setsockopt(i32 %9, i32 %17, i32 %18, %struct.timeval* %4, i32 8)
%20 = icmp eq i32 %19, -1
br i1 %20, label %30, label %21
21: ; preds = %16
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %22, i32* %23
%24 = call i32 @htons(i32 53)
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %24, i32* %25
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %0, i32* %26
%27 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%28 = call i32 @connect(i32 %9, %struct.sockaddr* %27, i32 4)
%29 = icmp eq i32 %28, -1
br i1 %29, label %30, label %32
30: ; preds = %21, %16, %11
%31 = call i32 @close(i32 %9)
br label %32
32: ; preds = %30, %21, %1
%33 = phi i32 [ -1, %1 ], [ %9, %21 ], [ -1, %30 ]
ret i32 %33
}