#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 scalar_t__ pid_t;

/* Variables and functions */
int EXIT_FAILURE;
int EXIT_SUCCESS;
int /*<<< orphan*/ SIGTERM;
int /*<<< orphan*/ exit(int);
scalar_t__ fork();
int /*<<< orphan*/ gpsProcess();
int /*<<< orphan*/ kill(scalar_t__, int /*<<< orphan*/);
int log_init(char *);
int /*<<< orphan*/ log_term();
int /*<<< orphan*/ log_write(char *, char *, int /*<<< orphan*/ *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ sleep(int);
char *strerror(int);
int /*<<< orphan*/ waitpid(scalar_t__, int /*<<< orphan*/ *,
                           int /*<<< orphan*/);
int main() {
  int ret;
  pid_t cpid;
  if ((ret = log_init("extra/gpsProcessTestLog"))) {
    printf(strerror(ret));
    exit(EXIT_FAILURE);
  }
  cpid = fork();
  if (cpid < 0) {
    log_write("gpsProcess test", "Process failed to fork, quiting.", NULL);
    log_term();
    exit(EXIT_FAILURE);
  }
  if (!cpid) {
    log_write("gpsProcess test",
              "Process forked successfully, running gpsProcess from child.",
              NULL);
    gpsProcess();
    log_write("gpsProcess test", "gpsProcess terminated.", NULL);
    exit(EXIT_SUCCESS);
  } else {
    log_write("gpsProcess test", "Parent process will sleep for 30 seconds.",
              NULL);
    sleep(30);
    log_write("gpsProcess test", "Parent woke up. Will terminate child now.",
              NULL);
    kill(cpid, SIGTERM);
  }
  log_write("gpsProcess test", "Waiting for child to terminate.", NULL);
  waitpid(cpid, NULL, 0);
  log_write("gpsProcess test", "Closing log file.", NULL);
  log_term();
  return EXIT_SUCCESS;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x10,%rsp
movl   $0x0,-0x4(%rbp)
lea    0xe3a(%rip),%rdi
call   1030 <log_init@plt>
mov    %eax,-0x8(%rbp)
cmp    $0x0,%eax
je     11f5 <main+0x45>
mov    -0x8(%rbp),%edi
call   10b0 <strerror@plt>
mov    %rax,%rdi
call   1050 <printf@plt>
mov    0x2dea(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
mov    $0x0,%al
call   10d0 <fork@plt>
mov    %rax,-0x10(%rbp)
cmpq   $0x0,-0x10(%rbp)
jge    1237 <main+0x87>
lea    0xe06(%rip),%rdi
lea    0xe0f(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <log_write@plt>
mov    $0x0,%al
call   1060 <log_term@plt>
mov    0x2da8(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
cmpq   $0x0,-0x10(%rbp)
jne    1285 <main+0xd5>
lea    0xdcf(%rip),%rdi
lea    0xdf9(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <log_write@plt>
mov    $0x0,%al
call   1040 <gpsProcess@plt>
lea    0xdb1(%rip),%rdi
lea    0xe17(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <log_write@plt>
mov    0x2d52(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd8c(%rip),%rdi
lea    0xe09(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <log_write@plt>
mov    $0x1e,%edi
call   10c0 <sleep@plt>
lea    0xd6b(%rip),%rdi
lea    0xe12(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <log_write@plt>
mov    -0x10(%rbp),%rdi
mov    0x2d20(%rip),%rax
mov    (%rax),%esi
call   1070 <kill@plt>
lea    0xd42(%rip),%rdi
lea    0xe13(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <log_write@plt>
mov    -0x10(%rbp),%rdi
xor    %eax,%eax
mov    %eax,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
lea    0xd1c(%rip),%rdi
lea    0xe0d(%rip),%rsi
xor    %eax,%eax
mov    %eax,%edx
call   1090 <log_write@plt>
mov    $0x0,%al
call   1060 <log_term@plt>
mov    0x2cb6(%rip),%rax
mov    (%rax),%eax
add    $0x10,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r14
push   %rbx
push   %rax
lea    0xe45(%rip),%rdi
call   1030 <log_init@plt>
test   %eax,%eax
jne    126b <main+0xbb>
xor    %eax,%eax
call   10d0 <fork@plt>
test   %rax,%rax
js     127c <main+0xcc>
je     12a6 <main+0xf6>
mov    %rax,%rbx
lea    0xe30(%rip),%r14
lea    0xead(%rip),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   1090 <log_write@plt>
mov    $0x1e,%edi
call   10c0 <sleep@plt>
lea    0xebc(%rip),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   1090 <log_write@plt>
mov    0x2dcd(%rip),%rax
mov    (%rax),%esi
mov    %rbx,%rdi
call   1070 <kill@plt>
lea    0xec4(%rip),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   1090 <log_write@plt>
mov    %rbx,%rdi
xor    %esi,%esi
xor    %edx,%edx
call   1080 <waitpid@plt>
lea    0xec7(%rip),%rsi
mov    %r14,%rdi
xor    %edx,%edx
call   1090 <log_write@plt>
xor    %eax,%eax
call   1060 <log_term@plt>
mov    0x2d6f(%rip),%rax
mov    (%rax),%eax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret
mov    %eax,%edi
call   10b0 <strerror@plt>
mov    %rax,%rdi
call   1050 <printf@plt>
jmp    1298 <main+0xe8>
lea    0xd95(%rip),%rdi
lea    0xd9e(%rip),%rsi
xor    %edx,%edx
call   1090 <log_write@plt>
xor    %eax,%eax
call   1060 <log_term@plt>
mov    0x2d39(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>
lea    0xd6b(%rip),%rbx
lea    0xd95(%rip),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1090 <log_write@plt>
xor    %eax,%eax
call   1040 <gpsProcess@plt>
lea    0xdb9(%rip),%rsi
mov    %rbx,%rdi
xor    %edx,%edx
call   1090 <log_write@plt>
mov    0x2cf3(%rip),%rax
mov    (%rax),%edi
call   10a0 <exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"extra/gpsProcessTestLog\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"gpsProcess test\00"
@.str.2 = [33 x i8] c"Process failed to fork, quiting.\00"
@.str.3 = [60 x i8] c"Process forked successfully, running gpsProcess from child.\00"
@.str.4 = [23 x i8] c"gpsProcess terminated.\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [42 x i8] c"Parent process will sleep for 30 seconds.\00"
@.str.6 = [42 x i8] c"Parent woke up. Will terminate child now.\00"
@SIGTERM = global i32 0
@.str.7 = [32 x i8] c"Waiting for child to terminate.\00"
@.str.8 = [18 x i8] c"Closing log file.\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i64
store i32 0, i32* %1
%4 = call i32 @log_init(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
store i32 %4, i32* %2
%5 = icmp ne i32 %4, 0
br i1 %5, label %6, label %12
6: ; preds = %0
%7 = load i32, i32* %2
%8 = call i8* @strerror(i32 %7)
%9 = call i32 @printf(i8* %8)
%10 = load i32, i32* @EXIT_FAILURE
%11 = call i32 @exit(i32 %10)
unreachable
12: ; preds = %0
%13 = call i64 (...) @fork()
store i64 %13, i64* %3
%14 = load i64, i64* %3
%15 = icmp slt i64 %14, 0
br i1 %15, label %16, label %21
16: ; preds = %12
%17 = call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32* null)
%18 = call i32 (...) @log_term()
%19 = load i32, i32* @EXIT_FAILURE
%20 = call i32 @exit(i32 %19)
unreachable
21: ; preds = %12
%22 = load i64, i64* %3
%23 = icmp ne i64 %22, 0
br i1 %23, label %30, label %24
24: ; preds = %21
%25 = call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32* null)
%26 = call i32 (...) @gpsProcess()
%27 = call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32* null)
%28 = load i32, i32* @EXIT_SUCCESS
%29 = call i32 @exit(i32 %28)
unreachable
30: ; preds = %21
%31 = call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32* null)
%32 = call i32 @sleep(i32 30)
%33 = call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32* null)
%34 = load i64, i64* %3
%35 = load i32, i32* @SIGTERM
%36 = call i32 @kill(i64 %34, i32 %35)
br label %37
37: ; preds = %30
%38 = call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32* null)
%39 = load i64, i64* %3
%40 = call i32 @waitpid(i64 %39, i32* null, i32 0)
%41 = call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32* null)
%42 = call i32 (...) @log_term()
%43 = load i32, i32* @EXIT_SUCCESS
ret i32 %43
}

LLVM IR (-O2):
@.str = [24 x i8] c"extra/gpsProcessTestLog\00"
@EXIT_FAILURE = global i32 0
@.str.1 = [16 x i8] c"gpsProcess test\00"
@.str.2 = [33 x i8] c"Process failed to fork, quiting.\00"
@.str.3 = [60 x i8] c"Process forked successfully, running gpsProcess from child.\00"
@.str.4 = [23 x i8] c"gpsProcess terminated.\00"
@EXIT_SUCCESS = global i32 0
@.str.5 = [42 x i8] c"Parent process will sleep for 30 seconds.\00"
@.str.6 = [42 x i8] c"Parent woke up. Will terminate child now.\00"
@SIGTERM = global i32 0
@.str.7 = [32 x i8] c"Waiting for child to terminate.\00"
@.str.8 = [18 x i8] c"Closing log file.\00"
define i32 @main() {
%1 = tail call i32 @log_init(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0))
%2 = icmp eq i32 %1, 0
br i1 %2, label %8, label %3
3: ; preds = %0
%4 = tail call i8* @strerror(i32 %1)
%5 = tail call i32 @printf(i8* dereferenceable(1) %4)
%6 = load i32, i32* @EXIT_FAILURE
%7 = tail call i32 @exit(i32 %6)
unreachable
8: ; preds = %0
%9 = tail call i64 (...) @fork()
%10 = icmp slt i64 %9, 0
br i1 %10, label %11, label %16
11: ; preds = %8
%12 = tail call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([33 x i8], [33 x i8]* @.str.2, i64 0, i64 0), i32* null)
%13 = tail call i32 (...) @log_term()
%14 = load i32, i32* @EXIT_FAILURE
%15 = tail call i32 @exit(i32 %14)
unreachable
16: ; preds = %8
%17 = icmp eq i64 %9, 0
br i1 %17, label %18, label %24
18: ; preds = %16
%19 = tail call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([60 x i8], [60 x i8]* @.str.3, i64 0, i64 0), i32* null)
%20 = tail call i32 (...) @gpsProcess()
%21 = tail call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i32* null)
%22 = load i32, i32* @EXIT_SUCCESS
%23 = tail call i32 @exit(i32 %22)
unreachable
24: ; preds = %16
%25 = tail call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.5, i64 0, i64 0), i32* null)
%26 = tail call i32 @sleep(i32 30)
%27 = tail call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([42 x i8], [42 x i8]* @.str.6, i64 0, i64 0), i32* null)
%28 = load i32, i32* @SIGTERM
%29 = tail call i32 @kill(i64 %9, i32 %28)
%30 = tail call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([32 x i8], [32 x i8]* @.str.7, i64 0, i64 0), i32* null)
%31 = tail call i32 @waitpid(i64 %9, i32* null, i32 0)
%32 = tail call i32 @log_write(i8* getelementptr inbounds ([16 x i8], [16 x i8]* @.str.1, i64 0, i64 0), i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.8, i64 0, i64 0), i32* null)
%33 = tail call i32 (...) @log_term()
%34 = load i32, i32* @EXIT_SUCCESS
ret i32 %34
}