#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 int /*<<< orphan*/ buf1;

/* Variables and functions */
int EAGAIN;
int EINTR;
int /*<<< orphan*/ STDERR_FILENO;
int /*<<< orphan*/ STDIN_FILENO;
int /*<<< orphan*/ STDOUT_FILENO;
int atoi(char *);
int /*<<< orphan*/ close(int /*<<< orphan*/);
int errno;
char *getenv(char *);
int read(int /*<<< orphan*/, char *, int);
int /*<<< orphan*/ sleep(int);
int /*<<< orphan*/ sprintf(char *, char *, int);
int strlen(char *);
int write(int /*<<< orphan*/, char *, int);
int main(int argc, char **argv) {
  char buf1[128], buf2[128], *tmp;
  int size, j, procid = -1, rc = 0;
  tmp = getenv("SLURM_PROCID");
  if (tmp)
    procid = atoi(tmp);
  sprintf(buf1, "task %d write to stdout:", procid);
  rc = write(STDOUT_FILENO, buf1, strlen(buf1));
  sprintf(buf1, "task %d write to stderr:", procid);
  rc = write(STDOUT_FILENO, buf1, strlen(buf1));
  while ((size = read(STDIN_FILENO, buf1, sizeof(buf1))) != 0) {
    if (size > 0) {
      int offset;
      sprintf(buf2, "task %d read from stdin:", procid);
      offset = strlen(buf2);
      for (j = 0; j < size; j++)
        buf2[offset + j] = buf1[j];
      buf2[offset + j] = ':';
      buf2[offset + j + 1] = '\0';
      rc = write(STDOUT_FILENO, buf2, strlen(buf2));
      break;
    } else {
      if ((errno == EINTR) || (errno == EAGAIN)) {
        sleep(1);
        continue;
      }
      sprintf(buf1, "io read errno:%d:", errno);
      rc = write(STDOUT_FILENO, buf1, strlen(buf1));
      break;
    }
  }
  close(STDIN_FILENO);
  close(STDOUT_FILENO);
  close(STDERR_FILENO);
  return (0);
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0x170,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
movl   $0xffffffff,-0x124(%rbp)
ff ff ff
movl   $0x0,-0x128(%rbp)
lea    0xe4c(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
je     11e0 <main+0x60>
mov    -0x118(%rbp),%rdi
call   1080 <atoi@plt>
mov    %eax,-0x124(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x124(%rbp),%edx
lea    0xe19(%rip),%rsi
call   1090 <sprintf@plt>
mov    0x2df0(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x14c(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x148(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x14c(%rbp),%edi
mov    -0x148(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x128(%rbp)
lea    -0x90(%rbp),%rdi
mov    -0x124(%rbp),%edx
lea    0xdd6(%rip),%rsi
call   1090 <sprintf@plt>
mov    0x2d94(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x13c(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x138(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x13c(%rbp),%edi
mov    -0x138(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x128(%rbp)
mov    0x2d21(%rip),%rax
mov    (%rax),%edi
lea    -0x90(%rbp),%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,-0x11c(%rbp)
cmp    $0x0,%eax
je     1461 <main+0x2e1>
cmpl   $0x0,-0x11c(%rbp)
jle    13c3 <main+0x243>
lea    -0x110(%rbp),%rdi
mov    -0x124(%rbp),%edx
lea    0xd5d(%rip),%rsi
call   1090 <sprintf@plt>
lea    -0x110(%rbp),%rdi
call   1050 <strlen@plt>
mov    %eax,-0x12c(%rbp)
movl   $0x0,-0x120(%rbp)
mov    -0x120(%rbp),%eax
cmp    -0x11c(%rbp),%eax
jge    134c <main+0x1cc>
movslq -0x120(%rbp),%rax
mov    -0x90(%rbp,%rax,1),%cl
mov    -0x12c(%rbp),%eax
add    -0x120(%rbp),%eax
cltq
mov    %cl,-0x110(%rbp,%rax,1)
mov    -0x120(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x120(%rbp)
jmp    1303 <main+0x183>
mov    -0x12c(%rbp),%eax
add    -0x120(%rbp),%eax
cltq
movb   $0x3a,-0x110(%rbp,%rax,1)
3a
mov    -0x12c(%rbp),%eax
add    -0x120(%rbp),%eax
add    $0x1,%eax
cltq
movb   $0x0,-0x110(%rbp,%rax,1)
mov    0x2c6e(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x15c(%rbp)
lea    -0x110(%rbp),%rax
mov    %rax,-0x158(%rbp)
lea    -0x110(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x15c(%rbp),%edi
mov    -0x158(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x128(%rbp)
jmp    1461 <main+0x2e1>
mov    0x2c0e(%rip),%rax
mov    (%rax),%eax
mov    0x2bf5(%rip),%rcx
cmp    (%rcx),%eax
je     13f3 <main+0x273>
mov    0x2bf6(%rip),%rax
mov    (%rax),%eax
mov    0x2bc5(%rip),%rcx
cmp    (%rcx),%eax
jne    1402 <main+0x282>
mov    $0x1,%edi
call   10a0 <sleep@plt>
jmp    1298 <main+0x118>
lea    -0x90(%rbp),%rdi
mov    0x2bc8(%rip),%rax
mov    (%rax),%edx
lea    0xc3f(%rip),%rsi
call   1090 <sprintf@plt>
mov    0x2bcb(%rip),%rax
mov    (%rax),%eax
mov    %eax,-0x16c(%rbp)
lea    -0x90(%rbp),%rax
mov    %rax,-0x168(%rbp)
lea    -0x90(%rbp),%rdi
call   1050 <strlen@plt>
mov    -0x16c(%rbp),%edi
mov    -0x168(%rbp),%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    %eax,-0x128(%rbp)
mov    0x2b58(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2b7a(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2b4c(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x170,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<main>:
push   %rbp
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x118,%rsp
lea    0xe58(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     11be <main+0x2e>
mov    %rax,%rdi
call   1090 <atoi@plt>
mov    %eax,%ebx
jmp    11c3 <main+0x33>
mov    $0xffffffff,%ebx
lea    0xe43(%rip),%rsi
lea    0x90(%rsp),%r15
mov    %r15,%rdi
mov    %ebx,%edx
call   10a0 <sprintf@plt>
mov    0x2e0d(%rip),%r14
mov    (%r14),%ebp
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
lea    0xe25(%rip),%rsi
mov    %r15,%rdi
mov    %ebx,0xc(%rsp)
mov    %ebx,%edx
call   10a0 <sprintf@plt>
mov    (%r14),%ebp
mov    %r15,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %r15,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    0x2d93(%rip),%r12
mov    (%r12),%edi
mov    %r15,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
test   %eax,%eax
je     131c <main+0x18c>
mov    %eax,%ebp
mov    0x2d89(%rip),%r13
mov    0x2d72(%rip),%r15
mov    0x2d53(%rip),%r14
lea    0x90(%rsp),%rbx
jmp    1295 <main+0x105>
nopw   0x0(%rax,%rax,1)
mov    $0x1,%edi
call   10b0 <sleep@plt>
mov    (%r12),%edi
mov    %rbx,%rsi
mov    $0x80,%edx
call   1070 <read@plt>
mov    %eax,%ebp
test   %eax,%eax
je     131c <main+0x18c>
test   %ebp,%ebp
jg     12b8 <main+0x128>
mov    0x0(%r13),%edx
cmp    (%r15),%edx
je     1270 <main+0xe0>
cmp    (%r14),%edx
je     1270 <main+0xe0>
lea    0xdaa(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <sprintf@plt>
jmp    12ff <main+0x16f>
lea    0x10(%rsp),%rbx
lea    0xd7b(%rip),%rsi
mov    %rbx,%rdi
mov    0xc(%rsp),%edx
call   10a0 <sprintf@plt>
mov    %rbx,%rdi
call   1050 <strlen@plt>
movslq %eax,%r14
lea    (%rsp,%r14,1),%rdi
add    $0x10,%rdi
mov    %ebp,%edx
lea    0x90(%rsp),%rsi
call   1080 <memcpy@plt>
add    %ebp,%r14d
movslq %r14d,%rax
movw   $0x3a,0x10(%rsp,%rax,1)
mov    0x2cea(%rip),%rax
mov    (%rax),%ebp
mov    %rbx,%rdi
call   1050 <strlen@plt>
mov    %ebp,%edi
mov    %rbx,%rsi
mov    %eax,%edx
call   1040 <write@plt>
mov    (%r12),%edi
call   1060 <close@plt>
mov    0x2cc4(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
mov    0x2c96(%rip),%rax
mov    (%rax),%edi
call   1060 <close@plt>
xor    %eax,%eax
add    $0x118,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
pop    %rbp
ret

LLVM IR (-O0):
@.str = [13 x i8] c"SLURM_PROCID\00"
@.str.1 = [25 x i8] c"task %d write to stdout:\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [25 x i8] c"task %d write to stderr:\00"
@STDIN_FILENO = global i32 0
@.str.3 = [25 x i8] c"task %d read from stdin:\00"
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@.str.4 = [18 x i8] c"io read errno:%d:\00"
@STDERR_FILENO = global i32 0
define i32 @main(i32 %0, i8** %1) {
%3 = alloca i32
%4 = alloca i32
%5 = alloca i8**
%6 = alloca [128 x i8]
%7 = alloca [128 x i8]
%8 = alloca i8*
%9 = alloca i32
%10 = alloca i32
%11 = alloca i32
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %3
store i32 %0, i32* %4
store i8** %1, i8*** %5
store i32 -1, i32* %11
store i32 0, i32* %12
%14 = call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
store i8* %14, i8** %8
%15 = load i8*, i8** %8
%16 = icmp ne i8* %15, null
br i1 %16, label %17, label %20
17: ; preds = %2
%18 = load i8*, i8** %8
%19 = call i32 @atoi(i8* %18)
store i32 %19, i32* %11
br label %20
20: ; preds = %17, %2
%21 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%22 = load i32, i32* %11
%23 = call i32 @sprintf(i8* %21, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %22)
%24 = load i32, i32* @STDOUT_FILENO
%25 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%26 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%27 = call i32 @strlen(i8* %26)
%28 = call i32 @write(i32 %24, i8* %25, i32 %27)
store i32 %28, i32* %12
%29 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%30 = load i32, i32* %11
%31 = call i32 @sprintf(i8* %29, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %30)
%32 = load i32, i32* @STDOUT_FILENO
%33 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%34 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%35 = call i32 @strlen(i8* %34)
%36 = call i32 @write(i32 %32, i8* %33, i32 %35)
store i32 %36, i32* %12
br label %37
37: ; preds = %93, %20
%38 = load i32, i32* @STDIN_FILENO
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%40 = call i32 @read(i32 %38, i8* %39, i32 128)
store i32 %40, i32* %9
%41 = icmp ne i32 %40, 0
br i1 %41, label %42, label %104
42: ; preds = %37
%43 = load i32, i32* %9
%44 = icmp sgt i32 %43, 0
br i1 %44, label %45, label %85
45: ; preds = %42
%46 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%47 = load i32, i32* %11
%48 = call i32 @sprintf(i8* %46, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %47)
%49 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%50 = call i32 @strlen(i8* %49)
store i32 %50, i32* %13
store i32 0, i32* %10
br label %51
51: ; preds = %65, %45
%52 = load i32, i32* %10
%53 = load i32, i32* %9
%54 = icmp slt i32 %52, %53
br i1 %54, label %55, label %68
55: ; preds = %51
%56 = load i32, i32* %10
%57 = sext i32 %56 to i64
%58 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 %57
%59 = load i8, i8* %58
%60 = load i32, i32* %13
%61 = load i32, i32* %10
%62 = add nsw i32 %60, %61
%63 = sext i32 %62 to i64
%64 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %63
store i8 %59, i8* %64
br label %65
65: ; preds = %55
%66 = load i32, i32* %10
%67 = add nsw i32 %66, 1
store i32 %67, i32* %10
br label %51
68: ; preds = %51
%69 = load i32, i32* %13
%70 = load i32, i32* %10
%71 = add nsw i32 %69, %70
%72 = sext i32 %71 to i64
%73 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %72
store i8 58, i8* %73
%74 = load i32, i32* %13
%75 = load i32, i32* %10
%76 = add nsw i32 %74, %75
%77 = add nsw i32 %76, 1
%78 = sext i32 %77 to i64
%79 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 %78
store i8 0, i8* %79
%80 = load i32, i32* @STDOUT_FILENO
%81 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%82 = getelementptr inbounds [128 x i8], [128 x i8]* %7, i64 0, i64 0
%83 = call i32 @strlen(i8* %82)
%84 = call i32 @write(i32 %80, i8* %81, i32 %83)
store i32 %84, i32* %12
br label %104
85: ; preds = %42
%86 = load i32, i32* @errno
%87 = load i32, i32* @EINTR
%88 = icmp eq i32 %86, %87
br i1 %88, label %93, label %89
89: ; preds = %85
%90 = load i32, i32* @errno
%91 = load i32, i32* @EAGAIN
%92 = icmp eq i32 %90, %91
br i1 %92, label %93, label %95
93: ; preds = %89, %85
%94 = call i32 @sleep(i32 1)
br label %37
95: ; preds = %89
%96 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%97 = load i32, i32* @errno
%98 = call i32 @sprintf(i8* %96, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %97)
%99 = load i32, i32* @STDOUT_FILENO
%100 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%101 = getelementptr inbounds [128 x i8], [128 x i8]* %6, i64 0, i64 0
%102 = call i32 @strlen(i8* %101)
%103 = call i32 @write(i32 %99, i8* %100, i32 %102)
store i32 %103, i32* %12
br label %104
104: ; preds = %95, %68, %37
%105 = load i32, i32* @STDIN_FILENO
%106 = call i32 @close(i32 %105)
%107 = load i32, i32* @STDOUT_FILENO
%108 = call i32 @close(i32 %107)
%109 = load i32, i32* @STDERR_FILENO
%110 = call i32 @close(i32 %109)
ret i32 0
}

LLVM IR (-O2):
@.str = [13 x i8] c"SLURM_PROCID\00"
@.str.1 = [25 x i8] c"task %d write to stdout:\00"
@STDOUT_FILENO = global i32 0
@.str.2 = [25 x i8] c"task %d write to stderr:\00"
@STDIN_FILENO = global i32 0
@.str.3 = [25 x i8] c"task %d read from stdin:\00"
@errno = global i32 0
@EINTR = global i32 0
@EAGAIN = global i32 0
@.str.4 = [18 x i8] c"io read errno:%d:\00"
@STDERR_FILENO = global i32 0
define i32 @main(i32 %0, i8** nocapture readnone %1) {
%3 = alloca [128 x i8]
%4 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%5 = alloca [128 x i8]
%6 = getelementptr inbounds [128 x i8], [128 x i8]* %3, i64 0, i64 0
%7 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 0
%8 = tail call i8* @getenv(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str, i64 0, i64 0))
%9 = icmp eq i8* %8, null
br i1 %9, label %12, label %10
10: ; preds = %2
%11 = tail call i32 @atoi(i8* %8)
br label %12
12: ; preds = %10, %2
%13 = phi i32 [ %11, %10 ], [ -1, %2 ]
%14 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i64 0, i64 0), i32 %13)
%15 = load i32, i32* @STDOUT_FILENO
%16 = call i32 @strlen(i8* %6)
%17 = call i32 @write(i32 %15, i8* %6, i32 %16)
%18 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i32 %13)
%19 = load i32, i32* @STDOUT_FILENO
%20 = call i32 @strlen(i8* %6)
%21 = call i32 @write(i32 %19, i8* %6, i32 %20)
%22 = load i32, i32* @STDIN_FILENO
%23 = call i32 @read(i32 %22, i8* %6, i32 128)
%24 = icmp eq i32 %23, 0
br i1 %24, label %59, label %25
25: ; preds = %12, %47
%26 = phi i32 [ %50, %47 ], [ %23, %12 ]
%27 = icmp sgt i32 %26, 0
br i1 %27, label %28, label %40
28: ; preds = %25
%29 = call i32 @sprintf(i8* %7, i8* dereferenceable(1) getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i32 %13)
%30 = call i32 @strlen(i8* %7)
%31 = sext i32 %30 to i64
%32 = getelementptr [128 x i8], [128 x i8]* %5, i64 0, i64 %31
%33 = zext i32 %26 to i64
%34 = add nsw i32 %26, %30
%35 = sext i32 %34 to i64
%36 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %35
store i8 58, i8* %36
%37 = add nsw i32 %34, 1
%38 = sext i32 %37 to i64
%39 = getelementptr inbounds [128 x i8], [128 x i8]* %5, i64 0, i64 %38
store i8 0, i8* %39
br label %54
40: ; preds = %25
%41 = load i32, i32* @errno
%42 = load i32, i32* @EINTR
%43 = icmp eq i32 %41, %42
%44 = load i32, i32* @EAGAIN
%45 = icmp eq i32 %41, %44
%46 = select i1 %43, i1 true, i1 %45
br i1 %46, label %47, label %52
47: ; preds = %40
%48 = call i32 @sleep(i32 1)
%49 = load i32, i32* @STDIN_FILENO
%50 = call i32 @read(i32 %49, i8* %6, i32 128)
%51 = icmp eq i32 %50, 0
br i1 %51, label %59, label %25
52: ; preds = %40
%53 = call i32 @sprintf(i8* %6, i8* dereferenceable(1) getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %41)
br label %54
54: ; preds = %28, %52
%55 = phi i8* [ %6, %52 ], [ %7, %28 ]
%56 = load i32, i32* @STDOUT_FILENO
%57 = call i32 @strlen(i8* %55)
%58 = call i32 @write(i32 %56, i8* %55, i32 %57)
br label %59
59: ; preds = %47, %54, %12
%60 = load i32, i32* @STDIN_FILENO
%61 = call i32 @close(i32 %60)
%62 = load i32, i32* @STDOUT_FILENO
%63 = call i32 @close(i32 %62)
%64 = load i32, i32* @STDERR_FILENO
%65 = call i32 @close(i32 %64)
ret i32 0
}