#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 */
typedef struct TYPE_2__ TYPE_1__;

/* Type definitions */
struct TYPE_2__ {
  int /*<<< orphan*/ s_addr;
};
struct sockaddr_in {
  TYPE_1__ sin_addr;
  int /*<<< orphan*/ sin_port;
  int /*<<< orphan*/ sin_family;
};
struct sockaddr {
  int dummy;
};
typedef int /*<<< orphan*/ echoServAddr;
typedef int /*<<< orphan*/ echoClntAddr;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ DieWithError(char *);
int /*<<< orphan*/ HandleTCPClient(int, unsigned char);
int /*<<< orphan*/ INADDR_ANY;
int /*<<< orphan*/ IPPROTO_TCP;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_STREAM;
int accept(int, struct sockaddr *, unsigned int *);
unsigned short atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int /*<<< orphan*/ htonl(int /*<<< orphan*/);
int /*<<< orphan*/ htons(unsigned short);
char *inet_ntoa(TYPE_1__);
scalar_t__ listen(int, int);
int /*<<< orphan*/ memset(struct sockaddr_in *, int /*<<< orphan*/, int);
int /*<<< orphan*/ printf(char *, char *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char *argv[]) {
  int servSock;
  int clntSock;
  struct sockaddr_in echoServAddr;
  struct sockaddr_in echoClntAddr;
  unsigned short echoServPort;
  unsigned char modbusAddr;
  unsigned int clntLen;
  if (argc != 3) {
    fprintf(stderr, "Usage:  %s <Server Port> <Modbus Addr>\n", argv[0]);
    exit(1);
  }
  echoServPort = atoi(argv[1]);
  modbusAddr = (unsigned char)atoi(argv[2]);
  if ((servSock = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0)
    DieWithError("socket() failed");
  memset(&echoServAddr, 0, sizeof(echoServAddr));
  echoServAddr.sin_family = AF_INET;
  echoServAddr.sin_addr.s_addr = htonl(INADDR_ANY);
  echoServAddr.sin_port = htons(echoServPort);
  if (bind(servSock, (struct sockaddr *)&echoServAddr, sizeof(echoServAddr)) <
      0)
    DieWithError("bind() failed");
  if (listen(servSock, 5) < 0)
    DieWithError("listen() failed");
  for (;;) {
    clntLen = sizeof(echoClntAddr);
    if ((clntSock =
             accept(servSock, (struct sockaddr *)&echoClntAddr, &clntLen)) < 0)
      DieWithError("accept() failed");
    printf("Handling client %s\n", inet_ntoa(echoClntAddr.sin_addr));
    HandleTCPClient(clntSock, modbusAddr);
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     1226 <main+0x46>
mov    0x2dc1(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xde9(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10d0 <atoi@plt>
mov    %ax,-0x3a(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10d0 <atoi@plt>
mov    %al,-0x3b(%rbp)
mov    0x2d6a(%rip),%rax
mov    (%rax),%edi
mov    0x2d69(%rip),%rax
mov    (%rax),%esi
mov    0x2d78(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,-0x14(%rbp)
cmp    $0x0,%eax
jge    127f <main+0x9f>
lea    0xdae(%rip),%rdi
call   10a0 <DieWithError@plt>
lea    -0x28(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x20(%rbp)
mov    0x2d3e(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,-0x28(%rbp)
movzwl -0x3a(%rbp),%edi
call   1040 <htons@plt>
mov    %eax,-0x24(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x28(%rbp),%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
cmp    $0x0,%rax
jge    12df <main+0xff>
lea    0xd5e(%rip),%rdi
call   10a0 <DieWithError@plt>
mov    -0x14(%rbp),%edi
mov    $0x5,%esi
call   1090 <listen@plt>
cmp    $0x0,%rax
jge    1302 <main+0x122>
lea    0xd49(%rip),%rdi
call   10a0 <DieWithError@plt>
jmp    1307 <main+0x127>
movl   $0xc,-0x40(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x38(%rbp),%rsi
lea    -0x40(%rbp),%rdx
call   10c0 <accept@plt>
mov    %eax,-0x18(%rbp)
cmp    $0x0,%eax
jge    1336 <main+0x156>
lea    0xd25(%rip),%rdi
call   10a0 <DieWithError@plt>
mov    -0x38(%rbp),%edi
call   1030 <inet_ntoa@plt>
mov    %rax,%rsi
lea    0xd1e(%rip),%rdi
call   1050 <printf@plt>
mov    -0x18(%rbp),%edi
movzbl -0x3b(%rbp),%esi
call   10f0 <HandleTCPClient@plt>
jmp    1307 <main+0x127>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x28,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1329 <main+0x149>
mov    0x8(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%ebp
mov    0x10(%rbx),%rdi
call   10d0 <atoi@plt>
mov    %eax,%r12d
mov    0x2da0(%rip),%rax
mov    (%rax),%edi
mov    0x2d9f(%rip),%rax
mov    (%rax),%esi
mov    0x2dae(%rip),%rax
mov    (%rax),%edx
call   1100 <socket@plt>
mov    %eax,%r15d
test   %eax,%eax
jns    1244 <main+0x64>
lea    0xde9(%rip),%rdi
call   10a0 <DieWithError@plt>
lea    0x8(%rsp),%rbx
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1070 <memset@plt>
mov    0x2d89(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x10(%rsp)
mov    0x2d74(%rip),%rax
mov    (%rax),%edi
call   1060 <htonl@plt>
mov    %eax,0x8(%rsp)
movzwl %bp,%edi
call   1040 <htons@plt>
mov    %eax,0xc(%rsp)
mov    %r15d,%edi
mov    %rbx,%rsi
mov    $0xc,%edx
call   10b0 <bind@plt>
test   %rax,%rax
jns    12a4 <main+0xc4>
lea    0xd99(%rip),%rdi
call   10a0 <DieWithError@plt>
mov    %r15d,%edi
mov    $0x5,%esi
call   1090 <listen@plt>
test   %rax,%rax
jns    12c2 <main+0xe2>
lea    0xd89(%rip),%rdi
call   10a0 <DieWithError@plt>
lea    0x18(%rsp),%r13
lea    0x4(%rsp),%r14
lea    0xd93(%rip),%rbp
movzbl %r12b,%ebx
jmp    12fe <main+0x11e>
nopl   0x0(%rax)
mov    0x18(%rsp),%edi
call   1030 <inet_ntoa@plt>
mov    %rbp,%rdi
mov    %rax,%rsi
call   1050 <printf@plt>
mov    %r12d,%edi
mov    %ebx,%esi
call   10f0 <HandleTCPClient@plt>
movl   $0xc,0x4(%rsp)
mov    %r15d,%edi
mov    %r13,%rsi
mov    %r14,%rdx
call   10c0 <accept@plt>
mov    %eax,%r12d
test   %eax,%eax
jns    12e0 <main+0x100>
lea    0xd34(%rip),%rdi
call   10a0 <DieWithError@plt>
jmp    12e0 <main+0x100>
mov    0x2c98(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xcc4(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0x1,%edi
call   10e0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s <Server Port> <Modbus Addr>\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [16 x i8] c"socket() failed\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [14 x i8] c"bind() failed\00"
@.str.3 = [16 x i8] c"listen() failed\00"
@.str.4 = [16 x i8] c"accept() failed\00"
@.str.5 = [20 x i8] c"Handling client %s\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 %struct.sockaddr_in
%9 = alloca %struct.sockaddr_in
%10 = alloca i16
%11 = alloca i8
%12 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%13 = load i32, i32* %4
%14 = icmp ne i32 %13, 3
br i1 %14, label %15, label %22
15: ; preds = %2
%16 = load i32, i32* @stderr
%17 = load i8**, i8*** %5
%18 = getelementptr inbounds i8*, i8** %17, i64 0
%19 = load i8*, i8** %18
%20 = call i32 @fprintf(i32 %16, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %19)
%21 = call i32 @exit(i32 1)
unreachable
22: ; preds = %2
%23 = load i8**, i8*** %5
%24 = getelementptr inbounds i8*, i8** %23, i64 1
%25 = load i8*, i8** %24
%26 = call zeroext i16 @atoi(i8* %25)
store i16 %26, i16* %10
%27 = load i8**, i8*** %5
%28 = getelementptr inbounds i8*, i8** %27, i64 2
%29 = load i8*, i8** %28
%30 = call zeroext i16 @atoi(i8* %29)
%31 = trunc i16 %30 to i8
store i8 %31, i8* %11
%32 = load i32, i32* @PF_INET
%33 = load i32, i32* @SOCK_STREAM
%34 = load i32, i32* @IPPROTO_TCP
%35 = call i32 @socket(i32 %32, i32 %33, i32 %34)
store i32 %35, i32* %6
%36 = icmp slt i32 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %22
%38 = call i32 @DieWithError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %39
39: ; preds = %37, %22
%40 = call i32 @memset(%struct.sockaddr_in* %8, i32 0, i32 12)
%41 = load i32, i32* @AF_INET
%42 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 2
store i32 %41, i32* %42
%43 = load i32, i32* @INADDR_ANY
%44 = call i32 @htonl(i32 %43)
%45 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 0
%46 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %45, i32 0, i32 0
store i32 %44, i32* %46
%47 = load i16, i16* %10
%48 = call i32 @htons(i16 zeroext %47)
%49 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %8, i32 0, i32 1
store i32 %48, i32* %49
%50 = load i32, i32* %6
%51 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%52 = call i64 @bind(i32 %50, %struct.sockaddr* %51, i32 12)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %56
54: ; preds = %39
%55 = call i32 @DieWithError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %56
56: ; preds = %54, %39
%57 = load i32, i32* %6
%58 = call i64 @listen(i32 %57, i32 5)
%59 = icmp slt i64 %58, 0
br i1 %59, label %60, label %62
60: ; preds = %56
%61 = call i32 @DieWithError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %62
62: ; preds = %60, %56
br label %63
63: ; preds = %70, %62
store i32 12, i32* %12
%64 = load i32, i32* %6
%65 = bitcast %struct.sockaddr_in* %9 to %struct.sockaddr*
%66 = call i32 @accept(i32 %64, %struct.sockaddr* %65, i32* %12)
store i32 %66, i32* %7
%67 = icmp slt i32 %66, 0
br i1 %67, label %68, label %70
68: ; preds = %63
%69 = call i32 @DieWithError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %70
70: ; preds = %68, %63
%71 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %9, i32 0, i32 0
%72 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %71, i32 0, i32 0
%73 = load i32, i32* %72
%74 = call i8* @inet_ntoa(i32 %73)
%75 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %74)
%76 = load i32, i32* %7
%77 = load i8, i8* %11
%78 = call i32 @HandleTCPClient(i32 %76, i8 zeroext %77)
br label %63
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [40 x i8] c"Usage: %s <Server Port> <Modbus Addr>\0A\00"
@PF_INET = global i32 0
@SOCK_STREAM = global i32 0
@IPPROTO_TCP = global i32 0
@.str.1 = [16 x i8] c"socket() failed\00"
@AF_INET = global i32 0
@INADDR_ANY = global i32 0
@.str.2 = [14 x i8] c"bind() failed\00"
@.str.3 = [16 x i8] c"listen() failed\00"
@.str.4 = [16 x i8] c"accept() failed\00"
@.str.5 = [20 x i8] c"Handling client %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = bitcast %struct.sockaddr_in* %3 to i8*
%7 = bitcast %struct.sockaddr_in* %4 to i8*
%8 = bitcast i32* %5 to i8*
%9 = icmp eq i32 %0, 3
br i1 %9, label %15, label %10
10: ; preds = %2
%11 = load i32, i32* @stderr
%12 = load i8*, i8** %1
%13 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([40 x i8], [40 x i8]* @.str, i64 0, i64 0), i8* %12)
%14 = tail call i32 @exit(i32 1)
unreachable
15: ; preds = %2
%16 = getelementptr inbounds i8*, i8** %1, i64 1
%17 = load i8*, i8** %16
%18 = tail call zeroext i16 @atoi(i8* %17)
%19 = getelementptr inbounds i8*, i8** %1, i64 2
%20 = load i8*, i8** %19
%21 = tail call zeroext i16 @atoi(i8* %20)
%22 = trunc i16 %21 to i8
%23 = load i32, i32* @PF_INET
%24 = load i32, i32* @SOCK_STREAM
%25 = load i32, i32* @IPPROTO_TCP
%26 = tail call i32 @socket(i32 %23, i32 %24, i32 %25)
%27 = icmp slt i32 %26, 0
br i1 %27, label %28, label %30
28: ; preds = %15
%29 = tail call i32 @DieWithError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0))
br label %30
30: ; preds = %28, %15
%31 = call i32 @memset(%struct.sockaddr_in* %3, i32 0, i32 12)
%32 = load i32, i32* @AF_INET
%33 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %32, i32* %33
%34 = load i32, i32* @INADDR_ANY
%35 = call i32 @htonl(i32 %34)
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0, i32 0
store i32 %35, i32* %36
%37 = call i32 @htons(i16 zeroext %18)
%38 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %37, i32* %38
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i64 @bind(i32 %26, %struct.sockaddr* %39, i32 12)
%41 = icmp slt i64 %40, 0
br i1 %41, label %42, label %44
42: ; preds = %30
%43 = call i32 @DieWithError(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.2, i64 0, i64 0))
br label %44
44: ; preds = %42, %30
%45 = call i64 @listen(i32 %26, i32 5)
%46 = icmp slt i64 %45, 0
br i1 %46, label %47, label %49
47: ; preds = %44
%48 = call i32 @DieWithError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.3, i64 0, i64 0))
br label %49
49: ; preds = %47, %44
%50 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
%51 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %4, i64 0, i32 0, i32 0
br label %52
52: ; preds = %57, %49
store i32 12, i32* %5
%53 = call i32 @accept(i32 %26, %struct.sockaddr* %50, i32* %5)
%54 = icmp slt i32 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %52
%56 = call i32 @DieWithError(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.4, i64 0, i64 0))
br label %57
57: ; preds = %55, %52
%58 = load i32, i32* %51
%59 = call i8* @inet_ntoa(i32 %58)
%60 = call i32 @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.5, i64 0, i64 0), i8* %59)
%61 = call i32 @HandleTCPClient(i32 %53, i8 zeroext %22)
br label %52
}