#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 sockaddr_in {
  int /*<<< orphan*/ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ listenAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ SOL_SOCKET;
int /*<<< orphan*/ SO_BROADCAST;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ displayError(char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, int);
int read(int, char *, int);
int setsockopt(int, int /*<<< orphan*/, int /*<<< orphan*/, int *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main(int argc, char **argv) {
  struct sockaddr_in srvAddr = {.sin_family = AF_INET,
                                .sin_port = htons(67),
                                .sin_addr = inet_addr("10.1.4.1")};
  struct sockaddr_in listenAddr = {.sin_family = AF_INET,
                                   .sin_port = htons(67),
                                   .sin_addr = inet_addr("0.0.0.0")};
  struct sockaddr_in bcAddr = {.sin_family = AF_INET,
                               .sin_port = htons(68),
                               .sin_addr = inet_addr("10.1.4.255")};
  int s = socket(AF_INET, SOCK_DGRAM, 0);
  if (s == -1)
    displayError("socket()");
  int so_broadcast = 1;
  if (setsockopt(s, SOL_SOCKET, SO_BROADCAST, &so_broadcast,
                 sizeof so_broadcast) == -1)
    displayError("setsockopt(SO_BROADCAST)");
  int r = bind(s, (struct sockaddr *)&listenAddr, sizeof(listenAddr));
  if (r == -1)
    displayError("bind()");
  for (;;) {
    char buffer[512];
    int r = read(s, buffer, 512);
    printf("Got %d bytes\n", r);
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x260,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
lea    0xe60(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x20(%rbp)
mov    $0x43,%edi
call   1040 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2e24(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
lea    0xe41(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x30(%rbp)
mov    $0x43,%edi
call   1040 <htons@plt>
mov    %eax,-0x2c(%rbp)
mov    0x2dfc(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x28(%rbp)
lea    0xe21(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,-0x40(%rbp)
mov    $0x44,%edi
call   1040 <htons@plt>
mov    %eax,-0x3c(%rbp)
mov    0x2dd4(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x38(%rbp)
mov    0x2dc8(%rip),%rax
mov    (%rax),%edi
mov    0x2db7(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,-0x44(%rbp)
cmpl   $0xffffffff,-0x44(%rbp)
jne    1243 <main+0xc3>
lea    0xdde(%rip),%rdi
call   1080 <displayError@plt>
movl   $0x1,-0x48(%rbp)
mov    -0x44(%rbp),%edi
mov    0x2d94(%rip),%rax
mov    (%rax),%esi
mov    0x2d73(%rip),%rax
mov    (%rax),%edx
lea    -0x48(%rbp),%rcx
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1283 <main+0x103>
lea    0xda7(%rip),%rdi
call   1080 <displayError@plt>
mov    -0x44(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0xc,%edx
call   1090 <bind@plt>
mov    %eax,-0x4c(%rbp)
cmpl   $0xffffffff,-0x4c(%rbp)
jne    12ad <main+0x12d>
lea    0xd96(%rip),%rdi
call   1080 <displayError@plt>
jmp    12b2 <main+0x132>
mov    -0x44(%rbp),%edi
lea    -0x250(%rbp),%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %eax,-0x254(%rbp)
mov    -0x254(%rbp),%esi
lea    0xd6c(%rip),%rdi
call   1050 <printf@plt>
jmp    12b2 <main+0x132>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x220,%rsp
lea    0xe6e(%rip),%rdi
call   1070 <inet_addr@plt>
mov    $0x43,%edi
call   1040 <htons@plt>
lea    0xe61(%rip),%rdi
call   1070 <inet_addr@plt>
mov    %eax,0x10(%rsp)
mov    $0x43,%edi
call   1040 <htons@plt>
mov    %eax,0x14(%rsp)
mov    0x2e1a(%rip),%rbx
mov    (%rbx),%eax
mov    %eax,0x18(%rsp)
lea    0xe3e(%rip),%rdi
call   1070 <inet_addr@plt>
mov    $0x44,%edi
call   1040 <htons@plt>
mov    (%rbx),%edi
mov    0x2ded(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10a0 <socket@plt>
mov    %eax,%r14d
cmp    $0xffffffff,%eax
jne    1208 <main+0x88>
lea    0xe19(%rip),%rdi
call   1080 <displayError@plt>
movl   $0x1,0xc(%rsp)
mov    0x2dd1(%rip),%rax
mov    (%rax),%esi
mov    0x2db0(%rip),%rax
mov    (%rax),%edx
lea    0xc(%rsp),%rcx
mov    %r14d,%edi
mov    $0x4,%r8d
call   1030 <setsockopt@plt>
cmp    $0xffffffff,%eax
jne    1246 <main+0xc6>
lea    0xde4(%rip),%rdi
call   1080 <displayError@plt>
lea    0x10(%rsp),%rsi
mov    %r14d,%edi
mov    $0xc,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
jne    1269 <main+0xe9>
lea    0xdda(%rip),%rdi
call   1080 <displayError@plt>
lea    0x20(%rsp),%rbx
lea    0xdd0(%rip),%rbp
cs nopw 0x0(%rax,%rax,1)
nop
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x200,%edx
call   1060 <read@plt>
mov    %rbp,%rdi
mov    %eax,%esi
call   1050 <printf@plt>
jmp    1280 <main+0x100>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"10.1.4.1\00"
@AF_INET = global i32 0
@.str.1 = [8 x i8] c"0.0.0.0\00"
@.str.2 = [11 x i8] c"10.1.4.255\00"
@SOCK_DGRAM = global i32 0
@.str.3 = [9 x i8] c"socket()\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.4 = [25 x i8] c"setsockopt(SO_BROADCAST)\00"
@.str.5 = [7 x i8] c"bind()\00"
@.str.6 = [14 x i8] c"Got %d bytes\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca %struct.sockaddr_in
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca [512 x i8]
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%15 = call i32 @inet_addr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
store i32 %15, i32* %14
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
%17 = call i32 @htons(i32 67)
store i32 %17, i32* %16
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
%19 = load i32, i32* @AF_INET
store i32 %19, i32* %18
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 0
%21 = call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %21, i32* %20
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 1
%23 = call i32 @htons(i32 67)
store i32 %23, i32* %22
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %7, i32 0, i32 2
%25 = load i32, i32* @AF_INET
store i32 %25, i32* %24
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%27 = call i32 @inet_addr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
store i32 %27, i32* %26
%28 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
%29 = call i32 @htons(i32 68)
store i32 %29, i32* %28
%30 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
%31 = load i32, i32* @AF_INET
store i32 %31, i32* %30
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_DGRAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
store i32 %34, i32* %9
%35 = load i32, i32* %9
%36 = icmp eq i32 %35, -1
br i1 %36, label %37, label %39
37: ; preds = %2
%38 = call i32 @displayError(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %37, %2
store i32 1, i32* %10
%40 = load i32, i32* %9
%41 = load i32, i32* @SOL_SOCKET
%42 = load i32, i32* @SO_BROADCAST
%43 = call i32 @setsockopt(i32 %40, i32 %41, i32 %42, i32* %10, i32 4)
%44 = icmp eq i32 %43, -1
br i1 %44, label %45, label %47
45: ; preds = %39
%46 = call i32 @displayError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %47
47: ; preds = %45, %39
%48 = load i32, i32* %9
%49 = bitcast %struct.sockaddr_in* %7 to %struct.sockaddr*
%50 = call i32 @bind(i32 %48, %struct.sockaddr* %49, i32 12)
store i32 %50, i32* %11
%51 = load i32, i32* %11
%52 = icmp eq i32 %51, -1
br i1 %52, label %53, label %55
53: ; preds = %47
%54 = call i32 @displayError(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %55
55: ; preds = %53, %47
br label %56
56: ; preds = %56, %55
%57 = load i32, i32* %9
%58 = getelementptr inbounds [512 x i8], [512 x i8]* %12, i64 0, i64 0
%59 = call i32 @read(i32 %57, i8* %58, i32 512)
store i32 %59, i32* %13
%60 = load i32, i32* %13
%61 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %60)
br label %56
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@.str = [9 x i8] c"10.1.4.1\00"
@AF_INET = global i32 0
@.str.1 = [8 x i8] c"0.0.0.0\00"
@.str.2 = [11 x i8] c"10.1.4.255\00"
@SOCK_DGRAM = global i32 0
@.str.3 = [9 x i8] c"socket()\00"
@SOL_SOCKET = global i32 0
@SO_BROADCAST = global i32 0
@.str.4 = [25 x i8] c"setsockopt(SO_BROADCAST)\00"
@.str.5 = [7 x i8] c"bind()\00"
@.str.6 = [14 x i8] c"Got %d bytes\0A\00"
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca i32
%5 = alloca [512 x i8]
%6 = tail call i32 @inet_addr(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0))
%7 = tail call i32 @htons(i32 67)
%8 = bitcast %struct.sockaddr_in* %3 to i8*
%9 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%10 = tail call i32 @inet_addr(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str.1, i64 0, i64 0))
store i32 %10, i32* %9
%11 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
%12 = tail call i32 @htons(i32 67)
store i32 %12, i32* %11
%13 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
%14 = load i32, i32* @AF_INET
store i32 %14, i32* %13
%15 = tail call i32 @inet_addr(i8* getelementptr inbounds ([11 x i8], [11 x i8]* @.str.2, i64 0, i64 0))
%16 = tail call i32 @htons(i32 68)
%17 = load i32, i32* @AF_INET
%18 = load i32, i32* @SOCK_DGRAM
%19 = tail call i32 @socket(i32 %17, i32 %18, i32 0)
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %2
%22 = tail call i32 @displayError(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.3, i64 0, i64 0))
br label %23
23: ; preds = %21, %2
%24 = bitcast i32* %4 to i8*
store i32 1, i32* %4
%25 = load i32, i32* @SOL_SOCKET
%26 = load i32, i32* @SO_BROADCAST
%27 = call i32 @setsockopt(i32 %19, i32 %25, i32 %26, i32* %4, i32 4)
%28 = icmp eq i32 %27, -1
br i1 %28, label %29, label %31
29: ; preds = %23
%30 = call i32 @displayError(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0))
br label %31
31: ; preds = %29, %23
%32 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%33 = call i32 @bind(i32 %19, %struct.sockaddr* %32, i32 12)
%34 = icmp eq i32 %33, -1
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @displayError(i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %31
%38 = getelementptr inbounds [512 x i8], [512 x i8]* %5, i64 0, i64 0
br label %39
39: ; preds = %39, %37
%40 = call i32 @read(i32 %19, i8* %38, i32 512)
%41 = call i32 @printf(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.6, i64 0, i64 0), i32 %40)
br label %39
}