#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);
long chdir(char *);
int /*<<< orphan*/ fclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *fopen(char *, char *);
int /*<<< orphan*/ fprintf(int /*<<< orphan*/, char *, long);
int /*<<< orphan*/ fscanf(int /*<<< orphan*/ *, char *, char *);
int /*<<< orphan*/ perror(char *);
int /*<<< orphan*/ printf(char *, long, char *);
int /*<<< orphan*/ stderr;
void bwchdir(long *iproc) {
  FILE *f;
  char mypath[256];
  long i;
  if ((f = fopen("MPIDIRS", "r")) == NULL) {
    fprintf(stderr, "Cannot open MPIDIRS, %d\n", *iproc);
    _exit(10);
  }
  for (i = *iproc; i > 0; --i)
    fscanf(f, "%s", mypath);
  printf("Process %d WORKDIR %s\n", *iproc, mypath);
  i = chdir(mypath);
  if (i) {
    perror("Cannot chdir");
    _exit(10);
  }
  fclose(f);
}


x86 assembly (clang, -O0):
<bwchdir>:
push   %rbp
mov    %rsp,%rbp
sub    $0x120,%rsp
mov    %rdi,-0x8(%rbp)
lea    0xe6a(%rip),%rdi
lea    0xea9(%rip),%rsi
call   1090 <fopen@plt>
mov    %rax,-0x10(%rbp)
cmp    $0x0,%rax
jne    11d6 <bwchdir+0x56>
mov    0x2e29(%rip),%rax
mov    (%rax),%edi
mov    -0x8(%rbp),%rax
mov    (%rax),%rdx
lea    0xe41(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xa,%edi
call   1030 <_exit@plt>
mov    -0x8(%rbp),%rax
mov    (%rax),%rax
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
jle    1220 <bwchdir+0xa0>
mov    -0x10(%rbp),%rdi
lea    -0x110(%rbp),%rdx
lea    0xe1d(%rip),%rsi
call   1070 <fscanf@plt>
mov    -0x118(%rbp),%rax
add    $0xffffffffffffffff,%rax
mov    %rax,-0x118(%rbp)
jmp    11e4 <bwchdir+0x64>
mov    -0x8(%rbp),%rax
mov    (%rax),%rsi
lea    -0x110(%rbp),%rdx
lea    0xdef(%rip),%rdi
call   1060 <printf@plt>
lea    -0x110(%rbp),%rdi
call   1050 <chdir@plt>
mov    %rax,-0x118(%rbp)
cmpq   $0x0,-0x118(%rbp)
je     1271 <bwchdir+0xf1>
lea    0xdd9(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xa,%edi
call   1030 <_exit@plt>
mov    -0x10(%rbp),%rdi
call   1040 <fclose@plt>
add    $0x120,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<bwchdir>:
push   %r15
push   %r14
push   %r13
push   %r12
push   %rbx
sub    $0x100,%rsp
mov    %rdi,%r14
lea    0xe66(%rip),%rdi
lea    0xea5(%rip),%rsi
call   1090 <fopen@plt>
test   %rax,%rax
je     1226 <bwchdir+0xa6>
mov    %rax,%r15
mov    (%r14),%rbx
test   %rbx,%rbx
jle    11eb <bwchdir+0x6b>
add    $0x1,%rbx
lea    0xe60(%rip),%r12
mov    %rsp,%r13
cs nopw 0x0(%rax,%rax,1)
xchg   %ax,%ax
mov    %r15,%rdi
mov    %r12,%rsi
mov    %r13,%rdx
call   1070 <fscanf@plt>
add    $0xffffffffffffffff,%rbx
cmp    $0x1,%rbx
ja     11d0 <bwchdir+0x50>
mov    (%r14),%rbx
lea    0xe32(%rip),%rdi
mov    %rsp,%r14
mov    %rbx,%rsi
mov    %r14,%rdx
call   1060 <printf@plt>
mov    %r14,%rdi
call   1050 <chdir@plt>
test   %rax,%rax
jne    1248 <bwchdir+0xc8>
mov    %r15,%rdi
call   1040 <fclose@plt>
add    $0x100,%rsp
pop    %rbx
pop    %r12
pop    %r13
pop    %r14
pop    %r15
ret
mov    0x2db3(%rip),%rax
mov    (%rax),%edi
mov    (%r14),%rdx
lea    0xdcf(%rip),%rsi
call   1080 <fprintf@plt>
mov    $0xa,%edi
call   1030 <_exit@plt>
lea    0xdec(%rip),%rdi
call   10a0 <perror@plt>
mov    $0xa,%edi
call   1030 <_exit@plt>

LLVM IR (-O0):
@.str = [8 x i8] c"MPIDIRS\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Cannot open MPIDIRS, %d\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [23 x i8] c"Process %d WORKDIR %s\0A\00"
@.str.5 = [13 x i8] c"Cannot chdir\00"
define void @bwchdir(i64* %0) {
%2 = alloca i64*
%3 = alloca i32*
%4 = alloca [256 x i8]
%5 = alloca i64
store i64* %0, i64** %2
%6 = call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %6, i32** %3
%7 = icmp eq i32* %6, null
br i1 %7, label %8, label %14
8: ; preds = %1
%9 = load i32, i32* @stderr
%10 = load i64*, i64** %2
%11 = load i64, i64* %10
%12 = call i32 @fprintf(i32 %9, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64 %11)
%13 = call i32 @_exit(i32 10)
unreachable
14: ; preds = %1
%15 = load i64*, i64** %2
%16 = load i64, i64* %15
store i64 %16, i64* %5
br label %17
17: ; preds = %24, %14
%18 = load i64, i64* %5
%19 = icmp sgt i64 %18, 0
br i1 %19, label %20, label %27
20: ; preds = %17
%21 = load i32*, i32** %3
%22 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%23 = call i32 @fscanf(i32* %21, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %22)
br label %24
24: ; preds = %20
%25 = load i64, i64* %5
%26 = add nsw i64 %25, -1
store i64 %26, i64* %5
br label %17
27: ; preds = %17
%28 = load i64*, i64** %2
%29 = load i64, i64* %28
%30 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%31 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i64 %29, i8* %30)
%32 = getelementptr inbounds [256 x i8], [256 x i8]* %4, i64 0, i64 0
%33 = call i64 @chdir(i8* %32)
store i64 %33, i64* %5
%34 = load i64, i64* %5
%35 = icmp ne i64 %34, 0
br i1 %35, label %36, label %39
36: ; preds = %27
%37 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%38 = call i32 @_exit(i32 10)
unreachable
39: ; preds = %27
%40 = load i32*, i32** %3
%41 = call i32 @fclose(i32* %40)
ret void
}

LLVM IR (-O2):
@.str = [8 x i8] c"MPIDIRS\00"
@.str.1 = [2 x i8] c"r\00"
@stderr = global i32 0
@.str.2 = [25 x i8] c"Cannot open MPIDIRS, %d\0A\00"
@.str.3 = [3 x i8] c"%s\00"
@.str.4 = [23 x i8] c"Process %d WORKDIR %s\0A\00"
@.str.5 = [13 x i8] c"Cannot chdir\00"
define void @bwchdir(i64* nocapture %0) {
%2 = alloca [256 x i8]
%3 = getelementptr inbounds [256 x i8], [256 x i8]* %2, i64 0, i64 0
%4 = tail call i32* @fopen(i8* getelementptr inbounds ([8 x i8], [8 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%5 = icmp eq i32* %4, null
br i1 %5, label %6, label %11
6: ; preds = %1
%7 = load i32, i32* @stderr
%8 = load i64, i64* %0
%9 = tail call i32 @fprintf(i32 %7, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.2, i64 0, i64 0), i64 %8)
%10 = tail call i32 @_exit(i32 10)
unreachable
11: ; preds = %1
%12 = load i64, i64* %0
%13 = icmp sgt i64 %12, 0
br i1 %13, label %14, label %21
14: ; preds = %11, %14
%15 = phi i64 [ %17, %14 ], [ %12, %11 ]
%16 = call i32 @fscanf(i32* %4, i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str.3, i64 0, i64 0), i8* %3)
%17 = add nsw i64 %15, -1
%18 = icmp ugt i64 %15, 1
br i1 %18, label %14, label %19
19: ; preds = %14
%20 = load i64, i64* %0
br label %21
21: ; preds = %19, %11
%22 = phi i64 [ %20, %19 ], [ %12, %11 ]
%23 = call i32 @printf(i8* getelementptr inbounds ([23 x i8], [23 x i8]* @.str.4, i64 0, i64 0), i64 %22, i8* %3)
%24 = call i64 @chdir(i8* %3)
%25 = icmp eq i64 %24, 0
br i1 %25, label %29, label %26
26: ; preds = %21
%27 = call i32 @perror(i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.5, i64 0, i64 0))
%28 = call i32 @_exit(i32 10)
unreachable
29: ; preds = %21
%30 = call i32 @fclose(i32* %4)
ret void
}