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

/* 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 setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int sntp_socket(void) {
  int sock;
  sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (sock >= 0) {
    struct timeval timeout = {1, 0};
    if (setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout,
                   sizeof(struct timeval)) == -1) {
      close(sock);
      sock = -1;
    } else if (setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &timeout,
                          sizeof(struct timeval)) == -1) {
      close(sock);
      sock = -1;
    }
  }
  return sock;
}


x86 assembly (clang, -O0):
<sntp_socket>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    0x2e99(%rip),%rax
mov    (%rax),%edi
mov    0x2e88(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,-0x4(%rbp)
cmpl   $0x0,-0x4(%rbp)
jl     11f0 <sntp_socket+0xc0>
mov    0xe9b(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x4(%rbp),%edi
mov    0x2e6d(%rip),%rax
mov    (%rax),%esi
mov    0x2e44(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11aa <sntp_socket+0x7a>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <sntp_socket+0xbb>
mov    -0x4(%rbp),%edi
mov    0x2e2c(%rip),%rax
mov    (%rax),%esi
mov    0x2e33(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11e6 <sntp_socket+0xb6>
mov    -0x4(%rbp),%edi
call   1040 <close@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11eb <sntp_socket+0xbb>
jmp    11f0 <sntp_socket+0xc0>
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<sntp_socket>:
push   %r14
push   %rbx
push   %rax
mov    0x2e9d(%rip),%rax
mov    (%rax),%edi
mov    0x2e8c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1050 <socket@plt>
mov    %eax,%ebx
test   %eax,%eax
js     11b0 <sntp_socket+0x80>
movq   $0x1,(%rsp)
mov    0x2e7e(%rip),%r14
mov    (%r14),%esi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     11a4 <sntp_socket+0x74>
mov    (%r14),%esi
mov    0x2e63(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11b0 <sntp_socket+0x80>
mov    %ebx,%edi
call   1040 <close@plt>
mov    $0xffffffff,%ebx
mov    %ebx,%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i32 }
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@__const.sntp_socket.timeout = %struct.timeval { i32 1, i32 0 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@SO_SNDTIMEO = global i32 0
define i32 @sntp_socket() {
%1 = alloca i32
%2 = alloca %struct.timeval
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = call i32 @socket(i32 %3, i32 %4, i32 0)
store i32 %5, i32* %1
%6 = load i32, i32* %1
%7 = icmp sge i32 %6, 0
br i1 %7, label %8, label %29
8: ; preds = %0
%9 = bitcast %struct.timeval* %2 to i8*
%10 = load i32, i32* %1
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_RCVTIMEO
%13 = call i32 @setsockopt(i32 %10, i32 %11, i32 %12, %struct.timeval* %2, i32 8)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %18
15: ; preds = %8
%16 = load i32, i32* %1
%17 = call i32 @close(i32 %16)
store i32 -1, i32* %1
br label %28
18: ; preds = %8
%19 = load i32, i32* %1
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_SNDTIMEO
%22 = call i32 @setsockopt(i32 %19, i32 %20, i32 %21, %struct.timeval* %2, i32 8)
%23 = icmp eq i32 %22, -1
br i1 %23, label %24, label %27
24: ; preds = %18
%25 = load i32, i32* %1
%26 = call i32 @close(i32 %25)
store i32 -1, i32* %1
br label %27
27: ; preds = %24, %18
br label %28
28: ; preds = %27, %15
br label %29
29: ; preds = %28, %0
%30 = load i32, i32* %1
ret i32 %30
}

LLVM IR (-O2):
%struct.timeval = type { i32, 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 @sntp_socket() {
%1 = alloca i64
%2 = bitcast i64* %1 to %struct.timeval*
%3 = load i32, i32* @AF_INET
%4 = load i32, i32* @SOCK_DGRAM
%5 = tail call i32 @socket(i32 %3, i32 %4, i32 0)
%6 = icmp sgt i32 %5, -1
br i1 %6, label %7, label %22
7: ; preds = %0
%8 = bitcast i64* %1 to i8*
store i64 1, i64* %1
%9 = load i32, i32* @SOL_SOCKET
%10 = load i32, i32* @SO_RCVTIMEO
%11 = call i32 @setsockopt(i32 %5, i32 %9, i32 %10, %struct.timeval* %2, i32 8)
%12 = icmp eq i32 %11, -1
br i1 %12, label %18, label %13
13: ; preds = %7
%14 = load i32, i32* @SOL_SOCKET
%15 = load i32, i32* @SO_SNDTIMEO
%16 = call i32 @setsockopt(i32 %5, i32 %14, i32 %15, %struct.timeval* %2, i32 8)
%17 = icmp eq i32 %16, -1
br i1 %17, label %18, label %20
18: ; preds = %13, %7
%19 = call i32 @close(i32 %5)
br label %20
20: ; preds = %18, %13
%21 = phi i32 [ %5, %13 ], [ -1, %18 ]
br label %22
22: ; preds = %20, %0
%23 = phi i32 [ %21, %20 ], [ %5, %0 ]
ret i32 %23
}