#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 dummy;
};
struct sockaddr {
  int dummy;
};
typedef int socklen_t;
typedef int /*<<< orphan*/ cli_addr;

/* Variables and functions */
int /*<<< orphan*/ atoi(char *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *);
int make_dgram_server(int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int recvfrom(int, char *, int, int /*<<< orphan*/, struct sockaddr *, int *);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ sys_who_called(struct sockaddr_in *);
int main(int argc, char *argv[]) {
  int sock;
  char buf[1024];
  int msg_len;
  socklen_t cli_len;
  struct sockaddr_in cli_addr;
  if (argc != 2) {
    fprintf(stderr, "Usage: %s <port>\n", argv[0]);
    exit(1);
  }
  sock = make_dgram_server(atoi(argv[1]));
  if (sock == -1) {
    perror("make_dgram_server() failed");
    exit(1);
  };
  while (1) {
    cli_len = sizeof(cli_addr);
    msg_len =
        recvfrom(sock, buf, 1024, 0, (struct sockaddr *)&cli_addr, &cli_len);
    buf[msg_len] = '\0';
    printf("dgrecv: got a message %s\n", buf);
    sys_who_called(&cli_addr);
  }
  close(sock);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11c9 <main+0x49>
mov    0x2e36(%rip),%rax
mov    (%rax),%edi
mov    -0x10(%rbp),%rax
mov    (%rax),%rdx
lea    0xe46(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1050 <make_dgram_server@plt>
mov    %eax,-0x14(%rbp)
cmpl   $0xffffffff,-0x14(%rbp)
jne    1200 <main+0x80>
lea    0xe21(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
jmp    1205 <main+0x85>
movl   $0x4,-0x428(%rbp)
mov    -0x14(%rbp),%edi
lea    -0x420(%rbp),%rsi
lea    -0x430(%rbp),%r8
mov    $0x400,%edx
xor    %ecx,%ecx
lea    -0x428(%rbp),%r9
call   1070 <recvfrom@plt>
mov    %eax,-0x424(%rbp)
movslq -0x424(%rbp),%rax
movb   $0x0,-0x420(%rbp,%rax,1)
lea    -0x420(%rbp),%rsi
lea    0xdd7(%rip),%rdi
call   1030 <printf@plt>
lea    -0x430(%rbp),%rdi
call   1060 <sys_who_called@plt>
jmp    1205 <main+0x85>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
cmp    $0x2,%edi
jne    120c <main+0x8c>
mov    0x8(%rsi),%rdi
call   1090 <atoi@plt>
mov    %eax,%edi
call   1050 <make_dgram_server@plt>
cmp    $0xffffffff,%eax
je     122e <main+0xae>
mov    %eax,%r14d
lea    0x10(%rsp),%rbx
lea    0x8(%rsp),%rbp
lea    0x4(%rsp),%r15
lea    0xe67(%rip),%r12
cs nopw 0x0(%rax,%rax,1)
movl   $0x4,0x4(%rsp)
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %rbp,%r8
mov    %r15,%r9
call   1070 <recvfrom@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r12,%rdi
mov    %rbx,%rsi
call   1030 <printf@plt>
mov    %rbp,%rdi
call   1060 <sys_who_called@plt>
jmp    11d0 <main+0x50>
mov    0x2dcd(%rip),%rax
mov    (%rax),%edi
mov    (%rsi),%rdx
lea    0xde1(%rip),%rsi
call   1040 <fprintf@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>
lea    0xddd(%rip),%rdi
call   1080 <perror@plt>
mov    $0x1,%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@.str.1 = [27 x i8] c"make_dgram_server() failed\00"
@.str.2 = [26 x i8] c"dgrecv: got a message %s\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca [1024 x i8]
%8 = alloca i32
%9 = alloca i32
%10 = alloca %struct.sockaddr_in
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%11 = load i32, i32* %4
%12 = icmp ne i32 %11, 2
br i1 %12, label %13, label %20
13: ; preds = %2
%14 = load i32, i32* @stderr
%15 = load i8**, i8*** %5
%16 = getelementptr inbounds i8*, i8** %15, i64 0
%17 = load i8*, i8** %16
%18 = call i32 @fprintf(i32 %14, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i8* %17)
%19 = call i32 @exit(i32 1)
unreachable
20: ; preds = %2
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @atoi(i8* %23)
%25 = call i32 @make_dgram_server(i32 %24)
store i32 %25, i32* %6
%26 = load i32, i32* %6
%27 = icmp eq i32 %26, -1
br i1 %27, label %28, label %31
28: ; preds = %20
%29 = call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%30 = call i32 @exit(i32 1)
unreachable
31: ; preds = %20
br label %32
32: ; preds = %31, %32
store i32 4, i32* %9
%33 = load i32, i32* %6
%34 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%35 = bitcast %struct.sockaddr_in* %10 to %struct.sockaddr*
%36 = call i32 @recvfrom(i32 %33, i8* %34, i32 1024, i32 0, %struct.sockaddr* %35, i32* %9)
store i32 %36, i32* %8
%37 = load i32, i32* %8
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %38
store i8 0, i8* %39
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%41 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %40)
%42 = call i32 @sys_who_called(%struct.sockaddr_in* %10)
br label %32
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32 }
%struct.sockaddr = type { i32 }
@stderr = global i32 0
@.str = [18 x i8] c"Usage: %s <port>\0A\00"
@.str.1 = [27 x i8] c"make_dgram_server() failed\00"
@.str.2 = [26 x i8] c"dgrecv: got a message %s\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [1024 x i8]
%4 = alloca i32
%5 = alloca %struct.sockaddr_in
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 0
%7 = bitcast i32* %4 to i8*
%8 = bitcast %struct.sockaddr_in* %5 to i8*
%9 = icmp eq i32 %0, 2
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 ([18 x i8], [18 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 i32 @atoi(i8* %17)
%19 = tail call i32 @make_dgram_server(i32 %18)
%20 = icmp eq i32 %19, -1
br i1 %20, label %23, label %21
21: ; preds = %15
%22 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
br label %26
23: ; preds = %15
%24 = tail call i32 @perror(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.1, i64 0, i64 0))
%25 = tail call i32 @exit(i32 1)
unreachable
26: ; preds = %21, %26
store i32 4, i32* %4
%27 = call i32 @recvfrom(i32 %19, i8* %6, i32 1024, i32 0, %struct.sockaddr* %22, i32* %4)
%28 = sext i32 %27 to i64
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %3, i64 0, i64 %28
store i8 0, i8* %29
%30 = call i32 @printf(i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%31 = call i32 @sys_who_called(%struct.sockaddr_in* %5)
br label %26
}