#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*/ FILE;

/* Variables and functions */
int /*<<< orphan*/ _exit(int);
int /*<<< orphan*/ errno;
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, int *, int *);
char *getenv(char *);
int getpid();
int /*<<< orphan*/ printf(char *, ...);
int /*<<< orphan*/ snprintf(char *, int, char *, int);
char *strerror(int /*<<< orphan*/);
int main(int argc, char *argv[], char *envp[]) {
  int i;
  char *env;
  printf("hello exe=`%s` argc=%d\n", argv[0], argc);
  for (i = 1; i < argc; i++) {
    printf("arg %d \"%s\"\n", i, argv[i]);
  }
  if ((env = getenv("HOSTNAME"))) {
    printf("%s\n", env);
  }
  char buf[100];
  snprintf(buf, 100, "/proc/%d/app-info", getpid());
  FILE *fp = fopen(buf, "r");
  if (!fp) {
    printf("fopen() failed, %s\n", strerror(errno));
    _exit(-1);
  }
  int nRanks, myRank;
  fscanf(fp, "%d %d", &nRanks, &myRank);
  printf("nRanks=%d myRank=%d\n", nRanks, myRank);
  _exit(0);
  return 0;
}


x86 assembly (clang, -O0):
<main>:
push   %rbp
mov    %rsp,%rbp
sub    $0xb0,%rsp
movl   $0x0,-0x4(%rbp)
mov    %edi,-0x8(%rbp)
mov    %rsi,-0x10(%rbp)
mov    %rdx,-0x18(%rbp)
mov    -0x10(%rbp),%rax
mov    (%rax),%rsi
mov    -0x8(%rbp),%edx
lea    0xe52(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
movl   $0x1,-0x1c(%rbp)
mov    -0x1c(%rbp),%eax
cmp    -0x8(%rbp),%eax
jge    11f3 <main+0x73>
mov    -0x1c(%rbp),%esi
mov    -0x10(%rbp),%rax
movslq -0x1c(%rbp),%rcx
mov    (%rax,%rcx,8),%rdx
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    -0x1c(%rbp),%eax
add    $0x1,%eax
mov    %eax,-0x1c(%rbp)
jmp    11bc <main+0x3c>
lea    0xe2b(%rip),%rdi
call   1030 <getenv@plt>
mov    %rax,-0x28(%rbp)
cmp    $0x0,%rax
je     121f <main+0x9f>
mov    -0x28(%rbp),%rsi
lea    0xe3a(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
lea    -0x90(%rbp),%rax
mov    %rax,-0xa8(%rbp)
mov    $0x0,%al
call   1050 <getpid@plt>
mov    -0xa8(%rbp),%rdi
mov    %eax,%ecx
mov    $0x64,%esi
lea    0xde5(%rip),%rdx
call   1070 <snprintf@plt>
lea    -0x90(%rbp),%rdi
lea    0xde4(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x98(%rbp)
cmpq   $0x0,-0x98(%rbp)
jne    129f <main+0x11f>
mov    0x2d63(%rip),%rax
mov    (%rax),%edi
call   10a0 <strerror@plt>
mov    %rax,%rsi
lea    0xdb4(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <_exit@plt>
mov    -0x98(%rbp),%rdi
lea    0xda9(%rip),%rsi
lea    -0x9c(%rbp),%rdx
lea    -0xa0(%rbp),%rcx
call   1080 <fscanf@plt>
mov    -0x9c(%rbp),%esi
mov    -0xa0(%rbp),%edx
lea    0xd89(%rip),%rdi
mov    $0x0,%al
call   1060 <printf@plt>
xor    %edi,%edi
call   1040 <_exit@plt>

x86 assembly (clang, -O2):
<main>:
push   %r15
push   %r14
push   %r12
push   %rbx
sub    $0x78,%rsp
mov    %rsi,%r14
mov    %edi,%ebx
mov    (%rsi),%rsi
lea    0xe56(%rip),%rdi
mov    %ebx,%edx
xor    %eax,%eax
call   1070 <printf@plt>
cmp    $0x2,%ebx
jl     11e9 <main+0x59>
mov    %ebx,%r12d
mov    $0x1,%ebx
lea    0xe51(%rip),%r15
nopw   0x0(%rax,%rax,1)
mov    (%r14,%rbx,8),%rdx
mov    %r15,%rdi
mov    %ebx,%esi
xor    %eax,%eax
call   1070 <printf@plt>
add    $0x1,%rbx
cmp    %rbx,%r12
jne    11d0 <main+0x40>
lea    0xe35(%rip),%rdi
call   1030 <getenv@plt>
test   %rax,%rax
je     1202 <main+0x72>
mov    %rax,%rdi
call   1050 <puts@plt>
xor    %eax,%eax
call   1060 <getpid@plt>
lea    0xe1e(%rip),%rdx
lea    0x10(%rsp),%rbx
mov    %rbx,%rdi
mov    $0x64,%esi
mov    %eax,%ecx
call   1080 <snprintf@plt>
lea    0xe15(%rip),%rsi
mov    %rbx,%rdi
call   10a0 <fopen@plt>
test   %rax,%rax
jne    1261 <main+0xd1>
mov    0x2da1(%rip),%rax
mov    (%rax),%edi
call   10b0 <strerror@plt>
lea    0xdf5(%rip),%rdi
mov    %rax,%rsi
xor    %eax,%eax
call   1070 <printf@plt>
mov    $0xffffffff,%edi
call   1040 <_exit@plt>
lea    0xdee(%rip),%rsi
lea    0xc(%rsp),%rdx
lea    0x8(%rsp),%rcx
mov    %rax,%rdi
call   1090 <fscanf@plt>
mov    0xc(%rsp),%esi
mov    0x8(%rsp),%edx
lea    0xdd3(%rip),%rdi
xor    %eax,%eax
call   1070 <printf@plt>
xor    %edi,%edi
call   1040 <_exit@plt>

LLVM IR (-O0):
@.str = [24 x i8] c"hello exe=`%s` argc=%d\0A\00"
@.str.1 = [13 x i8] c"arg %d \22%s\22\0A\00"
@.str.2 = [9 x i8] c"HOSTNAME\00"
@.str.3 = [4 x i8] c"%s\0A\00"
@.str.4 = [18 x i8] c"/proc/%d/app-info\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [20 x i8] c"fopen() failed, %s\0A\00"
@errno = global i32 0
@.str.7 = [6 x i8] c"%d %d\00"
@.str.8 = [21 x i8] c"nRanks=%d myRank=%d\0A\00"
define i32 @main(i32 %0, i8** %1, i8** %2) {
%4 = alloca i32
%5 = alloca i32
%6 = alloca i8**
%7 = alloca i8**
%8 = alloca i32
%9 = alloca i8*
%10 = alloca [100 x i8]
%11 = alloca i32*
%12 = alloca i32
%13 = alloca i32
store i32 0, i32* %4
store i32 %0, i32* %5
store i8** %1, i8*** %6
store i8** %2, i8*** %7
%14 = load i8**, i8*** %6
%15 = getelementptr inbounds i8*, i8** %14, i64 0
%16 = load i8*, i8** %15
%17 = load i32, i32* %5
%18 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %16, i32 %17)
store i32 1, i32* %8
br label %19
19: ; preds = %31, %3
%20 = load i32, i32* %8
%21 = load i32, i32* %5
%22 = icmp slt i32 %20, %21
br i1 %22, label %23, label %34
23: ; preds = %19
%24 = load i32, i32* %8
%25 = load i8**, i8*** %6
%26 = load i32, i32* %8
%27 = sext i32 %26 to i64
%28 = getelementptr inbounds i8*, i8** %25, i64 %27
%29 = load i8*, i8** %28
%30 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %24, i8* %29)
br label %31
31: ; preds = %23
%32 = load i32, i32* %8
%33 = add nsw i32 %32, 1
store i32 %33, i32* %8
br label %19
34: ; preds = %19
%35 = call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
store i8* %35, i8** %9
%36 = icmp ne i8* %35, null
br i1 %36, label %37, label %40
37: ; preds = %34
%38 = load i8*, i8** %9
%39 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([4 x i8], [4 x i8]* @.str.3, i64 0, i64 0), i8* %38)
br label %40
40: ; preds = %37, %34
%41 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%42 = call i32 (...) @getpid()
%43 = call i32 @snprintf(i8* %41, i32 100, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %42)
%44 = getelementptr inbounds [100 x i8], [100 x i8]* %10, i64 0, i64 0
%45 = call i32* @fopen(i8* %44, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
store i32* %45, i32** %11
%46 = load i32*, i32** %11
%47 = icmp ne i32* %46, null
br i1 %47, label %53, label %48
48: ; preds = %40
%49 = load i32, i32* @errno
%50 = call i8* @strerror(i32 %49)
%51 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %50)
%52 = call i32 @_exit(i32 -1)
unreachable
53: ; preds = %40
%54 = load i32*, i32** %11
%55 = call i32 @fscanf(i32* %54, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32* %12, i32* %13)
%56 = load i32, i32* %12
%57 = load i32, i32* %13
%58 = call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i32 %56, i32 %57)
%59 = call i32 @_exit(i32 0)
unreachable
}

