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

/* Variables and functions */
int /*<<< orphan*/ AF_INET;
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ PF_INET;
int /*<<< orphan*/ SOCK_DGRAM;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ *fgets(char *, int, int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ htons(int);
int /*<<< orphan*/ inet_aton(char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ recvfrom(int, char *, int, int /*<<< orphan*/,
                            int /*<<< orphan*/ *, int /*<<< orphan*/ *);
int /*<<< orphan*/ sendto(int, char *, int, int /*<<< orphan*/,
                          struct sockaddr *, int);
int socket(int /*<<< orphan*/, int /*<<< orphan*/, int /*<<< orphan*/);
int /*<<< orphan*/ stderr;
int /*<<< orphan*/ stdin;
char *strerror(int /*<<< orphan*/);
int main(void) {
  int fd;
  int ret;
  socklen_t len;
  char buf[1024] = {};
  struct sockaddr_in addr = {};
  if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) == -1)
    do {
      fprintf(stderr, "[%s][%d]:%s %s\n",
              "/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/"
              "2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/"
              "master/apue/day13/updcli.c",
              26, "socket", strerror(errno));
      exit(EXIT_FAILURE);
    } while (0);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(9999);
  inet_aton("192.168.0.200", &addr.sin_addr);
  len = sizeof(addr);
  while (fgets(buf, 1024, stdin) != NULL) {
    sendto(fd, buf, sizeof(buf), 0, (struct sockaddr *)&addr, len);
    memset(buf, 0x00, sizeof(buf));
    recvfrom(fd, buf, sizeof(buf), 0, NULL, NULL);
    printf("%s", buf);
    memset(buf, 0x00, sizeof(buf));
  }
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
lea    -0x420(%rbp),%rdi
xor    %esi,%esi
mov    $0xc,%edx
call   1050 <memset@plt>
mov    0x2dc9(%rip),%rax
mov    (%rax),%edi
mov    0x2de0(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    1265 <main+0xb5>
jmp    1212 <main+0x62>
mov    0x2daf(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x424(%rbp)
mov    0x2d98(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
mov    -0x424(%rbp),%edi
mov    %rax,%r9
lea    0xdc1(%rip),%rsi
lea    0xdca(%rip),%rdx
mov    $0x1a,%ecx
lea    0xe53(%rip),%r8
call   1080 <fprintf@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>
mov    0x2d7c(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x418(%rbp)
mov    $0x270f,%edi
call   1030 <htons@plt>
mov    %eax,-0x41c(%rbp)
lea    -0x420(%rbp),%rsi
lea    0xe1a(%rip),%rdi
call   1090 <inet_aton@plt>
movl   $0xc,-0x10(%rbp)
lea    -0x410(%rbp),%rdi
mov    0x2d34(%rip),%rax
mov    (%rax),%edx
mov    $0x400,%esi
call   1070 <fgets@plt>
cmp    $0x0,%rax
je     133f <main+0x18f>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
lea    -0x420(%rbp),%r8
mov    -0x10(%rbp),%r9d
mov    $0x400,%edx
xor    %ecx,%ecx
call   1060 <sendto@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    -0x8(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %eax,%eax
mov    %eax,%r9d
mov    %r9,%r8
call   10a0 <recvfrom@plt>
lea    -0x410(%rbp),%rsi
lea    0xd98(%rip),%rdi
call   1040 <printf@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
jmp    129e <main+0xee>
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x410,%rsp
lea    0x10(%rsp),%rdi
mov    $0x400,%edx
xor    %esi,%esi
call   1050 <memset@plt>
movl   $0x0,(%rsp)
mov    0x2dda(%rip),%rax
mov    (%rax),%edi
mov    0x2df1(%rip),%rax
mov    (%rax),%esi
xor    %edx,%edx
call   10d0 <socket@plt>
cmp    $0xffffffff,%eax
je     12e2 <main+0x132>
mov    %eax,%ebp
mov    0x2de6(%rip),%rax
mov    (%rax),%eax
mov    %eax,0x8(%rsp)
mov    $0x270f,%edi
call   1030 <htons@plt>
mov    %eax,0x4(%rsp)
lea    0xe8f(%rip),%rdi
mov    %rsp,%rsi
call   1090 <inet_aton@plt>
mov    0x2db4(%rip),%r12
mov    (%r12),%edx
lea    0x10(%rsp),%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
test   %rax,%rax
je     12d0 <main+0x120>
lea    0x10(%rsp),%rbx
mov    %rsp,%r14
lea    0xe63(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
mov    %r14,%r8
mov    $0xc,%r9d
call   1060 <sendto@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
xor    %ecx,%ecx
xor    %r8d,%r8d
xor    %r9d,%r9d
call   10a0 <recvfrom@plt>
mov    %r15,%rdi
mov    %rbx,%rsi
call   1040 <printf@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1050 <memset@plt>
mov    (%r12),%edx
mov    %rbx,%rdi
mov    $0x400,%esi
call   1070 <fgets@plt>
test   %rax,%rax
jne    1260 <main+0xb0>
xor    %eax,%eax
add    $0x410,%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret
mov    0x2cdf(%rip),%rax
mov    (%rax),%ebx
mov    0x2cce(%rip),%rax
mov    (%rax),%edi
call   10c0 <strerror@plt>
lea    0xd00(%rip),%rsi
lea    0xd09(%rip),%rdx
lea    0xd97(%rip),%r8
mov    %ebx,%edi
mov    $0x1a,%ecx
mov    %rax,%r9
call   1080 <fprintf@plt>
mov    0x2c8c(%rip),%rax
mov    (%rax),%edi
call   10b0 <exit@plt>

LLVM IR (-O0):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [149 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day13/updcli.c\00"
@.str.2 = [7 x i8] c"socket\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@.str.3 = [14 x i8] c"192.168.0.200\00"
@stdin = global i32 0
@.str.4 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca i32
%5 = alloca [1024 x i8]
%6 = alloca %struct.sockaddr_in
store i32 0, i32* %1
%7 = bitcast [1024 x i8]* %5 to i8*
%8 = bitcast %struct.sockaddr_in* %6 to i8*
%9 = load i32, i32* @PF_INET
%10 = load i32, i32* @SOCK_DGRAM
%11 = call i32 @socket(i32 %9, i32 %10, i32 0)
store i32 %11, i32* %2
%12 = icmp eq i32 %11, -1
br i1 %12, label %13, label %22
13: ; preds = %0
br label %14
14: ; preds = %13
%15 = load i32, i32* @stderr
%16 = load i32, i32* @errno
%17 = call i8* @strerror(i32 %16)
%18 = call i32 @fprintf(i32 %15, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.1, i64 0, i64 0), i32 26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; No predecessors!
br label %22
22: ; preds = %21, %0
%23 = load i32, i32* @AF_INET
%24 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 2
store i32 %23, i32* %24
%25 = call i32 @htons(i32 9999)
%26 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 1
store i32 %25, i32* %26
%27 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %6, i32 0, i32 0
%28 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32* %27)
store i32 12, i32* %4
br label %29
29: ; preds = %34, %22
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%31 = load i32, i32* @stdin
%32 = call i32* @fgets(i8* %30, i32 1024, i32 %31)
%33 = icmp ne i32* %32, null
br i1 %33, label %34, label %49
34: ; preds = %29
%35 = load i32, i32* %2
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%37 = bitcast %struct.sockaddr_in* %6 to %struct.sockaddr*
%38 = load i32, i32* %4
%39 = call i32 @sendto(i32 %35, i8* %36, i32 1024, i32 0, %struct.sockaddr* %37, i32 %38)
%40 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%41 = call i32 @memset(i8* %40, i32 0, i32 1024)
%42 = load i32, i32* %2
%43 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%44 = call i32 @recvfrom(i32 %42, i8* %43, i32 1024, i32 0, i32* null, i32* null)
%45 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%46 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %45)
%47 = getelementptr inbounds [1024 x i8], [1024 x i8]* %5, i64 0, i64 0
%48 = call i32 @memset(i8* %47, i32 0, i32 1024)
br label %29
49: ; preds = %29
%50 = load i32, i32* %1
ret i32 %50
}

LLVM IR (-O2):
%struct.sockaddr_in = type { i32, i32, i32 }
%struct.sockaddr = type { i32 }
@PF_INET = global i32 0
@SOCK_DGRAM = global i32 0
@stderr = global i32 0
@.str = [16 x i8] c"[%s][%d]:%s %s\0A\00"
@.str.1 = [149 x i8] c"/scratch/repos/new/home/jordi_armengol_estape/c-scraper/outputs/2022-01-22/02-19-57/repos/drsin/linuxCoreLearn/refs/heads/master/apue/day13/updcli.c\00"
@.str.2 = [7 x i8] c"socket\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@AF_INET = global i32 0
@.str.3 = [14 x i8] c"192.168.0.200\00"
@stdin = global i32 0
@.str.4 = [3 x i8] c"%s\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.sockaddr_in
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%4 = bitcast %struct.sockaddr_in* %2 to i8*
%5 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
store i32 0, i32* %5
%6 = load i32, i32* @PF_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 %17
10: ; preds = %0
%11 = load i32, i32* @stderr
%12 = load i32, i32* @errno
%13 = tail call i8* @strerror(i32 %12)
%14 = tail call i32 @fprintf(i32 %11, i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([149 x i8], [149 x i8]* @.str.1, i64 0, i64 0), i32 26, i8* getelementptr inbounds ([7 x i8], [7 x i8]* @.str.2, i64 0, i64 0), i8* %13)
%15 = load i32, i32* @EXIT_FAILURE
%16 = tail call i32 @exit(i32 %15)
unreachable
17: ; preds = %0
%18 = load i32, i32* @AF_INET
%19 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 2
store i32 %18, i32* %19
%20 = tail call i32 @htons(i32 9999)
%21 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 1
store i32 %20, i32* %21
%22 = getelementptr inbounds %struct.sockaddr_in, %struct.sockaddr_in* %2, i64 0, i32 0
%23 = call i32 @inet_aton(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0), i32* %22)
%24 = load i32, i32* @stdin
%25 = call i32* @fgets(i8* %3, i32 1024, i32 %24)
%26 = icmp eq i32* %25, null
br i1 %26, label %38, label %27
27: ; preds = %17
%28 = bitcast %struct.sockaddr_in* %2 to %struct.sockaddr*
br label %29
29: ; preds = %27, %29
%30 = call i32 @sendto(i32 %8, i8* %3, i32 1024, i32 0, %struct.sockaddr* %28, i32 12)
%31 = call i32 @memset(i8* %3, i32 0, i32 1024)
%32 = call i32 @recvfrom(i32 %8, i8* %3, i32 1024, i32 0, i32* null, i32* null)
%33 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.4, i64 0, i64 0), i8* %3)
%34 = call i32 @memset(i8* %3, i32 0, i32 1024)
%35 = load i32, i32* @stdin
%36 = call i32* @fgets(i8* %3, i32 1024, i32 %35)
%37 = icmp eq i32* %36, null
br i1 %37, label %38, label %29
38: ; preds = %29, %17
ret i32 0
}