#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*/ fflush(int /*<<< orphan*/);
char *malloc(int);
int /*<<< orphan*/ mlock(char *, int);
int /*<<< orphan*/ printf(char *, char *);
int /*<<< orphan*/ putpwd(char *);
int /*<<< orphan*/ readpwd(char *, int);
int /*<<< orphan*/ stdout;
char *getpwd(char *msg) {
  char *p = malloc(512);
  printf("%s", msg);
  fflush(stdout);
  mlock(p, 512);
  if (!readpwd(p, 512)) {
    putpwd(p);
    return NULL;
  }
  return p;
}


x86 assembly (clang, -O0):
<getpwd>:
push   %rbp
mov    %rsp,%rbp
sub    $0x20,%rsp
mov    %rdi,-0x10(%rbp)
mov    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,-0x18(%rbp)
mov    -0x10(%rbp),%rsi
lea    0xe7b(%rip),%rdi
call   1040 <printf@plt>
mov    0x2e4f(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    -0x18(%rbp),%rdi
mov    $0x200,%esi
call   1030 <mlock@plt>
mov    -0x18(%rbp),%rdi
mov    $0x200,%esi
call   1050 <readpwd@plt>
cmp    $0x0,%eax
jne    11d3 <getpwd+0x73>
mov    -0x18(%rbp),%rdi
call   1080 <putpwd@plt>
movq   $0x0,-0x8(%rbp)
jmp    11db <getpwd+0x7b>
mov    -0x18(%rbp),%rax
mov    %rax,-0x8(%rbp)
mov    -0x8(%rbp),%rax
add    $0x20,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<getpwd>:
push   %r14
push   %rbx
push   %rax
mov    %rdi,%r14
mov    $0x200,%edi
call   1060 <malloc@plt>
mov    %rax,%rbx
lea    0xe85(%rip),%rdi
mov    %r14,%rsi
call   1040 <printf@plt>
mov    0x2e56(%rip),%rax
mov    (%rax),%edi
call   1070 <fflush@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
call   1030 <mlock@plt>
mov    %rbx,%rdi
mov    $0x200,%esi
call   1050 <readpwd@plt>
test   %eax,%eax
jne    11b9 <getpwd+0x59>
mov    %rbx,%rdi
call   1080 <putpwd@plt>
xor    %ebx,%ebx
mov    %rbx,%rax
add    $0x8,%rsp
pop    %rbx
pop    %r14
ret

LLVM IR (-O0):
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
define i8* @getpwd(i8* %0) {
%2 = alloca i8*
%3 = alloca i8*
%4 = alloca i8*
store i8* %0, i8** %3
%5 = call i8* @malloc(i32 512)
store i8* %5, i8** %4
%6 = load i8*, i8** %3
%7 = call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %6)
%8 = load i32, i32* @stdout
%9 = call i32 @fflush(i32 %8)
%10 = load i8*, i8** %4
%11 = call i32 @mlock(i8* %10, i32 512)
%12 = load i8*, i8** %4
%13 = call i32 @readpwd(i8* %12, i32 512)
%14 = icmp ne i32 %13, 0
br i1 %14, label %18, label %15
15: ; preds = %1
%16 = load i8*, i8** %4
%17 = call i32 @putpwd(i8* %16)
store i8* null, i8** %2
br label %20
18: ; preds = %1
%19 = load i8*, i8** %4
store i8* %19, i8** %2
br label %20
20: ; preds = %18, %15
%21 = load i8*, i8** %2
ret i8* %21
}

LLVM IR (-O2):
@.str = [3 x i8] c"%s\00"
@stdout = global i32 0
define i8* @getpwd(i8* %0) {
%2 = tail call dereferenceable_or_null(512) i8* @malloc(i32 512)
%3 = tail call i32 @printf(i8* getelementptr inbounds ([3 x i8], [3 x i8]* @.str, i64 0, i64 0), i8* %0)
%4 = load i32, i32* @stdout
%5 = tail call i32 @fflush(i32 %4)
%6 = tail call i32 @mlock(i8* %2, i32 512)
%7 = tail call i32 @readpwd(i8* %2, i32 512)
%8 = icmp eq i32 %7, 0
br i1 %8, label %9, label %11
9: ; preds = %1
%10 = tail call i32 @putpwd(i8* %2)
br label %11
11: ; preds = %1, %9
%12 = phi i8* [ null, %9 ], [ %2, %1 ]
ret i8* %12
}