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

/* Variables and functions */
int /*<<< orphan*/ md5(unsigned char *, int, unsigned char *);
int /*<<< orphan*/ memcpy(unsigned char *, unsigned char *, int);
int read_fill(int, unsigned char *, int);
int /*<<< orphan*/ snprintf(char *, int, char *, unsigned char const *);
int strlen(char *);
int write_fill(int, unsigned char *, int);
int confd_ipc_access_check(int sock, const unsigned char *secret) {
  unsigned char chall[1 + 16];
  unsigned char buf[1024];
  int n;
  if ((n = read_fill(sock, chall, sizeof(chall))) != sizeof(chall)) {
    return n;
  }
  if (chall[0] != 11) {
    return 0;
  }
  snprintf((char *)buf, sizeof(buf) - 16, "%s", secret);
  n = strlen((char *)buf);
  memcpy(&buf[n], &chall[1], 16);
  md5(buf, n + 16, &chall[1]);
  chall[0] = 12;
  if ((n = write_fill(sock, chall, sizeof(chall))) != sizeof(chall)) {
    return n;
  }
  if ((n = read_fill(sock, chall, 1)) != 1) {
    return n;
  }
  if (chall[0] != 0) {
    return 0;
  }
  return 1;
}


x86 assembly (clang, -O0):
<confd_ipc_access_check>:
push   %rbp
mov    %rsp,%rbp
sub    $0x440,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x11,%edx
call   1080 <read_fill@plt>
mov    %eax,-0x434(%rbp)
cltq
cmp    $0x11,%rax
je     11a3 <confd_ipc_access_check+0x43>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ad <confd_ipc_access_check+0x14d>
movzbl -0x30(%rbp),%eax
cmp    $0xb,%eax
je     11bc <confd_ipc_access_check+0x5c>
movl   $0x0,-0x4(%rbp)
jmp    12ad <confd_ipc_access_check+0x14d>
lea    -0x430(%rbp),%rdi
mov    -0x10(%rbp),%rcx
mov    $0x3f0,%esi
lea    0xe2d(%rip),%rdx
call   1040 <snprintf@plt>
lea    -0x430(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,-0x434(%rbp)
movslq -0x434(%rbp),%rax
lea    -0x430(%rbp),%rdi
add    %rax,%rdi
lea    -0x30(%rbp),%rsi
add    $0x1,%rsi
mov    $0x10,%edx
call   1050 <memcpy@plt>
lea    -0x430(%rbp),%rdi
mov    -0x434(%rbp),%esi
add    $0x10,%esi
lea    -0x30(%rbp),%rdx
add    $0x1,%rdx
call   1070 <md5@plt>
movb   $0xc,-0x30(%rbp)
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x11,%edx
call   1060 <write_fill@plt>
mov    %eax,-0x434(%rbp)
cltq
cmp    $0x11,%rax
je     125f <confd_ipc_access_check+0xff>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ad <confd_ipc_access_check+0x14d>
mov    -0x8(%rbp),%edi
lea    -0x30(%rbp),%rsi
mov    $0x1,%edx
call   1080 <read_fill@plt>
mov    %eax,-0x434(%rbp)
cmp    $0x1,%eax
je     128d <confd_ipc_access_check+0x12d>
mov    -0x434(%rbp),%eax
mov    %eax,-0x4(%rbp)
jmp    12ad <confd_ipc_access_check+0x14d>
movzbl -0x30(%rbp),%eax
cmp    $0x0,%eax
je     12a6 <confd_ipc_access_check+0x146>
movl   $0x0,-0x4(%rbp)
jmp    12ad <confd_ipc_access_check+0x14d>
movl   $0x1,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x440,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<confd_ipc_access_check>:
push   %rbp
push   %r15
push   %r14
push   %rbx
sub    $0x428,%rsp
mov    %rsi,%r14
mov    %edi,%ebp
mov    %rsp,%rsi
mov    $0x11,%edx
call   1080 <read_fill@plt>
cmp    $0x11,%eax
jne    121a <confd_ipc_access_check+0xba>
xor    %eax,%eax
cmpb   $0xb,(%rsp)
jne    121a <confd_ipc_access_check+0xba>
lea    0xe65(%rip),%rdx
lea    0x20(%rsp),%r15
mov    %r15,%rdi
mov    $0x3f0,%esi
mov    %r14,%rcx
call   1040 <snprintf@plt>
mov    %r15,%rdi
call   1030 <strlen@plt>
movslq %eax,%rbx
lea    (%rsp,%rbx,1),%rdi
add    $0x20,%rdi
lea    0x1(%rsp),%r14
mov    %r14,%rsi
mov    $0x10,%edx
call   1050 <memcpy@plt>
add    $0x10,%ebx
mov    %r15,%rdi
mov    %ebx,%esi
mov    %r14,%rdx
call   1070 <md5@plt>
movb   $0xc,(%rsp)
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x11,%edx
call   1060 <write_fill@plt>
cmp    $0x11,%eax
jne    121a <confd_ipc_access_check+0xba>
mov    %rsp,%rsi
mov    %ebp,%edi
mov    $0x1,%edx
call   1080 <read_fill@plt>
cmp    $0x1,%eax
jne    121a <confd_ipc_access_check+0xba>
xor    %eax,%eax
cmpb   $0x0,(%rsp)
sete   %al
add    $0x428,%rsp
pop    %rbx
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
define i32 @confd_ipc_access_check(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca [17 x i8]
%7 = alloca [1024 x i8]
%8 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
%9 = load i32, i32* %4
%10 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%11 = call i32 @read_fill(i32 %9, i8* %10, i32 17)
store i32 %11, i32* %8
%12 = sext i32 %11 to i64
%13 = icmp ne i64 %12, 17
br i1 %13, label %14, label %16
14: ; preds = %2
%15 = load i32, i32* %8
store i32 %15, i32* %3
br label %60
16: ; preds = %2
%17 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%18 = load i8, i8* %17
%19 = zext i8 %18 to i32
%20 = icmp ne i32 %19, 11
br i1 %20, label %21, label %22
21: ; preds = %16
store i32 0, i32* %3
br label %60
22: ; preds = %16
%23 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%24 = load i8*, i8** %5
%25 = call i32 @snprintf(i8* %23, i32 1008, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %24)
%26 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
store i32 %27, i32* %8
%28 = load i32, i32* %8
%29 = sext i32 %28 to i64
%30 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 %29
%31 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 1
%32 = call i32 @memcpy(i8* %30, i8* %31, i32 16)
%33 = getelementptr inbounds [1024 x i8], [1024 x i8]* %7, i64 0, i64 0
%34 = load i32, i32* %8
%35 = add nsw i32 %34, 16
%36 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 1
%37 = call i32 @md5(i8* %33, i32 %35, i8* %36)
%38 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
store i8 12, i8* %38
%39 = load i32, i32* %4
%40 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%41 = call i32 @write_fill(i32 %39, i8* %40, i32 17)
store i32 %41, i32* %8
%42 = sext i32 %41 to i64
%43 = icmp ne i64 %42, 17
br i1 %43, label %44, label %46
44: ; preds = %22
%45 = load i32, i32* %8
store i32 %45, i32* %3
br label %60
46: ; preds = %22
%47 = load i32, i32* %4
%48 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%49 = call i32 @read_fill(i32 %47, i8* %48, i32 1)
store i32 %49, i32* %8
%50 = icmp ne i32 %49, 1
br i1 %50, label %51, label %53
51: ; preds = %46
%52 = load i32, i32* %8
store i32 %52, i32* %3
br label %60
53: ; preds = %46
%54 = getelementptr inbounds [17 x i8], [17 x i8]* %6, i64 0, i64 0
%55 = load i8, i8* %54
%56 = zext i8 %55 to i32
%57 = icmp ne i32 %56, 0
br i1 %57, label %58, label %59
58: ; preds = %53
store i32 0, i32* %3
br label %60
59: ; preds = %53
store i32 1, i32* %3
br label %60
60: ; preds = %59, %58, %51, %44, %21, %14
%61 = load i32, i32* %3
ret i32 %61
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
define i32 @confd_ipc_access_check(i32 %0, i8* %1) {
%3 = alloca [17 x i8]
%4 = alloca [1024 x i8]
%5 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 0
%6 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 0
%7 = call i32 @read_fill(i32 %0, i8* %5, i32 17)
%8 = icmp eq i32 %7, 17
br i1 %8, label %9, label %30
9: ; preds = %2
%10 = load i8, i8* %5
%11 = icmp eq i8 %10, 11
br i1 %11, label %12, label %30
12: ; preds = %9
%13 = call i32 @snprintf(i8* %6, i32 1008, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %1)
%14 = call i32 @strlen(i8* %6)
%15 = sext i32 %14 to i64
%16 = getelementptr inbounds [1024 x i8], [1024 x i8]* %4, i64 0, i64 %15
%17 = getelementptr inbounds [17 x i8], [17 x i8]* %3, i64 0, i64 1
%18 = call i32 @memcpy(i8* %16, i8* %17, i32 16)
%19 = add nsw i32 %14, 16
%20 = call i32 @md5(i8* %6, i32 %19, i8* %17)
store i8 12, i8* %5
%21 = call i32 @write_fill(i32 %0, i8* %5, i32 17)
%22 = icmp eq i32 %21, 17
br i1 %22, label %23, label %30
23: ; preds = %12
%24 = call i32 @read_fill(i32 %0, i8* %5, i32 1)
%25 = icmp eq i32 %24, 1
br i1 %25, label %26, label %30
26: ; preds = %23
%27 = load i8, i8* %5
%28 = icmp eq i8 %27, 0
%29 = zext i1 %28 to i32
br label %30
30: ; preds = %26, %23, %12, %9, %2
%31 = phi i32 [ %7, %2 ], [ 0, %9 ], [ %21, %12 ], [ %24, %23 ], [ %29, %26 ]
ret i32 %31
}