#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*/ perror(char *);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, struct timeval *,
               int);
int set_socket_timeout(int sock) {
  struct timeval timeout;
  timeout.tv_sec = (20);
  timeout.tv_usec = 0;
  int ret =
      setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout));
  if (ret != 0) {
    perror("setsockopt(SO_RCVTIMEO)");
    return ret;
  }
  return 0;
}


x86 assembly (clang, -O0):
<set_socket_timeout>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x14,-0x18(%rbp)
movq   $0x0,-0x10(%rbp)
mov    -0x8(%rbp),%edi
mov    0x2ea4(%rip),%rax
mov    (%rax),%esi
mov    0x2e8b(%rip),%rax
mov    (%rax),%edx
lea    -0x18(%rbp),%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
je     1182 <set_socket_timeout+0x62>
lea    0xe8e(%rip),%rdi
call   1040 <perror@plt>
mov    -0x1c(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    1189 <set_socket_timeout+0x69>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<set_socket_timeout>:
push   %rbx
sub    $0x10,%rsp
movl   $0x14,(%rsp)
movq   $0x0,0x8(%rsp)
mov    0x2eac(%rip),%rax
mov    (%rax),%esi
mov    0x2e93(%rip),%rax
mov    (%rax),%edx
mov    %rsp,%rcx
mov    $0x10,%r8d
call   1030 <setsockopt@plt>
mov    %eax,%ebx
test   %eax,%eax
jne    1163 <set_socket_timeout+0x43>
mov    %ebx,%eax
add    $0x10,%rsp
pop    %rbx
ret
lea    0xe96(%rip),%rdi
call   1040 <perror@plt>
jmp    115b <set_socket_timeout+0x3b>

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

LLVM IR (-O2):
%struct.timeval = type { i32, i64 }
@SOL_SOCKET = global i32 0
@SO_RCVTIMEO = global i32 0
@.str = [24 x i8] c"setsockopt(SO_RCVTIMEO)\00"
define i32 @set_socket_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 20, 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 i32 @setsockopt(i32 %0, i32 %6, i32 %7, %struct.timeval* %2, i32 16)
%9 = icmp eq i32 %8, 0
br i1 %9, label %12, label %10
10: ; preds = %1
%11 = call i32 @perror(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
br label %12
12: ; preds = %1, %10
ret i32 %8
}