#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*/ msg;

/* Variables and functions */
int /*<<< orphan*/ logMsg(char *, int);
int /*<<< orphan*/ memset(char *, char, int);
int /*<<< orphan*/ putchar(char);
int read(int, char *, int);
int /*<<< orphan*/ sprintf(char *, char *, int, unsigned int);
int /*<<< orphan*/ strlen(char *);
scalar_t__ write(int, char *, int /*<<< orphan*/);
int doServer(int sock) {
  char msg[128];
  int bytes_read = 0;
  memset(msg, '\0', 128);
  putchar('3');
  putchar('\n');
  if ((bytes_read = read(sock, msg, sizeof(msg))) <= 0) {
    return bytes_read;
  }
  putchar('4');
  putchar('\n');
  if (bytes_read == 0 || (bytes_read == 1 && msg[0] == '\n')) {
    return 0;
  }
  msg[bytes_read] = '\0';
  putchar('5');
  putchar('\n');
  logMsg(msg, bytes_read);
  putchar('7');
  putchar('\n');
  memset(msg, '\0', 128);
  sprintf(msg, "Msg of %3uB recv'd and logged, secret: 0x%08x\n", bytes_read,
          (unsigned int)msg);
  if (write(sock, msg, strlen(msg)) < 0) {
    return -1;
  }
  return bytes_read;
}


