#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 */
struct iovec {
  int iov_len;
  char *iov_base;
};
typedef int pid_t;
typedef int /*<<< orphan*/ buf;

/* Variables and functions */
int /*<<< orphan*/ NT_PRSTATUS;
int /*<<< orphan*/ assert(int);
int do_child();
int err_out(char *);
int fork();
int non_empty(char *, int);
int ptrace(int, int, int /*<<< orphan*/, struct iovec *);
int /*<<< orphan*/ puts(char *);
int wait(int *);
int main(void) {
  char buf[1024];
  struct iovec iov;
  pid_t cpid, pid;
  int status;
  cpid = fork();
  if (cpid == -1)
    return err_out("fork");
  if (cpid == 0)
    return do_child();
  pid = wait(&status);
  if (pid == -1)
    return err_out("wait");
  iov.iov_len = sizeof(buf);
  iov.iov_base = buf;
  if (ptrace(0x4204, cpid, NT_PRSTATUS, &iov) == -1)
    return err_out("ptrace getregset");
  assert(iov.iov_base == buf);
  assert(iov.iov_len > 0 && iov.iov_len < sizeof(buf));
  assert(non_empty(buf, iov.iov_len));
  puts("OK");
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x430,%rsp
movl   $0x0,-0x4(%rbp)
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %eax,-0x424(%rbp)
cmpl   $0xffffffff,-0x424(%rbp)
jne    11c0 <main+0x40>
lea    0xe4d(%rip),%rdi
call   1060 <err_out@plt>
mov    %eax,-0x4(%rbp)
jmp    12e9 <main+0x169>
cmpl   $0x0,-0x424(%rbp)
jne    11dc <main+0x5c>
mov    $0x0,%al
call   1040 <do_child@plt>
mov    %eax,-0x4(%rbp)
jmp    12e9 <main+0x169>
lea    -0x42c(%rbp),%rdi
call   1090 <wait@plt>
mov    %eax,-0x428(%rbp)
cmpl   $0xffffffff,-0x428(%rbp)
jne    120f <main+0x8f>
lea    0xe03(%rip),%rdi
call   1060 <err_out@plt>
mov    %eax,-0x4(%rbp)
jmp    12e9 <main+0x169>
movl   $0x400,-0x420(%rbp)
lea    -0x410(%rbp),%rax
mov    %rax,-0x418(%rbp)
mov    -0x424(%rbp),%esi
mov    0x2dc4(%rip),%rax
mov    (%rax),%edx
mov    $0x4204,%edi
lea    -0x420(%rbp),%rcx
call   1080 <ptrace@plt>
cmp    $0xffffffff,%eax
jne    1264 <main+0xe4>
lea    0xdb3(%rip),%rdi
call   1060 <err_out@plt>
mov    %eax,-0x4(%rbp)
jmp    12e9 <main+0x169>
mov    -0x418(%rbp),%rax
lea    -0x410(%rbp),%rcx
cmp    %rcx,%rax
sete   %al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
xor    %eax,%eax
cmpl   $0x0,-0x420(%rbp)
mov    %al,-0x42d(%rbp)
jle    12ad <main+0x12d>
movslq -0x420(%rbp),%rax
cmp    $0x400,%rax
setb   %al
mov    %al,-0x42d(%rbp)
mov    -0x42d(%rbp),%al
and    $0x1,%al
movzbl %al,%edi
call   1070 <assert@plt>
lea    -0x410(%rbp),%rdi
mov    -0x420(%rbp),%esi
call   1030 <non_empty@plt>
mov    %eax,%edi
call   1070 <assert@plt>
lea    0xd3e(%rip),%rdi
call   1050 <puts@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x430,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %rbx
sub    $0x428,%rsp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %eax,%eax
je     11b0 <main+0x30>
mov    %eax,%ebp
cmp    $0xffffffff,%eax
jne    11c0 <main+0x40>
lea    0xe5e(%rip),%rdi
add    $0x428,%rsp
pop    %rbx
pop    %rbp
jmp    1060 <err_out@plt>
xor    %eax,%eax
add    $0x428,%rsp
pop    %rbx
pop    %rbp
jmp    1040 <do_child@plt>
lea    0x1c(%rsp),%rdi
call   1090 <wait@plt>
cmp    $0xffffffff,%eax
je     124a <main+0xca>
movl   $0x400,0x8(%rsp)
lea    0x20(%rsp),%rbx
mov    %rbx,0x10(%rsp)
mov    0x2e10(%rip),%rax
mov    (%rax),%edx
lea    0x8(%rsp),%rcx
mov    $0x4204,%edi
mov    %ebp,%esi
call   1080 <ptrace@plt>
cmp    $0xffffffff,%eax
je     1253 <main+0xd3>
xor    %edi,%edi
cmp    %rbx,0x10(%rsp)
sete   %dil
call   1070 <assert@plt>
mov    0x8(%rsp),%eax
add    $0xffffffff,%eax
xor    %edi,%edi
cmp    $0x3ff,%eax
setb   %dil
call   1070 <assert@plt>
mov    0x8(%rsp),%esi
mov    %rbx,%rdi
call   1030 <non_empty@plt>
mov    %eax,%edi
call   1070 <assert@plt>
lea    0xdda(%rip),%rdi
call   1050 <puts@plt>
xor    %eax,%eax
jmp    125f <main+0xdf>
lea    0xdb4(%rip),%rdi
jmp    125a <main+0xda>
lea    0xdb0(%rip),%rdi
call   1060 <err_out@plt>
add    $0x428,%rsp
pop    %rbx
pop    %rbp
ret

LLVM IR (-O0):
%struct.iovec = type { i32, i8* }
@.str = [5 x i8] c"fork\00"
@.str.1 = [5 x i8] c"wait\00"
@NT_PRSTATUS = global i32 0
@.str.2 = [17 x i8] c"ptrace getregset\00"
@.str.3 = [3 x i8] c"OK\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca [1024 x i8]
%3 = alloca %struct.iovec
%4 = alloca i32
%5 = alloca i32
%6 = alloca i32
store i32 0, i32* %1
%7 = call i32 (...) @fork()
store i32 %7, i32* %4
%8 = load i32, i32* %4
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %0
%11 = call i32 @err_out(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
store i32 %11, i32* %1
br label %58
12: ; preds = %0
%13 = load i32, i32* %4
%14 = icmp eq i32 %13, 0
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 (...) @do_child()
store i32 %16, i32* %1
br label %58
17: ; preds = %12
%18 = call i32 @wait(i32* %6)
store i32 %18, i32* %5
%19 = load i32, i32* %5
%20 = icmp eq i32 %19, -1
br i1 %20, label %21, label %23
21: ; preds = %17
%22 = call i32 @err_out(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
store i32 %22, i32* %1
br label %58
23: ; preds = %17
%24 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i32 0, i32 0
store i32 1024, i32* %24
%25 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%26 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i32 0, i32 1
store i8* %25, i8** %26
%27 = load i32, i32* %4
%28 = load i32, i32* @NT_PRSTATUS
%29 = call i32 @ptrace(i32 16900, i32 %27, i32 %28, %struct.iovec* %3)
%30 = icmp eq i32 %29, -1
br i1 %30, label %31, label %33
31: ; preds = %23
%32 = call i32 @err_out(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
store i32 %32, i32* %1
br label %58
33: ; preds = %23
%34 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i32 0, i32 1
%35 = load i8*, i8** %34
%36 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%37 = icmp eq i8* %35, %36
%38 = zext i1 %37 to i32
%39 = call i32 @assert(i32 %38)
%40 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i32 0, i32 0
%41 = load i32, i32* %40
%42 = icmp sgt i32 %41, 0
br i1 %42, label %43, label %48
43: ; preds = %33
%44 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i32 0, i32 0
%45 = load i32, i32* %44
%46 = sext i32 %45 to i64
%47 = icmp ult i64 %46, 1024
br label %48
48: ; preds = %43, %33
%49 = phi i1 [ false, %33 ], [ %47, %43 ]
%50 = zext i1 %49 to i32
%51 = call i32 @assert(i32 %50)
%52 = getelementptr inbounds [1024 x i8], [1024 x i8]* %2, i64 0, i64 0
%53 = getelementptr inbounds %struct.iovec, %struct.iovec* %3, i32 0, i32 0
%54 = load i32, i32* %53
%55 = call i32 @non_empty(i8* %52, i32 %54)
%56 = call i32 @assert(i32 %55)
%57 = call i32 @puts(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
store i32 0, i32* %1
br label %58
58: ; preds = %48, %31, %21, %15, %10
%59 = load i32, i32* %1
ret i32 %59
}

LLVM IR (-O2):
%struct.iovec = type { i32, i8* }
@.str = [5 x i8] c"fork\00"
@.str.1 = [5 x i8] c"wait\00"
@NT_PRSTATUS = global i32 0
@.str.2 = [17 x i8] c"ptrace getregset\00"
@.str.3 = [3 x i8] c"OK\00"
define i32 @main() {
%1 = alloca [1024 x i8]
%2 = alloca %struct.iovec
%3 = alloca i32
%4 = getelementptr inbounds [1024 x i8], [1024 x i8]* %1, i64 0, i64 0
%5 = bitcast %struct.iovec* %2 to i8*
%6 = bitcast i32* %3 to i8*
%7 = tail call i32 (...) @fork()
switch i32 %7, label %12 [
i32 -1, label %8
i32 0, label %10
]
8: ; preds = %0
%9 = tail call i32 @err_out(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str, i64 0, i64 0))
br label %39
10: ; preds = %0
%11 = tail call i32 (...) @do_child()
br label %39
12: ; preds = %0
%13 = call i32 @wait(i32* %3)
%14 = icmp eq i32 %13, -1
br i1 %14, label %15, label %17
15: ; preds = %12
%16 = call i32 @err_out(i8* getelementptr inbounds ([5 x i8], [5 x i8]* @.str.1, i64 0, i64 0))
br label %39
17: ; preds = %12
%18 = getelementptr inbounds %struct.iovec, %struct.iovec* %2, i64 0, i32 0
store i32 1024, i32* %18
%19 = getelementptr inbounds %struct.iovec, %struct.iovec* %2, i64 0, i32 1
store i8* %4, i8** %19
%20 = load i32, i32* @NT_PRSTATUS
%21 = call i32 @ptrace(i32 16900, i32 %7, i32 %20, %struct.iovec* %2)
%22 = icmp eq i32 %21, -1
br i1 %22, label %23, label %25
23: ; preds = %17
%24 = call i32 @err_out(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str.2, i64 0, i64 0))
br label %39
25: ; preds = %17
%26 = load i8*, i8** %19
%27 = icmp eq i8* %26, %4
%28 = zext i1 %27 to i32
%29 = call i32 @assert(i32 %28)
%30 = load i32, i32* %18
%31 = add i32 %30, -1
%32 = icmp ult i32 %31, 1023
%33 = zext i1 %32 to i32
%34 = call i32 @assert(i32 %33)
%35 = load i32, i32* %18
%36 = call i32 @non_empty(i8* %4, i32 %35)
%37 = call i32 @assert(i32 %36)
%38 = call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0))
br label %39
39: ; preds = %25, %23, %15, %10, %8
%40 = phi i32 [ %9, %8 ], [ %11, %10 ], [ %16, %15 ], [ %24, %23 ], [ 0, %25 ]
ret i32 %40
}