#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 O_CREAT;
int /*<<< orphan*/ O_RDONLY;
int O_TRUNC;
int O_WRONLY;
int /*<<< orphan*/ assert(int);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ memcmp(char *, char *, int);
int open(char *, int, ...);
scalar_t__ parse_string(char *, char *, int);
char *readfile(int);
int /*<<< orphan*/ write_section_header(int);
int /*<<< orphan*/ write_string(int, char *, int);
void test_strings() {
  char test[11 + 1] = "hello world";
  char a[11 + 1];
  char *p;
  int fd;
  test[11] = (char)0;
  fd = open("testfile", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  assert(fd > 1 && "Couldn't open file to write test output");
  write_string(fd, test, 11);
  close(fd);
  fd = open("testfile", O_RDONLY);
  assert(fd > 1 && "Couldn't open file to read test input");
  p = readfile(fd);
  parse_string(p, a, 11);
  assert(parse_string(p, a, 11) == 0);
  assert(!memcmp(test, a, 11));
  close(fd);
  fd = open("testfile", O_WRONLY | O_CREAT | O_TRUNC, 0666);
  assert(fd > 1 && "Couldn't open file to write test output");
  write_string(fd, test, -1);
  write_section_header(fd);
  close(fd);
  fd = open("testfile", O_RDONLY);
  assert(fd > 1 && "Couldn't open file to read test input");
  p = readfile(fd);
  assert(parse_string(p, a, -1) == 0);
  assert(!memcmp(test, a, 11 + 1));
  close(fd);
}


x86 assembly (clang, -O0):
<test_strings>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    0xe71(%rip),%rax
mov    %rax,-0xc(%rbp)
mov    0xe6f(%rip),%eax
mov    %eax,-0x4(%rbp)
movb   $0x0,-0x1(%rbp)
mov    0x2e19(%rip),%rax
mov    (%rax),%esi
mov    0x2e48(%rip),%rax
or     (%rax),%esi
mov    0x2e37(%rip),%rax
or     (%rax),%esi
lea    0xe4a(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x24(%rbp)
mov    %al,-0x25(%rbp)
jle    11ea <test_strings+0x6a>
mov    $0x1,%al
mov    %al,-0x25(%rbp)
jmp    11ea <test_strings+0x6a>
mov    -0x25(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x24(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0xb,%edx
call   1050 <write_string@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    0x2dc1(%rip),%rax
mov    (%rax),%esi
lea    0xdec(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x24(%rbp)
mov    %al,-0x26(%rbp)
jle    1243 <test_strings+0xc3>
mov    $0x1,%al
mov    %al,-0x26(%rbp)
jmp    1243 <test_strings+0xc3>
mov    -0x26(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x24(%rbp),%edi
call   10a0 <readfile@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xb,%edx
call   1030 <parse_string@plt>
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xb,%edx
call   1030 <parse_string@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
lea    -0xc(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xb,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    0x2cfc(%rip),%rax
mov    (%rax),%esi
mov    0x2d2b(%rip),%rax
or     (%rax),%esi
mov    0x2d1a(%rip),%rax
or     (%rax),%esi
lea    0xd2d(%rip),%rdi
mov    $0x1b6,%edx
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x24(%rbp)
mov    %al,-0x27(%rbp)
jle    1307 <test_strings+0x187>
mov    $0x1,%al
mov    %al,-0x27(%rbp)
jmp    1307 <test_strings+0x187>
mov    -0x27(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x24(%rbp),%edi
lea    -0xc(%rbp),%rsi
mov    $0xffffffff,%edx
call   1050 <write_string@plt>
mov    -0x24(%rbp),%edi
call   1040 <write_section_header@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
mov    0x2c9c(%rip),%rax
mov    (%rax),%esi
lea    0xcc7(%rip),%rdi
mov    $0x0,%al
call   1090 <open@plt>
mov    %eax,-0x24(%rbp)
xor    %eax,%eax
cmpl   $0x1,-0x24(%rbp)
mov    %al,-0x28(%rbp)
jle    1368 <test_strings+0x1e8>
mov    $0x1,%al
mov    %al,-0x28(%rbp)
jmp    1368 <test_strings+0x1e8>
mov    -0x28(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x24(%rbp),%edi
call   10a0 <readfile@plt>
mov    %rax,-0x20(%rbp)
mov    -0x20(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xffffffff,%edx
call   1030 <parse_string@plt>
cmp    $0x0,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
lea    -0xc(%rbp),%rdi
lea    -0x18(%rbp),%rsi
mov    $0xc,%edx
call   1070 <memcmp@plt>
cmp    $0x0,%eax
setne  %al
xor    $0xff,%al
and    $0x1,%al
movzbl %al,%edi
call   1080 <assert@plt>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<test_strings>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x18,%rsp
movabs $0x6f77206f6c6c6568,%rax
20 77 6f
mov    %rax,(%rsp)
movl   $0x646c726f,0x7(%rsp)
64
mov    0x2e15(%rip),%rax
mov    0x2e46(%rip),%rcx
mov    (%rcx),%esi
or     (%rax),%esi
movb   $0x0,0xb(%rsp)
mov    0x2e2e(%rip),%rax
or     (%rax),%esi
lea    0xe41(%rip),%r15
mov    %r15,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0x2,%eax
setge  %dil
call   1080 <assert@plt>
mov    %rsp,%r14
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xb,%edx
call   1050 <write_string@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x2dce(%rip),%r13
mov    0x0(%r13),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0x2,%eax
setge  %dil
call   1080 <assert@plt>
mov    %ebp,%edi
call   10a0 <readfile@plt>
mov    %rax,%rbx
lea    0xc(%rsp),%r12
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0xb,%edx
call   1030 <parse_string@plt>
mov    %rbx,%rdi
mov    %r12,%rsi
mov    $0xb,%edx
call   1030 <parse_string@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1080 <assert@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0xb,%edx
call   1070 <memcmp@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1080 <assert@plt>
mov    %ebp,%edi
call   1060 <close@plt>
mov    0x2d68(%rip),%rax
mov    (%rax),%esi
mov    0x2d27(%rip),%rax
or     (%rax),%esi
mov    0x2d4e(%rip),%rax
or     (%rax),%esi
mov    %r15,%rdi
mov    $0x1b6,%edx
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebx
xor    %edi,%edi
cmp    $0x2,%eax
setge  %dil
call   1080 <assert@plt>
mov    %ebx,%edi
mov    %r14,%rsi
mov    $0xffffffff,%edx
call   1050 <write_string@plt>
mov    %ebx,%edi
call   1040 <write_section_header@plt>
mov    %ebx,%edi
call   1060 <close@plt>
mov    0x0(%r13),%esi
mov    %r15,%rdi
xor    %eax,%eax
call   1090 <open@plt>
mov    %eax,%ebp
xor    %edi,%edi
cmp    $0x2,%eax
setge  %dil
call   1080 <assert@plt>
mov    %ebp,%edi
call   10a0 <readfile@plt>
mov    %rax,%rdi
mov    %r12,%rsi
mov    $0xffffffff,%edx
call   1030 <parse_string@plt>
xor    %edi,%edi
test   %rax,%rax
sete   %dil
call   1080 <assert@plt>
mov    %r14,%rdi
mov    %r12,%rsi
mov    $0xc,%edx
call   1070 <memcmp@plt>
xor    %edi,%edi
test   %eax,%eax
sete   %dil
call   1080 <assert@plt>
mov    %ebp,%edi
call   1060 <close@plt>
add    $0x18,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@__const.test_strings.test = [12 x i8] c"hello world\00"
@.str = [9 x i8] c"testfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@.str.1 = [40 x i8] c"Couldn't open file to write test output\00"
@O_RDONLY = global i32 0
@.str.2 = [38 x i8] c"Couldn't open file to read test input\00"
define void @test_strings() {
%1 = alloca [12 x i8]
%2 = alloca [12 x i8]
%3 = alloca i8*
%4 = alloca i32
%5 = bitcast [12 x i8]* %1 to i8*
%6 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 11
store i8 0, i8* %6
%7 = load i32, i32* @O_WRONLY
%8 = load i32, i32* @O_CREAT
%9 = or i32 %7, %8
%10 = load i32, i32* @O_TRUNC
%11 = or i32 %9, %10
%12 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %11, i32 438)
store i32 %12, i32* %4
%13 = load i32, i32* %4
%14 = icmp sgt i32 %13, 1
br i1 %14, label %15, label %16
15: ; preds = %0
br label %16
16: ; preds = %15, %0
%17 = phi i1 [ false, %0 ], [ true, %15 ]
%18 = zext i1 %17 to i32
%19 = call i32 @assert(i32 %18)
%20 = load i32, i32* %4
%21 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%22 = call i32 @write_string(i32 %20, i8* %21, i32 11)
%23 = load i32, i32* %4
%24 = call i32 @close(i32 %23)
%25 = load i32, i32* @O_RDONLY
%26 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %25)
store i32 %26, i32* %4
%27 = load i32, i32* %4
%28 = icmp sgt i32 %27, 1
br i1 %28, label %29, label %30
29: ; preds = %16
br label %30
30: ; preds = %29, %16
%31 = phi i1 [ false, %16 ], [ true, %29 ]
%32 = zext i1 %31 to i32
%33 = call i32 @assert(i32 %32)
%34 = load i32, i32* %4
%35 = call i8* @readfile(i32 %34)
store i8* %35, i8** %3
%36 = load i8*, i8** %3
%37 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%38 = call i64 @parse_string(i8* %36, i8* %37, i32 11)
%39 = load i8*, i8** %3
%40 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%41 = call i64 @parse_string(i8* %39, i8* %40, i32 11)
%42 = icmp eq i64 %41, 0
%43 = zext i1 %42 to i32
%44 = call i32 @assert(i32 %43)
%45 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%46 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%47 = call i32 @memcmp(i8* %45, i8* %46, i32 11)
%48 = icmp ne i32 %47, 0
%49 = xor i1 %48, true
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = load i32, i32* %4
%53 = call i32 @close(i32 %52)
%54 = load i32, i32* @O_WRONLY
%55 = load i32, i32* @O_CREAT
%56 = or i32 %54, %55
%57 = load i32, i32* @O_TRUNC
%58 = or i32 %56, %57
%59 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %58, i32 438)
store i32 %59, i32* %4
%60 = load i32, i32* %4
%61 = icmp sgt i32 %60, 1
br i1 %61, label %62, label %63
62: ; preds = %30
br label %63
63: ; preds = %62, %30
%64 = phi i1 [ false, %30 ], [ true, %62 ]
%65 = zext i1 %64 to i32
%66 = call i32 @assert(i32 %65)
%67 = load i32, i32* %4
%68 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%69 = call i32 @write_string(i32 %67, i8* %68, i32 -1)
%70 = load i32, i32* %4
%71 = call i32 @write_section_header(i32 %70)
%72 = load i32, i32* %4
%73 = call i32 @close(i32 %72)
%74 = load i32, i32* @O_RDONLY
%75 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %74)
store i32 %75, i32* %4
%76 = load i32, i32* %4
%77 = icmp sgt i32 %76, 1
br i1 %77, label %78, label %79
78: ; preds = %63
br label %79
79: ; preds = %78, %63
%80 = phi i1 [ false, %63 ], [ true, %78 ]
%81 = zext i1 %80 to i32
%82 = call i32 @assert(i32 %81)
%83 = load i32, i32* %4
%84 = call i8* @readfile(i32 %83)
store i8* %84, i8** %3
%85 = load i8*, i8** %3
%86 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%87 = call i64 @parse_string(i8* %85, i8* %86, i32 -1)
%88 = icmp eq i64 %87, 0
%89 = zext i1 %88 to i32
%90 = call i32 @assert(i32 %89)
%91 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%92 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%93 = call i32 @memcmp(i8* %91, i8* %92, i32 12)
%94 = icmp ne i32 %93, 0
%95 = xor i1 %94, true
%96 = zext i1 %95 to i32
%97 = call i32 @assert(i32 %96)
%98 = load i32, i32* %4
%99 = call i32 @close(i32 %98)
ret void
}

LLVM IR (-O2):
@__const.test_strings.test = [12 x i8] c"hello world\00"
@.str = [9 x i8] c"testfile\00"
@O_WRONLY = global i32 0
@O_CREAT = global i32 0
@O_TRUNC = global i32 0
@O_RDONLY = global i32 0
define void @test_strings() {
%1 = alloca [12 x i8]
%2 = alloca [12 x i8]
%3 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 0
%4 = getelementptr inbounds [12 x i8], [12 x i8]* %2, i64 0, i64 0
%5 = getelementptr inbounds [12 x i8], [12 x i8]* %1, i64 0, i64 11
store i8 0, i8* %5
%6 = load i32, i32* @O_WRONLY
%7 = load i32, i32* @O_CREAT
%8 = or i32 %7, %6
%9 = load i32, i32* @O_TRUNC
%10 = or i32 %8, %9
%11 = tail call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %10, i32 438)
%12 = icmp sgt i32 %11, 1
%13 = zext i1 %12 to i32
%14 = tail call i32 @assert(i32 %13)
%15 = call i32 @write_string(i32 %11, i8* %3, i32 11)
%16 = call i32 @close(i32 %11)
%17 = load i32, i32* @O_RDONLY
%18 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %17)
%19 = icmp sgt i32 %18, 1
%20 = zext i1 %19 to i32
%21 = call i32 @assert(i32 %20)
%22 = call i8* @readfile(i32 %18)
%23 = call i64 @parse_string(i8* %22, i8* %4, i32 11)
%24 = call i64 @parse_string(i8* %22, i8* %4, i32 11)
%25 = icmp eq i64 %24, 0
%26 = zext i1 %25 to i32
%27 = call i32 @assert(i32 %26)
%28 = call i32 @memcmp(i8* %3, i8* %4, i32 11)
%29 = icmp eq i32 %28, 0
%30 = zext i1 %29 to i32
%31 = call i32 @assert(i32 %30)
%32 = call i32 @close(i32 %18)
%33 = load i32, i32* @O_WRONLY
%34 = load i32, i32* @O_CREAT
%35 = or i32 %34, %33
%36 = load i32, i32* @O_TRUNC
%37 = or i32 %35, %36
%38 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %37, i32 438)
%39 = icmp sgt i32 %38, 1
%40 = zext i1 %39 to i32
%41 = call i32 @assert(i32 %40)
%42 = call i32 @write_string(i32 %38, i8* %3, i32 -1)
%43 = call i32 @write_section_header(i32 %38)
%44 = call i32 @close(i32 %38)
%45 = load i32, i32* @O_RDONLY
%46 = call i32 (i8*, i32, ...) @open(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str, i64 0, i64 0), i32 %45)
%47 = icmp sgt i32 %46, 1
%48 = zext i1 %47 to i32
%49 = call i32 @assert(i32 %48)
%50 = call i8* @readfile(i32 %46)
%51 = call i64 @parse_string(i8* %50, i8* %4, i32 -1)
%52 = icmp eq i64 %51, 0
%53 = zext i1 %52 to i32
%54 = call i32 @assert(i32 %53)
%55 = call i32 @memcmp(i8* %3, i8* %4, i32 12)
%56 = icmp eq i32 %55, 0
%57 = zext i1 %56 to i32
%58 = call i32 @assert(i32 %57)
%59 = call i32 @close(i32 %46)
ret void
}