x86 assembly (clang, -O0):
<doServer>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
mov    %edi,-0x8(%rbp)
movl   $0x0,-0x94(%rbp)
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    $0x33,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    -0x8(%rbp),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x94(%rbp)
cmp    $0x0,%eax
jg     11e0 <doServer+0x70>
mov    -0x94(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12fe <doServer+0x18e>
mov    $0x34,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmpl   $0x0,-0x94(%rbp)
je     121e <doServer+0xae>
cmpl   $0x1,-0x94(%rbp)
jne    122a <doServer+0xba>
movsbl -0x90(%rbp),%eax
cmp    $0xa,%eax
jne    122a <doServer+0xba>
movl   $0x0,-0x4(%rbp)
jmp    12fe <doServer+0x18e>
movslq -0x94(%rbp),%rax
movb   $0x0,-0x90(%rbp,%rax,1)
mov    $0x35,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    -0x90(%rbp),%rdi
mov    -0x94(%rbp),%esi
call   1080 <logMsg@plt>
mov    $0x37,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
lea    -0x90(%rbp),%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    -0x90(%rbp),%rdi
mov    -0x94(%rbp),%edx
lea    -0x90(%rbp),%rax
mov    %eax,%ecx
lea    0xd5d(%rip),%rsi
call   1090 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0xa4(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0xa0(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0xa4(%rbp),%edi
mov    -0xa0(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
cmp    $0x0,%rax
jge    12f5 <doServer+0x185>
movl   $0xffffffff,-0x4(%rbp)
jmp    12fe <doServer+0x18e>
mov    -0x94(%rbp),%eax
mov    %eax,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0xb0,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<doServer>:
push   %rbp
push   %r14
push   %rbx
sub    $0x80,%rsp
mov    %edi,%r14d
mov    %rsp,%rbx
xor    %ebp,%ebp
mov    %rbx,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
mov    $0x33,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %r14d,%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,%ebx
test   %eax,%eax
jle    1262 <doServer+0xf2>
mov    $0x34,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
cmp    $0x1,%ebx
jne    11e3 <doServer+0x73>
cmpb   $0xa,(%rsp)
je     1264 <doServer+0xf4>
mov    %ebx,%eax
movb   $0x0,(%rsp,%rax,1)
mov    $0x35,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rsp,%rbp
mov    %rbp,%rdi
mov    %ebx,%esi
call   1080 <logMsg@plt>
mov    $0x37,%edi
call   1030 <putchar@plt>
mov    $0xa,%edi
call   1030 <putchar@plt>
mov    %rbp,%rdi
xor    %esi,%esi
mov    $0x80,%edx
call   1060 <memset@plt>
lea    0xdcc(%rip),%rsi
mov    %rbp,%rdi
mov    %ebx,%edx
mov    %ebp,%ecx
call   1090 <sprintf@plt>
mov    %rbp,%rdi
call   1050 <strlen@plt>
mov    %r14d,%edi
mov    %rbp,%rsi
mov    %eax,%edx
call   1040 <write@plt>
test   %rax,%rax
mov    $0xffffffff,%ebp
cmovns %ebx,%ebp
jmp    1264 <doServer+0xf4>
mov    %ebx,%ebp
mov    %ebp,%eax
add    $0x80,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [47 x i8] c"Msg of %3uB recv'd and logged, secret: 0x%08x\0A\00"
define i32 @doServer(i32 %0) {
%2 = alloca i32
%3 = alloca i32
%4 = alloca [128 x i8]
%5 = alloca i32
store i32 %0, i32* %3
store i32 0, i32* %5
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%7 = call i32 @memset(i8* %6, i8 signext 0, i32 128)
%8 = call i32 @putchar(i8 signext 51)
%9 = call i32 @putchar(i8 signext 10)
%10 = load i32, i32* %3
%11 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%12 = call i32 @read(i32 %10, i8* %11, i32 128)
store i32 %12, i32* %5
%13 = icmp sle i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %1
%15 = load i32, i32* %5
store i32 %15, i32* %2
br label %57
16: ; preds = %1
%17 = call i32 @putchar(i8 signext 52)
%18 = call i32 @putchar(i8 signext 10)
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, 0
br i1 %20, label %29, label %21
21: ; preds = %16
%22 = load i32, i32* %5
%23 = icmp eq i32 %22, 1
br i1 %23, label %24, label %30
24: ; preds = %21
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%26 = load i8, i8* %25
%27 = sext i8 %26 to i32
%28 = icmp eq i32 %27, 10
br i1 %28, label %29, label %30
29: ; preds = %24, %16
store i32 0, i32* %2
br label %57
30: ; preds = %24, %21
%31 = load i32, i32* %5
%32 = sext i32 %31 to i64
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 %32
store i8 0, i8* %33
%34 = call i32 @putchar(i8 signext 53)
%35 = call i32 @putchar(i8 signext 10)
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%37 = load i32, i32* %5
%38 = call i32 @logMsg(i8* %36, i32 %37)
%39 = call i32 @putchar(i8 signext 55)
%40 = call i32 @putchar(i8 signext 10)
%41 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%42 = call i32 @memset(i8* %41, i8 signext 0, i32 128)
%43 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%44 = load i32, i32* %5
%45 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%46 = ptrtoint i8* %45 to i32
%47 = call i32 @sprintf(i8* %43, i8* getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %44, i32 %46)
%48 = load i32, i32* %3
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%50 = getelementptr inbounds [128 x i8], [128 x i8]* %4, i64 0, i64 0
%51 = call i32 @strlen(i8* %50)
%52 = call i64 @write(i32 %48, i8* %49, i32 %51)
%53 = icmp slt i64 %52, 0
br i1 %53, label %54, label %55
54: ; preds = %30
store i32 -1, i32* %2
br label %57
55: ; preds = %30
%56 = load i32, i32* %5
store i32 %56, i32* %2
br label %57
57: ; preds = %55, %54, %29, %14
%58 = load i32, i32* %2
ret i32 %58
}

LLVM IR (-O2):
@.str = [47 x i8] c"Msg of %3uB recv'd and logged, secret: 0x%08x\0A\00"
define i32 @doServer(i32 %0) {
%2 = alloca [128 x i8]
%3 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 0
%4 = call i32 @memset(i8* %3, i8 signext 0, i32 128)
%5 = call i32 @putchar(i8 signext 51)
%6 = call i32 @putchar(i8 signext 10)
%7 = call i32 @read(i32 %0, i8* %3, i32 128)
%8 = icmp slt i32 %7, 1
br i1 %8, label %32, label %9
9: ; preds = %1
%10 = call i32 @putchar(i8 signext 52)
%11 = call i32 @putchar(i8 signext 10)
%12 = icmp eq i32 %7, 1
%13 = load i8, i8* %3
%14 = icmp eq i8 %13, 10
%15 = select i1 %12, i1 %14, i1 false
br i1 %15, label %32, label %16
16: ; preds = %9
%17 = zext i32 %7 to i64
%18 = getelementptr inbounds [128 x i8], [128 x i8]* %2, i64 0, i64 %17
store i8 0, i8* %18
%19 = call i32 @putchar(i8 signext 53)
%20 = call i32 @putchar(i8 signext 10)
%21 = call i32 @logMsg(i8* %3, i32 %7)
%22 = call i32 @putchar(i8 signext 55)
%23 = call i32 @putchar(i8 signext 10)
%24 = call i32 @memset(i8* %3, i8 signext 0, i32 128)
%25 = ptrtoint [128 x i8]* %2 to i64
%26 = trunc i64 %25 to i32
%27 = call i32 @sprintf(i8* %3, i8* dereferenceable(1) getelementptr inbounds ([47 x i8], [47 x i8]* @.str, i64 0, i64 0), i32 %7, i32 %26)
%28 = call i32 @strlen(i8* %3)
%29 = call i64 @write(i32 %0, i8* %3, i32 %28)
%30 = icmp slt i64 %29, 0
%31 = select i1 %30, i32 -1, i32 %7
br label %32
32: ; preds = %16, %9, %1
%33 = phi i32 [ %7, %1 ], [ 0, %9 ], [ %31, %16 ]
ret i32 %33
}