#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 /*<<< orphan*/ O_WRONLY;
int /*<<< orphan*/ close(int);
int getpid();
int open(char *, int /*<<< orphan*/);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int, int);
scalar_t__ strlen(char *);
int /*<<< orphan*/ write(int, char *, scalar_t__);
int main() {
  int fd, i;
  char msg[100];
  do {
    fd = open("pipezinho", O_WRONLY);
    if (fd == -1)
      sleep(1);
  } while (fd == -1);
  for (i = 1; i <= 3; i++) {
    sprintf(msg, "Hello [%d] from process %d\n", i, getpid());
    write(fd, msg, strlen(msg) + 1);
    sleep(3);
  }
  close(fd);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x90,%rsp
movl   $0x0,-0x4(%rbp)
mov    0x2e4f(%rip),%rax
mov    (%rax),%esi
lea    0xe6e(%rip),%rdi
call   1070 <open@plt>
mov    %eax,-0x8(%rbp)
cmpl   $0xffffffff,-0x8(%rbp)
jne    11ae <main+0x3e>
mov    $0x1,%edi
call   1090 <sleep@plt>
jmp    11b3 <main+0x43>
cmpl   $0xffffffff,-0x8(%rbp)
je     1182 <main+0x12>
movl   $0x1,-0xc(%rbp)
cmpl   $0x3,-0xc(%rbp)
jg     1240 <main+0xd0>
lea    -0x70(%rbp),%rax
mov    %rax,-0x88(%rbp)
mov    -0xc(%rbp),%eax
mov    %eax,-0x80(%rbp)
mov    $0x0,%al
call   1040 <getpid@plt>
mov    -0x88(%rbp),%rdi
mov    -0x80(%rbp),%edx
mov    %eax,%ecx
lea    0xe11(%rip),%rsi
call   1080 <sprintf@plt>
mov    -0x8(%rbp),%eax
mov    %eax,-0x7c(%rbp)
lea    -0x70(%rbp),%rax
mov    %rax,-0x78(%rbp)
lea    -0x70(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x7c(%rbp),%edi
mov    -0x78(%rbp),%rsi
mov    %rax,%rdx
add    $0x1,%rdx
call   1030 <write@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    -0xc(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0xc(%rbp)
jmp    11c4 <main+0x54>
mov    -0x8(%rbp),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x90,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %rbx
sub    $0x70,%rsp
mov    0x2e58(%rip),%r15
mov    (%r15),%esi
lea    0xe76(%rip),%rdi
call   1070 <open@plt>
mov    %eax,%ebx
cmp    $0xffffffff,%eax
jne    11bc <main+0x4c>
lea    0xe63(%rip),%r14
nopl   (%rax)
mov    $0x1,%edi
call   1090 <sleep@plt>
mov    (%r15),%esi
mov    %r14,%rdi
call   1070 <open@plt>
cmp    $0xffffffff,%eax
je     11a0 <main+0x30>
mov    %eax,%ebx
xor    %eax,%eax
call   1040 <getpid@plt>
lea    0xe40(%rip),%r15
mov    %rsp,%r14
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x1,%edx
mov    %eax,%ecx
call   1080 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x2,%edx
mov    %eax,%ecx
call   1080 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
xor    %eax,%eax
call   1040 <getpid@plt>
mov    %r14,%rdi
mov    %r15,%rsi
mov    $0x3,%edx
mov    %eax,%ecx
call   1080 <sprintf@plt>
mov    %r14,%rdi
call   1050 <strlen@plt>
lea    0x1(%rax),%rdx
mov    %ebx,%edi
mov    %r14,%rsi
call   1030 <write@plt>
mov    $0x3,%edi
call   1090 <sleep@plt>
mov    %ebx,%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x70,%rsp
pop    %rbx
pop    %r14
pop    %r15
ret

LLVM IR (-O0):
@.str = [10 x i8] c"pipezinho\00"
@O_WRONLY = global i32 0
@.str.1 = [28 x i8] c"Hello [%d] from process %d\0A\00"
define i32 @main() {
%1 = alloca i32
%2 = alloca i32
%3 = alloca i32
%4 = alloca [100 x i8]
store i32 0, i32* %1
br label %5
5: ; preds = %13, %0
%6 = load i32, i32* @O_WRONLY
%7 = call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %6)
store i32 %7, i32* %2
%8 = load i32, i32* %2
%9 = icmp eq i32 %8, -1
br i1 %9, label %10, label %12
10: ; preds = %5
%11 = call i32 @sleep(i32 1)
br label %12
12: ; preds = %10, %5
br label %13
13: ; preds = %12
%14 = load i32, i32* %2
%15 = icmp eq i32 %14, -1
br i1 %15, label %5, label %16
16: ; preds = %13
store i32 1, i32* %3
br label %17
17: ; preds = %32, %16
%18 = load i32, i32* %3
%19 = icmp sle i32 %18, 3
br i1 %19, label %20, label %35
20: ; preds = %17
%21 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%22 = load i32, i32* %3
%23 = call i32 (...) @getpid()
%24 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 %22, i32 %23)
%25 = load i32, i32* %2
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%27 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%28 = call i64 @strlen(i8* %27)
%29 = add nsw i64 %28, 1
%30 = call i32 @write(i32 %25, i8* %26, i64 %29)
%31 = call i32 @sleep(i32 3)
br label %32
32: ; preds = %20
%33 = load i32, i32* %3
%34 = add nsw i32 %33, 1
store i32 %34, i32* %3
br label %17
35: ; preds = %17
%36 = load i32, i32* %2
%37 = call i32 @close(i32 %36)
ret i32 0
}

LLVM IR (-O2):
@.str = [10 x i8] c"pipezinho\00"
@O_WRONLY = global i32 0
@.str.1 = [28 x i8] c"Hello [%d] from process %d\0A\00"
define i32 @main() {
%1 = alloca [100 x i8]
%2 = getelementptr inbounds [100 x i8], [100 x i8]* %1, i64 0, i64 0
%3 = load i32, i32* @O_WRONLY
%4 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %3)
%5 = icmp eq i32 %4, -1
br i1 %5, label %27, label %6
6: ; preds = %27, %0
%7 = phi i32 [ %4, %0 ], [ %30, %27 ]
%8 = tail call i32 (...) @getpid()
%9 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 1, i32 %8)
%10 = call i64 @strlen(i8* %2)
%11 = add nsw i64 %10, 1
%12 = call i32 @write(i32 %7, i8* %2, i64 %11)
%13 = tail call i32 @sleep(i32 3)
%14 = tail call i32 (...) @getpid()
%15 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 2, i32 %14)
%16 = call i64 @strlen(i8* %2)
%17 = add nsw i64 %16, 1
%18 = call i32 @write(i32 %7, i8* %2, i64 %17)
%19 = tail call i32 @sleep(i32 3)
%20 = tail call i32 (...) @getpid()
%21 = call i32 @sprintf(i8* %2, i8* dereferenceable(1) getelementptr inbounds ([28 x i8], [28 x i8]* @.str.1, i64 0, i64 0), i32 3, i32 %20)
%22 = call i64 @strlen(i8* %2)
%23 = add nsw i64 %22, 1
%24 = call i32 @write(i32 %7, i8* %2, i64 %23)
%25 = tail call i32 @sleep(i32 3)
%26 = tail call i32 @close(i32 %7)
ret i32 0
27: ; preds = %0, %27
%28 = tail call i32 @sleep(i32 1)
%29 = load i32, i32* @O_WRONLY
%30 = tail call i32 @open(i8* getelementptr inbounds ([10 x i8], [10 x i8]* @.str, i64 0, i64 0), i32 %29)
%31 = icmp eq i32 %30, -1
br i1 %31, label %27, label %6
}