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

/* Variables and functions */
int /*<<< orphan*/ EXIT_FAILURE;
int /*<<< orphan*/ errno;
int /*<<< orphan*/ exit(int /*<<< orphan*/);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, char *, int, char *,
                           char *);
int /*<<< orphan*/ memset(char *, int, int);
int /*<<< orphan*/ printf(char *, ...);
int read(int, char *, int);
int /*<<< orphan*/ stderr;
char *strerror(int /*<<< orphan*/);
void do_service(int connfd) {
  int ret;
  char readbuf[1024] = {};
  while (1) {
    if ((ret = read(connfd, readbuf, sizeof(readbuf))) == -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/day11/thread.c",
                24, "read", strerror(errno));
        exit(EXIT_FAILURE);
      } while (0);
    else if (ret == 0) {
      printf("cli close!\n");
      break;
    }
    printf("%s\n", readbuf);
    memset(readbuf, 0x00, sizeof(readbuf));
  }
}


x86 assembly (clang, -O0):
<do_service>:
push   %rbp
mov    %rsp,%rbp
sub    $0x420,%rsp
mov    %edi,-0x4(%rbp)
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
mov    -0x4(%rbp),%edi
lea    -0x410(%rbp),%rsi
mov    $0x400,%edx
call   1050 <read@plt>
mov    %eax,-0x8(%rbp)
cmp    $0xffffffff,%eax
jne    11f9 <do_service+0x99>
jmp    11a6 <do_service+0x46>
mov    0x2e33(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x414(%rbp)
mov    0x2e1c(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
mov    -0x414(%rbp),%edi
mov    %rax,%r9
lea    0xe2d(%rip),%rsi
lea    0xe36(%rip),%rdx
mov    $0x18,%ecx
lea    0xebf(%rip),%r8
call   1060 <fprintf@plt>
mov    0x2dde(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>
cmpl   $0x0,-0x8(%rbp)
jne    1216 <do_service+0xb6>
lea    0xea0(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
jmp    1248 <do_service+0xe8>
jmp    121b <do_service+0xbb>
lea    -0x410(%rbp),%rsi
lea    0xde3(%rip),%rdi
mov    $0x0,%al
call   1030 <printf@plt>
lea    -0x410(%rbp),%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
jmp    1181 <do_service+0x21>
add    $0x420,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<do_service>:
push   %rbp
push   %rbx
sub    $0x408,%rsp
mov    %edi,%ebp
mov    %rsp,%rbx
mov    $0x400,%edx
mov    %rbx,%rdi
xor    %esi,%esi
call   1040 <memset@plt>
nopl   (%rax)
mov    %ebp,%edi
mov    %rbx,%rsi
mov    $0x400,%edx
call   1050 <read@plt>
test   %eax,%eax
je     11b1 <do_service+0x51>
cmp    $0xffffffff,%eax
je     11c7 <do_service+0x67>
mov    %rbx,%rdi
call   1030 <puts@plt>
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x400,%edx
call   1040 <memset@plt>
jmp    1180 <do_service+0x20>
lea    0xef2(%rip),%rdi
call   1030 <puts@plt>
add    $0x408,%rsp
pop    %rbx
pop    %rbp
ret
mov    0x2e12(%rip),%rax
mov    (%rax),%ebx
mov    0x2e01(%rip),%rax
mov    (%rax),%edi
call   1080 <strerror@plt>
lea    0xe1b(%rip),%rsi
lea    0xe24(%rip),%rdx
lea    0xeb2(%rip),%r8
mov    %ebx,%edi
mov    $0x18,%ecx
mov    %rax,%r9
call   1060 <fprintf@plt>
mov    0x2dc7(%rip),%rax
mov    (%rax),%edi
call   1070 <exit@plt>

LLVM IR (-O0):
@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/day11/thread.c\00"
@.str.2 = [5 x i8] c"read\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@.str.3 = [12 x i8] c"cli close!\0A\00"
@.str.4 = [4 x i8] c"%s\0A\00"
define void @do_service(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [1024 x i8]
store i32 %0, i32* %2
%5 = bitcast [1024 x i8]* %4 to i8*
br label %6
6: ; preds = %1, %26
%7 = load i32, i32* %2
%8 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%9 = call i32 @read(i32 %7, i8* %8, i32 1024)
store i32 %9, i32* %3
%10 = icmp eq i32 %9, -1
br i1 %10, label %11, label %20
11: ; preds = %6
br label %12
12: ; preds = %11
%13 = load i32, i32* @stderr
%14 = load i32, i32* @errno
%15 = call i8* @strerror(i32 %14)
%16 = call i32 @fprintf(i32 %13, 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 24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %15)
%17 = load i32, i32* @EXIT_FAILURE
%18 = call i32 @exit(i32 %17)
unreachable
19: ; No predecessors!
br label %26
20: ; preds = %6
%21 = load i32, i32* %3
%22 = icmp eq i32 %21, 0
br i1 %22, label %23, label %25
23: ; preds = %20
%24 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str.3, i64 0, i64 0))
br label %31
25: ; preds = %20
br label %26
26: ; preds = %25, %19
%27 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%28 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.4, i64 0, i64 0), i8* %27)
%29 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%30 = call i32 @memset(i8* %29, i32 0, i32 1024)
br label %6
31: ; preds = %23
ret void
}

LLVM IR (-O2):
@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/day11/thread.c\00"
@.str.2 = [5 x i8] c"read\00"
@errno = global i32 0
@EXIT_FAILURE = global i32 0
@str = [11 x i8] c"cli close!\00"
define void @do_service(i32 %0) {
%2 = alloca [1024 x i8]
%3 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
br label %4
4: ; preds = %15, %1
%5 = call i32 @read(i32 %0, i8* %3, i32 1024)
switch i32 %5, label %15 [
i32 -1, label %6
i32 0, label %13
]
6: ; preds = %4
%7 = load i32, i32* @stderr
%8 = load i32, i32* @errno
%9 = call i8* @strerror(i32 %8)
%10 = call i32 @fprintf(i32 %7, 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 24, i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.2, i64 0, i64 0), i8* %9)
%11 = load i32, i32* @EXIT_FAILURE
%12 = call i32 @exit(i32 %11)
unreachable
13: ; preds = %4
%14 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([11 x i8], [11 x i8]* @str, i64 0, i64 0))
ret void
15: ; preds = %4
%16 = call i32 @puts(i8* %3)
%17 = call i32 @memset(i8* %3, i32 0, i32 1024)
br label %4
}