LLVM IR (-O2):
@.str = [24 x i8] c"hello exe=`%s` argc=%d\0A\00"
@.str.1 = [13 x i8] c"arg %d \22%s\22\0A\00"
@.str.2 = [9 x i8] c"HOSTNAME\00"
@.str.4 = [18 x i8] c"/proc/%d/app-info\00"
@.str.5 = [2 x i8] c"r\00"
@.str.6 = [20 x i8] c"fopen() failed, %s\0A\00"
@errno = global i32 0
@.str.7 = [6 x i8] c"%d %d\00"
@.str.8 = [21 x i8] c"nRanks=%d myRank=%d\0A\00"
define i32 @main(i32 %0, i8** nocapture %1, i8** nocapture readnone %2) {
%4 = alloca [100 x i8]
%5 = alloca i32
%6 = alloca i32
%7 = load i8*, i8** %1
%8 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([24 x i8], [24 x i8]* @.str, i64 0, i64 0), i8* %7, i32 %0)
%9 = icmp sgt i32 %0, 1
br i1 %9, label %10, label %20
10: ; preds = %3
%11 = zext i32 %0 to i64
br label %12
12: ; preds = %10, %12
%13 = phi i64 [ 1, %10 ], [ %18, %12 ]
%14 = getelementptr inbounds i8*, i8** %1, i64 %13
%15 = load i8*, i8** %14
%16 = trunc i64 %13 to i32
%17 = tail call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([13 x i8], [13 x i8]* @.str.1, i64 0, i64 0), i32 %16, i8* %15)
%18 = add nuw nsw i64 %13, 1
%19 = icmp eq i64 %18, %11
br i1 %19, label %20, label %12
20: ; preds = %12, %3
%21 = tail call i8* @getenv(i8* getelementptr inbounds ([9 x i8], [9 x i8]* @.str.2, i64 0, i64 0))
%22 = icmp eq i8* %21, null
br i1 %22, label %25, label %23
23: ; preds = %20
%24 = tail call i32 @puts(i8* %21)
br label %25
25: ; preds = %23, %20
%26 = getelementptr inbounds [100 x i8], [100 x i8]* %4, i64 0, i64 0
%27 = tail call i32 (...) @getpid()
%28 = call i32 @snprintf(i8* %26, i32 100, i8* getelementptr inbounds ([18 x i8], [18 x i8]* @.str.4, i64 0, i64 0), i32 %27)
%29 = call i32* @fopen(i8* %26, i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.5, i64 0, i64 0))
%30 = icmp eq i32* %29, null
br i1 %30, label %31, label %36
31: ; preds = %25
%32 = load i32, i32* @errno
%33 = call i8* @strerror(i32 %32)
%34 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([20 x i8], [20 x i8]* @.str.6, i64 0, i64 0), i8* %33)
%35 = call i32 @_exit(i32 -1)
unreachable
36: ; preds = %25
%37 = bitcast i32* %5 to i8*
%38 = bitcast i32* %6 to i8*
%39 = call i32 @fscanf(i32* %29, i8* getelementptr inbounds ([6 x i8], [6 x i8]* @.str.7, i64 0, i64 0), i32* %5, i32* %6)
%40 = load i32, i32* %5
%41 = load i32, i32* %6
%42 = call i32 (i8*, ...) @printf(i8* dereferenceable(1) getelementptr inbounds ([21 x i8], [21 x i8]* @.str.8, i64 0, i64 0), i32 %40, i32 %41)
%43 = call i32 @_exit(i32 0)
unreachable
}