#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 list_node {
  int dummy;
};
typedef scalar_t__ pid_t;

/* Variables and functions */
int /*<<< orphan*/ assert(struct list_node *);
scalar_t__ dwarf(int, char **);
scalar_t__ fork();
struct list_node *func_addr(char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ run_debugger(scalar_t__, struct list_node *);
int /*<<< orphan*/ run_target(char *);
int main(int argc, char *argv[]) {
  if (argc != 2) {
    printf("usage:%s target\n", argv[0]);
    return -1;
  }
  pid_t pid = fork();
  if (pid == 0) {
    run_target(argv[1]);
  } else if (pid > 0) {
    char *dwarf_argv[] = {"dwarf", argv[1]};
    if (dwarf(2, dwarf_argv) != 0) {
      perror("cannot read debug infos.\ncompile with -g option?\n");
      return -1;
    }
    struct list_node *func_list = func_addr(argv[1]);
    assert(func_list);
    run_debugger(pid, func_list);
  } else {
    perror("error fork()\n");
  }
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x40,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
cmpl   $0x2,-0x8(%rbp)
je     11bf <main+0x3f>
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
lea    0xe52(%rip),%rdi
call   1030 <printf@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <main+0xfd>
mov    $0x0,%al
call   10a0 <fork@plt>
mov    %rax,-0x18(%rbp)
cmpq   $0x0,-0x18(%rbp)
jne    11e7 <main+0x67>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1060 <run_target@plt>
jmp    1276 <main+0xf6>
cmpq   $0x0,-0x18(%rbp)
jle    1265 <main+0xe5>
lea    0xe18(%rip),%rax
mov    %rax,-0x30(%rbp)
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rax
mov    %rax,-0x28(%rbp)
lea    -0x30(%rbp),%rsi
mov    $0x2,%edi
call   1090 <dwarf@plt>
cmp    $0x0,%rax
je     1239 <main+0xb9>
lea    0xdef(%rip),%rdi
call   1070 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    127d <main+0xfd>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   1040 <func_addr@plt>
mov    %rax,-0x38(%rbp)
mov    -0x38(%rbp),%rdi
call   1050 <assert@plt>
mov    -0x18(%rbp),%rdi
mov    -0x38(%rbp),%rsi
call   1080 <run_debugger@plt>
jmp    1271 <main+0xf1>
lea    0xddd(%rip),%rdi
call   1070 <perror@plt>
jmp    1276 <main+0xf6>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x40,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
sub    $0x10,%rsp
mov    %rsi,%rbx
cmp    $0x2,%edi
jne    11ec <main+0x6c>
xor    %ebp,%ebp
xor    %eax,%eax
call   10a0 <fork@plt>
test   %rax,%rax
je     1202 <main+0x82>
jle    1216 <main+0x96>
mov    %rax,%r14
lea    0xe67(%rip),%rax
mov    %rax,(%rsp)
mov    0x8(%rbx),%rax
mov    %rax,0x8(%rsp)
mov    %rsp,%rsi
mov    $0x2,%edi
call   1090 <dwarf@plt>
test   %rax,%rax
jne    1224 <main+0xa4>
mov    0x8(%rbx),%rdi
call   1040 <func_addr@plt>
mov    %rax,%rbx
mov    %rax,%rdi
call   1050 <assert@plt>
mov    %r14,%rdi
mov    %rbx,%rsi
call   1080 <run_debugger@plt>
xor    %ebp,%ebp
jmp    120b <main+0x8b>
mov    (%rbx),%rsi
lea    0xe0a(%rip),%rdi
call   1030 <printf@plt>
mov    $0xffffffff,%ebp
jmp    120b <main+0x8b>
mov    0x8(%rbx),%rdi
call   1060 <run_target@plt>
mov    %ebp,%eax
add    $0x10,%rsp
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xe2c(%rip),%rdi
call   1070 <perror@plt>
jmp    11e8 <main+0x68>
lea    0xdec(%rip),%rdi
call   1070 <perror@plt>
mov    $0xffffffff,%ebp
jmp    120b <main+0x8b>

LLVM IR (-O0):
%struct.list_node = type { i32 }
@.str = [17 x i8] c"usage:%s target\0A\00"
@.str.1 = [6 x i8] c"dwarf\00"
@.str.2 = [50 x i8] c"cannot read debug infos.\0Acompile with -g option?\0A\00"
@.str.3 = [14 x i8] c"error fork()\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i64
%7 = alloca [2 x i8*]
%8 = alloca %struct.list_node*
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
%9 = load i32, i32* %4
%10 = icmp ne i32 %9, 2
br i1 %10, label %11, label %16
11: ; preds = %2
%12 = load i8**, i8*** %5
%13 = getelementptr inbounds i8*, i8** %12, i64 0
%14 = load i8*, i8** %13
%15 = call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %14)
store i32 -1, i32* %3
br label %53
16: ; preds = %2
%17 = call i64 (...) @fork()
store i64 %17, i64* %6
%18 = load i64, i64* %6
%19 = icmp eq i64 %18, 0
br i1 %19, label %20, label %25
20: ; preds = %16
%21 = load i8**, i8*** %5
%22 = getelementptr inbounds i8*, i8** %21, i64 1
%23 = load i8*, i8** %22
%24 = call i32 @run_target(i8* %23)
br label %52
25: ; preds = %16
%26 = load i64, i64* %6
%27 = icmp sgt i64 %26, 0
br i1 %27, label %28, label %49
28: ; preds = %25
%29 = getelementptr inbounds [2 x i8*], [2 x i8*]* %7, i64 0, i64 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %29
%30 = getelementptr inbounds i8*, i8** %29, i64 1
%31 = load i8**, i8*** %5
%32 = getelementptr inbounds i8*, i8** %31, i64 1
%33 = load i8*, i8** %32
store i8* %33, i8** %30
%34 = getelementptr inbounds [2 x i8*], [2 x i8*]* %7, i64 0, i64 0
%35 = call i64 @dwarf(i32 2, i8** %34)
%36 = icmp ne i64 %35, 0
br i1 %36, label %37, label %39
37: ; preds = %28
%38 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %53
39: ; preds = %28
%40 = load i8**, i8*** %5
%41 = getelementptr inbounds i8*, i8** %40, i64 1
%42 = load i8*, i8** %41
%43 = call %struct.list_node* @func_addr(i8* %42)
store %struct.list_node* %43, %struct.list_node** %8
%44 = load %struct.list_node*, %struct.list_node** %8
%45 = call i32 @assert(%struct.list_node* %44)
%46 = load i64, i64* %6
%47 = load %struct.list_node*, %struct.list_node** %8
%48 = call i32 @run_debugger(i64 %46, %struct.list_node* %47)
br label %51
49: ; preds = %25
%50 = call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %51
51: ; preds = %49, %39
br label %52
52: ; preds = %51, %20
store i32 0, i32* %3
br label %53
53: ; preds = %52, %37, %11
%54 = load i32, i32* %3
ret i32 %54
}

