#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_usec;
  int /*<<< orphan*/ tv_sec;
};

/* Variables and functions */
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_RCVTIMEO;
int /*<<< orphan*/ SO_SNDTIMEO;
int /*<<< orphan*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, char *, int);
int _set_socket_timeout(int sfd) {
  struct timeval timeout = {
      .tv_sec = 0,
      .tv_usec = (500) * 1000,
  };
  if (setsockopt(sfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout,
                 sizeof(timeout)) == -1) {
    perror("setsockopt failed\n");
    return 1;
  }
  if (setsockopt(sfd, SOL_SOCKET, SO_SNDTIMEO, (char *)&timeout,
                 sizeof(timeout)) == -1) {
    perror("setsockopt failed\n");
    return 1;
  }
  return 0;
}


x86 assembly (clang, -O0):
<_set_socket_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
mov    %edi,-0x8(%rbp)
mov    0xece(%rip),%rax
mov    %rax,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea0(%rip),%rax
mov    (%rax),%esi
mov    0x2e87(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    117b <_set_socket_timeout+0x5b>
lea    0xe9e(%rip),%rdi
call   1040 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c7 <_set_socket_timeout+0xa7>
mov    -0x8(%rbp),%edi
mov    0x2e5b(%rip),%rax
mov    (%rax),%esi
mov    0x2e62(%rip),%rax
mov    (%rax),%edx
lea    -0x10(%rbp),%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    11c0 <_set_socket_timeout+0xa0>
lea    0xe59(%rip),%rdi
call   1040 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    11c7 <_set_socket_timeout+0xa7>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<_set_socket_timeout>:
push   %r14
push   %rbx
push   %rax
mov    %edi,%ebx
movq   $0x7a120,(%rsp)
mov    0x2eab(%rip),%r14
mov    (%r14),%esi
mov    0x2e91(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
je     1181 <_set_socket_timeout+0x61>
mov    (%r14),%esi
mov    0x2e92(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    %ebx,%edi
mov    $0x8,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ecx
xor    %eax,%eax
cmp    $0xffffffff,%ecx
je     1181 <_set_socket_timeout+0x61>
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
lea    0xe78(%rip),%rdi
call   1040 <perror@plt>
mov    $0x1,%eax
jmp    1179 <_set_socket_timeout+0x59>

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

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