#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 */
scalar_t__ fgets(char *, int, int /*<<< orphan*/ *);
int /*<<< orphan*/ pclose(int /*<<< orphan*/ *);
int /*<<< orphan*/ *popen(char *, char *);
int strlen(char *);
int find_login(char *buf, int len) {
  FILE *fp = popen("which login", "r");
  if (fp) {
    if (fgets(buf, len, fp))
      buf[strlen(buf) - 1] = 0;
    pclose(fp);
    if (!buf[0])
      return -1;
    return 0;
  }
  return -1;
}


x86 assembly (clang, -O0):
<find_login>:
push   %rbp
mov    %rsp,%rbp
sub    $0x30,%rsp
mov    %rdi,-0x10(%rbp)
mov    %esi,-0x14(%rbp)
lea    0xeaa(%rip),%rdi
lea    0xeaf(%rip),%rsi
call   1060 <popen@plt>
mov    %rax,-0x20(%rbp)
cmpq   $0x0,-0x20(%rbp)
je     11da <find_login+0x9a>
mov    -0x10(%rbp),%rdi
mov    -0x14(%rbp),%esi
mov    -0x20(%rbp),%rdx
call   1050 <fgets@plt>
cmp    $0x0,%rax
je     11ac <find_login+0x6c>
mov    -0x10(%rbp),%rax
mov    %rax,-0x28(%rbp)
mov    -0x10(%rbp),%rdi
call   1030 <strlen@plt>
mov    %eax,%ecx
mov    -0x28(%rbp),%rax
sub    $0x1,%ecx
movslq %ecx,%rcx
movb   $0x0,(%rax,%rcx,1)
mov    -0x20(%rbp),%rdi
call   1040 <pclose@plt>
mov    -0x10(%rbp),%rax
cmpb   $0x0,(%rax)
jne    11ce <find_login+0x8e>
movl   $0xffffffff,-0x4(%rbp)
jmp    11e1 <find_login+0xa1>
movl   $0x0,-0x4(%rbp)
jmp    11e1 <find_login+0xa1>
movl   $0xffffffff,-0x4(%rbp)
mov    -0x4(%rbp),%eax
add    $0x30,%rsp
pop    %rbp
ret

x86 assembly (clang, -O2):
<find_login>:
push   %rbp
push   %r14
push   %rbx
mov    %esi,%ebp
mov    %rdi,%r14
lea    0xeb0(%rip),%rdi
lea    0xeb5(%rip),%rsi
call   1060 <popen@plt>
test   %rax,%rax
je     1196 <find_login+0x56>
mov    %rax,%rbx
mov    %r14,%rdi
mov    %ebp,%esi
mov    %rax,%rdx
call   1050 <fgets@plt>
test   %rax,%rax
je     1186 <find_login+0x46>
mov    %r14,%rdi
call   1030 <strlen@plt>
cltq
movb   $0x0,-0x1(%rax,%r14,1)
mov    %rbx,%rdi
call   1040 <pclose@plt>
cmpb   $0x1,(%r14)
sbb    %eax,%eax
jmp    119b <find_login+0x5b>
mov    $0xffffffff,%eax
pop    %rbx
pop    %r14
pop    %rbp
ret

LLVM IR (-O0):
@.str = [12 x i8] c"which login\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @find_login(i8* %0, i32 %1) {
%3 = alloca i32
%4 = alloca i8*
%5 = alloca i32
%6 = alloca i32*
store i8* %0, i8** %4
store i32 %1, i32* %5
%7 = call i32* @popen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
store i32* %7, i32** %6
%8 = load i32*, i32** %6
%9 = icmp ne i32* %8, null
br i1 %9, label %10, label %32
10: ; preds = %2
%11 = load i8*, i8** %4
%12 = load i32, i32* %5
%13 = load i32*, i32** %6
%14 = call i64 @fgets(i8* %11, i32 %12, i32* %13)
%15 = icmp ne i64 %14, 0
br i1 %15, label %16, label %23
16: ; preds = %10
%17 = load i8*, i8** %4
%18 = load i8*, i8** %4
%19 = call i32 @strlen(i8* %18)
%20 = sub nsw i32 %19, 1
%21 = sext i32 %20 to i64
%22 = getelementptr inbounds i8, i8* %17, i64 %21
store i8 0, i8* %22
br label %23
23: ; preds = %16, %10
%24 = load i32*, i32** %6
%25 = call i32 @pclose(i32* %24)
%26 = load i8*, i8** %4
%27 = getelementptr inbounds i8, i8* %26, i64 0
%28 = load i8, i8* %27
%29 = icmp ne i8 %28, 0
br i1 %29, label %31, label %30
30: ; preds = %23
store i32 -1, i32* %3
br label %33
31: ; preds = %23
store i32 0, i32* %3
br label %33
32: ; preds = %2
store i32 -1, i32* %3
br label %33
33: ; preds = %32, %31, %30
%34 = load i32, i32* %3
ret i32 %34
}

LLVM IR (-O2):
@.str = [12 x i8] c"which login\00"
@.str.1 = [2 x i8] c"r\00"
define i32 @find_login(i8* %0, i32 %1) {
%3 = tail call i32* @popen(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @.str, i64 0, i64 0), i8* getelementptr inbounds ([2 x i8], [2 x i8]* @.str.1, i64 0, i64 0))
%4 = icmp eq i32* %3, null
br i1 %4, label %18, label %5
5: ; preds = %2
%6 = tail call i64 @fgets(i8* %0, i32 %1, i32* %3)
%7 = icmp eq i64 %6, 0
br i1 %7, label %13, label %8
8: ; preds = %5
%9 = tail call i32 @strlen(i8* %0)
%10 = add nsw i32 %9, -1
%11 = sext i32 %10 to i64
%12 = getelementptr inbounds i8, i8* %0, i64 %11
store i8 0, i8* %12
br label %13
13: ; preds = %8, %5
%14 = tail call i32 @pclose(i32* %3)
%15 = load i8, i8* %0
%16 = icmp eq i8 %15, 0
%17 = sext i1 %16 to i32
br label %18
18: ; preds = %2, %13
%19 = phi i32 [ %17, %13 ], [ -1, %2 ]
ret i32 %19
}