#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ SEEK_SET;
int atoi(char const *);
int /*<<< orphan*/ close(int);
int /*<<< orphan*/ do_main(int);
scalar_t__ fork();
scalar_t__ lseek(int, int, int /*<<< orphan*/);
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *);
int /*<<< orphan*/ wait(int /*<<< orphan*/ *);
scalar_t__ write(int, char *, int);
int main(int argc, const char *argv[]) {
  int n_childs = 0;
  pid_t pid;
  int fd;
  printf("#################\nLCD test x86\n#################\n");
  if (argc > 1) {
    n_childs = atoi(argv[1]);
  }
  fd = open("DEV", O_WRONLY);
  if (fd < 0) {
    perror("cannot open device\n");
    return -1;
  }
  if (lseek(fd, 2, SEEK_SET) < 0) {
    perror("lseek error\n");
    return -1;
  }
  if (write(fd, "Distance (cm)", 13) < 0) {
    perror("write error\n");
    return -1;
  }
  close(fd);
  while (n_childs) {
    pid = fork();
    if (!pid) {
      do_main(n_childs);
    } else if (pid < 0) {
      perror("error forking\n");
    }
    --n_childs;
  }
  do_main(0);
  wait(NULL);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0x0,-0x14(%rbp)
lea    0xe3c(%rip),%rdi
call   1040 <printf@plt>
cmpl   $0x1,-0x8(%rbp)
jle    11e3 <main+0x43>
mov    -0x10(%rbp),%rax
mov    0x8(%rax),%rdi
call   10a0 <atoi@plt>
mov    %eax,-0x14(%rbp)
mov    0x2de6(%rip),%rax
mov    (%rax),%esi
lea    0xe3f(%rip),%rdi
call   1080 <open@plt>
mov    %eax,-0x24(%rbp)
cmpl   $0x0,-0x24(%rbp)
jge    121d <main+0x7d>
lea    0xe2a(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <main+0x161>
mov    -0x24(%rbp),%edi
mov    0x2db9(%rip),%rax
mov    (%rax),%edx
mov    $0x2,%esi
call   1050 <lseek@plt>
cmp    $0x0,%rax
jge    1255 <main+0xb5>
lea    0xe06(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <main+0x161>
mov    -0x24(%rbp),%edi
lea    0xdf8(%rip),%rsi
mov    $0xd,%edx
call   1030 <write@plt>
cmp    $0x0,%rax
jge    128b <main+0xeb>
lea    0xdeb(%rip),%rdi
call   1090 <perror@plt>
movl   $0xffffffff,-0x4(%rbp)
jmp    1301 <main+0x161>
mov    -0x24(%rbp),%edi
call   1060 <close@plt>
cmpl   $0x0,-0x14(%rbp)
je     12ea <main+0x14a>
mov    $0x0,%al
call   10c0 <fork@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
jne    12c0 <main+0x120>
mov    -0x14(%rbp),%edi
call   1070 <do_main@plt>
jmp    12dc <main+0x13c>
cmpq   $0x0,-0x20(%rbp)
jge    12d7 <main+0x137>
lea    0xda0(%rip),%rdi
call   1090 <perror@plt>
jmp    12dc <main+0x13c>
mov    -0x14(%rbp),%eax
add    $0xffffffff,%eax
mov    %eax,-0x14(%rbp)
jmp    1293 <main+0xf3>
xor    %edi,%edi
call   1070 <do_main@plt>
xor    %eax,%eax
mov    %eax,%edi
call   10b0 <wait@plt>
movl   $0x0,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r14
push   %rbx
mov    %rsi,%r14
mov    %edi,%ebp
lea    0xe9f(%rip),%rdi
call   1030 <puts@plt>
xor    %ebx,%ebx
cmp    $0x2,%ebp
jl     11c7 <main+0x27>
mov    0x8(%r14),%rdi
call   10a0 <atoi@plt>
mov    %eax,%ebx
mov    0x2e02(%rip),%rax
mov    (%rax),%esi
lea    0xe29(%rip),%rdi
call   1080 <open@plt>
test   %eax,%eax
js     126b <main+0xcb>
mov    %eax,%ebp
mov    0x2df3(%rip),%rax
mov    (%rax),%edx
mov    %ebp,%edi
mov    $0x2,%esi
call   1050 <lseek@plt>
test   %rax,%rax
js     1274 <main+0xd4>
lea    0xe1e(%rip),%rsi
mov    %ebp,%edi
mov    $0xd,%edx
call   1040 <write@plt>
test   %rax,%rax
js     127d <main+0xdd>
mov    %ebp,%edi
call   1060 <close@plt>
test   %ebx,%ebx
je     1254 <main+0xb4>
lea    0xe16(%rip),%rbp
jmp    123c <main+0x9c>
nopl   0x0(%rax)
mov    %ebx,%edi
call   1070 <do_main@plt>
add    $0xffffffff,%ebx
je     1254 <main+0xb4>
xor    %eax,%eax
call   10c0 <fork@plt>
test   %rax,%rax
je     1230 <main+0x90>
jns    1237 <main+0x97>
mov    %rbp,%rdi
call   1090 <perror@plt>
jmp    1237 <main+0x97>
xor    %ebx,%ebx
xor    %edi,%edi
call   1070 <do_main@plt>
xor    %edi,%edi
call   10b0 <wait@plt>
mov    %ebx,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret
lea    0xd92(%rip),%rdi
jmp    1284 <main+0xe4>
lea    0xd9d(%rip),%rdi
jmp    1284 <main+0xe4>
lea    0xdaf(%rip),%rdi
call   1090 <perror@plt>
mov    $0xffffffff,%ebx
jmp    1264 <main+0xc4>

LLVM IR (-O0):
@.str = [50 x i8] c"#################\0ALCD test x86\0A#################\0A\00"
@.str.1 = [4 x i8] c"DEV\00"
@O_WRONLY = global i32 0
@.str.2 = [20 x i8] c"cannot open device\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"lseek error\0A\00"
@.str.4 = [14 x i8] c"Distance (cm)\00"
@.str.5 = [13 x i8] c"write error\0A\00"
@.str.6 = [15 x i8] c"error forking\0A\00"
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca i32
%7 = alloca i64
%8 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 0, i32* %6
%9 = call i32 @printf(i8* getelementptr inbounds ([50 x i8], [50 x i8]* @.str, i64 0, i64 0))
%10 = load i32, i32* %4
%11 = icmp sgt i32 %10, 1
br i1 %11, label %12, label %17
12: ; preds = %2
%13 = load i8**, i8*** %5
%14 = getelementptr inbounds i8*, i8** %13, i64 1
%15 = load i8*, i8** %14
%16 = call i32 @atoi(i8* %15)
store i32 %16, i32* %6
br label %17
17: ; preds = %12, %2
%18 = load i32, i32* @O_WRONLY
%19 = call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %18)
store i32 %19, i32* %8
%20 = load i32, i32* %8
%21 = icmp slt i32 %20, 0
br i1 %21, label %22, label %24
22: ; preds = %17
%23 = call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
24: ; preds = %17
%25 = load i32, i32* %8
%26 = load i32, i32* @SEEK_SET
%27 = call i64 @lseek(i32 %25, i32 2, i32 %26)
%28 = icmp slt i64 %27, 0
br i1 %28, label %29, label %31
29: ; preds = %24
%30 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
31: ; preds = %24
%32 = load i32, i32* %8
%33 = call i64 @write(i32 %32, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 13)
%34 = icmp slt i64 %33, 0
br i1 %34, label %35, label %37
35: ; preds = %31
%36 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
store i32 -1, i32* %3
br label %62
37: ; preds = %31
%38 = load i32, i32* %8
%39 = call i32 @close(i32 %38)
br label %40
40: ; preds = %56, %37
%41 = load i32, i32* %6
%42 = icmp ne i32 %41, 0
br i1 %42, label %43, label %59
43: ; preds = %40
%44 = call i64 (...) @fork()
store i64 %44, i64* %7
%45 = load i64, i64* %7
%46 = icmp ne i64 %45, 0
br i1 %46, label %50, label %47
47: ; preds = %43
%48 = load i32, i32* %6
%49 = call i32 @do_main(i32 %48)
br label %56
50: ; preds = %43
%51 = load i64, i64* %7
%52 = icmp slt i64 %51, 0
br i1 %52, label %53, label %55
53: ; preds = %50
%54 = call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %55
55: ; preds = %53, %50
br label %56
56: ; preds = %55, %47
%57 = load i32, i32* %6
%58 = add nsw i32 %57, -1
store i32 %58, i32* %6
br label %40
59: ; preds = %40
%60 = call i32 @do_main(i32 0)
%61 = call i32 @wait(i32* null)
store i32 0, i32* %3
br label %62
62: ; preds = %59, %35, %29, %22
%63 = load i32, i32* %3
ret i32 %63
}

LLVM IR (-O2):
@.str.1 = [4 x i8] c"DEV\00"
@O_WRONLY = global i32 0
@.str.2 = [20 x i8] c"cannot open device\0A\00"
@SEEK_SET = global i32 0
@.str.3 = [13 x i8] c"lseek error\0A\00"
@.str.4 = [14 x i8] c"Distance (cm)\00"
@.str.5 = [13 x i8] c"write error\0A\00"
@.str.6 = [15 x i8] c"error forking\0A\00"
@str = [49 x i8] c"#################\0ALCD test x86\0A#################\00"
define i32 @main(i32 %0, i8** nocapture %1) {
%3 = tail call i32 @puts(i8* dereferenceable(1) getelementptr inbounds ([49 x i8], [49 x i8]* @str, i64 0, i64 0))
%4 = icmp sgt i32 %0, 1
br i1 %4, label %5, label %9
5: ; preds = %2
%6 = getelementptr inbounds i8*, i8** %1, i64 1
%7 = load i8*, i8** %6
%8 = tail call i32 @atoi(i8* %7)
br label %9
9: ; preds = %5, %2
%10 = phi i32 [ %8, %5 ], [ 0, %2 ]
%11 = load i32, i32* @O_WRONLY
%12 = tail call i32 @open(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.1, i64 0, i64 0), i32 %11)
%13 = icmp slt i32 %12, 0
br i1 %13, label %14, label %16
14: ; preds = %9
%15 = tail call i32 @perror(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.2, i64 0, i64 0))
br label %46
16: ; preds = %9
%17 = load i32, i32* @SEEK_SET
%18 = tail call i64 @lseek(i32 %12, i32 2, i32 %17)
%19 = icmp slt i64 %18, 0
br i1 %19, label %20, label %22
20: ; preds = %16
%21 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.3, i64 0, i64 0))
br label %46
22: ; preds = %16
%23 = tail call i64 @write(i32 %12, i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.4, i64 0, i64 0), i32 13)
%24 = icmp slt i64 %23, 0
br i1 %24, label %25, label %27
25: ; preds = %22
%26 = tail call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
br label %46
27: ; preds = %22
%28 = tail call i32 @close(i32 %12)
%29 = icmp eq i32 %10, 0
br i1 %29, label %43, label %30
30: ; preds = %27, %40
%31 = phi i32 [ %41, %40 ], [ %10, %27 ]
%32 = tail call i64 (...) @fork()
%33 = icmp eq i64 %32, 0
br i1 %33, label %34, label %36
34: ; preds = %30
%35 = tail call i32 @do_main(i32 %31)
br label %40
36: ; preds = %30
%37 = icmp slt i64 %32, 0
br i1 %37, label %38, label %40
38: ; preds = %36
%39 = tail call i32 @perror(i8* getelementptr inbounds ([15 x i8], [15 x i8]* @.str.6, i64 0, i64 0))
br label %40
40: ; preds = %36, %38, %34
%41 = add nsw i32 %31, -1
%42 = icmp eq i32 %41, 0
br i1 %42, label %43, label %30
43: ; preds = %40, %27
%44 = tail call i32 @do_main(i32 0)
%45 = tail call i32 @wait(i32* null)
br label %46
46: ; preds = %43, %25, %20, %14
%47 = phi i32 [ -1, %14 ], [ -1, %20 ], [ -1, %25 ], [ 0, %43 ]
ret i32 %47
}