#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;
};

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ SO_SNDTIMEO;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
scalar_t__ setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ stderr;
int set_timeout(int socketfd) {
  struct timeval timeout;
  timeout.tv_sec = 10;
  timeout.tv_usec = 0;
  if (setsockopt(socketfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
                 sizeof(timeout)) < 0) {
    fprintf(stderr, "setsockopt failed SO_RCVTIMEO\n");
    return -1;
  }
  if (setsockopt(socketfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
                 sizeof(timeout)) < 0) {
    fprintf(stderr, "setsockopt failed SO_SNDTIMEO\n");
    return -1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0xa,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2e9c(%rip),%rax
mov    (%rax),%esi
mov    0x2e83(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    1189 <set_timeout+0x69>
mov    0x2e59(%rip),%rax
mov    (%rax),%edi
lea    0xe88(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <set_timeout+0xbf>
mov    -0x8(%rbp),%edi
mov    0x2e4d(%rip),%rax
mov    (%rax),%esi
mov    0x2e54(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
cmp    $0x0,%rax
jge    11d8 <set_timeout+0xb8>
mov    0x2e0a(%rip),%rax
mov    (%rax),%edi
lea    0xe58(%rip),%rsi
call   1040 <fprintf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    11df <set_timeout+0xbf>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_timeout>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    %edi,%ebx
movl   $0xa,0x8(%rsp)
movq   $0x0,0x10(%rsp)
mov    0x2e9f(%rip),%r14
mov    (%r14),%esi
mov    0x2e85(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
test   %rax,%rax
js     1193 <set_timeout+0x73>
mov    (%r14),%esi
mov    0x2e84(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    %ebx,%edi
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
jns    11ad <set_timeout+0x8d>
lea    0xe8e(%rip),%rsi
jmp    119a <set_timeout+0x7a>
lea    0xe66(%rip),%rsi
mov    0x2e27(%rip),%rax
mov    (%rax),%edi
call   1040 <fprintf@plt>
mov    $0xffffffff,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"setsockopt failed SO_RCVTIMEO\0A\00"
@SO_SNDTIMEO = global i32 0
@.str.1 = [31 x i8] c"setsockopt failed SO_SNDTIMEO\0A\00"
define i32 @set_timeout(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca %struct.timeval
store i32 %0, i32* %3
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 0
store i32 10, i32* %5
%6 = getelementptr inbounds %struct.timeval, %struct.timeval* %4, i32 0, i32 1
store i64 0, i64* %6
%7 = load i32, i32* %3
%8 = load i32, i32* @SOL_SOCKET
%9 = load i32, i32* @SO_RCVTIMEO
%10 = bitcast %struct.timeval* %4 to i8*
%11 = call i64 @setsockopt(i32 %7, i32 %8, i32 %9, i8* %10, i32 16)
%12 = icmp slt i64 %11, 0
br i1 %12, label %13, label %16
13: ; preds = %1
%14 = load i32, i32* @stderr
%15 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
16: ; preds = %1
%17 = load i32, i32* %3
%18 = load i32, i32* @SOL_SOCKET
%19 = load i32, i32* @SO_SNDTIMEO
%20 = bitcast %struct.timeval* %4 to i8*
%21 = call i64 @setsockopt(i32 %17, i32 %18, i32 %19, i8* %20, i32 16)
%22 = icmp slt i64 %21, 0
br i1 %22, label %23, label %26
23: ; preds = %16
%24 = load i32, i32* @stderr
%25 = call i32 @fprintf(i32 %24, i8* getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0))
store i32 -1, i32* %2
br label %27
26: ; preds = %16
store i32 0, i32* %2
br label %27
27: ; preds = %26, %23, %13
%28 = load i32, i32* %2
ret i32 %28
}

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@stderr = global i32 0
@.str = [31 x i8] c"setsockopt failed SO_RCVTIMEO\0A\00"
@SO_SNDTIMEO = global i32 0
@.str.1 = [31 x i8] c"setsockopt failed SO_SNDTIMEO\0A\00"
define i32 @set_timeout(i32 %0) {
%2 = alloca %struct.timeval
%3 = bitcast %struct.timeval* %2 to i8*
%4 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 0
store i32 10, i32* %4
%5 = getelementptr inbounds %struct.timeval, %struct.timeval* %2, i64 0, i32 1
store i64 0, i64* %5
%6 = load i32, i32* @SOL_SOCKET
%7 = load i32, i32* @SO_RCVTIMEO
%8 = call i64 @setsockopt(i32 %0, i32 %6, i32 %7, i8* %3, i32 16)
%9 = icmp slt i64 %8, 0
br i1 %9, label %15, label %10
10: ; preds = %1
%11 = load i32, i32* @SOL_SOCKET
%12 = load i32, i32* @SO_SNDTIMEO
%13 = call i64 @setsockopt(i32 %0, i32 %11, i32 %12, i8* %3, i32 16)
%14 = icmp slt i64 %13, 0
br i1 %14, label %15, label %19
15: ; preds = %10, %1
%16 = phi i8* [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str, i64 0, i64 0), %1 ], [ getelementptr inbounds ([31 x i8], [31 x i8]* @.str.1, i64 0, i64 0), %10 ]
%17 = load i32, i32* @stderr
%18 = call i32 @fprintf(i32 %17, i8* %16)
br label %19
19: ; preds = %15, %10
%20 = phi i32 [ 0, %10 ], [ -1, %15 ]
ret i32 %20
}