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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int bind(int, struct sockaddr *, int);
int /*<<< orphan*/ bzero(char *, int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ exit(int);
int getpid();
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_addr(char *);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int main() {
  int fd;
  int r;
  char buf[300];
  printf("pid : %d,fd : %d\n", getpid(), fd);
  fd = socket(AF_INET, SOCK_DGRAM, 0);
  if (fd == -1) {
    printf("socket create error:%m\n");
    exit(-1);
  }
  printf("socket create success....\n");
  struct sockaddr_in addr = {0};
  addr.sin_family = AF_INET;
  addr.sin_port = htons(9999);
  addr.sin_addr.s_addr = inet_addr("192.168.137.3");
  r = bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  if (r == -1) {
    printf("socket bind error:%m\n");
    exit(-1);
  }
  printf("socket bind success....\n");
  while (1) {
    bzero(buf, sizeof(buf));
    r = read(fd, buf, sizeof(buf));
    buf[r] = 0;
    printf("get data : %s\n", buf);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x150,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   1030 <getpid@plt>
mov    %eax,%esi
mov    -0x8(%rbp),%edx
lea    0xe3b(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    0x2e15(%rip),%rax
mov    (%rax),%edi
mov    0x2e04(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    120a <main+0x6a>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xe19(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x150(%rbp),%rdi
xor    %esi,%esi
mov    $0x10,%edx
call   1060 <memset@plt>
mov    0x2db6(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x148(%rbp)
mov    $0x270f,%edi
call   1040 <htons@plt>
mov    %eax,-0x14c(%rbp)
lea    0xdf4(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,-0x150(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x150(%rbp),%rsi
mov    $0x10,%edx
call   1090 <bind@plt>
mov    %eax,-0xc(%rbp)
cmpl   $0xffffffff,-0xc(%rbp)
jne    1295 <main+0xf5>
lea    0xdcf(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>
lea    0xdcd(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
lea    -0x140(%rbp),%rdi
mov    $0x12c,%esi
call   10a0 <bzero@plt>
mov    -0x8(%rbp),%edi
lea    -0x140(%rbp),%rsi
mov    $0x12c,%edx
call   1070 <read@plt>
mov    %eax,-0xc(%rbp)
movslq -0xc(%rbp),%rax
movb   $0x0,-0x140(%rbp,%rax,1)
lea    -0x140(%rbp),%rsi
lea    0xd9d(%rip),%rdi
mov    $0x0,%al
call   1050 <printf@plt>
jmp    12a3 <main+0x103>

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x140,%rsp
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe47(%rip),%rdi
mov    %eax,%esi
xor    %eax,%eax
call   1060 <printf@plt>
mov    0x2e1f(%rip),%rbx
mov    (%rbx),%edi
mov    0x2e0e(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10c0 <socket@plt>
cmp    $0xffffffff,%eax
je     1282 <main+0xe2>
mov    %eax,%ebp
lea    0xe70(%rip),%rdi
call   1030 <puts@plt>
movl   $0x0,0xc(%rsp)
mov    (%rbx),%eax
mov    %eax,0x8(%rsp)
mov    $0x270f,%edi
call   1050 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe15(%rip),%rdi
call   1080 <inet_addr@plt>
mov    %eax,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x10,%edx
call   1090 <bind@plt>
cmp    $0xffffffff,%eax
je     128b <main+0xeb>
lea    0xe3f(%rip),%rdi
call   1030 <puts@plt>
lea    0x10(%rsp),%rbx
lea    0xe05(%rip),%r14
nopl   0x0(%rax)
mov    %rbx,%rdi
mov    $0x12c,%esi
call   10a0 <bzero@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x12c,%edx
call   1070 <read@plt>
cltq
movb   $0x0,0x10(%rsp,%rax,1)
mov    %r14,%rdi
mov    %rbx,%rsi
xor    %eax,%eax
call   1060 <printf@plt>
jmp    1250 <main+0xb0>
lea    0xd89(%rip),%rdi
jmp    1292 <main+0xf2>
lea    0xda6(%rip),%rdi
xor    %eax,%eax
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"pid : %d,fd : %d\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [24 x i8] c"socket create error:%m\0A\00"
@.str.2 = [27 x i8] c"socket create success....\0A\00"
@.str.3 = [14 x i8] c"192.168.137.3\00"
@.str.4 = [22 x i8] c"socket bind error:%m\0A\00"
@.str.5 = [25 x i8] c"socket bind success....\0A\00"
@.str.6 = [15 x i8] c"get data : %s\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [300 x i8]
%5 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%6 = call i32 (...) @getpid()
%7 = load i32, i32* %2
%8 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %6, i32 %7)
%9 = load i32, i32* @AF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = load i32, i32* %2
%13 = icmp eq i32 %12, -1
br i1 %13, label %14, label %17
14: ; preds = %0
%15 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%16 = call i32 @exit(i32 -1)
unreachable
17: ; preds = %0
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([27 x i8], [27 x i8]* @.str.2, i64 0, i64 0))
%19 = bitcast %struct.sockaddr_in* %5 to i8*
%20 = load i32, i32* @AF_INET
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 2
store i32 %20, i32* %21
%22 = call i32 @htons(i32 9999)
%23 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 1
store i32 %22, i32* %23
%24 = call i32 @inet_addr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%25 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %5, i32 0, i32 0
%26 = getelementptr inbounds %struct.TYPE_2__, %struct.TYPE_2__* %25, i32 0, i32 0
store i32 %24, i32* %26
%27 = load i32, i32* %2
%28 = bitcast %struct.sockaddr_in* %5 to %struct.sockaddr*
%29 = call i32 @bind(i32 %27, %struct.sockaddr* %28, i32 16)
store i32 %29, i32* %3
%30 = load i32, i32* %3
%31 = icmp eq i32 %30, -1
br i1 %31, label %32, label %35
32: ; preds = %17
%33 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%34 = call i32 @exit(i32 -1)
unreachable
35: ; preds = %17
%36 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.5, i64 0, i64 0))
br label %37
37: ; preds = %35, %37
%38 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%39 = call i32 @bzero(i8* %38, i32 300)
%40 = load i32, i32* %2
%41 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%42 = call i32 @read(i32 %40, i8* %41, i32 300)
store i32 %42, i32* %3
%43 = load i32, i32* %3
%44 = sext i32 %43 to i64
%45 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 %44
store i8 0, i8* %45
%46 = getelementptr inbounds [300 x i8], [300 x i8]* %4, i64 0, i64 0
%47 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %46)
br label %37
}

LLVM IR (-O2):
%struct.sockaddr_in = type { %struct.TYPE_2__, i32, i32, i32 }
%struct.TYPE_2__ = type { i32 }
%struct.sockaddr = type { i32 }
@.str = [18 x i8] c"pid : %d,fd : %d\0A\00"
@AF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@.str.1 = [24 x i8] c"socket create error:%m\0A\00"
@.str.3 = [14 x i8] c"192.168.137.3\00"
@.str.4 = [22 x i8] c"socket bind error:%m\0A\00"
@.str.6 = [15 x i8] c"get data : %s\0A\00"
@str = [26 x i8] c"socket create success....\00"
@str.7 = [24 x i8] c"socket bind success....\00"
define i32 @main() {
%1 = alloca [300 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 0
%4 = tail call i32 (...) @getpid()
%5 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str, i64 0, i64 0), i32 %4, i32 undef)
%6 = load i32, i32* @AF_INET
%7 = load i32, i32* @SOCK_DGRAM
%8 = tail call i32 @socket(i32 %6, i32 %7, i32 0)
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %13
10: ; preds = %0
%11 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str.1, i64 0, i64 0))
%12 = tail call i32 @exit(i32 -1)
unreachable
13: ; preds = %0
%14 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([26 x i8], [26 x i8]* @str, i64 0, i64 0))
%15 = bitcast %struct.sockaddr_in* %2 to i8*
%16 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 3
store i32 0, i32* %16
%17 = load i32, i32* @AF_INET
%18 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %17, i32* %18
%19 = tail call i32 @htons(i32 9999)
%20 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %19, i32* %20
%21 = tail call i32 @inet_addr(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0, i32 0
store i32 %21, i32* %22
%23 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
%24 = call i32 @bind(i32 %8, %struct.sockaddr* %23, i32 16)
%25 = icmp eq i32 %24, -1
br i1 %25, label %26, label %29
26: ; preds = %13
%27 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([22 x i8], [22 x i8]* @.str.4, i64 0, i64 0))
%28 = call i32 @exit(i32 -1)
unreachable
29: ; preds = %13
%30 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @str.7, i64 0, i64 0))
br label %31
31: ; preds = %31, %29
%32 = call i32 @bzero(i8* %3, i32 300)
%33 = call i32 @read(i32 %8, i8* %3, i32 300)
%34 = sext i32 %33 to i64
%35 = getelementptr inbounds [300 x i8], [300 x i8]* %1, i64 0, i64 %34
store i8 0, i8* %35
%36 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0), i8* %3)
br label %31
}