#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 socklen_t;
typedef int /*<<< orphan*/ servaddr;
typedef int /*<<< orphan*/ clientaddr;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ atoi(char *);
scalar_t__ bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(struct sockaddr_in *, int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, ...);
int /*<<< orphan*/ htons(int /*<<< orphan*/);
int inet_pton(int /*<<< orphan*/, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ perror(char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int main(int argc, char **argv) {
  if (argc != 3) {
    fprintf(stderr, "usage: %s <IP> <port>\n", argv[0]);
    return 1;
  }
  struct sockaddr_in servaddr;
  bzero(&servaddr, sizeof(servaddr));
  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(atoi(argv[2]));
  if (inet_pton(AF_INET, argv[1], &servaddr.sin_addr) != 1) {
    fprintf(stderr, "invalid IP address\n");
    return 1;
  }
  int servfd = socket(AF_INET, SOCK_DGRAM, 0);
  if (servfd < 0) {
    perror("cannot create socket");
    return 1;
  }
  if (bind(servfd, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
    perror("cannot bind");
    return 1;
  }
  while (1) {
    struct sockaddr_in clientaddr;
    socklen_t clientaddrlen = sizeof(clientaddr);
    char buf[1024];
    int ret = recvfrom(servfd, buf, sizeof(buf), 0,
                       (struct sockaddr *)&clientaddr, &clientaddrlen);
    if (ret < 0) {
      continue;
    }
    int result = 0;
    int i;
    for (i = 0; i < ret; ++i) {
      result += buf[i];
    }
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x450,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x3,-0x8(%rbp)
je     11dd <main+0x4d>
mov    0x2e16(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe36(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1361 <main+0x1d1>
lea    -0x20(%rbp),%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2df6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    0x10(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,-0x1c(%rbp)
mov    0x2dd3(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rsi
lea    -0x20(%rbp),%rdx
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
je     1254 <main+0xc4>
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
lea    0xdd6(%rip),%rsi
mov    $0x0,%al
call   1040 <fprintf@plt>
movl   $0x1,-0x4(%rbp)
jmp    1361 <main+0x1d1>
mov    0x2d8d(%rip),%rax
mov    (%rax),%edi
mov    0x2d7c(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    1292 <main+0x102>
lea    0xdaa(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1361 <main+0x1d1>
mov    -0x24(%rbp),%edi
lea    -0x20(%rbp),%rsi
mov    $0xc,%edx
call   1070 <bind@plt>
cmp    $0x0,%rax
je     12c5 <main+0x135>
lea    0xd8c(%rip),%rdi
call   1080 <perror@plt>
movl   $0x1,-0x4(%rbp)
jmp    1361 <main+0x1d1>
jmp    12ca <main+0x13a>
movl   $0xc,-0x34(%rbp)
mov    -0x24(%rbp),%edi
lea    -0x440(%rbp),%rsi
lea    -0x30(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x34(%rbp),%r9
call   1060 <recvfrom@plt>
mov    %eax,-0x444(%rbp)
cmpl   $0x0,-0x444(%rbp)
jge    1307 <main+0x177>
jmp    12ca <main+0x13a>
movl   $0x0,-0x448(%rbp)
movl   $0x0,-0x44c(%rbp)
mov    -0x44c(%rbp),%eax
cmp    -0x444(%rbp),%eax
jge    135c <main+0x1cc>
movslq -0x44c(%rbp),%rax
movsbl -0x440(%rbp,%rax,1),%eax
ff
add    -0x448(%rbp),%eax
mov    %eax,-0x448(%rbp)
mov    -0x44c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x44c(%rbp)
jmp    131b <main+0x18b>
jmp    12ca <main+0x13a>
mov    -0x4(%rbp),%eax
add    $0x450,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x438,%rsp
mov    %rsi,%rbx
cmp    $0x3,%edi
jne    1261 <main+0xd1>
lea    0x10(%rsp),%r14
mov    %r14,%rdi
mov    $0xc,%esi
call   1090 <bzero@plt>
mov    0x2e25(%rip),%r15
mov    (%r15),%eax
mov    %eax,0x18(%rsp)
mov    0x10(%rbx),%rdi
call   10a0 <atoi@plt>
mov    %eax,%edi
call   1030 <htons@plt>
mov    %eax,0x14(%rsp)
mov    (%r15),%edi
mov    0x8(%rbx),%rsi
mov    %r14,%rdx
call   1050 <inet_pton@plt>
cmp    $0x1,%eax
jne    127d <main+0xed>
mov    (%r15),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10b0 <socket@plt>
test   %eax,%eax
js     12a8 <main+0x118>
mov    %eax,%ebx
lea    0x10(%rsp),%rsi
mov    %eax,%edi
mov    $0xc,%edx
call   1070 <bind@plt>
test   %rax,%rax
jne    12b1 <main+0x121>
lea    0x30(%rsp),%r14
lea    0x20(%rsp),%r15
lea    0xc(%rsp),%r12
nopl   0x0(%rax)
movl   $0xc,0xc(%rsp)
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r15,%r8
mov    %r12,%r9
call   1060 <recvfrom@plt>
jmp    1240 <main+0xb0>
mov    0x2d68(%rip),%rax
mov    (%rax),%edi
mov    (%rbx),%rdx
lea    0xd8c(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
jmp    1294 <main+0x104>
mov    0x2d4c(%rip),%rax
mov    (%rax),%edi
lea    0xd8a(%rip),%rsi
xor    %eax,%eax
call   1040 <fprintf@plt>
mov    $0x1,%eax
add    $0x438,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
ret
lea    0xd7c(%rip),%rdi
jmp    12b8 <main+0x128>
lea    0xd88(%rip),%rdi
call   1080 <perror@plt>
jmp    1294 <main+0x104>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"usage: %s <IP> <port>\0A\00"
@AF_INET = global i32 0
@.str.1 = [20 x i8] c"invalid IP address\0A\00"
@SOCK_DGRAM = global i32 0
@.str.2 = [21 x i8] c"cannot create socket\00"
@.str.3 = [12 x i8] c"cannot bind\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca %struct.sockaddr_in
%7 = alloca i32
%8 = alloca %struct.sockaddr_in
%9 = alloca i32
%10 = alloca [1024 x i8]
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%14 = load i32, i32* %4
%15 = icmp ne i32 %14, 3
br i1 %15, label %16, label %22
16: ; preds = %2
%17 = load i32, i32* @stderr
%18 = load i8**, i8*** %5
%19 = getelementptr inbounds i8*, i8** %18, i64 0
%20 = load i8*, i8** %19
%21 = call i32 (i32, i8*, ...) @fprintf(i32 %17, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %20)
store i32 1, i32* %3
br label %83
22: ; preds = %2
%23 = call i32 @bzero(%struct.sockaddr_in* %6, i32 12)
%24 = load i32, i32* @AF_INET
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %24, i32* %25
%26 = load i8**, i8*** %5
%27 = getelementptr inbounds i8*, i8** %26, i64 2
%28 = load i8*, i8** %27
%29 = call i32 @atoi(i8* %28)
%30 = call i32 @htons(i32 %29)
%31 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %30, i32* %31
%32 = load i32, i32* @AF_INET
%33 = load i8**, i8*** %5
%34 = getelementptr inbounds i8*, i8** %33, i64 1
%35 = load i8*, i8** %34
%36 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%37 = call i32 @inet_pton(i32 %32, i8* %35, i32* %36)
%38 = icmp ne i32 %37, 1
br i1 %38, label %39, label %42
39: ; preds = %22
%40 = load i32, i32* @stderr
%41 = call i32 (i32, i8*, ...) @fprintf(i32 %40, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
store i32 1, i32* %3
br label %83
42: ; preds = %22
%43 = load i32, i32* @AF_INET
%44 = load i32, i32* @SOCK_DGRAM
%45 = call i32 @socket(i32 %43, i32 %44, i32 0)
store i32 %45, i32* %7
%46 = load i32, i32* %7
%47 = icmp slt i32 %46, 0
br i1 %47, label %48, label %50
48: ; preds = %42
%49 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
store i32 1, i32* %3
br label %83
50: ; preds = %42
%51 = load i32, i32* %7
%52 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%53 = call i64 @bind(i32 %51, %struct.sockaddr* %52, i32 12)
%54 = icmp ne i64 %53, 0
br i1 %54, label %55, label %57
55: ; preds = %50
%56 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
store i32 1, i32* %3
br label %83
57: ; preds = %50
br label %58
58: ; preds = %57, %65, %82
store i32 12, i32* %9
%59 = load i32, i32* %7
%60 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 0
%61 = bitcast %struct.sockaddr_in* %8 to %struct.sockaddr*
%62 = call i32 @recvfrom(i32 %59, i8* %60, i32 1024, i32 0, %struct.sockaddr* %61, i32* %9)
store i32 %62, i32* %11
%63 = load i32, i32* %11
%64 = icmp slt i32 %63, 0
br i1 %64, label %65, label %66
65: ; preds = %58
br label %58
66: ; preds = %58
store i32 0, i32* %12
store i32 0, i32* %13
br label %67
67: ; preds = %79, %66
%68 = load i32, i32* %13
%69 = load i32, i32* %11
%70 = icmp slt i32 %68, %69
br i1 %70, label %71, label %82
71: ; preds = %67
%72 = load i32, i32* %13
%73 = sext i32 %72 to i64
%74 = getelementptr inbounds [1024 x i8], [1024 x i8]* %10, i64 0, i64 %73
%75 = load i8, i8* %74
%76 = sext i8 %75 to i32
%77 = load i32, i32* %12
%78 = add nsw i32 %77, %76
store i32 %78, i32* %12
br label %79
79: ; preds = %71
%80 = load i32, i32* %13
%81 = add nsw i32 %80, 1
store i32 %81, i32* %13
br label %67
82: ; preds = %67
br label %58
83: ; preds = %55, %48, %39, %16
%84 = load i32, i32* %3
ret i32 %84
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [23 x i8] c"usage: %s <IP> <port>\0A\00"
@AF_INET = global i32 0
@.str.1 = [20 x i8] c"invalid IP address\0A\00"
@SOCK_DGRAM = global i32 0
@.str.2 = [21 x i8] c"cannot create socket\00"
@.str.3 = [12 x i8] c"cannot bind\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca %struct.sockaddr_in
%4 = alloca %struct.sockaddr_in
%5 = alloca i32
%6 = alloca [1024 x i8]
%7 = icmp eq i32 %0, 3
br i1 %7, label %12, label %8
8: ; preds = %2
%9 = load i32, i32* @stderr
%10 = load i8*, i8** %1
%11 = tail call i32 (i32, i8*, ...) @fprintf(i32 %9, i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str, i64 0, i64 0), i8* %10)
br label %52
12: ; preds = %2
%13 = bitcast %struct.sockaddr_in* %3 to i8*
%14 = call i32 @bzero(%struct.sockaddr_in* %3, i32 12)
%15 = load i32, i32* @AF_INET
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 2
store i32 %15, i32* %16
%17 = getelementptr inbounds i8*, i8** %1, i64 2
%18 = load i8*, i8** %17
%19 = tail call i32 @atoi(i8* %18)
%20 = tail call i32 @htons(i32 %19)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 1
store i32 %20, i32* %21
%22 = load i32, i32* @AF_INET
%23 = getelementptr inbounds i8*, i8** %1, i64 1
%24 = load i8*, i8** %23
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %3, i64 0, i32 0
%26 = call i32 @inet_pton(i32 %22, i8* %24, i32* %25)
%27 = icmp eq i32 %26, 1
br i1 %27, label %31, label %28
28: ; preds = %12
%29 = load i32, i32* @stderr
%30 = call i32 (i32, i8*, ...) @fprintf(i32 %29, i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.1, i64 0, i64 0))
br label %51
31: ; preds = %12
%32 = load i32, i32* @AF_INET
%33 = load i32, i32* @SOCK_DGRAM
%34 = call i32 @socket(i32 %32, i32 %33, i32 0)
%35 = icmp slt i32 %34, 0
br i1 %35, label %36, label %38
36: ; preds = %31
%37 = call i32 @perror(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.2, i64 0, i64 0))
br label %51
38: ; preds = %31
%39 = bitcast %struct.sockaddr_in* %3 to %struct.sockaddr*
%40 = call i64 @bind(i32 %34, %struct.sockaddr* %39, i32 12)
%41 = icmp eq i64 %40, 0
br i1 %41, label %42, label %47
42: ; preds = %38
%43 = bitcast %struct.sockaddr_in* %4 to i8*
%44 = bitcast i32* %5 to i8*
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %6, i64 0, i64 0
%46 = bitcast %struct.sockaddr_in* %4 to %struct.sockaddr*
br label %49
47: ; preds = %38
%48 = call i32 @perror(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %51
49: ; preds = %42, %49
store i32 12, i32* %5
%50 = call i32 @recvfrom(i32 %34, i8* %45, i32 1024, i32 0, %struct.sockaddr* %46, i32* %5)
br label %49
51: ; preds = %36, %47, %28
br label %52
52: ; preds = %51, %8
ret i32 1
}