#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 socklen_t;
typedef int /*<<< orphan*/ so_type;
typedef int /*<<< orphan*/ so_flags;

/* Variables and functions */
int /*<<< orphan*/ PF_INET;
int SOCK_STREAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SOL_TCP;
int /*<<< orphan*/ SO_TYPE;
int /*<<< orphan*/ TCP_NODELAY;
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *);
int getsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, void *, int *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int socket(int /*<<< orphan*/, int, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  int ret;
  socklen_t optlen;
  int fd = socket(PF_INET, SOCK_STREAM, 0);
  if (fd < 0) {
    perror("socket failed");
    return 1;
  }
  int so_type;
  optlen = sizeof(so_type);
  ret = getsockopt(fd, SOL_SOCKET, SO_TYPE, &so_type, &optlen);
  if (ret < 0) {
    perror("getsockopt(SOL_SOCKET, SO_TYPE) failed");
    return 1;
  }
  if (optlen != sizeof(so_type) || so_type != SOCK_STREAM) {
    fprintf(stderr, "getsockopt(SOL_SOCKET, SO_TYPE) failed\n");
    return 1;
  }
  printf("getsockopt: Got socket type OK\n");
  int so_flags = 1;
  optlen = sizeof(so_flags);
  ret = getsockopt(fd, SOL_TCP, TCP_NODELAY, (void *)&so_flags, &optlen);
  if (ret < 0) {
    perror("getsockopt(SOL_TCP, TCP_NODELAY) failed");
    return 1;
  }
  if (optlen != sizeof(so_flags) || (so_flags != 0 && so_flags != 1)) {
    fprintf(stderr, "getsockopt(SOL_TCP, TCP_NODELAY) failed\n");
    return 1;
  }
  printf("getsockopt: Got TCP_NODELAY flag OK\n");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    0x2e43(%rip),%rax
mov    (%rax),%edi
mov    0x2e42(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   1070 <socket@plt>
mov    %eax,-0x1c(%rbp)
cmpl   $0x0,-0x1c(%rbp)
jge    11a4 <main+0x54>
lea    0xe6d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ea <main+0x19a>
movl   $0x4,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2e33(%rip),%rax
mov    (%rax),%esi
mov    0x2e1a(%rip),%rax
mov    (%rax),%edx
lea    -0x20(%rbp),%rcx
lea    -0x18(%rbp),%r8
call   1040 <getsockopt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    11f2 <main+0xa2>
lea    0xe2d(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ea <main+0x19a>
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
jne    1212 <main+0xc2>
mov    -0x20(%rbp),%eax
mov    0x2dae(%rip),%rcx
cmp    (%rcx),%eax
je     1233 <main+0xe3>
mov    0x2da7(%rip),%rax
mov    (%rax),%edi
lea    0xe13(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ea <main+0x19a>
lea    0xe23(%rip),%rdi
call   1030 <printf@plt>
movl   $0x1,-0x24(%rbp)
movl   $0x4,-0x18(%rbp)
mov    -0x1c(%rbp),%edi
mov    0x2d79(%rip),%rax
mov    (%rax),%esi
mov    0x2d80(%rip),%rax
mov    (%rax),%edx
lea    -0x24(%rbp),%rcx
lea    -0x18(%rbp),%r8
call   1040 <getsockopt@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0x0,-0x14(%rbp)
jge    1294 <main+0x144>
lea    0xdfa(%rip),%rdi
call   1060 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ea <main+0x19a>
movslq -0x18(%rbp),%rax
cmp    $0x4,%rax
jne    12b6 <main+0x166>
cmpl   $0x0,-0x24(%rbp)
je     12d7 <main+0x187>
cmpl   $0x1,-0x24(%rbp)
je     12d7 <main+0x187>
mov    0x2d03(%rip),%rax
mov    (%rax),%edi
lea    0xddf(%rip),%rsi
call   1050 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    12ea <main+0x19a>
lea    0xdf0(%rip),%rdi
call   1030 <printf@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
sub    $0x18,%rsp
mov    0x2e52(%rip),%rax
mov    (%rax),%edi
mov    0x2e51(%rip),%r14
mov    (%r14),%esi
xor    %edx,%edx
call   1070 <socket@plt>
test   %eax,%eax
js     1253 <main+0x103>
mov    %eax,%ebx
movl   $0x4,0xc(%rsp)
mov    0x2e5e(%rip),%rax
mov    (%rax),%esi
mov    0x2e45(%rip),%rax
mov    (%rax),%edx
lea    0x14(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %ebx,%edi
call   1040 <getsockopt@plt>
test   %eax,%eax
js     125c <main+0x10c>
cmpl   $0x4,0xc(%rsp)
jne    121f <main+0xcf>
mov    0x14(%rsp),%eax
cmp    (%r14),%eax
jne    121f <main+0xcf>
lea    0xee9(%rip),%rdi
call   1030 <puts@plt>
movl   $0x1,0x10(%rsp)
movl   $0x4,0xc(%rsp)
mov    0x2def(%rip),%rax
mov    (%rax),%esi
mov    0x2df6(%rip),%rax
mov    (%rax),%edx
lea    0x10(%rsp),%rcx
lea    0xc(%rsp),%r8
mov    %ebx,%edi
call   1040 <getsockopt@plt>
test   %eax,%eax
js     1265 <main+0x115>
cmpl   $0x4,0xc(%rsp)
jne    1241 <main+0xf1>
cmpl   $0x2,0x10(%rsp)
jae    1241 <main+0xf1>
lea    0xeb7(%rip),%rdi
call   1030 <puts@plt>
xor    %eax,%eax
jmp    1239 <main+0xe9>
mov    0x2d9a(%rip),%rax
mov    (%rax),%edi
lea    0xe06(%rip),%rsi
call   1050 <fprintf@plt>
mov    $0x1,%eax
add    $0x18,%rsp
pop    %rbx
pop    %r14
ret
mov    0x2d78(%rip),%rax
mov    (%rax),%edi
lea    0xe34(%rip),%rsi
jmp    122f <main+0xdf>
lea    0xda6(%rip),%rdi
jmp    126c <main+0x11c>
lea    0xdab(%rip),%rdi
jmp    126c <main+0x11c>
lea    0xdf1(%rip),%rdi
call   1060 <perror@plt>
jmp    1234 <main+0xe4>

LLVM IR (-O0):
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"socket failed\00"
@SOL_SOCKET = global i32 0
@SO_TYPE = global i32 0
@.str.1 = [39 x i8] c"getsockopt(SOL_SOCKET, SO_TYPE) failed\00"
@stderr = global i32 0
@.str.2 = [40 x i8] c"getsockopt(SOL_SOCKET, SO_TYPE) failed\0A\00"
@.str.3 = [32 x i8] c"getsockopt: Got socket type OK\0A\00"
@SOL_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.4 = [40 x i8] c"getsockopt(SOL_TCP, TCP_NODELAY) failed\00"
@.str.5 = [41 x i8] c"getsockopt(SOL_TCP, TCP_NODELAY) failed\0A\00"
@.str.6 = [37 x i8] c"getsockopt: Got TCP_NODELAY flag OK\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i32
%8 = alloca i32
%9 = alloca i32
%10 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* @PF_INET
%12 = load i32, i32* @SOCK_STREAM
%13 = call i32 @socket(i32 %11, i32 %12, i32 0)
store i32 %13, i32* %8
%14 = load i32, i32* %8
%15 = icmp slt i32 %14, 0
br i1 %15, label %16, label %18
16: ; preds = %2
%17 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
store i32 1, i32* %3
br label %65
18: ; preds = %2
store i32 4, i32* %7
%19 = load i32, i32* %8
%20 = load i32, i32* @SOL_SOCKET
%21 = load i32, i32* @SO_TYPE
%22 = bitcast i32* %9 to i8*
%23 = call i32 @getsockopt(i32 %19, i32 %20, i32 %21, i8* %22, i32* %7)
store i32 %23, i32* %6
%24 = load i32, i32* %6
%25 = icmp slt i32 %24, 0
br i1 %25, label %26, label %28
26: ; preds = %18
%27 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %65
28: ; preds = %18
%29 = load i32, i32* %7
%30 = sext i32 %29 to i64
%31 = icmp ne i64 %30, 4
br i1 %31, label %36, label %32
32: ; preds = %28
%33 = load i32, i32* %9
%34 = load i32, i32* @SOCK_STREAM
%35 = icmp ne i32 %33, %34
br i1 %35, label %36, label %39
36: ; preds = %32, %28
%37 = load i32, i32* @stderr
%38 = call i32 @fprintf(i32 %37, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %65
39: ; preds = %32
%40 = call i32 @printf(i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %10
store i32 4, i32* %7
%41 = load i32, i32* %8
%42 = load i32, i32* @SOL_TCP
%43 = load i32, i32* @TCP_NODELAY
%44 = bitcast i32* %10 to i8*
%45 = call i32 @getsockopt(i32 %41, i32 %42, i32 %43, i8* %44, i32* %7)
store i32 %45, i32* %6
%46 = load i32, i32* %6
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %39
%49 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
store i32 1, i32* %3
br label %65
50: ; preds = %39
%51 = load i32, i32* %7
%52 = sext i32 %51 to i64
%53 = icmp ne i64 %52, 4
br i1 %53, label %60, label %54
54: ; preds = %50
%55 = load i32, i32* %10
%56 = icmp ne i32 %55, 0
br i1 %56, label %57, label %63
57: ; preds = %54
%58 = load i32, i32* %10
%59 = icmp ne i32 %58, 1
br i1 %59, label %60, label %63
60: ; preds = %57, %50
%61 = load i32, i32* @stderr
%62 = call i32 @fprintf(i32 %61, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
store i32 1, i32* %3
br label %65
63: ; preds = %57, %54
%64 = call i32 @printf(i8* getelementptr inbounds ([37 x i8], [37 x i8]* @.str.6, i64 0, i64 0))
store i32 0, i32* %3
br label %65
65: ; preds = %63, %60, %48, %36, %26, %16
%66 = load i32, i32* %3
ret i32 %66
}

LLVM IR (-O2):
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@.str = [14 x i8] c"socket failed\00"
@SOL_SOCKET = global i32 0
@SO_TYPE = global i32 0
@.str.1 = [39 x i8] c"getsockopt(SOL_SOCKET, SO_TYPE) failed\00"
@stderr = global i32 0
@.str.2 = [40 x i8] c"getsockopt(SOL_SOCKET, SO_TYPE) failed\0A\00"
@SOL_TCP = global i32 0
@TCP_NODELAY = global i32 0
@.str.4 = [40 x i8] c"getsockopt(SOL_TCP, TCP_NODELAY) failed\00"
@.str.5 = [41 x i8] c"getsockopt(SOL_TCP, TCP_NODELAY) failed\0A\00"
@str = [31 x i8] c"getsockopt: Got socket type OK\00"
@str.7 = [36 x i8] c"getsockopt: Got TCP_NODELAY flag OK\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i32
%6 = bitcast i32* %3 to i8*
%7 = load i32, i32* @PF_INET
%8 = load i32, i32* @SOCK_STREAM
%9 = tail call i32 @socket(i32 %7, i32 %8, i32 0)
%10 = icmp slt i32 %9, 0
br i1 %10, label %11, label %13
11: ; preds = %2
%12 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str, i64 0, i64 0))
br label %55
13: ; preds = %2
%14 = bitcast i32* %4 to i8*
store i32 4, i32* %3
%15 = load i32, i32* @SOL_SOCKET
%16 = load i32, i32* @SO_TYPE
%17 = call i32 @getsockopt(i32 %9, i32 %15, i32 %16, i8* %14, i32* %3)
%18 = icmp slt i32 %17, 0
br i1 %18, label %19, label %21
19: ; preds = %13
%20 = call i32 @perror(i8* getelementptr inbounds ([39 x i8], [39 x i8]* @.str.1, i64 0, i64 0))
br label %53
21: ; preds = %13
%22 = load i32, i32* %3
%23 = icmp eq i32 %22, 4
br i1 %23, label %24, label %28
24: ; preds = %21
%25 = load i32, i32* %4
%26 = load i32, i32* @SOCK_STREAM
%27 = icmp eq i32 %25, %26
br i1 %27, label %31, label %28
28: ; preds = %24, %21
%29 = load i32, i32* @stderr
%30 = call i32 @fprintf(i32 %29, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.2, i64 0, i64 0))
br label %53
31: ; preds = %24
%32 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([31 x i8], [31 x i8]* @str, i64 0, i64 0))
%33 = bitcast i32* %5 to i8*
store i32 1, i32* %5
store i32 4, i32* %3
%34 = load i32, i32* @SOL_TCP
%35 = load i32, i32* @TCP_NODELAY
%36 = call i32 @getsockopt(i32 %9, i32 %34, i32 %35, i8* %33, i32* %3)
%37 = icmp slt i32 %36, 0
br i1 %37, label %38, label %40
38: ; preds = %31
%39 = call i32 @perror(i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str.4, i64 0, i64 0))
br label %51
40: ; preds = %31
%41 = load i32, i32* %3
%42 = icmp eq i32 %41, 4
%43 = load i32, i32* %5
%44 = icmp ult i32 %43, 2
%45 = select i1 %42, i1 %44, i1 false
br i1 %45, label %49, label %46
46: ; preds = %40
%47 = load i32, i32* @stderr
%48 = call i32 @fprintf(i32 %47, i8* getelementptr inbounds ([41 x i8], [41 x i8]* @.str.5, i64 0, i64 0))
br label %51
49: ; preds = %40
%50 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([36 x i8], [36 x i8]* @str.7, i64 0, i64 0))
br label %51
51: ; preds = %49, %46, %38
%52 = phi i32 [ 1, %38 ], [ 1, %46 ], [ 0, %49 ]
br label %53
53: ; preds = %51, %28, %19
%54 = phi i32 [ 1, %19 ], [ 1, %28 ], [ %52, %51 ]
br label %55
55: ; preds = %53, %11
%56 = phi i32 [ 1, %11 ], [ %54, %53 ]
ret i32 %56
}