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

/* Variables and functions */
int /*<<< orphan*/ dd_log(int, char *, ...);
int read(int, char *, int);
int strlen(char *);
scalar_t__ strncmp(char *, char *, int);
int check_string(int fd, char *string) {
  int buffer_read_bytes = 0;
  int read_size = strlen(string);
  char read_buffer[read_size + 1];
  if ((buffer_read_bytes = read(fd, read_buffer, read_size)) == -1) {
    dd_log(10, "check_string: Failed to read");
    return -1;
  }
  if (buffer_read_bytes != read_size) {
    dd_log(10, "check_string: Failed to read %llu bytes, actual read was %llu",
           read_size, buffer_read_bytes);
    return -1;
  }
  dd_log(1, "check_string: read '%s'", read_buffer);
  return (strncmp(string, read_buffer, read_size) == 0);
}


x86 assembly (clang, -O0):
<check_string>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
mov    -0x10(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x18(%rbp)
mov    -0x18(%rbp),%eax
add    $0x1,%eax
mov    %rsp,%rcx
mov    %rcx,-0x20(%rbp)
mov    %rax,%rcx
add    $0xf,%rcx
and    $0xfffffffffffffff0,%rcx
mov    %rsp,%rsi
sub    %rcx,%rsi
mov    %rsi,-0x38(%rbp)
mov    %rsi,%rsp
mov    %rax,-0x28(%rbp)
mov    -0x8(%rbp),%edi
mov    -0x18(%rbp),%edx
call   1060 <read@plt>
mov    %eax,-0x14(%rbp)
cmp    $0xffffffff,%eax
jne    11c8 <check_string+0x88>
mov    $0xa,%edi
lea    0xe52(%rip),%rsi
mov    $0x0,%al
call   1030 <dd_log@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    123d <check_string+0xfd>
mov    -0x14(%rbp),%eax
cmp    -0x18(%rbp),%eax
je     1200 <check_string+0xc0>
mov    -0x18(%rbp),%edx
mov    -0x14(%rbp),%ecx
mov    $0xa,%edi
lea    0xe37(%rip),%rsi
mov    $0x0,%al
call   1030 <dd_log@plt>
movl   $0xffffffff,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
jmp    123d <check_string+0xfd>
mov    -0x38(%rbp),%rdx
mov    $0x1,%edi
lea    0xe4b(%rip),%rsi
mov    $0x0,%al
call   1030 <dd_log@plt>
mov    -0x38(%rbp),%rsi
mov    -0x10(%rbp),%rdi
mov    -0x18(%rbp),%edx
call   1040 <strncmp@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%eax
mov    %eax,-0x4(%rbp)
movl   $0x1,-0x2c(%rbp)
mov    -0x20(%rbp),%rax
mov    %rax,%rsp
mov    -0x4(%rbp),%eax
mov    %rbp,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<check_string>:
push   %rbp
mov    %rsp,%rbp
push   %r15
push   %r14
push   %r12
push   %rbx
mov    %rsi,%r14
mov    %edi,%r12d
mov    %rsi,%rdi
call   1050 <strlen@plt>
mov    %eax,%r15d
lea    0x1(%r15),%eax
mov    %rsp,%rbx
add    $0xf,%rax
and    $0xfffffffffffffff0,%rax
sub    %rax,%rbx
mov    %rbx,%rsp
mov    %r12d,%edi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1060 <read@plt>
cmp    $0xffffffff,%eax
je     11ba <check_string+0x7a>
cmp    %r15d,%eax
jne    11cf <check_string+0x8f>
lea    0xecb(%rip),%rsi
mov    $0x1,%edi
mov    %rbx,%rdx
xor    %eax,%eax
call   1030 <dd_log@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
mov    %r15d,%edx
call   1040 <strncmp@plt>
mov    %rax,%rcx
xor    %eax,%eax
test   %rcx,%rcx
sete   %al
jmp    11ec <check_string+0xac>
lea    0xe3f(%rip),%rsi
mov    $0xa,%edi
xor    %eax,%eax
call   1030 <dd_log@plt>
jmp    11e7 <check_string+0xa7>
lea    0xe47(%rip),%rsi
mov    $0xa,%edi
mov    %r15d,%edx
mov    %eax,%ecx
xor    %eax,%eax
call   1030 <dd_log@plt>
mov    $0xffffffff,%eax
lea    -0x20(%rbp),%rsp
pop    %rbx
pop    %r12
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [29 x i8] c"check_string: Failed to read\00"
@.str.1 = [62 x i8] c"check_string: Failed to read %llu bytes, actual read was %llu\00"
@.str.2 = [24 x i8] c"check_string: read '%s'\00"
define i32 @check_string(i32 %0, i8* %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8*
%6 = alloca i32
%7 = alloca i32
%8 = alloca i8*
%9 = alloca i64
%10 = alloca i32
store i32 %0, i32* %4
store i8* %1, i8** %5
store i32 0, i32* %6
%11 = load i8*, i8** %5
%12 = call i32 @strlen(i8* %11)
store i32 %12, i32* %7
%13 = load i32, i32* %7
%14 = add nsw i32 %13, 1
%15 = zext i32 %14 to i64
%16 = call i8* @llvm.stacksave()
store i8* %16, i8** %8
%17 = alloca i8, i64 %15
store i64 %15, i64* %9
%18 = load i32, i32* %4
%19 = load i32, i32* %7
%20 = call i32 @read(i32 %18, i8* %17, i32 %19)
store i32 %20, i32* %6
%21 = icmp eq i32 %20, -1
br i1 %21, label %22, label %24
22: ; preds = %2
%23 = call i32 (i32, i8*, ...) @dd_log(i32 10, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
store i32 -1, i32* %3
store i32 1, i32* %10
br label %39
24: ; preds = %2
%25 = load i32, i32* %6
%26 = load i32, i32* %7
%27 = icmp ne i32 %25, %26
br i1 %27, label %28, label %32
28: ; preds = %24
%29 = load i32, i32* %7
%30 = load i32, i32* %6
%31 = call i32 (i32, i8*, ...) @dd_log(i32 10, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %29, i32 %30)
store i32 -1, i32* %3
store i32 1, i32* %10
br label %39
32: ; preds = %24
%33 = call i32 (i32, i8*, ...) @dd_log(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %17)
%34 = load i8*, i8** %5
%35 = load i32, i32* %7
%36 = call i64 @strncmp(i8* %34, i8* %17, i32 %35)
%37 = icmp eq i64 %36, 0
%38 = zext i1 %37 to i32
store i32 %38, i32* %3
store i32 1, i32* %10
br label %39
39: ; preds = %32, %28, %22
%40 = load i8*, i8** %8
%41 = load i32, i32* %3
ret i32 %41
}

LLVM IR (-O2):
@.str = [29 x i8] c"check_string: Failed to read\00"
@.str.1 = [62 x i8] c"check_string: Failed to read %llu bytes, actual read was %llu\00"
@.str.2 = [24 x i8] c"check_string: read '%s'\00"
define i32 @check_string(i32 %0, i8* %1) {
%3 = tail call i32 @strlen(i8* %1)
%4 = add nsw i32 %3, 1
%5 = zext i32 %4 to i64
%6 = alloca i8, i64 %5
%7 = call i32 @read(i32 %0, i8* %6, i32 %3)
%8 = icmp eq i32 %7, -1
br i1 %8, label %9, label %11
9: ; preds = %2
%10 = tail call i32 (i32, i8*, ...) @dd_log(i32 10, i8* getelementptr inbounds ([29 x i8], [29 x i8]* @.str, i64 0, i64 0))
br label %20
11: ; preds = %2
%12 = icmp eq i32 %7, %3
br i1 %12, label %15, label %13
13: ; preds = %11
%14 = tail call i32 (i32, i8*, ...) @dd_log(i32 10, i8* getelementptr inbounds ([62 x i8], [62 x i8]* @.str.1, i64 0, i64 0), i32 %3, i32 %7)
br label %20
15: ; preds = %11
%16 = call i32 (i32, i8*, ...) @dd_log(i32 1, i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str.2, i64 0, i64 0), i8* %6)
%17 = call i64 @strncmp(i8* %1, i8* %6, i32 %3)
%18 = icmp eq i64 %17, 0
%19 = zext i1 %18 to i32
br label %20
20: ; preds = %15, %13, %9
%21 = phi i32 [ -1, %9 ], [ -1, %13 ], [ %19, %15 ]
ret i32 %21
}