LLVM IR (-O2):
%struct.list_node = type { i32 }
@.str = [17 x i8] c"usage:%s target\0A\00"
@.str.1 = [6 x i8] c"dwarf\00"
@.str.2 = [50 x i8] c"cannot read debug infos.\0Acompile with -g option?\0A\00"
@.str.3 = [14 x i8] c"error fork()\0A\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = alloca [2 x i8*]
%4 = icmp eq i32 %0, 2
br i1 %4, label %8, label %5
5: ; preds = %2
%6 = load i8*, i8** %1
%7 = tail call i32 @printf(i8* getelementptr inbounds ([17 x i8], [17 x i8]* @.str, i64 0, i64 0), i8* %6)
br label %34
8: ; preds = %2
%9 = tail call i64 (...) @fork()
%10 = icmp eq i64 %9, 0
br i1 %10, label %11, label %15
11: ; preds = %8
%12 = getelementptr inbounds i8*, i8** %1, i64 1
%13 = load i8*, i8** %12
%14 = tail call i32 @run_target(i8* %13)
br label %34
15: ; preds = %8
%16 = icmp sgt i64 %9, 0
br i1 %16, label %17, label %32
17: ; preds = %15
%18 = bitcast [2 x i8*]* %3 to i8*
%19 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 0
store i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.1, i64 0, i64 0), i8** %19
%20 = getelementptr inbounds [2 x i8*], [2 x i8*]* %3, i64 0, i64 1
%21 = getelementptr inbounds i8*, i8** %1, i64 1
%22 = load i8*, i8** %21
store i8* %22, i8** %20
%23 = call i64 @dwarf(i32 2, i8** %19)
%24 = icmp eq i64 %23, 0
br i1 %24, label %25, label %30
25: ; preds = %17
%26 = load i8*, i8** %21
%27 = call %struct.list_node* @func_addr(i8* %26)
%28 = call i32 @assert(%struct.list_node* %27)
%29 = call i32 @run_debugger(i64 %9, %struct.list_node* %27)
br label %34
30: ; preds = %17
%31 = call i32 @perror(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str.2, i64 0, i64 0))
br label %34
32: ; preds = %15
%33 = tail call i32 @perror(i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.3, i64 0, i64 0))
br label %34
34: ; preds = %11, %32, %25, %30, %5
%35 = phi i32 [ -1, %5 ], [ -1, %30 ], [ 0, %25 ], [ 0, %32 ], [ 0, %11 ]
ret i32 %35
}